| 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 'constants.dart'; | 13 part 'constants.dart'; |
| 13 part 'resources.dart'; | 14 part 'resources.dart'; |
| 14 part 'running_isolate.dart'; | 15 part 'running_isolate.dart'; |
| 15 part 'running_isolates.dart'; | 16 part 'running_isolates.dart'; |
| 16 part 'service_request.dart'; | 17 part 'message.dart'; |
| 17 part 'service_request_router.dart'; | 18 part 'message_router.dart'; |
| 18 | 19 |
| 19 class VMService { | 20 class VMService extends MessageRouter { |
| 20 static VMService _instance; | 21 static VMService _instance; |
| 22 /// Collection of currently connected clients. |
| 23 final Set<Client> clients = new Set<Client>(); |
| 24 /// Collection of currently running isolates. |
| 21 RunningIsolates runningIsolates = new RunningIsolates(); | 25 RunningIsolates runningIsolates = new RunningIsolates(); |
| 26 /// Isolate startup and shutdown messages are sent on this port. |
| 22 final RawReceivePort receivePort; | 27 final RawReceivePort receivePort; |
| 23 | 28 |
| 24 void controlMessageHandler(int code, int port_id, SendPort sp, String name) { | 29 void controlMessageHandler(int code, int port_id, SendPort sp, String name) { |
| 25 switch (code) { | 30 switch (code) { |
| 26 case Constants.ISOLATE_STARTUP_MESSAGE_ID: | 31 case Constants.ISOLATE_STARTUP_MESSAGE_ID: |
| 27 runningIsolates.isolateStartup(port_id, sp, name); | 32 runningIsolates.isolateStartup(port_id, sp, name); |
| 28 break; | 33 break; |
| 29 case Constants.ISOLATE_SHUTDOWN_MESSAGE_ID: | 34 case Constants.ISOLATE_SHUTDOWN_MESSAGE_ID: |
| 30 runningIsolates.isolateShutdown(port_id, sp); | 35 runningIsolates.isolateShutdown(port_id, sp); |
| 31 break; | 36 break; |
| 32 } | 37 } |
| 33 } | 38 } |
| 34 | 39 |
| 40 void _addClient(Client client) { |
| 41 clients.add(client); |
| 42 } |
| 43 |
| 44 void _removeClient(Client client) { |
| 45 clients.remove(client); |
| 46 } |
| 47 |
| 35 void messageHandler(message) { | 48 void messageHandler(message) { |
| 36 assert(message is List); | 49 assert(message is List); |
| 37 assert(message.length == 4); | 50 assert(message.length == 4); |
| 38 if (message is List && message.length == 4) { | 51 if (message is List && message.length == 4) { |
| 39 controlMessageHandler(message[0], message[1], message[2], message[3]); | 52 controlMessageHandler(message[0], message[1], message[2], message[3]); |
| 40 } | 53 } |
| 41 } | 54 } |
| 42 | 55 |
| 43 VMService._internal() : receivePort = new RawReceivePort() { | 56 VMService._internal() : receivePort = new RawReceivePort() { |
| 44 receivePort.handler = messageHandler; | 57 receivePort.handler = messageHandler; |
| 45 } | 58 } |
| 46 | 59 |
| 47 factory VMService() { | 60 factory VMService() { |
| 48 if (VMService._instance == null) { | 61 if (VMService._instance == null) { |
| 49 VMService._instance = new VMService._internal(); | 62 VMService._instance = new VMService._internal(); |
| 50 } | 63 } |
| 51 return _instance; | 64 return _instance; |
| 52 } | 65 } |
| 66 |
| 67 void _clientCollection(Message message) { |
| 68 var members = []; |
| 69 var result = {}; |
| 70 clients.forEach((client) { |
| 71 members.add(client.toJson()); |
| 72 }); |
| 73 result['type'] = 'ClientList'; |
| 74 result['members'] = members; |
| 75 message.setResponse(JSON.encode(result)); |
| 76 } |
| 77 |
| 78 Future<String> route(Message message) { |
| 79 if (message.completed) { |
| 80 return message.response; |
| 81 } |
| 82 if ((message.path.length == 1) && (message.path[0] == 'clients')) { |
| 83 _clientCollection(message); |
| 84 return message.response; |
| 85 } |
| 86 return runningIsolates.route(message); |
| 87 } |
| 53 } | 88 } |
| 54 | 89 |
| 55 void sendServiceMessage(SendPort sp, ReceivePort rp, Object m) | 90 void sendServiceMessage(SendPort sp, ReceivePort rp, Object m) |
| 56 native "VMService_SendServiceMessage"; | 91 native "VMService_SendServiceMessage"; |
| OLD | NEW |