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 part of vmservice; | |
6 | |
7 class RunningIsolates implements MessageRouter { | |
8 final Map<int, RunningIsolate> isolates = new Map<int, RunningIsolate>(); | |
9 | |
10 RunningIsolates(); | |
11 | |
12 void isolateStartup(int portId, SendPort sp, String name) { | |
13 if (isolates[portId] != null) { | |
14 throw new StateError('Duplicate isolate startup.'); | |
15 } | |
16 var ri = new RunningIsolate(portId, sp, name); | |
17 isolates[portId] = ri; | |
18 } | |
19 | |
20 void isolateShutdown(int portId, SendPort sp) { | |
21 if (isolates[portId] == null) { | |
22 throw new StateError('Unknown isolate.'); | |
23 } | |
24 isolates.remove(portId); | |
25 } | |
26 | |
27 void _isolateCollectionRequest(Message message) { | |
28 var members = []; | |
29 var result = {}; | |
30 isolates.forEach((portId, runningIsolate) { | |
31 members.add({ | |
32 'type': 'Isolate', | |
33 'id': 'isolates/$portId', | |
34 'name': runningIsolate.name, | |
35 }); | |
36 }); | |
37 result['type'] = 'IsolateList'; | |
38 result['members'] = members; | |
39 message.setResponse(JSON.encode(result)); | |
40 } | |
41 | |
42 Future<String> route(Message message) { | |
43 if (message.path.length == 0) { | |
44 message.setErrorResponse('No path.'); | |
45 return message.response; | |
46 } | |
47 if (message.path[0] != 'isolates') { | |
48 message.setErrorResponse('Path must begin with /isolates/.'); | |
49 return message.response; | |
50 } | |
51 if (message.path.length == 1) { | |
52 // Requesting list of running isolates. | |
53 _isolateCollectionRequest(message); | |
54 return message.response; | |
55 } | |
56 var isolateId; | |
57 try { | |
58 isolateId = int.parse(message.path[1]); | |
59 } catch (e) { | |
60 message.setErrorResponse('Could not parse isolate id: $e'); | |
61 return message.response; | |
62 } | |
63 var isolate = isolates[isolateId]; | |
64 if (isolate == null) { | |
65 message.setErrorResponse('Cannot find isolate id: $isolateId'); | |
66 return message.response; | |
67 } | |
68 // Consume '/isolates/isolateId' | |
69 message.path.removeRange(0, 2); | |
70 if (message.path.length == 0) { | |
71 // The message now has an empty path. | |
72 message.setErrorResponse('Empty path for isolate: /isolates/$isolateId'); | |
73 return message.response; | |
74 } | |
75 return isolate.route(message); | |
76 } | |
77 } | |
OLD | NEW |