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 dart._vmservice; | 5 part of dart._vmservice; |
6 | 6 |
| 7 typedef void ClientServiceHandle(Message response); |
| 8 |
7 // A service client. | 9 // A service client. |
8 abstract class Client { | 10 abstract class Client { |
9 final VMService service; | 11 final VMService service; |
10 final bool sendEvents; | 12 final bool sendEvents; |
11 final Set<String> streams = new Set<String>(); | 13 final Set<String> streams = new Set<String>(); |
| 14 final Map<String, String> services = new Map<String, String>(); |
| 15 final Map<String, ClientServiceHandle> serviceHandles = |
| 16 new Map<String, ClientServiceHandle>(); |
12 | 17 |
13 Client(this.service, {bool sendEvents: true}) : this.sendEvents = sendEvents { | 18 Client(this.service, {bool sendEvents: true}) : this.sendEvents = sendEvents { |
14 service._addClient(this); | 19 service._addClient(this); |
15 } | 20 } |
16 | 21 |
17 // Disconnects the client. | 22 // Disconnects the client. |
18 disconnect(); | 23 disconnect(); |
19 | 24 |
20 /// When implementing, call [close] when the network connection closes. | 25 /// When implementing, call [close] when the network connection closes. |
21 void close() { | 26 void close() { |
22 service._removeClient(this); | 27 service._removeClient(this); |
23 } | 28 } |
24 | 29 |
25 /// Call to process a message. Response will be posted with 'seq'. | 30 /// Call to process a request. Response will be posted with 'seq'. |
26 void onMessage(var seq, Message message) { | 31 void onRequest(Message message) { |
27 try { | 32 // In JSON-RPC 2.0 messages with and id are Request and must be answered |
28 // Send message to service. | 33 // http://www.jsonrpc.org/specification#notification |
29 service.route(message).then((response) { | 34 service.routeRequest(message).then((response) => post(response)); |
30 // Call post when the response arrives. | 35 } |
31 post(response); | 36 |
32 }); | 37 void onResponse(Message message) { |
33 } catch (e, st) { | 38 service.routeResponse(message); |
34 message.setErrorResponse(kInternalError, 'Unexpected exception:$e\n$st'); | 39 } |
35 post(message.response); | 40 |
36 } | 41 /// Call to process a notification. Response will not be posted. |
| 42 void onNotification(Message message) { |
| 43 // In JSON-RPC 2.0 messages without an id are Notification |
| 44 // and should not be answered |
| 45 // http://www.jsonrpc.org/specification#notification |
| 46 service.routeRequest(message); |
37 } | 47 } |
38 | 48 |
39 // Sends a result to the client. Implemented in subclasses. | 49 // Sends a result to the client. Implemented in subclasses. |
40 void post(dynamic result); | 50 void post(dynamic result); |
41 | 51 |
42 dynamic toJson() { | 52 dynamic toJson() { |
43 return {}; | 53 return {}; |
44 } | 54 } |
45 } | 55 } |
OLD | NEW |