OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014, 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 library json_rpc_2.peer; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import '../error_code.dart' as error_code; | |
10 import 'client.dart'; | |
11 import 'exception.dart'; | |
12 import 'parameters.dart'; | |
13 import 'server.dart'; | |
14 import 'two_way_stream.dart'; | |
15 | |
16 /// A JSON-RPC 2.0 client *and* server. | |
17 /// | |
18 /// This supports bidirectional peer-to-peer communication with another JSON-RPC | |
19 /// 2.0 endpoint. It sends both requests and responses across the same | |
20 /// communication channel and expects to connect to a peer that does the same. | |
21 class Peer implements Client, Server { | |
22 TwoWayStream _streams; | |
23 | |
24 /// The underlying client that handles request-sending and response-receiving | |
25 /// logic. | |
26 Client _client; | |
27 | |
28 /// The underlying server that handles request-receiving and response-sending | |
29 /// logic. | |
30 Server _server; | |
31 | |
32 /// A stream controller that forwards incomping messages to [_server] if | |
Bob Nystrom
2014/11/07 17:41:51
"incoming", here and below.
Also, referencing a p
nweiz
2014/11/10 21:45:30
Done.
| |
33 /// they're requests. | |
34 final serverIncomingForwarder = new StreamController(sync: true); | |
35 | |
36 /// A stream controller that forwards incomping messages to [_client] if | |
37 /// they're responses. | |
38 final clientIncomingForwarder = new StreamController(sync: true); | |
39 | |
40 /// A stream controller that forwards outgoing messages from both [_server] | |
41 /// and [_client]. | |
42 final outgoingForwarder = new StreamController(sync: true); | |
43 | |
44 /// Creates a [Peer] that reads incoming messages from [incoming] and writes | |
45 /// outgoing messages to [outgoing]. | |
46 /// | |
47 /// If [incoming] is a [StreamSink] as well as a [Stream] (for example, a | |
48 /// `WebSocket`), [outgoing] may be omitted. | |
49 /// | |
50 /// Note that the peer won't begin listening to [incoming] until [Peer.listen] | |
51 /// is called. | |
52 Peer(Stream<String> incoming, [StreamSink<String> outgoing]) { | |
53 _streams = new TwoWayStream("Peer", incoming, "incoming", | |
54 outgoing, "outgoing", onInvalidInput: (message, error) { | |
55 _streams.add(new RpcException(error_code.PARSE_ERROR, | |
56 'Invalid JSON: ${error.message}').serialize(message)); | |
57 }); | |
58 | |
59 outgoingForwarder.stream.listen(_streams.add); | |
60 _server = new Server.withoutJson( | |
61 serverIncomingForwarder.stream, outgoingForwarder); | |
62 _client = new Client.withoutJson( | |
63 clientIncomingForwarder.stream, outgoingForwarder); | |
64 } | |
65 | |
66 /// Creates a [Peer] that reads incoming decoded messages from [incoming] and | |
67 /// writes outgoing decoded messages to [outgoing]. | |
68 /// | |
69 /// Unlike [new Peer], this doesn't read or write JSON strings. Instead, it | |
70 /// reads and writes decoded maps or lists. | |
71 /// | |
72 /// If [incoming] is a [StreamSink] as well as a [Stream], [outgoing] may be | |
73 /// omitted. | |
74 /// | |
75 /// Note that the peer won't begin listening to [incoming] until | |
76 /// [Peer.listen] is called. | |
77 Peer.withoutJson(Stream incoming, [StreamSink outgoing]) { | |
78 _streams = new TwoWayStream.withoutJson("Peer", incoming, "incoming", | |
79 outgoing, "outgoing"); | |
80 | |
81 outgoingForwarder.stream.listen(_streams.add); | |
82 _server = new Server.withoutJson( | |
83 serverIncomingForwarder.stream, outgoingForwarder); | |
84 _client = new Client.withoutJson( | |
85 clientIncomingForwarder.stream, outgoingForwarder); | |
86 } | |
87 | |
88 // Client methods. | |
89 | |
90 Future sendRequest(String method, [parameters]) => | |
91 _client.sendRequest(method, parameters); | |
92 | |
93 void sendNotification(String method, [parameters]) => | |
94 _client.sendNotification(method, parameters); | |
95 | |
96 withBatch(callback()) => _client.withBatch(callback); | |
97 | |
98 // Server methods. | |
99 | |
100 void registerMethod(String name, Function callback) => | |
101 _server.registerMethod(name, callback); | |
102 | |
103 void registerFallback(callback(Parameters parameters)) => | |
104 _server.registerFallback(callback); | |
105 | |
106 // Shared methods. | |
107 | |
108 Future listen() { | |
109 _client.listen(); | |
110 _server.listen(); | |
111 return _streams.listen((message) { | |
112 if (message is Map) { | |
113 if (message.containsKey('result') || message.containsKey('error')) { | |
114 clientIncomingForwarder.add(message); | |
115 } else { | |
116 serverIncomingForwarder.add(message); | |
117 } | |
118 } else if (message is List) { | |
119 if (message.isEmpty) return; | |
120 if (message.first.containsKey('result') || | |
Bob Nystrom
2014/11/07 17:41:51
What if message.first isn't a Map?
nweiz
2014/11/10 21:45:30
Done.
| |
121 message.first.containsKey('error')) { | |
122 clientIncomingForwarder.add(message); | |
123 } else { | |
124 serverIncomingForwarder.add(message); | |
125 } | |
126 } else { | |
127 // Non-Map and -List messages are ill-formed, so we pass them to the | |
128 // server since it knows how to send error responses. | |
129 serverIncomingForwarder.add(message); | |
130 } | |
131 }); | |
132 } | |
133 | |
134 Future close() => | |
135 Future.wait([_client.close(), _server.close(), _streams.close()]); | |
136 } | |
OLD | NEW |