Add namespaces from structs and restructure exports

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/10/head
trivernis 3 years ago
parent b1f0ad5131
commit 21d8f7afcd
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

2
Cargo.lock generated

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

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

@ -76,21 +76,39 @@ async fn main() {
**Server:**
```rust
use rmp_ipc::IPCBuilder;
use rmp_ipc::{IPCBuilder, EventHandler, namespace, command, Event, context::Context};
// create the server
pub struct MyNamespace;
impl MyNamespace {
async fn ping(_ctx: &Context, _event: Event) -> Result<()> {
println!("My namespace received a ping");
Ok(())
}
}
impl NamespaceProvider for MyNamespace {
fn name() -> String {String::from("my_namespace")}
fn register(handler: &mut EventHandler) {
handler.on("ping", callback!(Self::ping))
}
}
#[tokio::main]
async fn main() {
IPCBuilder::new()
.address("127.0.0.1:2020")
// register namespace
.namespace("mainspace-server")
// register callback (without macro)
// register callback
.on("ping", |_ctx, _event| Box::pin(async move {
println!("Received ping event.");
Ok(())
}))
.build()
.add_namespace(namespace!(MyNamespace))
.build_server().await.unwrap();
}
```

@ -1,6 +1,6 @@
use crate::error::{Error, Result};
use crate::event::Event;
use crate::ipc::stream_emitter::StreamEmitter;
use crate::Event;
use std::collections::HashMap;
use std::mem;
use std::sync::Arc;
@ -11,11 +11,9 @@ use typemap_rev::TypeMap;
/// An object provided to each callback function.
/// Currently it only holds the event emitter to emit response events in event callbacks.
/// ```rust
/// use rmp_ipc::context::Context;
/// use rmp_ipc::Event;
/// use rmp_ipc::error::Result;
/// use rmp_ipc::prelude::*;
///
/// async fn my_callback(ctx: &Context, _event: Event) -> Result<()> {
/// async fn my_callback(ctx: &Context, _event: Event) -> IPCResult<()> {
/// // use the emitter on the context object to emit events
/// // inside callbacks
/// ctx.emitter.emit("ping", ()).await?;

@ -1,8 +1,7 @@
use crate::context::Context;
use crate::error_event::{ErrorEventData, ERROR_EVENT_NAME};
use crate::events::event_handler::EventHandler;
use crate::namespaces::namespace::Namespace;
use crate::Event;
use crate::prelude::*;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::net::tcp::OwnedReadHalf;

@ -1,6 +1,6 @@
use crate::context::Context;
use crate::error::Result;
use crate::events::event::Event;
use crate::ipc::context::Context;
use serde::Serialize;
use std::sync::Arc;
use tokio::io::AsyncWriteExt;

@ -2,16 +2,33 @@
//! messagepack. All calls are asynchronous and event based.
//! Client Example:
//! ```no_run
//! use rmp_ipc::{callback, Event, context::Context, IPCBuilder, error::Result};
//! use rmp_ipc::prelude::*;
//!
//! /// Callback ping function
//! async fn handle_ping(ctx: &Context, event: Event) -> Result<()> {
//! async fn handle_ping(ctx: &Context, event: Event) -> IPCResult<()> {
//! println!("Received ping event.");
//! ctx.emitter.emit_response(event.id(), "pong", ()).await?;
//!
//! Ok(())
//! }
//!
//! pub struct MyNamespace;
//!
//! impl MyNamespace {
//! async fn ping(_ctx: &Context, _event: Event) -> IPCResult<()> {
//! println!("My namespace received a ping");
//! Ok(())
//! }
//! }
//!
//! impl NamespaceProvider for MyNamespace {
//! fn name() -> String {String::from("my_namespace")}
//!
//! fn register(handler: &mut EventHandler) {
//! handler.on("ping", callback!(Self::ping))
//! }
//!}
//!
//! #[tokio::main]
//! async fn main() {
//! // create the client
@ -27,6 +44,7 @@
//! Ok(())
//! }))
//! .build()
//! .add_namespace(namespace!(MyNamespace))
//! .build_client().await.unwrap();
//!
//! // emit an initial event
@ -81,14 +99,29 @@ mod tests;
pub mod error;
mod events;
mod ipc;
pub mod ipc;
mod macros;
mod namespaces;
pub use events::error_event;
pub use events::event::Event;
pub use events::event;
pub use events::event_handler;
pub use ipc::builder::IPCBuilder;
pub use ipc::*;
pub use macros::*;
pub use namespaces::builder::NamespaceBuilder;
pub use namespaces::namespace::Namespace;
pub use namespaces::namespace;
pub use namespaces::provider_trait;
pub mod prelude {
pub use crate::error::Error as IPCError;
pub use crate::error::Result as IPCResult;
pub use crate::event::Event;
pub use crate::event_handler::EventHandler;
pub use crate::ipc::context::Context;
pub use crate::ipc::*;
pub use crate::macros::*;
pub use crate::namespace::Namespace;
pub use crate::namespaces::builder::NamespaceBuilder;
pub use crate::namespaces::provider_trait::*;
pub use crate::*;
}

@ -3,7 +3,17 @@ macro_rules! callback {
($cb:ident) => {
|ctx, event| Box::pin($cb(ctx, event))
};
($cb:path) => {
|ctx, event| Box::pin($cb(ctx, event))
};
($ctx:ident, $event:ident,$cb:expr) => {
move |$ctx, $event| Box::pin($cb)
};
}
#[macro_export]
macro_rules! namespace {
($nsp:path) => {
Namespace::from_provider::<$nsp>()
};
}

@ -1,8 +1,9 @@
use crate::context::Context;
use crate::error::Result;
use crate::event::Event;
use crate::events::event_handler::EventHandler;
use crate::ipc::context::Context;
use crate::namespaces::namespace::Namespace;
use crate::{Event, IPCBuilder};
use crate::IPCBuilder;
use std::future::Future;
use std::pin::Pin;

@ -1,2 +1,3 @@
pub mod builder;
pub mod namespace;
pub mod provider_trait;

@ -0,0 +1,17 @@
use crate::events::event_handler::EventHandler;
use crate::namespace::Namespace;
pub trait NamespaceProvider {
fn name() -> String;
fn register(handler: &mut EventHandler);
}
impl Namespace {
pub fn from_provider<N: NamespaceProvider>() -> Self {
let name = N::name();
let mut handler = EventHandler::new();
N::register(&mut handler);
Self::new(name, handler)
}
}

@ -1,17 +1,12 @@
use self::super::utils::PingEventData;
use crate::callback;
use crate::context::Context;
use crate::error::Error;
use crate::error::Result;
use crate::events::error_event::ErrorEventData;
use crate::prelude::*;
use crate::tests::utils::start_test_server;
use crate::{Event, IPCBuilder};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use typemap_rev::TypeMapKey;
async fn handle_ping_event(ctx: &Context, e: Event) -> Result<()> {
async fn handle_ping_event(ctx: &Context, e: Event) -> IPCResult<()> {
let mut ping_data = e.data::<PingEventData>()?;
ping_data.time = SystemTime::now();
ping_data.ttl -= 1;
@ -70,6 +65,25 @@ fn get_builder_with_ping_mainspace(address: &str) -> IPCBuilder {
.address(address)
}
pub struct TestNamespace;
impl TestNamespace {
async fn ping(_c: &Context, _e: Event) -> IPCResult<()> {
println!("Ping received");
Ok(())
}
}
impl NamespaceProvider for TestNamespace {
fn name() -> String {
String::from("Test")
}
fn register(handler: &mut EventHandler) {
handler.on("ping", callback!(Self::ping))
}
}
#[tokio::test]
async fn it_receives_namespaced_events() {
let builder = get_builder_with_ping_mainspace("127.0.0.1:8282");
@ -85,7 +99,11 @@ async fn it_receives_namespaced_events() {
while !server_running.load(Ordering::Relaxed) {
tokio::time::sleep(Duration::from_millis(10)).await;
}
let ctx = builder.build_client().await.unwrap();
let ctx = builder
.add_namespace(namespace!(TestNamespace))
.build_client()
.await
.unwrap();
let reply = ctx
.emitter
.emit_to(
@ -114,12 +132,12 @@ fn get_builder_with_error_handling(error_occurred: Arc<AtomicBool>, address: &st
IPCBuilder::new()
.insert::<ErrorOccurredKey>(error_occurred)
.on("ping", move |_, _| {
Box::pin(async move { Err(Error::from("ERRROROROROR")) })
Box::pin(async move { Err(IPCError::from("ERRROROROROR")) })
})
.on(
"error",
callback!(ctx, event, async move {
let error = event.data::<ErrorEventData>()?;
let error = event.data::<error_event::ErrorEventData>()?;
assert!(error.message.len() > 0);
assert_eq!(error.code, 500);
{

Loading…
Cancel
Save