OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library vmservice; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:convert'; | |
9 import 'dart:isolate'; | |
10 import 'dart:typed_data'; | |
11 | |
12 part 'client.dart'; | |
13 part 'constants.dart'; | |
14 part 'resources.dart'; | |
15 part 'running_isolate.dart'; | |
16 part 'running_isolates.dart'; | |
17 part 'message.dart'; | |
18 part 'message_router.dart'; | |
19 | |
20 class VMService extends MessageRouter { | |
21 static VMService _instance; | |
22 /// Collection of currently connected clients. | |
23 final Set<Client> clients = new Set<Client>(); | |
24 /// Collection of currently running isolates. | |
25 RunningIsolates runningIsolates = new RunningIsolates(); | |
26 /// Isolate startup and shutdown messages are sent on this port. | |
27 final RawReceivePort receivePort; | |
28 | |
29 void controlMessageHandler(int code, int port_id, SendPort sp, String name) { | |
30 switch (code) { | |
31 case Constants.ISOLATE_STARTUP_MESSAGE_ID: | |
32 runningIsolates.isolateStartup(port_id, sp, name); | |
33 break; | |
34 case Constants.ISOLATE_SHUTDOWN_MESSAGE_ID: | |
35 runningIsolates.isolateShutdown(port_id, sp); | |
36 break; | |
37 } | |
38 } | |
39 | |
40 void _addClient(Client client) { | |
41 clients.add(client); | |
42 } | |
43 | |
44 void _removeClient(Client client) { | |
45 clients.remove(client); | |
46 } | |
47 | |
48 void messageHandler(message) { | |
49 assert(message is List); | |
50 assert(message.length == 4); | |
51 if (message is List && message.length == 4) { | |
52 controlMessageHandler(message[0], message[1], message[2], message[3]); | |
53 } | |
54 } | |
55 | |
56 VMService._internal() : receivePort = new RawReceivePort() { | |
57 receivePort.handler = messageHandler; | |
58 } | |
59 | |
60 factory VMService() { | |
61 if (VMService._instance == null) { | |
62 VMService._instance = new VMService._internal(); | |
63 } | |
64 return _instance; | |
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 } | |
88 } | |
89 | |
90 void sendServiceMessage(SendPort sp, Object m) | |
91 native "VMService_SendServiceMessage"; | |
OLD | NEW |