Skip to content

Commit

Permalink
primary sender method in frames
Browse files Browse the repository at this point in the history
  • Loading branch information
divi255 committed Jun 12, 2022
1 parent 74a73fa commit 74886cb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
12 changes: 12 additions & 0 deletions docs/rpc_blocking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------------------------------

Expand Down
16 changes: 11 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
4 changes: 4 additions & 0 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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..]
}
Expand Down
4 changes: 4 additions & 0 deletions src/tools/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 74886cb

Please sign in to comment.