jsonrpc-core 4.0.0

Transport agnostic rust implementation of JSON-RPC 2.0 Specification.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! jsonrpc io
use std::sync::Arc;
use std::sync::mpsc;
use std::collections::HashMap;
use serde_json;

use control::{Ready, ResponseHandler, Handler, Data, Subscription, Session};
use commander::{MethodCommand, SyncMethod, SyncMethodCommand, NotificationCommand, SubscriptionCommand};
use request_handler::RequestHandler;
use params::Params;
use id::Id;
use version::Version;
use request::{Request, Notification};
use response::{Response, Failure};
use error::{Error, ErrorCode};
use super::Value;

struct DelegateMethod<T, F> where
	F: Fn(&T, Params) -> Data,
	F: Send + Sync,
	T: Send + Sync {
	delegate: Arc<T>,
	closure: F,
}

impl<T, F> MethodCommand for DelegateMethod<T, F> where
	F: Fn(&T, Params) -> Data,
	F: Send + Sync,
	T: Send + Sync {
	fn execute(&self, params: Params, ready: Ready) {
		let closure = &self.closure;
		ready.ready(closure(&self.delegate, params))
	}
}

struct DelegateAsyncMethod<T, F> where
	F: Fn(&T, Params, Ready),
	F: Send + Sync,
	T: Send + Sync {
	delegate: Arc<T>,
	closure: F,
}

impl<T, F> MethodCommand for DelegateAsyncMethod<T, F> where
	F: Fn(&T, Params, Ready),
	F: Send + Sync,
	T: Send + Sync {
	fn execute(&self, params: Params, ready: Ready) {
		let closure = &self.closure;
		closure(&self.delegate, params, ready)
	}
}

struct DelegateNotification<T, F> where
	F: Fn(&T, Params),
	F: Send + Sync,
	T: Send + Sync {
	delegate: Arc<T>,
	closure: F,
}

impl<T, F> NotificationCommand for DelegateNotification<T, F> where
	F: Fn(&T, Params),
	F: Send + Sync,
	T: Send + Sync {
	fn execute(&self, params: Params) {
		let closure = &self.closure;
		closure(&self.delegate, params)
	}
}

struct DelegateSubscription<T, F> where
	F: Fn(&T, Subscription),
	F: Send + Sync,
	T: Send + Sync {
	delegate: Arc<T>,
	closure: F,
}

impl<T, F> SubscriptionCommand for DelegateSubscription<T, F> where
	F: Fn(&T, Subscription),
	F: Send + Sync,
	T: Send + Sync {
	fn execute(&self, subscription: Subscription) {
		let closure = &self.closure;
		closure(&self.delegate, subscription)
	}
}

/// A set of RPC methods and notifications tied to single `delegate` struct.
pub struct IoDelegate<T> where T: Send + Sync + 'static {
	delegate: Arc<T>,
	methods: HashMap<String, Box<MethodCommand>>,
	notifications: HashMap<String, Box<NotificationCommand>>,
	subscriptions: HashMap<(String, String, String), Box<SubscriptionCommand>>,
}

impl<T> IoDelegate<T> where T: Send + Sync + 'static {
	/// Creates new `IoDelegate`
	pub fn new(delegate: Arc<T>) -> Self {
		IoDelegate {
			delegate: delegate,
			methods: HashMap::new(),
			notifications: HashMap::new(),
			subscriptions: HashMap::new(),
		}
	}

	/// Add new supported method
	pub fn add_method<F>(&mut self, name: &str, closure: F) where F: Fn(&T, Params) -> Result<Value, Error> + Send + Sync + 'static {
		let delegate = self.delegate.clone();
		self.methods.insert(name.to_owned(), Box::new(DelegateMethod {
			delegate: delegate,
			closure: closure
		}));
	}

	/// Add new supported asynchronous method
	pub fn add_async_method<F>(&mut self, name: &str, closure: F) where F: Fn(&T, Params, Ready) + Send + Sync + 'static {
		let delegate = self.delegate.clone();
		self.methods.insert(name.to_owned(), Box::new(DelegateAsyncMethod {
			delegate: delegate,
			closure: closure
		}));
	}

	/// Add new supported subscription
	pub fn add_subscription<F>(&mut self, subscribe: &str, subscription: &str, unsubscribe: &str, closure: F) where F: Fn(&T, Subscription) + Send + Sync + 'static {
		let delegate = self.delegate.clone();
		self.subscriptions.insert((subscribe.into(), subscription.into(), unsubscribe.into()), Box::new(DelegateSubscription {
			delegate: delegate,
			closure: closure,
		}));
	}

	/// Add new supported notification
	pub fn add_notification<F>(&mut self, name: &str, closure: F) where F: Fn(&T, Params) + Send + Sync + 'static {
		let delegate = self.delegate.clone();
		self.notifications.insert(name.to_owned(), Box::new(DelegateNotification {
			delegate: delegate,
			closure: closure
		}));
	}
}

/// Should be used to handle jsonrpc io.
///
/// ```rust
/// extern crate jsonrpc_core;
/// use jsonrpc_core::*;
///
/// fn main() {
/// 	let io = IoHandler::new();
/// 	struct SayHello;
///
/// 	// Implement `SyncMethodCommand` or `AsyncMethodCommand`
/// 	impl SyncMethodCommand for SayHello {
/// 		fn execute(&self, _params: Params) -> Result<Value, Error>  {
/// 			Ok(Value::String("hello".to_string()))
/// 		}
/// 	}
///
/// 	io.add_method("say_hello", SayHello);
/// 	// Or just use closures
/// 	io.add_async_method("say_hello_async", |_params: Params, ready: Ready| {
///			ready.ready(Ok(Value::String("hello".to_string())))
/// 	});
///
/// 	let request1 = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
/// 	let request2 = r#"{"jsonrpc": "2.0", "method": "say_hello_async", "params": [42, 23], "id": 1}"#;
/// 	let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;
///
/// 	assert_eq!(io.handle_request_sync(request1), Some(response.to_string()));
/// 	assert_eq!(io.handle_request_sync(request2), Some(response.to_string()));
/// }
/// ```
pub struct IoHandler {
	request_handler: RequestHandler
}

fn read_request(request_str: &str) -> Result<Request, Error> {
	serde_json::from_str(request_str).map_err(|_| Error::new(ErrorCode::ParseError))
}

fn write_response(response: Response) -> String {
	// this should never fail
	serde_json::to_string(&response).unwrap()
}

fn write_notification(notification: Notification) -> String {
	// this should never fail
	serde_json::to_string(&notification).unwrap()
}

impl IoHandler {
	/// Creates new `IoHandler`
	pub fn new() -> Self {
		IoHandler {
			request_handler: RequestHandler::new()
		}
	}

	/// Add supported method
	pub fn add_method<C>(&self, name: &str, command: C) where C: SyncMethodCommand + 'static {
		self.request_handler.add_method(name.into(), SyncMethod {
			command: command
		})
	}

	/// Add supported asynchronous method
	pub fn add_async_method<C>(&self, name: &str, command: C) where C: MethodCommand + 'static {
		self.request_handler.add_method(name.into(), command)
	}

	/// Add supported notification
	pub fn add_notification<C>(&self, name: &str, command: C) where C: NotificationCommand + 'static {
		self.request_handler.add_notification(name.into(), command)
	}

	/// Add supported subscription
	pub fn add_subscription<C>(&self, subscribe: &str, subscription: &str, unsubscribe: &str, command: C) where C: SubscriptionCommand + 'static {
		self.request_handler.add_subscription(subscribe.into(), subscription.into(), unsubscribe.into(), command);
	}

	/// Add delegate with supported methods.
	pub fn add_delegate<D>(&self, delegate: IoDelegate<D>) where D: Send + Sync {
		self.request_handler.add_methods(delegate.methods);
		self.request_handler.add_notifications(delegate.notifications);
		self.request_handler.add_subscriptions(delegate.subscriptions);
	}

	/// Handle given request synchronously - will block until response is available.
	/// If you have any asynchronous methods in your RPC it is much wiser to use
	/// `handle_request` instead and deal with asynchronous requests in a non-blocking fashion.
	pub fn handle_request_sync(&self, request_str: &str) -> Option<String> {
		let (tx, rx) = mpsc::channel();
		self.handle_request(request_str, move |response: Option<String>| {
			tx.send(response).expect("Receiver is never dropped.");
		});
		// Wait for response.
		rx.recv().expect("Transmitting end is never dropped.")
	}

	/// Handle given request asynchronously.
	pub fn handle_request<H: ResponseHandler<Option<String>, Option<String>> + 'static>(&self, request_str: &str, response_handler: H) {
		self.handle(request_str, response_handler, None);
	}

	fn handle<H: ResponseHandler<Option<String>, Option<String>> + 'static>(&self, request_str: &str, response_handler: H, session: Option<Session>) {
		trace!(target: "rpc", "Request: {} in session.", request_str);

		let handler = Handler::new(
			response_handler,
			move |response: Option<Response>| {
				let response = response.map(write_response);
				debug!(target: "rpc", "Response: {:?}", response);
				response
			},
			move |notification: Notification| {
				let notification = write_notification(notification);
				debug!(target: "rpc", "Notification: {:?}", notification);
				Some(notification)
			},
		);

		let request = read_request(request_str);
		match request {
			Ok(request) => self.request_handler.handle_request(request, handler, session),
			Err(error) => handler.send(Some(Response::from(Failure {
				id: Id::Null,
				jsonrpc: Version::V2,
				error: error
			}))),
		}
	}
}

/// Io Handler with session support
pub trait IoSessionHandler {
	/// Returns a new session object.
	fn session(&self) -> IoSession;
}

impl IoSessionHandler for Arc<IoHandler> {
	fn session(&self) -> IoSession {
		IoSession::new(self.clone())
	}
}

/// Represents a single client connected to this RPC server.
/// The client may send many requests.
pub struct IoSession {
	io_handler: Arc<IoHandler>,
	session: Session,
}

impl IoSession {
	/// Opens up a new session to handle many request and subscriptions.
	/// It should represent a single client.
	pub fn new(handler: Arc<IoHandler>) -> IoSession {
		IoSession {
			io_handler: handler,
			session: Session::default(),
		}
	}

	/// Handle a request within this session.
	pub fn handle_request<H: ResponseHandler<Option<String>, Option<String>> + 'static>(&self, request_str: &str, handler: H) {
		self.io_handler.handle(request_str, handler, Some(self.session.clone()));
	}
}

#[cfg(test)]
mod tests {
	use std::sync::{mpsc, Arc};
	use std::time::Duration;
	use std::thread;
	use super::super::*;

	#[test]
	fn test_io_handler() {
		let io = IoHandler::new();

		struct SayHello;
		impl SyncMethodCommand for SayHello {
			fn execute(&self, _params: Params) -> Result<Value, Error> {
				Ok(Value::String("hello".to_string()))
			}
		}

		io.add_method("say_hello", SayHello);

		let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
		let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;

		assert_eq!(io.handle_request_sync(request), Some(response.to_string()));
	}

	#[test]
	fn test_async_io_handler() {
		let io = IoHandler::new();

		struct SayHelloAsync;
		impl MethodCommand for SayHelloAsync {
			fn execute(&self, _params: Params, ready: Ready) {
				ready.ready(Ok(Value::String("hello".to_string())))
			}
		}

		io.add_async_method("say_hello", SayHelloAsync);

		let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
		let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;

		assert_eq!(io.handle_request_sync(request), Some(response.to_string()));
	}

	#[test]
	fn test_async_io_handler_thread() {
		let io = IoHandler::new();

		struct SayHelloAsync;
		impl MethodCommand for SayHelloAsync {
			fn execute(&self, _params: Params, ready: Ready) {
				thread::spawn(|| {
					thread::sleep(Duration::from_secs(1));
					ready.ready(Ok(Value::String("hello".to_string())))
				});
			}
		}

		io.add_async_method("say_hello", SayHelloAsync);

		let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
		let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;

		assert_eq!(io.handle_request_sync(request), Some(response.to_string()));
	}

	#[test]
	fn test_session_handler_with_subscription() {
		let io = Arc::new(IoHandler::new());

		struct SayHelloSubscription;
		impl SubscriptionCommand for SayHelloSubscription {
			fn execute(&self, subscription: Subscription) {
				match subscription {
					Subscription::Open { subscriber, .. } => {
						let subscriber = subscriber.assign_id(Value::U64(1));
						thread::spawn(move || {
							thread::sleep(Duration::from_millis(10));
							subscriber.notify(Ok(Value::String("hello".to_string())));
							thread::sleep(Duration::from_millis(10));
							subscriber.notify(Ok(Value::String("world".to_string())));
						});
					},
					Subscription::Close { id, .. } => {
						assert_eq!(id, Value::U64(1));
					},
				}
			}
		}

		io.add_subscription("say_hello", "hello_notification", "stop_saying_hello", SayHelloSubscription);

		let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
		let id = r#"{"jsonrpc":"2.0","result":1,"id":1}"#.to_owned();
		let response1 = r#"{"jsonrpc":"2.0","method":"hello_notification","params":{"result":"hello","subscription":1}}"#.to_owned();
		let response2 = r#"{"jsonrpc":"2.0","method":"hello_notification","params":{"result":"world","subscription":1}}"#.to_owned();

		let (tx, rx) = mpsc::channel();
		let session = io.session();
		session.handle_request(request, move |res| tx.send(res).unwrap());

		assert_eq!(rx.recv().unwrap(), Some(id));
		assert_eq!(rx.recv().unwrap(), Some(response1));
		assert_eq!(rx.recv().unwrap(), Some(response2));
		drop(session);
		assert!(rx.recv().is_err());
	}

}