| 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 part of vmservice; | 5 part of vmservice; |
| 6 | 6 |
| 7 class RunningIsolates implements MessageRouter { | 7 class RunningIsolates implements MessageRouter { |
| 8 final Map<int, RunningIsolate> isolates = new Map<int, RunningIsolate>(); | 8 final Map<int, RunningIsolate> isolates = new Map<int, RunningIsolate>(); |
| 9 int _rootPortId; | 9 int _rootPortId; |
| 10 | 10 |
| 11 RunningIsolates(); | 11 RunningIsolates(); |
| 12 | 12 |
| 13 void isolateStartup(int portId, SendPort sp, String name) { | 13 void isolateStartup(int portId, SendPort sp, String name) { |
| 14 if (_rootPortId == null) { | 14 if (_rootPortId == null) { |
| 15 _rootPortId = portId; | 15 _rootPortId = portId; |
| 16 } | 16 } |
| 17 var ri = new RunningIsolate(portId, sp, name); | 17 var ri = new RunningIsolate(portId, sp, name); |
| 18 isolates[portId] = ri; | 18 isolates[portId] = ri; |
| 19 } | 19 } |
| 20 | 20 |
| 21 void isolateShutdown(int portId, SendPort sp) { | 21 void isolateShutdown(int portId, SendPort sp) { |
| 22 if (_rootPortId == portId) { | 22 if (_rootPortId == portId) { |
| 23 _rootPortId = null; | 23 _rootPortId = null; |
| 24 } | 24 } |
| 25 isolates.remove(portId); | 25 isolates.remove(portId); |
| 26 } | 26 } |
| 27 | 27 |
| 28 Future<String> route(Message message) { | 28 Future<String> route(Message message) { |
| 29 if (message.isOld) { | 29 String isolateParam = message.params['isolateId']; |
| 30 return routeOld(message); | |
| 31 } | |
| 32 | |
| 33 String isolateParam = message.params['isolate']; | |
| 34 int isolateId; | 30 int isolateId; |
| 35 if (!isolateParam.startsWith('isolates/')) { | 31 if (!isolateParam.startsWith('isolates/')) { |
| 36 message.setErrorResponse('Malformed isolate id $isolateParam'); | 32 message.setErrorResponse('Malformed isolate id $isolateParam'); |
| 37 return message.response; | 33 return message.response; |
| 38 } | 34 } |
| 39 isolateParam = isolateParam.substring('isolates/'.length); | 35 isolateParam = isolateParam.substring('isolates/'.length); |
| 40 if (isolateParam == 'isolates/root') { | 36 if (isolateParam == 'isolates/root') { |
| 41 isolateId = _rootPortId; | 37 isolateId = _rootPortId; |
| 42 } else { | 38 } else { |
| 43 try { | 39 try { |
| 44 isolateId = int.parse(isolateParam); | 40 isolateId = int.parse(isolateParam); |
| 45 } catch (e) { | 41 } catch (e) { |
| 46 message.setErrorResponse('Could not parse isolate id: $e'); | 42 message.setErrorResponse('Could not parse isolate id: $e'); |
| 47 return message.response; | 43 return message.response; |
| 48 } | 44 } |
| 49 } | 45 } |
| 50 var isolate = isolates[isolateId]; | 46 var isolate = isolates[isolateId]; |
| 51 if (isolate == null) { | 47 if (isolate == null) { |
| 52 message.setErrorResponse('Cannot find isolate id: $isolateId'); | 48 message.setErrorResponse('Cannot find isolate id: $isolateId'); |
| 53 return message.response; | 49 return message.response; |
| 54 } | 50 } |
| 55 return isolate.route(message); | 51 return isolate.route(message); |
| 56 } | 52 } |
| 57 | |
| 58 Future<String> routeOld(Message message) { | |
| 59 if (message.path.length == 0) { | |
| 60 message.setErrorResponse('No path.'); | |
| 61 return message.response; | |
| 62 } | |
| 63 if (message.path[0] != 'isolates') { | |
| 64 message.setErrorResponse('Path must begin with /isolates/'); | |
| 65 return message.response; | |
| 66 } | |
| 67 if (message.path.length < 2) { | |
| 68 message.setErrorResponse('An isolate id must be provided'); | |
| 69 return message.response; | |
| 70 } | |
| 71 var isolateId; | |
| 72 if ((message.path[1] == 'root') && (_rootPortId != null)) { | |
| 73 isolateId = _rootPortId; | |
| 74 } else { | |
| 75 try { | |
| 76 isolateId = int.parse(message.path[1]); | |
| 77 } catch (e) { | |
| 78 message.setErrorResponse('Could not parse isolate id: $e'); | |
| 79 return message.response; | |
| 80 } | |
| 81 } | |
| 82 assert(isolateId != null); | |
| 83 var isolate = isolates[isolateId]; | |
| 84 if (isolate == null) { | |
| 85 message.setErrorResponse('Cannot find isolate id: $isolateId'); | |
| 86 return message.response; | |
| 87 } | |
| 88 // Consume '/isolates/isolateId' | |
| 89 message.path.removeRange(0, 2); | |
| 90 return isolate.route(message); | |
| 91 } | |
| 92 } | 53 } |
| OLD | NEW |