Merge pull request #9 from Trivernis/develop

Add callback macro
pull/24/head
Julius Riegel 3 years ago committed by GitHub
commit 06764c1b3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

2
Cargo.lock generated

@ -148,7 +148,7 @@ dependencies = [
[[package]]
name = "rmp-ipc"
version = "0.4.3"
version = "0.4.4"
dependencies = [
"lazy_static",
"log",

@ -1,6 +1,6 @@
[package]
name = "rmp-ipc"
version = "0.4.3"
version = "0.4.4"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
readme = "README.md"

@ -6,33 +6,45 @@ Interprocess Communication via TCP using Rust MessagePack.
**Client:**
```rust
use rmp_ipc::IPCBuilder;
// create the client
let ctx = IPCBuilder::new()
.address("127.0.0.1:2020")
// register callback
.on("ping", |_ctx, _event| Box::pin(async move {
println!("Received ping event.");
Ok(())
}))
.build_client().await.unwrap();
use rmp_ipc::{callback, Event, context::Context, IPCBuilder, error::Result};
/// Callback ping function
async fn handle_ping(ctx: &Context, event: Event) -> Result<()> {
println!("Received ping event.");
ctx.emitter.emit_response(event.id(), "pong", ()).await?;
Ok(())
}
#[tokio::main]
async fn main() {
// create the client
let ctx = IPCBuilder::new()
.address("127.0.0.1:2020")
// register callback
.on("ping", callback!(handle_ping))
.build_client().await.unwrap();
// emit an initial event
let response = ctx.emitter.emit("ping", ()).await?.await_response(&ctx).await?;
let response = ctx.emitter.emit("ping", ()).await?.await_response(&ctx).await?;
}
```
**Server:**
```rust
use rmp_ipc::IPCBuilder;
use rmp_ipc::{IPCBuilder, callback};
// create the server
IPCBuilder::new()
.address("127.0.0.1:2020")
// register callback
.on("ping", |_ctx, _event| Box::pin(async move {
println!("Received ping event.");
Ok(())
}))
.build_server().await.unwrap();
#[tokio::main]
async fn main() {
IPCBuilder::new()
.address("127.0.0.1:2020")
// register callback
.on("ping", callback!(ctx, event, async move {
println!("Received ping event.");
Ok(())
}))
.build_server().await.unwrap();
}
```
### Namespaces
@ -41,38 +53,46 @@ IPCBuilder::new()
```rust
use rmp_ipc::IPCBuilder;
// create the client
let ctx = IPCBuilder::new()
.address("127.0.0.1:2020")
// register namespace
.namespace("mainspace-client")
// register callback
.on("ping", |_ctx, _event| Box::pin(async move {
println!("Received ping event.");
Ok(())
}))
.build()
.build_client().await.unwrap();
#[tokio::main]
async fn main() {
let ctx = IPCBuilder::new()
.address("127.0.0.1:2020")
// register namespace
.namespace("mainspace-client")
// register callback (without macro)
.on("ping", |_ctx, _event| Box::pin(async move {
println!("Received ping event.");
Ok(())
}))
.build()
.build_client().await.unwrap();
// emit an initial event
let response = ctx.emitter.emit_to("mainspace-server", "ping", ()).await?
.await_response(&ctx).await?;
let response = ctx.emitter.emit_to("mainspace-server", "ping", ()).await?
.await_response(&ctx).await?;
}
```
**Server:**
```rust
use rmp_ipc::IPCBuilder;
// create the server
IPCBuilder::new()
.address("127.0.0.1:2020")
// register namespace
.namespace("mainspace-server")
// register callback
.on("ping", |_ctx, _event| Box::pin(async move {
println!("Received ping event.");
Ok(())
}))
.build()
.build_server().await.unwrap();
#[tokio::main]
async fn main() {
IPCBuilder::new()
.address("127.0.0.1:2020")
// register namespace
.namespace("mainspace-server")
// register callback (without macro)
.on("ping", |_ctx, _event| Box::pin(async move {
println!("Received ping event.");
Ok(())
}))
.build()
.build_server().await.unwrap();
}
```
## License

@ -2,37 +2,44 @@
//! messagepack. All calls are asynchronous and event based.
//! Client Example:
//! ```no_run
//! use rmp_ipc::IPCBuilder;
//! // create the client
//! # async fn a() {
//! use rmp_ipc::{callback, Event, context::Context, IPCBuilder, error::Result};
//!
//! let ctx = IPCBuilder::new()
//! .address("127.0.0.1:2020")
//! // register callback
//! .on("ping", |ctx, event| Box::pin(async move {
//! println!("Received ping event.");
//! ctx.emitter.emit_response(event.id(), "pong", ()).await?;
//! Ok(())
//! }))
//! .namespace("mainspace-client")
//! .on("something", |ctx, event| Box::pin(async move {
//! println!("I think the server did something");
//! ctx.emitter.emit_response_to(event.id(), "mainspace-server", "ok", ()).await?;
//! Ok(())
//! }))
//! .build()
//! .build_client().await.unwrap();
//! /// Callback ping function
//! async fn handle_ping(ctx: &Context, event: Event) -> Result<()> {
//! println!("Received ping event.");
//! ctx.emitter.emit_response(event.id(), "pong", ()).await?;
//!
//! // emit an initial event
//! let response = ctx.emitter.emit("ping", ()).await.unwrap().await_reply(&ctx).await.unwrap();
//! assert_eq!(response.name(), "pong");
//! # }
//! Ok(())
//! }
//!
//! #[tokio::main]
//! async fn main() {
//! // create the client
//! let ctx = IPCBuilder::new()
//! .address("127.0.0.1:2020")
//! // register callback
//! .on("ping", callback!(handle_ping))
//! .namespace("mainspace-client")
//! // register callback inline
//! .on("something", callback!(ctx, event, async move {
//! println!("I think the server did something");
//! ctx.emitter.emit_response_to(event.id(), "mainspace-server", "ok", ()).await?;
//! Ok(())
//! }))
//! .build()
//! .build_client().await.unwrap();
//!
//! // emit an initial event
//! let response = ctx.emitter.emit("ping", ()).await.unwrap().await_reply(&ctx).await.unwrap();
//! assert_eq!(response.name(), "pong");
//! }
//! ```
//!
//! Server Example:
//! ```no_run
//! use typemap_rev::TypeMapKey;
//! use rmp_ipc::IPCBuilder;
//! use rmp_ipc::callback;
//!
//! struct MyKey;
//!
@ -45,13 +52,13 @@
//! IPCBuilder::new()
//! .address("127.0.0.1:2020")
//! // register callback
//! .on("ping", |ctx, event| Box::pin(async move {
//! .on("ping", callback!(ctx, event, async move {
//! println!("Received ping event.");
//! ctx.emitter.emit_response(event.id(), "pong", ()).await?;
//! Ok(())
//! }))
//! .namespace("mainspace-server")
//! .on("do-something", |ctx, event| Box::pin(async move {
//! .on("do-something", callback!(ctx, event, async move {
//! println!("Doing something");
//! {
//! // access data
@ -75,11 +82,13 @@ mod tests;
pub mod error;
mod events;
mod ipc;
mod macros;
mod namespaces;
pub use events::error_event;
pub use events::event::Event;
pub use ipc::builder::IPCBuilder;
pub use ipc::*;
pub use macros::*;
pub use namespaces::builder::NamespaceBuilder;
pub use namespaces::namespace::Namespace;

@ -0,0 +1,9 @@
#[macro_export]
macro_rules! callback {
($cb:ident) => {
|ctx, event| Box::pin($cb(ctx, event))
};
($ctx:ident, $event:ident,$cb:expr) => {
move |$ctx, $event| Box::pin($cb)
};
}

@ -1,4 +1,5 @@
use self::super::utils::PingEventData;
use crate::callback;
use crate::context::Context;
use crate::error::Error;
use crate::error::Result;
@ -64,7 +65,7 @@ async fn it_receives_events() {
fn get_builder_with_ping_mainspace(address: &str) -> IPCBuilder {
IPCBuilder::new()
.namespace("mainspace")
.on("ping", |ctx, e| Box::pin(handle_ping_event(ctx, e)))
.on("ping", callback!(handle_ping_event))
.build()
.address(address)
}
@ -115,21 +116,20 @@ fn get_builder_with_error_handling(error_occurred: Arc<AtomicBool>, address: &st
.on("ping", move |_, _| {
Box::pin(async move { Err(Error::from("ERRROROROROR")) })
})
.on("error", {
move |ctx, e| {
Box::pin(async move {
let error = e.data::<ErrorEventData>()?;
assert!(error.message.len() > 0);
assert_eq!(error.code, 500);
{
let data = ctx.data.read().await;
let error_occurred = data.get::<ErrorOccurredKey>().unwrap();
error_occurred.store(true, Ordering::SeqCst);
}
Ok(())
})
}
})
.on(
"error",
callback!(ctx, event, async move {
let error = event.data::<ErrorEventData>()?;
assert!(error.message.len() > 0);
assert_eq!(error.code, 500);
{
let data = ctx.data.read().await;
let error_occurred = data.get::<ErrorOccurredKey>().unwrap();
error_occurred.store(true, Ordering::SeqCst);
}
Ok(())
}),
)
.address(address)
}

Loading…
Cancel
Save