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
use std::net::SocketAddr;
use std::sync::Arc;

use tokio_service;

use crate::jsonrpc::{middleware, FutureResult, MetaIoHandler, Metadata, Middleware};

pub struct Service<M: Metadata = (), S: Middleware<M> = middleware::Noop> {
	handler: Arc<MetaIoHandler<M, S>>,
	peer_addr: SocketAddr,
	meta: M,
}

impl<M: Metadata, S: Middleware<M>> Service<M, S> {
	pub fn new(peer_addr: SocketAddr, handler: Arc<MetaIoHandler<M, S>>, meta: M) -> Self {
		Service {
			peer_addr,
			handler,
			meta,
		}
	}
}

impl<M: Metadata, S: Middleware<M>> tokio_service::Service for Service<M, S> {
	// These types must match the corresponding protocol types:
	type Request = String;
	type Response = Option<String>;

	// For non-streaming protocols, service errors are always io::Error
	type Error = ();

	// The future for computing the response; box it for simplicity.
	type Future = FutureResult<S::Future, S::CallFuture>;

	// Produce a future for computing a response from a request.
	fn call(&self, req: Self::Request) -> Self::Future {
		trace!(target: "tcp", "Accepted request from peer {}: {}", &self.peer_addr, req);
		self.handler.handle_request(&req, self.meta.clone())
	}
}