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
mod service;
mod param;
mod client;
pub mod call_context;
pub mod server_api;
pub mod call_context_api;
pub mod param_api;
pub mod client_api;

use std::ops::Deref;
use std::sync::Arc;
use std::collections::HashMap;
use futures;
use futures::Future;

#[derive(Clone)]
pub struct RpcServer {
    inner: Arc<RpcServerImpl>
}

pub struct RpcServerImpl {
    config: RpcServerConfig
}

#[derive(Default)]
pub struct RpcServerConfig {
    pub methods: HashMap<
        String,
        Box<Fn(Vec<param::Param>) -> Box<Future<Item = param::Param, Error = ()>> + Send + Sync>
    >
}

impl Deref for RpcServer {
    type Target = RpcServerImpl;
    fn deref(&self) -> &Self::Target {
        &*self.inner
    }
}

impl RpcServer {
    pub fn new(config: RpcServerConfig) -> RpcServer {
        RpcServer {
            inner: Arc::new(RpcServerImpl {
                config: config
            })
        }
    }
    pub fn call(&self, name: &str, params: Vec<param::Param>) -> Box<Future<Item = param::Param, Error = ()>> {
        if let Some(target) = self.config.methods.get(name) {
            target(params)
        } else {
            Box::new(futures::future::ok(param::Param::Error(Box::new(param::Param::String(
                "Method not found".to_string()
            )))))
        }
    }
}

impl RpcServerConfig {
    pub fn new() -> RpcServerConfig {
        RpcServerConfig::default()
    }
}