OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library channel.byte_stream; | 5 library channel.byte_stream; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 * wait for it to complete before sending more data. There may or may not be | 81 * wait for it to complete before sending more data. There may or may not be |
82 * outstanding data in [_pendingOutput]. | 82 * outstanding data in [_pendingOutput]. |
83 */ | 83 */ |
84 static const int _STATE_FLUSH_PENDING = 2; | 84 static const int _STATE_FLUSH_PENDING = 2; |
85 | 85 |
86 final Stream input; | 86 final Stream input; |
87 | 87 |
88 final IOSink _output; | 88 final IOSink _output; |
89 | 89 |
90 /** | 90 /** |
91 * The instrumentation server that is to be used by this analysis server. | 91 * The instrumentation service that is to be used by this analysis server. |
92 */ | 92 */ |
93 final InstrumentationServer instrumentationServer; | 93 final InstrumentationService instrumentationService; |
94 | 94 |
95 /** | 95 /** |
96 * Completer that will be signalled when the input stream is closed. | 96 * Completer that will be signalled when the input stream is closed. |
97 */ | 97 */ |
98 final Completer _closed = new Completer(); | 98 final Completer _closed = new Completer(); |
99 | 99 |
100 /** | 100 /** |
101 * State of the output stream (see constants above). | 101 * State of the output stream (see constants above). |
102 */ | 102 */ |
103 int _outputState = _STATE_IDLE; | 103 int _outputState = _STATE_IDLE; |
104 | 104 |
105 /** | 105 /** |
106 * List of strings that need to be sent to [_output] at the next available | 106 * List of strings that need to be sent to [_output] at the next available |
107 * opportunity. | 107 * opportunity. |
108 */ | 108 */ |
109 List<String> _pendingOutput = <String>[]; | 109 List<String> _pendingOutput = <String>[]; |
110 | 110 |
111 /** | 111 /** |
112 * True if [close] has been called. | 112 * True if [close] has been called. |
113 */ | 113 */ |
114 bool _closeRequested = false; | 114 bool _closeRequested = false; |
115 | 115 |
116 ByteStreamServerChannel(this.input, this._output, this.instrumentationServer); | 116 ByteStreamServerChannel(this.input, this._output, |
| 117 this.instrumentationService); |
117 | 118 |
118 /** | 119 /** |
119 * Future that will be completed when the input stream is closed. | 120 * Future that will be completed when the input stream is closed. |
120 */ | 121 */ |
121 Future get closed { | 122 Future get closed { |
122 return _closed.future; | 123 return _closed.future; |
123 } | 124 } |
124 | 125 |
125 @override | 126 @override |
126 void close() { | 127 void close() { |
(...skipping 27 matching lines...) Expand all Loading... |
154 void sendNotification(Notification notification) { | 155 void sendNotification(Notification notification) { |
155 // Don't send any further notifications after the communication channel is | 156 // Don't send any further notifications after the communication channel is |
156 // closed. | 157 // closed. |
157 if (_closeRequested) { | 158 if (_closeRequested) { |
158 return; | 159 return; |
159 } | 160 } |
160 ServerCommunicationChannel.ToJson.start(); | 161 ServerCommunicationChannel.ToJson.start(); |
161 String jsonEncoding = JSON.encode(notification.toJson()); | 162 String jsonEncoding = JSON.encode(notification.toJson()); |
162 ServerCommunicationChannel.ToJson.stop(); | 163 ServerCommunicationChannel.ToJson.stop(); |
163 _outputLine(jsonEncoding); | 164 _outputLine(jsonEncoding); |
164 instrumentationServer.log(jsonEncoding); | 165 instrumentationService.logNotification(jsonEncoding); |
165 } | 166 } |
166 | 167 |
167 @override | 168 @override |
168 void sendResponse(Response response) { | 169 void sendResponse(Response response) { |
169 // Don't send any further responses after the communication channel is | 170 // Don't send any further responses after the communication channel is |
170 // closed. | 171 // closed. |
171 if (_closeRequested) { | 172 if (_closeRequested) { |
172 return; | 173 return; |
173 } | 174 } |
174 ServerCommunicationChannel.ToJson.start(); | 175 ServerCommunicationChannel.ToJson.start(); |
175 String jsonEncoding = JSON.encode(response.toJson()); | 176 String jsonEncoding = JSON.encode(response.toJson()); |
176 ServerCommunicationChannel.ToJson.stop(); | 177 ServerCommunicationChannel.ToJson.stop(); |
177 _outputLine(jsonEncoding); | 178 _outputLine(jsonEncoding); |
178 instrumentationServer.log(jsonEncoding); | 179 instrumentationService.logResponse(jsonEncoding); |
179 } | 180 } |
180 | 181 |
181 /** | 182 /** |
182 * Callback invoked after a flush of [_output] completes. Closes the stream | 183 * Callback invoked after a flush of [_output] completes. Closes the stream |
183 * if necessary. Otherwise schedules additional pending output. | 184 * if necessary. Otherwise schedules additional pending output. |
184 */ | 185 */ |
185 void _flushCompleted(_) { | 186 void _flushCompleted(_) { |
186 assert(_outputState == _STATE_FLUSH_PENDING); | 187 assert(_outputState == _STATE_FLUSH_PENDING); |
187 if (_pendingOutput.isNotEmpty) { | 188 if (_pendingOutput.isNotEmpty) { |
188 _output.write(_pendingOutput.join()); | 189 _output.write(_pendingOutput.join()); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 | 227 |
227 /** | 228 /** |
228 * Read a request from the given [data] and use the given function to handle | 229 * Read a request from the given [data] and use the given function to handle |
229 * the request. | 230 * the request. |
230 */ | 231 */ |
231 void _readRequest(Object data, void onRequest(Request request)) { | 232 void _readRequest(Object data, void onRequest(Request request)) { |
232 // Ignore any further requests after the communication channel is closed. | 233 // Ignore any further requests after the communication channel is closed. |
233 if (_closed.isCompleted) { | 234 if (_closed.isCompleted) { |
234 return; | 235 return; |
235 } | 236 } |
236 instrumentationServer.log(data); | 237 instrumentationService.logRequest(data); |
237 // Parse the string as a JSON descriptor and process the resulting | 238 // Parse the string as a JSON descriptor and process the resulting |
238 // structure as a request. | 239 // structure as a request. |
239 ServerCommunicationChannel.FromJson.start(); | 240 ServerCommunicationChannel.FromJson.start(); |
240 Request request = new Request.fromString(data); | 241 Request request = new Request.fromString(data); |
241 ServerCommunicationChannel.FromJson.stop(); | 242 ServerCommunicationChannel.FromJson.stop(); |
242 if (request == null) { | 243 if (request == null) { |
243 sendResponse(new Response.invalidRequestFormat()); | 244 sendResponse(new Response.invalidRequestFormat()); |
244 return; | 245 return; |
245 } | 246 } |
246 onRequest(request); | 247 onRequest(request); |
247 } | 248 } |
248 } | 249 } |
OLD | NEW |