From 74886cb68f1277a144cc33b35d93e5ddb06feb1b Mon Sep 17 00:00:00 2001 From: Sergei S Date: Sun, 12 Jun 2022 21:19:07 +0200 Subject: [PATCH] primary sender method in frames --- docs/rpc_blocking.rst | 12 ++++++++++++ src/cli.rs | 16 +++++++++++----- src/lib.rs | 12 ++++++++++++ src/rpc.rs | 4 ++++ src/tools/pubsub.rs | 4 ++++ 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/docs/rpc_blocking.rst b/docs/rpc_blocking.rst index 7aadc10..99ff941 100644 --- a/docs/rpc_blocking.rst +++ b/docs/rpc_blocking.rst @@ -111,6 +111,18 @@ client object: The secondary client is disconnected automatically if the primary one is disconnected or dropped. +Handling frames +~~~~~~~~~~~~~~~ + +ELBUS frames and events have two methods to identify the frame/event sender: + +* **frame.sender()** returns the full peer client name, including secondary + sender suffix. Must be used for RPC replies etc., where it is important to + deliver the frame back to the sender peer. + +* **frame.primary_sender()** returns the primary sender name. Must be used in + logging, ACL processing etc. + Creating secondary clients in other languages --------------------------------------------- diff --git a/src/cli.rs b/src/cli.rs index 396d8c0..fcad145 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -269,9 +269,10 @@ async fn subscribe_topics(client: &mut Client, topics: &[String]) -> Result<(), async fn print_frame(frame: &Frame) { info!("Incoming frame {} byte(s)", fnum!(frame.payload().len())); println!( - "{} from {}", + "{} from {} ({})", frame.kind().to_debug_string().yellow(), - frame.sender().bold() + frame.sender().bold(), + frame.primary_sender() ); if let Some(topic) = frame.topic() { println!("topic: {}", topic.magenta()); @@ -293,9 +294,10 @@ impl RpcHandlers for Handlers { fnum!(event.payload().len()) ); println!( - "{} from {}", + "{} from {} ({})", event.kind().to_debug_string().yellow(), - event.sender().bold() + event.sender().bold(), + event.primary_sender() ); print_payload(event.payload(), false).await; sep(); @@ -313,7 +315,11 @@ impl RpcHandlers for Handlers { .blue() .bold() ); - println!("from {}", event.sender().bold()); + println!( + "from {} ({})", + event.sender().bold(), + event.primary_sender() + ); print_payload(event.payload(), false).await; sep(); Ok(None) diff --git a/src/lib.rs b/src/lib.rs index cee294c..da68f69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -424,6 +424,18 @@ impl FrameData { pub fn sender(&self) -> &str { self.sender.as_ref().unwrap() } + /// # Panics + /// + /// Will panic if called for a prepared frame + #[inline] + pub fn primary_sender(&self) -> &str { + let primary_sender = self.sender.as_ref().unwrap(); + if let Some(pos) = primary_sender.find(SECONDARY_SEP) { + &primary_sender[..pos] + } else { + primary_sender + } + } /// Filled for pub/sub communications #[inline] pub fn topic(&self) -> Option<&str> { diff --git a/src/rpc.rs b/src/rpc.rs index 4214129..26bb34d 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -115,6 +115,10 @@ impl RpcEvent { self.frame.sender() } #[inline] + pub fn primary_sender(&self) -> &str { + self.frame.primary_sender() + } + #[inline] pub fn payload(&self) -> &[u8] { &self.frame().payload()[self.payload_pos..] } diff --git a/src/tools/pubsub.rs b/src/tools/pubsub.rs index e789b73..ad0ffed 100644 --- a/src/tools/pubsub.rs +++ b/src/tools/pubsub.rs @@ -19,6 +19,10 @@ impl Publication { pub fn sender(&self) -> &str { self.frame.sender() } + #[inline] + pub fn primary_sender(&self) -> &str { + self.frame.primary_sender() + } /// # Panics /// /// Will not panic as all processed frames always have topics