pub struct Proc { /* private fields */ }Expand description
A remote procedure published in netidx
Implementations§
Source§impl Proc
impl Proc
Sourcepub fn new<T: Send + 'static, F: FnMut(RpcCall) -> Option<T> + Send + 'static>(
publisher: &Publisher,
name: Path,
doc: Value,
args: impl IntoIterator<Item = ArgSpec>,
map: F,
handler: Option<Sender<T>>,
) -> Result<Proc>
pub fn new<T: Send + 'static, F: FnMut(RpcCall) -> Option<T> + Send + 'static>( publisher: &Publisher, name: Path, doc: Value, args: impl IntoIterator<Item = ArgSpec>, map: F, handler: Option<Sender<T>>, ) -> Result<Proc>
Publish a new remote procedure. If successful this will return
a Proc which, if dropped, will cause the removal of the
procedure from netidx.
§Arguments
publisher- A reference to the publisher that will publish the procedure.name- The path of the procedure in netidx.doc- The procedure level doc string to be published along with the procedureargs- An iterator containing the procedure argumentsmap- A function that will map the raw parameters into the type of the channel. if it returns None then nothing will be pushed into the channel.handler- The channel that will receive the rpc call invocations (if any)
If you can handle the procedure entirely without async (or blocking) then you only need to define map, you don’t need to pass a handler channel. Your map function should handle the call, reply to the client, and return None.
If you need to do something async in order to handle the call, then you must pass an mpsc channel that will receive the output of your map function. You can define as little or as much slack as you desire, however be aware that if the channel fills up then clients attempting to call your procedure will wait.
§Example
#[macro_use] extern crate netidx_protocols;
use netidx::{path::Path, subscriber::Value};
use netidx_protocols::rpc::server::{Proc, ArgSpec, RpcCall};
use arcstr::ArcStr;
let echo = define_rpc!(
&publisher,
Path::from("/examples/api/echo"),
"echos it's argument",
|mut c: RpcCall, arg: Value| -> Option<()> {
c.reply.send(arg);
None
},
None,
arg: Value = Value::Null; "argument to echo"
);§Notes
If more than one publisher is publishing the same compatible RPC (same arguments, same name, hopefully the same semantics!), then clients will randomly pick one procedure from the set at client creation time.
Arguments with the same key that are specified multiple times will overwrite previous versions; the procedure will receive only the last version set. *