client spawner

This commit is contained in:
NikVolf
2016-04-12 10:18:39 +03:00
parent 5609b555d2
commit 40e0d370c2
8 changed files with 72 additions and 16 deletions

View File

@@ -20,10 +20,11 @@ extern crate ethcore_ipc as ipc;
extern crate nanomsg;
#[macro_use] extern crate log;
pub use ipc::*;
pub use ipc::{WithSocket, IpcInterface, IpcConfig};
use std::sync::*;
use nanomsg::{Socket, Protocol, Error, Endpoint, PollRequest, PollFd, PollInOut};
use std::ops::Deref;
const POLL_TIMEOUT: isize = 100;
@@ -34,6 +35,36 @@ pub struct Worker<S> where S: IpcInterface<S> {
buf: Vec<u8>,
}
pub struct GuardedSocket<S> where S: WithSocket<Socket> {
client: Arc<S>,
_endpoint: Endpoint,
}
impl<S> Deref for GuardedSocket<S> where S: WithSocket<Socket> {
type Target = S;
fn deref(&self) -> &S {
&self.client
}
}
pub fn init_client<S>(socket_addr: &str) -> Result<GuardedSocket<S>, SocketError> where S: WithSocket<Socket> {
let mut socket = try!(Socket::new(Protocol::Pair).map_err(|e| {
warn!(target: "ipc", "Failed to create ipc socket: {:?}", e);
SocketError::DuplexLink
}));
let endpoint = try!(socket.bind(socket_addr).map_err(|e| {
warn!(target: "ipc", "Failed to bind socket to address '{}': {:?}", socket_addr, e);
SocketError::DuplexLink
}));
Ok(GuardedSocket {
client: Arc::new(S::init(socket)),
_endpoint: endpoint,
})
}
#[derive(Debug)]
pub enum SocketError {
DuplexLink
@@ -113,7 +144,7 @@ impl<S> Worker<S> where S: IpcInterface<S> {
}
#[cfg(test)]
mod tests {
mod service_tests {
use super::Worker;
use ipc::*;
@@ -150,6 +181,8 @@ mod tests {
}
}
impl IpcConfig for DummyService {}
fn dummy_write(addr: &str, buf: &[u8]) -> (Socket, Endpoint) {
let mut socket = Socket::new(Protocol::Pair).unwrap();
let endpoint = socket.connect(addr).unwrap();