|
1 | | -use capnp::capability::Promise; |
2 | | -use capnp_rpc::rpc_twoparty_capnp; |
3 | | -use common::rpc::new_rpc_system; |
4 | | -use futures::Future; |
5 | 1 | use std::error::Error; |
6 | 2 | use std::net::SocketAddr; |
7 | | -use tokio_core::net::TcpStream; |
8 | | -use tokio_core::reactor::Core; |
9 | 3 |
|
10 | 4 | use CLIENT_PROTOCOL_VERSION; |
| 5 | +use common::wrapped::WrappedRcRefCell; |
11 | 6 | use super::session::Session; |
| 7 | +use super::communicator::Communicator; |
12 | 8 |
|
13 | 9 | pub struct Client { |
14 | | - core: Core, |
15 | | - service: ::client_capnp::client_service::Client, |
| 10 | + comm: WrappedRcRefCell<Communicator>, |
16 | 11 | } |
17 | 12 |
|
18 | 13 | impl Client { |
19 | | - pub fn new(scheduler: &SocketAddr) -> Result<Self, Box<Error>> { |
20 | | - let mut core = Core::new()?; |
21 | | - let handle = core.handle(); |
22 | | - let stream = core.run(TcpStream::connect(&scheduler, &handle))?; |
23 | | - stream.set_nodelay(true)?; |
| 14 | + pub fn new(scheduler: SocketAddr) -> Result<Self, Box<Error>> { |
| 15 | + let comm = WrappedRcRefCell::wrap(Communicator::new(scheduler, CLIENT_PROTOCOL_VERSION)?); |
24 | 16 |
|
25 | | - debug!("Connection to server {} established", scheduler); |
26 | | - |
27 | | - let mut rpc = Box::new(new_rpc_system(stream, None)); |
28 | | - let bootstrap: ::server_capnp::server_bootstrap::Client = |
29 | | - rpc.bootstrap(rpc_twoparty_capnp::Side::Server); |
30 | | - handle.spawn(rpc.map_err(|err| panic!("RPC error: {}", err))); |
31 | | - |
32 | | - let mut request = bootstrap.register_as_client_request(); |
33 | | - request.get().set_version(CLIENT_PROTOCOL_VERSION); |
34 | | - |
35 | | - let service = core.run( |
36 | | - request |
37 | | - .send() |
38 | | - .promise |
39 | | - .and_then(|response| Promise::ok(pry!(response.get()).get_service())), |
40 | | - )??; |
41 | | - |
42 | | - Ok(Client { core, service }) |
| 17 | + Ok(Client { |
| 18 | + comm |
| 19 | + }) |
43 | 20 | } |
44 | 21 |
|
45 | | - pub fn new_session(&mut self) -> Result<Session, Box<Error>> { |
46 | | - let id: i32 = self.core.run( |
47 | | - self.service |
48 | | - .new_session_request() |
49 | | - .send() |
50 | | - .promise |
51 | | - .and_then(|response| Promise::ok(pry!(response.get()).get_session_id())), |
52 | | - )?; |
53 | | - |
54 | | - Ok(Session { id }) |
| 22 | + pub fn new_session(&self) -> Result<Session, Box<Error>> { |
| 23 | + let session_id = self.comm.get_mut().new_session()?; |
| 24 | + Ok(Session::new(session_id, self.comm.clone())) |
55 | 25 | } |
56 | 26 |
|
57 | | - pub fn terminate_server(&mut self) -> Result<(), Box<Error>> { |
58 | | - self.core |
59 | | - .run(self.service.terminate_server_request().send().promise)?; |
60 | | - Ok(()) |
| 27 | + pub fn terminate_server(&self) -> Result<(), Box<Error>> { |
| 28 | + self.comm.get_mut().terminate_server() |
61 | 29 | } |
62 | 30 | } |
0 commit comments