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
#![allow(dead_code, unused_must_use)]
#[macro_use]
extern crate log;

extern crate tokio_io as tio;
extern crate tokio_core as tcore;
extern crate tokio_proto as tproto;
extern crate tokio_service as tservice;
extern crate futures;
extern crate futures_cpupool;
extern crate serde_json;
extern crate bytes;
extern crate byteorder;

mod packets;
mod codecs;
mod service;

use tproto::{TcpClient, TcpServer};
use tproto::multiplex::{ClientProto, ServerProto, ClientService};
use tio::AsyncRead;
use tio::codec::Framed;
use tcore::net::TcpStream;
use tcore::reactor::Handle;
use futures::{Future, BoxFuture};
use tservice::Service;
use serde_json::value::Value as Json;

use std::collections::BTreeMap;
use std::io;
use std::sync::Arc;
use std::sync::atomic::{AtomicIsize, Ordering};
use std::net::SocketAddr;

use packets::*;
use codecs::*;
use service::*;

pub type JsonRpcFn = RpcFn<Json>;
pub type JsonRpcFnSync = RpcFnSync<Json>;

struct JsonSlacker;

impl ServerProto<TcpStream> for JsonSlacker {
    type Request = SlackerPacket<Json>;
    type Response = SlackerPacket<Json>;
    type Transport = Framed<TcpStream, JsonSlackerCodec>;
    type BindTransport = io::Result<Self::Transport>;

    fn bind_transport(&self, io: TcpStream) -> Self::BindTransport {
        io.set_nodelay(true);
        Ok(io.framed(JsonSlackerCodec))
    }
}

pub struct Server {
    addr: SocketAddr,
    funcs: Arc<BTreeMap<String, JsonRpcFn>>,
}

impl Server {
    pub fn new(addr: SocketAddr, funcs: BTreeMap<String, JsonRpcFn>) -> Self {
        Server {
            addr,
            funcs: Arc::new(funcs),
        }
    }

    pub fn serve(&self) {
        let new_service = NewSlackerService(self.funcs.clone());
        TcpServer::new(JsonSlacker, self.addr).serve(new_service);
    }
}

pub struct ThreadPoolServer {
    addr: SocketAddr,
    funcs: Arc<BTreeMap<String, JsonRpcFnSync>>,
    threads: usize,
}

impl ThreadPoolServer {
    pub fn new(addr: SocketAddr, funcs: BTreeMap<String, JsonRpcFnSync>, threads: usize) -> Self {
        ThreadPoolServer {
            addr,
            funcs: Arc::new(funcs),
            threads,
        }
    }

    pub fn serve(&self) {
        let new_service = NewSlackerServiceSync(self.funcs.clone(), self.threads);
        TcpServer::new(JsonSlacker, self.addr).serve(new_service);
    }
}

impl ClientProto<TcpStream> for JsonSlacker {
    type Request = SlackerPacket<Json>;
    type Response = SlackerPacket<Json>;
    type Transport = Framed<TcpStream, JsonSlackerCodec>;
    type BindTransport = io::Result<Self::Transport>;

    fn bind_transport(&self, io: TcpStream) -> Self::BindTransport {
        io.set_nodelay(true);
        Ok(io.framed(JsonSlackerCodec))
    }
}

pub struct Client {
    inner: ClientService<TcpStream, JsonSlacker>,
    serial_id_gen: AtomicIsize,
}

impl Service for Client {
    type Request = SlackerPacket<Json>;
    type Response = SlackerPacket<Json>;
    type Error = io::Error;
    type Future = BoxFuture<Self::Response, Self::Error>;

    fn call(&self, req: Self::Request) -> Self::Future {
        self.inner.call(req).boxed()
    }
}

impl Client {
    pub fn connect(addr: &SocketAddr,
                   handle: &Handle)
                   -> Box<Future<Item = Client, Error = io::Error>> {
        let rt = TcpClient::new(JsonSlacker)
            .connect(addr, handle)
            .map(|client_service| {
                     Client {
                         inner: client_service,
                         serial_id_gen: AtomicIsize::new(0),
                     }
                 });
        Box::new(rt)
    }

    pub fn rpc_call(&self,
                    ns_name: &str,
                    fn_name: &str,
                    args: Vec<Json>)
                    -> BoxFuture<Json, io::Error> {
        let mut fname = String::new();
        fname.push_str(ns_name);
        fname.push_str("/");
        fname.push_str(fn_name);

        let sid = self.serial_id_gen.fetch_add(1, Ordering::SeqCst) as i32;

        let packet = SlackerPacket::Request(SlackerRequest {
                                                version: PROTOCOL_VERSION,
                                                serial_id: sid,
                                                content_type: SlackerContentType::JSON,
                                                fname: fname,
                                                arguments: args,
                                            });
        self.call(packet)
            .map(|t| match t {
                     SlackerPacket::Response(r) => r.result,
                     _ => Json::Null,
                 })
            .boxed()
    }
}