rmp_ipc/
lib.rs

1//! This project provides an ipc server and client implementation using
2//! messagepack. All calls are asynchronous and event based.
3//! Client Example:
4//! ```no_run
5//! use rmp_ipc::prelude::*;
6//!
7//! /// Callback ping function
8//! async fn handle_ping(ctx: &Context, event: Event) -> IPCResult<()> {
9//!     println!("Received ping event.");
10//!     ctx.emitter.emit_response(event.id(), "pong", ()).await?;
11//!
12//!     Ok(())
13//! }
14//!
15//! pub struct MyNamespace;
16//!
17//! impl MyNamespace {
18//!     async fn ping(_ctx: &Context, _event: Event) -> IPCResult<()> {
19//!         println!("My namespace received a ping");
20//!         Ok(())
21//!     }
22//! }
23//!
24//! impl NamespaceProvider for MyNamespace {
25//!     fn name() -> &'static str {"my_namespace"}
26//!
27//!     fn register(handler: &mut EventHandler) {
28//!         events!(handler,
29//!             "ping" => Self::ping,
30//!             "ping2" => Self::ping
31//!         );
32//!     }
33//!}
34//!
35//! #[tokio::main]
36//! async fn main() {
37//!     // create the client
38//!     let ctx = IPCBuilder::new()
39//!         .address("127.0.0.1:2020")
40//!         // register callback
41//!         .on("ping", callback!(handle_ping))
42//!         .namespace("mainspace-client")
43//!         // register callback inline
44//!         .on("something", callback!(ctx, event, async move {
45//!             println!("I think the server did something");
46//!             ctx.emitter.emit_response_to(event.id(), "mainspace-server", "ok", ()).await?;
47//!             Ok(())
48//!         }))
49//!         .build()
50//!         .add_namespace(namespace!(MyNamespace))
51//!         .build_client().await.unwrap();
52//!
53//!     // emit an initial event
54//!     let response = ctx.emitter.emit("ping", ()).await.unwrap().await_reply(&ctx).await.unwrap();
55//!     assert_eq!(response.name(), "pong");
56//! }
57//! ```
58//!
59//! Server Example:
60//! ```no_run
61//! use typemap_rev::TypeMapKey;
62//! use rmp_ipc::IPCBuilder;
63//! use rmp_ipc::callback;
64//!
65//! struct MyKey;
66//!
67//! impl TypeMapKey for MyKey {
68//!     type Value = u32;
69//! }
70//!
71//! // create the server
72//!# async fn a() {
73//! IPCBuilder::new()
74//!     .address("127.0.0.1:2020")
75//!     // register callback
76//!     .on("ping", callback!(ctx, event, async move {
77//!         println!("Received ping event.");
78//!         ctx.emitter.emit_response(event.id(), "pong", ()).await?;
79//!         Ok(())
80//!     }))
81//!     .namespace("mainspace-server")
82//!     .on("do-something", callback!(ctx, event, async move {
83//!         println!("Doing something");
84//!         {
85//!             // access data
86//!             let mut data = ctx.data.write().await;
87//!             let mut my_key = data.get_mut::<MyKey>().unwrap();
88//!             *my_key += 1;
89//!         }
90//!         ctx.emitter.emit_response_to(event.id(), "mainspace-client", "something", ()).await?;
91//!         Ok(())
92//!     }))
93//!     .build()
94//!     // store additional data
95//!     .insert::<MyKey>(3)
96//!     .build_server().await.unwrap();
97//! # }
98//! ```
99
100#[cfg(test)]
101mod tests;
102
103pub mod error;
104mod events;
105pub mod ipc;
106mod macros;
107mod namespaces;
108
109pub use events::error_event;
110pub use events::event;
111pub use events::event_handler;
112pub use events::payload;
113pub use ipc::builder::IPCBuilder;
114pub use ipc::context;
115pub use macros::*;
116pub use namespaces::builder::NamespaceBuilder;
117pub use namespaces::namespace;
118pub use namespaces::provider_trait;
119
120pub mod prelude {
121    pub use crate::error::Error as IPCError;
122    pub use crate::error::Result as IPCResult;
123    pub use crate::event::Event;
124    pub use crate::event_handler::EventHandler;
125    pub use crate::ipc::context::Context;
126    pub use crate::ipc::context::{PoolGuard, PooledContext};
127    pub use crate::ipc::*;
128    pub use crate::macros::*;
129    pub use crate::namespace::Namespace;
130    pub use crate::namespaces::builder::NamespaceBuilder;
131    pub use crate::namespaces::provider_trait::*;
132    pub use crate::payload::*;
133    pub use crate::*;
134}