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; |
| 13 |
| 14 /// A set streamIds which describes the streams the client is connected to |
11 final Set<String> streams = new Set<String>(); | 15 final Set<String> streams = new Set<String>(); |
12 | 16 |
| 17 /// Services registered and their aliases |
| 18 /// key: service |
| 19 /// value: alias |
| 20 final Map<String, String> services = new Map<String, String>(); |
| 21 |
| 22 /// Callbacks registered for service invocations set to the client |
| 23 /// key: RPC id used for the request |
| 24 /// value: callback that should be invoked |
| 25 final Map<String, ClientServiceHandle> serviceHandles = |
| 26 new Map<String, ClientServiceHandle>(); |
| 27 |
13 Client(this.service, {bool sendEvents: true}) : this.sendEvents = sendEvents { | 28 Client(this.service, {bool sendEvents: true}) : this.sendEvents = sendEvents { |
14 service._addClient(this); | 29 service._addClient(this); |
15 } | 30 } |
16 | 31 |
17 // Disconnects the client. | 32 // Disconnects the client. |
18 disconnect(); | 33 disconnect(); |
19 | 34 |
20 /// When implementing, call [close] when the network connection closes. | 35 /// When implementing, call [close] when the network connection closes. |
21 void close() { | 36 void close() { |
22 service._removeClient(this); | 37 service._removeClient(this); |
23 } | 38 } |
24 | 39 |
25 /// Call to process a message. Response will be posted with 'seq'. | 40 /// Call to process a request. Response will be posted with 'seq'. |
26 void onMessage(var seq, Message message) { | 41 void onRequest(Message message) { |
27 try { | 42 // In JSON-RPC 2.0 messages with and id are Request and must be answered |
28 // Send message to service. | 43 // http://www.jsonrpc.org/specification#notification |
29 service.route(message).then((response) { | 44 service.routeRequest(message).then((response) => post(response)); |
30 // Call post when the response arrives. | 45 } |
31 post(response); | 46 |
32 }); | 47 void onResponse(Message message) { |
33 } catch (e, st) { | 48 service.routeResponse(message); |
34 message.setErrorResponse(kInternalError, 'Unexpected exception:$e\n$st'); | 49 } |
35 post(message.response); | 50 |
36 } | 51 /// Call to process a notification. Response will not be posted. |
| 52 void onNotification(Message message) { |
| 53 // In JSON-RPC 2.0 messages without an id are Notification |
| 54 // and should not be answered |
| 55 // http://www.jsonrpc.org/specification#notification |
| 56 service.routeRequest(message); |
37 } | 57 } |
38 | 58 |
39 // Sends a result to the client. Implemented in subclasses. | 59 // Sends a result to the client. Implemented in subclasses. |
40 void post(dynamic result); | 60 void post(dynamic result); |
41 | 61 |
42 dynamic toJson() { | 62 dynamic toJson() { |
43 return {}; | 63 return {}; |
44 } | 64 } |
45 } | 65 } |
OLD | NEW |