dispatch buf and proper polling

This commit is contained in:
NikVolf 2016-04-05 12:08:42 +03:00
parent 4cde01d81a
commit 0d7e52ac6f
3 changed files with 139 additions and 89 deletions

View File

@ -145,22 +145,6 @@ struct Dispatch {
return_type_ty: Option<P<Ty>>,
}
fn implement_dispatch_arm_invoke(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
dispatch: &Dispatch,
) -> P<ast::Expr>
{
let deserialize_expr = quote_expr!(cx, ::bincode::serde::deserialize_from(r, ::bincode::SizeLimit::Infinite).expect("ipc deserialization error, aborting"));
let input_type_id = builder.id(dispatch.input_type_name.clone().unwrap().as_str());
let function_name = builder.id(dispatch.function_name.as_str());
let output_type_id = builder.id(dispatch.return_type_name.clone().unwrap().as_str());
let input_args_exprs = dispatch.input_arg_names.iter().map(|ref arg_name| {
let arg_ident = builder.id(arg_name);
quote_expr!(cx, input. $arg_ident)
}).collect::<Vec<P<ast::Expr>>>();
// This is the expanded version of this:
//
// let invoke_serialize_stmt = quote_stmt!(cx, {
@ -169,7 +153,20 @@ fn implement_dispatch_arm_invoke(
//
// But the above does not allow comma-separated expressions for arbitrary number
// of parameters ...$hand_param_a, $hand_param_b, ... $hand_param_n
let invoke_serialize_stmt = {
fn implement_dispatch_arm_invoke_stmt(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
dispatch: &Dispatch,
) -> ast::Stmt
{
let function_name = builder.id(dispatch.function_name.as_str());
let output_type_id = builder.id(dispatch.return_type_name.clone().unwrap().as_str());
let input_args_exprs = dispatch.input_arg_names.iter().map(|ref arg_name| {
let arg_ident = builder.id(arg_name);
quote_expr!(cx, input. $arg_ident)
}).collect::<Vec<P<ast::Expr>>>();
let ext_cx = &*cx;
::quasi::parse_stmt_panic(&mut ::syntax::parse::new_parser_from_tts(
ext_cx.parse_sess(),
@ -216,8 +213,25 @@ fn implement_dispatch_arm_invoke(
tt.push(::syntax::ast::TokenTree::Token(_sp, ::syntax::parse::token::CloseDelim(::syntax::parse::token::Paren)));
tt.push(::syntax::ast::TokenTree::Token(_sp, ::syntax::parse::token::CloseDelim(::syntax::parse::token::Brace)));
tt
}))
})).unwrap()
}
fn implement_dispatch_arm_invoke(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
dispatch: &Dispatch,
buffer: bool,
) -> P<ast::Expr>
{
let deserialize_expr = if buffer {
quote_expr!(cx, ::bincode::serde::deserialize(buf).expect("ipc deserialization error, aborting"))
} else {
quote_expr!(cx, ::bincode::serde::deserialize_from(r, ::bincode::SizeLimit::Infinite).expect("ipc deserialization error, aborting"))
};
let input_type_id = builder.id(dispatch.input_type_name.clone().unwrap().as_str());
let invoke_serialize_stmt = implement_dispatch_arm_invoke_stmt(cx, builder, dispatch);
quote_expr!(cx, {
let input: $input_type_id = $deserialize_expr;
$invoke_serialize_stmt
@ -225,14 +239,31 @@ fn implement_dispatch_arm_invoke(
}
/// generates dispatch match for method id
fn implement_dispatch_arm(cx: &ExtCtxt, builder: &aster::AstBuilder, index: u32, dispatch: &Dispatch)
-> ast::Arm
fn implement_dispatch_arm(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
index: u32,
dispatch: &Dispatch,
buffer: bool,
) -> ast::Arm
{
let index_ident = builder.id(format!("{}", index).as_str());
let invoke_expr = implement_dispatch_arm_invoke(cx, builder, dispatch);
let invoke_expr = implement_dispatch_arm_invoke(cx, builder, dispatch, buffer);
quote_arm!(cx, $index_ident => { $invoke_expr } )
}
fn implement_dispatch_arms(
cx: &ExtCtxt,
builder: &aster::AstBuilder,
dispatches: &[Dispatch],
buffer: bool,
) -> Vec<ast::Arm>
{
let mut index = -1;
dispatches.iter()
.map(|dispatch| { index = index + 1; implement_dispatch_arm(cx, builder, index as u32, dispatch, buffer) }).collect()
}
/// generates client type for specified server type
/// for say `Service` it generates `ServiceClient`
fn push_client_struct(cx: &ExtCtxt, builder: &aster::AstBuilder, item: &Item, push: &mut FnMut(Annotatable)) {
@ -511,9 +542,9 @@ fn implement_interface(
dispatch_table.push(push_invoke_signature_aster(builder, &impl_item, signature, push));
}
}
let mut index = -1;
let dispatch_arms: Vec<_> = dispatch_table.iter()
.map(|dispatch| { index = index + 1; implement_dispatch_arm(cx, builder, index as u32, dispatch) }).collect();
let dispatch_arms = implement_dispatch_arms(cx, builder, &dispatch_table, false);
let dispatch_arms_buffered = implement_dispatch_arms(cx, builder, &dispatch_table, true);
Ok((quote_item!(cx,
impl $impl_generics ::ipc::IpcInterface<$ty> for $ty $where_clause {
@ -532,11 +563,10 @@ fn implement_interface(
}
}
fn dispatch_buf<R>(&self, method_num: u16, r: &mut R) -> Vec<u8>
where R: ::std::io::Read
fn dispatch_buf(&self, method_num: u16, buf: &[u8]) -> Vec<u8>
{
match method_num {
$dispatch_arms
$dispatch_arms_buffered
_ => vec![]
}
}

View File

@ -23,13 +23,11 @@ extern crate nanomsg;
pub use ipc::*;
use std::sync::*;
use std::io::{Write, Read};
use nanomsg::{Socket, Protocol, Error, Endpoint};
pub struct Worker<S> where S: IpcInterface<S> {
service: Arc<S>,
sockets: Vec<(Socket, Endpoint)>,
method_buf: [u8;2],
}
#[derive(Debug)]
@ -42,21 +40,26 @@ impl<S> Worker<S> where S: IpcInterface<S> {
Worker::<S> {
service: service.clone(),
sockets: Vec::new(),
method_buf: [0,0]
}
}
pub fn poll(&mut self) {
for item in self.sockets.iter_mut() {
let socket = &mut item.0;
let mut buf = Vec::new();
// non-blocking read only ok if there is something to read from socket
match socket.nb_read(&mut self.method_buf) {
match socket.nb_read_to_end(&mut buf) {
Ok(method_sign_len) => {
if method_sign_len == 2 {
let result = self.service.dispatch_buf(
self.method_buf[1] as u16 * 256 + self.method_buf[0] as u16,
socket);
if let Err(e) = socket.write(&result) {
if method_sign_len >= 2 {
// method_num
let method_num = buf[1] as u16 * 256 + buf[0] as u16;
// payload
let payload = &buf[2..];
// dispatching for ipc interface
let result = self.service.dispatch_buf(method_num, payload);
if let Err(e) = socket.nb_write(&result) {
warn!(target: "ipc", "Failed to write response: {:?}", e);
}
}
@ -67,7 +70,7 @@ impl<S> Worker<S> where S: IpcInterface<S> {
Err(Error::TryAgain) => {
},
Err(x) => {
warn!(target: "ipc", "Error polling connection {:?}", x);
warn!(target: "ipc", "Error polling connections {:?}", x);
panic!();
}
}
@ -97,8 +100,7 @@ mod tests {
use ipc::*;
use std::io::{Read, Write};
use std::sync::{Arc, RwLock};
use nanomsg::{Socket, Protocol};
use std::thread;
use nanomsg::{Socket, Protocol, Endpoint};
struct TestInvoke {
method_num: u16,
@ -119,23 +121,22 @@ mod tests {
fn dispatch<R>(&self, _r: &mut R) -> Vec<u8> where R: Read {
vec![]
}
fn dispatch_buf<R>(&self, method_num: u16, r: &mut R) -> Vec<u8> where R: Read {
let mut buf = vec![0u8; 4096];
let size = r.read_to_end(&mut buf).unwrap();
fn dispatch_buf(&self, method_num: u16, buf: &[u8]) -> Vec<u8> {
self.methods_stack.write().unwrap().push(
TestInvoke {
method_num: method_num,
params: unsafe { Vec::from_raw_parts(buf.as_mut_ptr(), size, size) }
params: buf.to_vec(),
});
vec![]
}
}
fn dummy_write(addr: &str, buf: &[u8]) {
fn dummy_write(addr: &str, buf: &[u8]) -> (Socket, Endpoint) {
let mut socket = Socket::new(Protocol::Pair).unwrap();
let endpoint = socket.connect(addr).unwrap();
thread::sleep_ms(10);
socket.write_all(buf).unwrap();
//thread::sleep_ms(10);
socket.write(buf).unwrap();
(socket, endpoint)
}
#[test]
@ -167,9 +168,28 @@ mod tests {
let mut worker = Worker::<DummyService>::new(Arc::new(DummyService::new()));
worker.add_duplex(url).unwrap();
dummy_write(url, &vec![0, 0, 7, 7, 6, 6]);
worker.poll();
let (_socket, _endpoint) = dummy_write(url, &vec![0, 0, 7, 7, 6, 6]);
for _ in 0..1000 { worker.poll(); }
assert_eq!(1, worker.service.methods_stack.read().unwrap().len());
assert_eq!(0, worker.service.methods_stack.read().unwrap()[0].method_num);
assert_eq!([7, 7, 6, 6], worker.service.methods_stack.read().unwrap()[0].params[..]);
}
#[test]
fn worker_can_poll_long() {
let url = "ipc:///tmp/parity-test30.ipc";
let mut worker = Worker::<DummyService>::new(Arc::new(DummyService::new()));
worker.add_duplex(url).unwrap();
let message = [0u8; 1024*1024];
let (_socket, _endpoint) = dummy_write(url, &message);
for _ in 0..10000 { worker.poll(); }
assert_eq!(1, worker.service.methods_stack.read().unwrap().len());
assert_eq!(0, worker.service.methods_stack.read().unwrap()[0].method_num);
assert_eq!(vec![0u8; 1024*1024-2], worker.service.methods_stack.read().unwrap()[0].params);
}
}

View File

@ -26,7 +26,7 @@ pub trait IpcInterface<T> {
/// deserialize the payload from the io `r` and invokes method specified by `method_num`
/// (for non-blocking io)
fn dispatch_buf<R>(&self, method_num: u16, r: &mut R) -> Vec<u8> where R: Read;
fn dispatch_buf(&self, method_num: u16, buf: &[u8]) -> Vec<u8>;
}
/// serializes method invocation (method_num and parameters) to the stream specified by `w`