summaryrefslogtreecommitdiffstats
path: root/crates
diff options
context:
space:
mode:
authorMarcel Müller <m.mueller@ifm.com>2022-02-15 10:17:10 +0100
committerMatthias Beyer <matthias.beyer@ifm.com>2022-03-04 15:03:53 +0100
commit16b31b20b7fa0a79544f1a827cee392a2f256789 (patch)
treecc17860e5c72374a734cd14c7eac1690d456b338 /crates
parentdf08d5bd1ea767ddfd67db7de39159f1588f86d5 (diff)
Merge PluginMessage and CoreMessage to Message
Having only a single message type makes the implementation easier and allows for a leaner core. This in turn allows for easier abstractions on the plugin side, as only a single type of messages exists. Signed-off-by: Marcel Müller <m.mueller@ifm.com> Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
Diffstat (limited to 'crates')
-rw-r--r--crates/core/tedge_api/examples/heartbeat.rs24
-rw-r--r--crates/core/tedge_api/src/errors.rs2
-rw-r--r--crates/core/tedge_api/src/lib.rs4
-rw-r--r--crates/core/tedge_api/src/messages.rs14
-rw-r--r--crates/core/tedge_api/src/plugins.rs17
5 files changed, 31 insertions, 30 deletions
diff --git a/crates/core/tedge_api/examples/heartbeat.rs b/crates/core/tedge_api/examples/heartbeat.rs
index cac8b973..e8d237f5 100644
--- a/crates/core/tedge_api/examples/heartbeat.rs
+++ b/crates/core/tedge_api/examples/heartbeat.rs
@@ -1,9 +1,7 @@
use async_trait::async_trait;
use tedge_api::{
- address::EndpointKind,
- messages::{CoreMessageKind, PluginMessageKind},
- plugins::Comms,
- Address, CoreMessage, Plugin, PluginBuilder, PluginConfiguration, PluginError, PluginMessage,
+ address::EndpointKind, Address, Comms, Message, MessageKind, Plugin, PluginBuilder,
+ PluginConfiguration, PluginError,
};
struct HeartbeatServiceBuilder;
@@ -50,21 +48,25 @@ impl HeartbeatService {
#[async_trait]
impl Plugin for HeartbeatService {
async fn setup(&mut self) -> Result<(), PluginError> {
- println!("Setting up heartbeat service with interval: {}!", self.config.interval);
+ println!(
+ "Setting up heartbeat service with interval: {}!",
+ self.config.interval
+ );
Ok(())
}
- async fn handle_message(&self, message: PluginMessage) -> Result<(), PluginError> {
+ async fn handle_message(&self, message: Message) -> Result<(), PluginError> {
match message.kind() {
- tedge_api::messages::PluginMessageKind::CheckReadyness => {
- let msg = CoreMessage::new(
+ MessageKind::CheckReadyness => {
+ let msg = Message::new(
message.origin().clone(),
- CoreMessageKind::SignalPluginState {
+ MessageKind::SignalPluginState {
state: String::from("Ok"),
},
);
self.comms.send(msg).await?;
}
+ msg => println!("Does not handle: {:#?}", msg),
}
Ok(())
@@ -97,9 +99,9 @@ async fn main() {
let handle = tokio::task::spawn(async move {
let hb = heartbeat;
- hb.handle_message(PluginMessage::new(
+ hb.handle_message(Message::new(
Address::new(EndpointKind::Core),
- PluginMessageKind::CheckReadyness,
+ MessageKind::CheckReadyness,
))
.await
.unwrap();
diff --git a/crates/core/tedge_api/src/errors.rs b/crates/core/tedge_api/src/errors.rs
index 88cae4f6..a0775319 100644
--- a/crates/core/tedge_api/src/errors.rs
+++ b/crates/core/tedge_api/src/errors.rs
@@ -4,6 +4,6 @@ use thiserror::Error;
#[error("An error occured while interacting with this plugin")]
pub enum PluginError {
#[error("The sender could not transfer the message to its receiving end. Did it get closed?")]
- CouldNotSendMessage(#[from] tokio::sync::mpsc::error::SendError<crate::CoreMessage>),
+ CouldNotSendMessage(#[from] tokio::sync::mpsc::error::SendError<crate::Message>),
Configuration(#[from] toml::de::Error),
}
diff --git a/crates/core/tedge_api/src/lib.rs b/crates/core/tedge_api/src/lib.rs
index ab9d2f9b..42ee78e3 100644
--- a/crates/core/tedge_api/src/lib.rs
+++ b/crates/core/tedge_api/src/lib.rs
@@ -1,7 +1,7 @@
#![allow(dead_code)]
pub mod plugins;
-pub use plugins::{Plugin, PluginBuilder, PluginConfiguration};
+pub use plugins::{Plugin, PluginBuilder, PluginConfiguration, Comms};
pub mod address;
pub use address::Address;
@@ -10,4 +10,4 @@ pub mod errors;
pub use errors::PluginError;
pub mod messages;
-pub use messages::{CoreMessage, PluginMessage};
+pub use messages::{Message, MessageKind};
diff --git a/crates/core/tedge_api/src/messages.rs b/crates/core/tedge_api/src/messages.rs
index 89c43682..e42ecaf4 100644
--- a/crates/core/tedge_api/src/messages.rs
+++ b/crates/core/tedge_api/src/messages.rs
@@ -2,18 +2,18 @@ use crate::address::Address;
/// A message to be handled by a plugin
#[derive(Debug)]
-pub struct PluginMessage {
+pub struct Message {
origin: Address,
- kind: PluginMessageKind,
+ kind: MessageKind,
}
-impl PluginMessage {
- pub fn new(origin: Address, kind: PluginMessageKind) -> Self {
+impl Message {
+ pub fn new(origin: Address, kind: MessageKind) -> Self {
Self { origin, kind }
}
/// Get a reference to the plugin message's kind.
- pub fn kind(&self) -> &PluginMessageKind {
+ pub fn kind(&self) -> &MessageKind {
&self.kind
}
@@ -24,9 +24,11 @@ impl PluginMessage {
}
#[derive(Debug)]
-pub enum PluginMessageKind {
+#[non_exhaustive]
+pub enum MessageKind {
/// The plugin is being asked if it is currently able to respond
/// to requests. It is meant to reply with `CoreMessageKind` stating
/// its status.
CheckReadyness,
+ SignalPluginState { state: String },
}
diff --git a/crates/core/tedge_api/src/plugins.rs b/crates/core/tedge_api/src/plugins.rs
index 7d23c8bf..66bca8e8 100644
--- a/crates/core/tedge_api/src/plugins.rs
+++ b/crates/core/tedge_api/src/plugins.rs
@@ -6,22 +6,19 @@
use async_trait::async_trait;
-use crate::{
- errors::PluginError,
- messages::{CoreMessage, PluginMessage},
-};
+use crate::{errors::PluginError, messages::Message};
#[derive(Clone)]
pub struct Comms {
- sender: tokio::sync::mpsc::Sender<CoreMessage>,
+ sender: tokio::sync::mpsc::Sender<Message>,
}
impl Comms {
- pub const fn new(sender: tokio::sync::mpsc::Sender<CoreMessage>) -> Self {
+ pub const fn new(sender: tokio::sync::mpsc::Sender<Message>) -> Self {
Self { sender }
}
- pub async fn send<T: Into<CoreMessage>>(&self, msg: T) -> Result<(), PluginError> {
+ pub async fn send<T: Into<Message>>(&self, msg: T) -> Result<(), PluginError> {
self.sender.send(msg.into()).await?;
Ok(())
@@ -62,7 +59,7 @@ pub trait Plugin: Sync + Send {
async fn setup(&mut self) -> Result<(), PluginError>;
/// Handle a message specific to this plugin
- async fn handle_message(&self, message: PluginMessage) -> Result<(), PluginError>;
+ async fn handle_message(&self, message: Message) -> Result<(), PluginError>;
/// Gracefully handle shutdown
async fn shutdown(&mut self) -> Result<(), PluginError>;
@@ -70,7 +67,7 @@ pub trait Plugin: Sync + Send {
#[cfg(test)]
mod tests {
- use super::{Comms, Plugin, PluginBuilder, PluginMessage};
+ use super::{Comms, Plugin, PluginBuilder, Message};
use static_assertions::{assert_impl_all, assert_obj_safe};
// Object Safety
@@ -79,5 +76,5 @@ mod tests {
// Sync + Send
assert_impl_all!(Comms: Send, Clone);
- assert_impl_all!(PluginMessage: Send);
+ assert_impl_all!(Message: Send);
}