| 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 final Set<Client> clients = new Set<Client>(); |
| 21 RunningIsolates runningIsolates = new RunningIsolates(); | 23 RunningIsolates runningIsolates = new RunningIsolates(); |
| 22 final RawReceivePort receivePort; | 24 final RawReceivePort receivePort; |
| 23 | 25 |
| 24 void controlMessageHandler(int code, int port_id, SendPort sp, String name) { | 26 void controlMessageHandler(int code, int port_id, SendPort sp, String name) { |
| 25 switch (code) { | 27 switch (code) { |
| 26 case Constants.ISOLATE_STARTUP_MESSAGE_ID: | 28 case Constants.ISOLATE_STARTUP_MESSAGE_ID: |
| 27 runningIsolates.isolateStartup(port_id, sp, name); | 29 runningIsolates.isolateStartup(port_id, sp, name); |
| 28 break; | 30 break; |
| 29 case Constants.ISOLATE_SHUTDOWN_MESSAGE_ID: | 31 case Constants.ISOLATE_SHUTDOWN_MESSAGE_ID: |
| 30 runningIsolates.isolateShutdown(port_id, sp); | 32 runningIsolates.isolateShutdown(port_id, sp); |
| 31 break; | 33 break; |
| 32 } | 34 } |
| 33 } | 35 } |
| 34 | 36 |
| 37 void addClient(Client client) { |
| 38 clients.add(client); |
| 39 } |
| 40 |
| 41 void removeClient(Client client) { |
| 42 clients.remove(client); |
| 43 } |
| 44 |
| 35 void messageHandler(message) { | 45 void messageHandler(message) { |
| 36 assert(message is List); | 46 assert(message is List); |
| 37 assert(message.length == 4); | 47 assert(message.length == 4); |
| 38 if (message is List && message.length == 4) { | 48 if (message is List && message.length == 4) { |
| 39 controlMessageHandler(message[0], message[1], message[2], message[3]); | 49 controlMessageHandler(message[0], message[1], message[2], message[3]); |
| 40 } | 50 } |
| 41 } | 51 } |
| 42 | 52 |
| 43 VMService._internal() : receivePort = new RawReceivePort() { | 53 VMService._internal() : receivePort = new RawReceivePort() { |
| 44 receivePort.handler = messageHandler; | 54 receivePort.handler = messageHandler; |
| 45 } | 55 } |
| 46 | 56 |
| 47 factory VMService() { | 57 factory VMService() { |
| 48 if (VMService._instance == null) { | 58 if (VMService._instance == null) { |
| 49 VMService._instance = new VMService._internal(); | 59 VMService._instance = new VMService._internal(); |
| 50 } | 60 } |
| 51 return _instance; | 61 return _instance; |
| 52 } | 62 } |
| 63 |
| 64 void _clientCollection(Message message) { |
| 65 var members = []; |
| 66 var result = {}; |
| 67 clients.forEach((client) { |
| 68 members.add(client.toJson()); |
| 69 }); |
| 70 result['type'] = 'ClientList'; |
| 71 result['members'] = members; |
| 72 message.setResponse(JSON.encode(result)); |
| 73 } |
| 74 |
| 75 Future route(Message message) { |
| 76 if (message.completed) { |
| 77 return message.future; |
| 78 } |
| 79 if ((message.path.length == 1) && (message.path[0] == 'clients')) { |
| 80 _clientCollection(message); |
| 81 return message.future; |
| 82 } |
| 83 return runningIsolates.route(message); |
| 84 } |
| 53 } | 85 } |
| 54 | 86 |
| 55 void sendServiceMessage(SendPort sp, ReceivePort rp, Object m) | 87 void sendServiceMessage(SendPort sp, ReceivePort rp, Object m) |
| 56 native "VMService_SendServiceMessage"; | 88 native "VMService_SendServiceMessage"; |
| OLD | NEW |