| 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_dartium; | 5 library vmservice_dartium; |
| 6 | 6 |
| 7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
| 8 import 'vmservice.dart'; | 8 import 'vmservice.dart'; |
| 9 | 9 |
| 10 // The receive port that isolate startup / shutdown messages are delivered on. | 10 // The receive port that isolate startup / shutdown messages are delivered on. |
| 11 RawReceivePort _receivePort; | 11 RawReceivePort _receivePort; |
| 12 // The receive port that service request messages are delivered on. | 12 // The receive port that service request messages are delivered on. |
| 13 RawReceivePort _requestPort; | 13 RawReceivePort _requestPort; |
| 14 | 14 |
| 15 // The native method that is called to post the response back to DevTools. | 15 // The native method that is called to post the response back to DevTools. |
| 16 void postResponse(String response, int cookie) native "VMService_PostResponse"; | 16 void postResponse(String response, int cookie) native "VMService_PostResponse"; |
| 17 | 17 |
| 18 void handleRequest(service, String uri, cookie) { | 18 /// Dartium recieves messages through the _requestPort and posts |
| 19 var serviceRequest = new ServiceRequest(); | 19 /// responses via postResponse. It has a single persistent client. |
| 20 var r = serviceRequest.parse(Uri.parse(uri)); | 20 class DartiumClient extends Client { |
| 21 if (r) { | 21 DartiumClient(port, service) : super(service) { |
| 22 var f = service.runningIsolates.route(serviceRequest); | 22 port.handler = ((message) { |
| 23 assert(f != null); | 23 if (message == null) { |
| 24 f.then((_) { | 24 return; |
| 25 postResponse(serviceRequest.response, cookie); | 25 } |
| 26 }).catchError((e) { | 26 if (message is! List) { |
| 27 // Error posting response back to Dartium. | 27 return; |
| 28 } |
| 29 if (message.length != 2) { |
| 30 return; |
| 31 } |
| 32 if (message[0] is! String) { |
| 33 return; |
| 34 } |
| 35 var uri = Uri.parse(message[0]); |
| 36 var cookie = message[1]; |
| 37 onMessage(cookie, new Message.fromUri(uri)); |
| 28 }); | 38 }); |
| 29 return; | |
| 30 } | 39 } |
| 31 postResponse(serviceRequest.response, cookie); | 40 |
| 41 void post(var seq, String response) { |
| 42 postResponse(response, seq); |
| 43 } |
| 44 |
| 45 dynamic toJson() { |
| 46 var map = super.toJson(); |
| 47 map['type'] = 'DartiumClient'; |
| 48 } |
| 32 } | 49 } |
| 33 | 50 |
| 51 |
| 34 main() { | 52 main() { |
| 35 // Create VmService. | 53 // Create VmService. |
| 36 var service = new VMService(); | 54 var service = new VMService(); |
| 37 _receivePort = service.receivePort; | 55 _receivePort = service.receivePort; |
| 38 _requestPort = new RawReceivePort((message) { | 56 _requestPort = new RawReceivePort(); |
| 39 if (message == null) { | 57 new DartiumClient(_requestPort, service); |
| 40 return; | |
| 41 } | |
| 42 if (message is! List) { | |
| 43 return; | |
| 44 } | |
| 45 if (message.length != 2) { | |
| 46 return; | |
| 47 } | |
| 48 var uri = message[0]; | |
| 49 if (uri is! String) { | |
| 50 return; | |
| 51 } | |
| 52 var cookie = message[1]; | |
| 53 handleRequest(service, uri, cookie); | |
| 54 }); | |
| 55 } | 58 } |
| OLD | NEW |