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 Message { | |
8 final Completer _completer = new Completer.sync(); | |
9 bool get completed => _completer.isCompleted; | |
10 /// Future of response. | |
11 Future<String> get response => _completer.future; | |
12 /// Path. | |
13 final List<String> path = new List<String>(); | |
14 /// Options. | |
15 final Map<String, String> options = new Map<String, String>(); | |
16 | |
17 void _setPath(List<String> pathSegments) { | |
18 if (pathSegments == null) { | |
19 return; | |
20 } | |
21 pathSegments.forEach((String segment) { | |
22 if (segment == null || segment == '') { | |
23 return; | |
24 } | |
25 path.add(segment); | |
26 }); | |
27 } | |
28 | |
29 Message.fromUri(Uri uri) { | |
30 var split = uri.path.split('/'); | |
31 if (split.length == 0) { | |
32 setErrorResponse('Invalid uri: $uri.'); | |
33 return; | |
34 } | |
35 _setPath(split); | |
36 options.addAll(uri.queryParameters); | |
37 } | |
38 | |
39 Message.fromMap(Map map) { | |
40 _setPath(map['path']); | |
41 if (map['options'] != null) { | |
42 options.addAll(map['options']); | |
43 } | |
44 } | |
45 | |
46 dynamic toJson() { | |
47 return { | |
48 'path': path, | |
49 'options': options | |
50 }; | |
51 } | |
52 | |
53 Future<String> send(SendPort sendPort) { | |
54 final receivePort = new RawReceivePort(); | |
55 receivePort.handler = (value) { | |
56 receivePort.close(); | |
57 if (value is Exception) { | |
58 _completer.completeError(value); | |
59 } else { | |
60 _completer.complete(value); | |
61 } | |
62 }; | |
63 var keys = options.keys.toList(); | |
64 var values = options.values.toList(); | |
65 var request = [receivePort.sendPort, path, keys, values]; | |
66 sendServiceMessage(sendPort, request); | |
67 return _completer.future; | |
68 } | |
69 | |
70 void setResponse(String response) { | |
71 _completer.complete(response); | |
72 } | |
73 | |
74 void setErrorResponse(String error) { | |
75 _completer.complete(JSON.encode({ | |
76 'type': 'Error', | |
77 'msg': error, | |
78 'path': path, | |
79 'options': options | |
80 })); | |
81 } | |
82 } | |
OLD | NEW |