| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 vmservice; | 5 library vmservice; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
| 11 | 11 |
| 12 part 'client.dart'; | 12 part 'client.dart'; |
| 13 part 'constants.dart'; | 13 part 'constants.dart'; |
| 14 part 'running_isolate.dart'; | 14 part 'running_isolate.dart'; |
| 15 part 'running_isolates.dart'; | 15 part 'running_isolates.dart'; |
| 16 part 'message.dart'; | 16 part 'message.dart'; |
| 17 part 'message_router.dart'; | 17 part 'message_router.dart'; |
| 18 | 18 |
| 19 final RawReceivePort isolateLifecyclePort = new RawReceivePort(); | 19 final RawReceivePort isolateLifecyclePort = new RawReceivePort(); |
| 20 final RawReceivePort scriptLoadPort = new RawReceivePort(); | 20 final RawReceivePort scriptLoadPort = new RawReceivePort(); |
| 21 | 21 |
| 22 typedef ShutdownCallback(); | 22 typedef ShutdownCallback(); |
| 23 | 23 |
| 24 // These must be kept in sync with the declarations in vm/json_stream.h. |
| 25 const kInvalidParams = -32602; |
| 26 const kInternalError = -32603; |
| 27 const kStreamAlreadySubscribed = 103; |
| 28 const kStreamNotSubscribed = 104; |
| 29 |
| 30 var _errorMessages = { |
| 31 kInvalidParams: 'Invalid params', |
| 32 kInternalError: 'Internal error', |
| 33 kStreamAlreadySubscribed: 'Stream already subscribed', |
| 34 kStreamNotSubscribed: 'Stream not subscribed', |
| 35 }; |
| 36 |
| 37 String encodeRpcError(Message message, int code, {String details}) { |
| 38 var response = { |
| 39 'jsonrpc': '2.0', |
| 40 'id' : message.serial, |
| 41 'error' : { |
| 42 'code': code, |
| 43 'message': _errorMessages[code], |
| 44 }, |
| 45 }; |
| 46 if (details != null) { |
| 47 response['error']['data'] = { |
| 48 'details': details, |
| 49 }; |
| 50 } |
| 51 return JSON.encode(response); |
| 52 } |
| 53 |
| 24 class VMService extends MessageRouter { | 54 class VMService extends MessageRouter { |
| 25 static VMService _instance; | 55 static VMService _instance; |
| 26 | 56 |
| 27 /// Collection of currently connected clients. | 57 /// Collection of currently connected clients. |
| 28 final Set<Client> clients = new Set<Client>(); | 58 final Set<Client> clients = new Set<Client>(); |
| 29 | 59 |
| 30 /// Collection of currently running isolates. | 60 /// Collection of currently running isolates. |
| 31 RunningIsolates runningIsolates = new RunningIsolates(); | 61 RunningIsolates runningIsolates = new RunningIsolates(); |
| 32 | 62 |
| 33 /// A port used to receive events from the VM. | 63 /// A port used to receive events from the VM. |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 var members = []; | 166 var members = []; |
| 137 var result = {}; | 167 var result = {}; |
| 138 clients.forEach((client) { | 168 clients.forEach((client) { |
| 139 members.add(client.toJson()); | 169 members.add(client.toJson()); |
| 140 }); | 170 }); |
| 141 result['type'] = 'ClientList'; | 171 result['type'] = 'ClientList'; |
| 142 result['members'] = members; | 172 result['members'] = members; |
| 143 message.setResponse(JSON.encode(result)); | 173 message.setResponse(JSON.encode(result)); |
| 144 } | 174 } |
| 145 | 175 |
| 146 // These must be kept in sync with the declarations in vm/json_stream.h. | |
| 147 static const _kInvalidParams = -32602; | |
| 148 static const _kStreamAlreadySubscribed = 103; | |
| 149 static const _kStreamNotSubscribed = 104; | |
| 150 | |
| 151 var _errorMessages = { | |
| 152 _kInvalidParams: 'Invalid params"', | |
| 153 _kStreamAlreadySubscribed: 'Stream already subscribed', | |
| 154 _kStreamNotSubscribed: 'Stream not subscribed', | |
| 155 }; | |
| 156 | |
| 157 String _encodeError(Message message, int code, {String details}) { | |
| 158 var response = { | |
| 159 'jsonrpc': '2.0', | |
| 160 'id' : message.serial, | |
| 161 'error' : { | |
| 162 'code': code, | |
| 163 'message': _errorMessages[code], | |
| 164 }, | |
| 165 }; | |
| 166 if (details != null) { | |
| 167 response['error']['data'] = { | |
| 168 'details': details, | |
| 169 }; | |
| 170 } | |
| 171 return JSON.encode(response); | |
| 172 } | |
| 173 | |
| 174 String _encodeResult(Message message, Map result) { | 176 String _encodeResult(Message message, Map result) { |
| 175 var response = { | 177 var response = { |
| 176 'jsonrpc': '2.0', | 178 'jsonrpc': '2.0', |
| 177 'id' : message.serial, | 179 'id' : message.serial, |
| 178 'result' : result, | 180 'result' : result, |
| 179 }; | 181 }; |
| 180 return JSON.encode(response); | 182 return JSON.encode(response); |
| 181 } | 183 } |
| 182 | 184 |
| 183 bool _isAnyClientSubscribed(String streamId) { | 185 bool _isAnyClientSubscribed(String streamId) { |
| 184 for (var client in clients) { | 186 for (var client in clients) { |
| 185 if (client.streams.contains(streamId)) { | 187 if (client.streams.contains(streamId)) { |
| 186 return true; | 188 return true; |
| 187 } | 189 } |
| 188 } | 190 } |
| 189 return false; | 191 return false; |
| 190 } | 192 } |
| 191 | 193 |
| 192 Future<String> _streamListen(Message message) async { | 194 Future<String> _streamListen(Message message) async { |
| 193 var client = message.client; | 195 var client = message.client; |
| 194 var streamId = message.params['streamId']; | 196 var streamId = message.params['streamId']; |
| 195 | 197 |
| 196 if (client.streams.contains(streamId)) { | 198 if (client.streams.contains(streamId)) { |
| 197 return _encodeError(message, _kStreamAlreadySubscribed); | 199 return encodeRpcError(message, kStreamAlreadySubscribed); |
| 198 } | 200 } |
| 199 if (!_isAnyClientSubscribed(streamId)) { | 201 if (!_isAnyClientSubscribed(streamId)) { |
| 200 if (!_vmListenStream(streamId)) { | 202 if (!_vmListenStream(streamId)) { |
| 201 return _encodeError( | 203 return encodeRpcError( |
| 202 message, _kInvalidParams, | 204 message, kInvalidParams, |
| 203 details:"streamListen: invalid 'streamId' parameter: ${streamId}"); | 205 details:"streamListen: invalid 'streamId' parameter: ${streamId}"); |
| 204 } | 206 } |
| 205 } | 207 } |
| 206 client.streams.add(streamId); | 208 client.streams.add(streamId); |
| 207 | 209 |
| 208 var result = { 'type' : 'Success' }; | 210 var result = { 'type' : 'Success' }; |
| 209 return _encodeResult(message, result); | 211 return _encodeResult(message, result); |
| 210 } | 212 } |
| 211 | 213 |
| 212 Future<String> _streamCancel(Message message) async { | 214 Future<String> _streamCancel(Message message) async { |
| 213 var client = message.client; | 215 var client = message.client; |
| 214 var streamId = message.params['streamId']; | 216 var streamId = message.params['streamId']; |
| 215 | 217 |
| 216 if (!client.streams.contains(streamId)) { | 218 if (!client.streams.contains(streamId)) { |
| 217 return _encodeError(message, _kStreamNotSubscribed); | 219 return encodeRpcError(message, kStreamNotSubscribed); |
| 218 } | 220 } |
| 219 client.streams.remove(streamId); | 221 client.streams.remove(streamId); |
| 220 if (!_isAnyClientSubscribed(streamId)) { | 222 if (!_isAnyClientSubscribed(streamId)) { |
| 221 _vmCancelStream(streamId); | 223 _vmCancelStream(streamId); |
| 222 } | 224 } |
| 223 | 225 |
| 224 var result = { 'type' : 'Success' }; | 226 var result = { 'type' : 'Success' }; |
| 225 return _encodeResult(message, result); | 227 return _encodeResult(message, result); |
| 226 } | 228 } |
| 227 | 229 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 service.runningIsolates.isolateStartup(port_id, sp, name); | 320 service.runningIsolates.isolateStartup(port_id, sp, name); |
| 319 } | 321 } |
| 320 | 322 |
| 321 void _onStart() native "VMService_OnStart"; | 323 void _onStart() native "VMService_OnStart"; |
| 322 | 324 |
| 323 void _onExit() native "VMService_OnExit"; | 325 void _onExit() native "VMService_OnExit"; |
| 324 | 326 |
| 325 bool _vmListenStream(String streamId) native "VMService_ListenStream"; | 327 bool _vmListenStream(String streamId) native "VMService_ListenStream"; |
| 326 | 328 |
| 327 void _vmCancelStream(String streamId) native "VMService_CancelStream"; | 329 void _vmCancelStream(String streamId) native "VMService_CancelStream"; |
| OLD | NEW |