blob: 776649124ee95db18804402bdb8240dbfa6065a4 [file] [log] [blame]
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
part of vmservice;
class RunningIsolates implements MessageRouter {
final Map<int, RunningIsolate> isolates = new Map<int, RunningIsolate>();
int _rootPortId;
RunningIsolates();
void isolateStartup(int portId, SendPort sp, String name) {
if (_rootPortId == null) {
_rootPortId = portId;
}
var ri = new RunningIsolate(portId, sp, name);
isolates[portId] = ri;
}
void isolateShutdown(int portId, SendPort sp) {
if (_rootPortId == portId) {
_rootPortId = null;
}
isolates.remove(portId);
}
Future<String> route(Message message) {
String isolateParam = message.params['isolateId'];
int isolateId;
if (!isolateParam.startsWith('isolates/')) {
message.setErrorResponse('Malformed isolate id $isolateParam');
return message.response;
}
isolateParam = isolateParam.substring('isolates/'.length);
if (isolateParam == 'isolates/root') {
isolateId = _rootPortId;
} else {
try {
isolateId = int.parse(isolateParam);
} catch (e) {
message.setErrorResponse('Could not parse isolate id: $e');
return message.response;
}
}
var isolate = isolates[isolateId];
if (isolate == null) {
message.setErrorResponse('Cannot find isolate id: $isolateId');
return message.response;
}
return isolate.route(message);
}
}