diff options
author | Matthias Beyer <matthias.beyer@ifm.com> | 2022-02-25 08:08:53 +0100 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-03-04 15:03:53 +0100 |
commit | 22740fe3147f0c9de9fd966e1c8bab298e02effd (patch) | |
tree | 244689a4a5b7ba286bd6c59e4268fb8d1162938d | |
parent | 9818249ea0f89e4fb71f702a9c00c9f4e998fb0b (diff) |
Add Comms::new_message()
This patch adds the Comms::new_message() function, which can be used to
construct a new message with the origin automatically set correctly.
When the Comms object is passed to a plugin, the plugin can use this
function to automatically create a message that contains the right
origin.
Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r-- | crates/core/tedge_api/examples/heartbeat.rs | 2 | ||||
-rw-r--r-- | crates/core/tedge_api/src/plugins.rs | 9 |
2 files changed, 9 insertions, 2 deletions
diff --git a/crates/core/tedge_api/examples/heartbeat.rs b/crates/core/tedge_api/examples/heartbeat.rs index e8d237f5..f6415897 100644 --- a/crates/core/tedge_api/examples/heartbeat.rs +++ b/crates/core/tedge_api/examples/heartbeat.rs @@ -83,7 +83,7 @@ async fn main() { let hsb = HeartbeatServiceBuilder; let (sender, mut receiver) = tokio::sync::mpsc::channel(10); - let comms = Comms::new(sender); + let comms = Comms::new("heartbeat-service".to_string(), sender); let config = toml::from_str( r#" diff --git a/crates/core/tedge_api/src/plugins.rs b/crates/core/tedge_api/src/plugins.rs index 67a91964..dc7b1496 100644 --- a/crates/core/tedge_api/src/plugins.rs +++ b/crates/core/tedge_api/src/plugins.rs @@ -17,18 +17,25 @@ type ReplyReceiver = tokio::sync::oneshot::Receiver<Message>; #[derive(Clone)] pub struct Comms { + plugin_name: String, sender: tokio::sync::mpsc::Sender<Message>, replymap: Arc<RwLock<HashMap<uuid::Uuid, ReplySender>>>, } impl Comms { - pub fn new(sender: tokio::sync::mpsc::Sender<Message>) -> Self { + pub fn new(plugin_name: String, sender: tokio::sync::mpsc::Sender<Message>) -> Self { Self { + plugin_name, sender, replymap: Arc::new(RwLock::new(HashMap::new())), } } + pub fn new_message(&self, kind: crate::MessageKind) -> Message { + let addr = crate::Address::new(crate::address::EndpointKind::Plugin { id: self.plugin_name.clone() }); + Message::new(addr, kind) + } + pub async fn send<T: Into<Message>>(&self, msg: T) -> Result<(), PluginError> { self.sender.send(msg.into()).await?; |