invoke with hand-written code and tests

This commit is contained in:
NikVolf 2016-03-29 01:40:43 +03:00
parent 15ecbaf59c
commit 44ea98801b
4 changed files with 46 additions and 8 deletions

View File

@ -57,6 +57,8 @@ pub fn expand_ipc_implementation(
Err(Error) => { return; } Err(Error) => { return; }
}; };
push_proxy(cx, &builder, &item, push);
push(Annotatable::Item(impl_item)) push(Annotatable::Item(impl_item))
} }
@ -220,6 +222,22 @@ fn implement_dispatch_arm(cx: &ExtCtxt, builder: &aster::AstBuilder, index: u32,
quote_arm!(cx, $index_ident => { $invoke_expr } ) quote_arm!(cx, $index_ident => { $invoke_expr } )
} }
fn push_proxy_struct(cx: &ExtCtxt, builder: &aster::AstBuilder, item: &Item, push: &mut FnMut(Annotatable)) {
let proxy_ident = builder.id(format!("{}Proxy", item.ident.name.as_str()));
let proxy_struct_item = quote_item!(cx,
pub struct $proxy_ident <S: ::ipc::IpcSocket> {
socket: ::std::cell::RefCell<S>,
phantom: ::std::marker::PhantomData<S>,
});
push(Annotatable::Item(proxy_struct_item.expect(&format!("could not generate proxy struct for {:?}", proxy_ident.name))));
}
fn push_proxy(cx: &ExtCtxt, builder: &aster::AstBuilder, item: &Item, push: &mut FnMut(Annotatable)) {
push_proxy_struct(cx, builder, item, push)
}
fn implement_interface( fn implement_interface(
cx: &ExtCtxt, cx: &ExtCtxt,
builder: &aster::AstBuilder, builder: &aster::AstBuilder,
@ -274,11 +292,6 @@ fn implement_interface(
_ => vec![] _ => vec![]
} }
} }
fn invoke<W>(&self, _method_num: u16, _payload: &Option<Vec<u8>>, _w: &mut W)
where W: ::std::io::Write
{
}
} }
).unwrap()) ).unwrap())
} }

View File

@ -7,3 +7,4 @@ license = "GPL-3.0"
[features] [features]
[dependencies] [dependencies]
ethcore-devtools = { path = "../../devtools" }

View File

@ -19,6 +19,28 @@
pub trait IpcInterface<T> { pub trait IpcInterface<T> {
/// reads the message from io, dispatches the call and returns result /// reads the message from io, dispatches the call and returns result
fn dispatch<R>(&self, r: &mut R) -> Vec<u8> where R: ::std::io::Read; fn dispatch<R>(&self, r: &mut R) -> Vec<u8> where R: ::std::io::Read;
/// encodes the invocation, writes payload and waits for result }
fn invoke<W>(&self, method_num: u16, params: &Option<Vec<u8>>, w: &mut W) where W: ::std::io::Write;
pub fn invoke<W>(method_num: u16, params: &Option<Vec<u8>>, w: &mut W) where W: ::std::io::Write {
let buf_len = match *params { None => 2, Some(ref val) => val.len() + 2 };
let mut buf = vec![0u8; buf_len];
buf[0] = (method_num & (255<<8)) as u8;
buf[1] = (method_num >> 8) as u8;
if params.is_some() {
buf[2..buf_len].clone_from_slice(params.as_ref().unwrap());
}
if w.write(&buf).unwrap() != buf_len
{
panic!("failed to write to socket");
}
}
pub trait IpcSocket: ::std::io::Read + ::std::io::Write {
fn ready(&self) -> ::std::sync::atomic::AtomicBool;
}
impl IpcSocket for ::devtools::TestSocket {
fn ready(&self) -> ::std::sync::atomic::AtomicBool {
::std::sync::atomic::AtomicBool::new(true)
}
} }

View File

@ -16,5 +16,7 @@
//! IPC RPC interface //! IPC RPC interface
extern crate ethcore_devtools as devtools;
pub mod interface; pub mod interface;
pub use interface::IpcInterface; pub use interface::{IpcInterface, IpcSocket, invoke};