diff options
-rw-r--r-- | Cargo.lock | 153 | ||||
-rw-r--r-- | crates/core/tedge_api/examples/heartbeat.rs | 23 | ||||
-rw-r--r-- | crates/core/tedge_api/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/core/tedge_api/src/message.rs | 3 | ||||
-rw-r--r-- | crates/core/tedge_api/src/plugin.rs | 34 |
5 files changed, 162 insertions, 53 deletions
@@ -50,9 +50,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.51" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203" +checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27" [[package]] name = "argh" @@ -169,9 +169,9 @@ dependencies = [ [[package]] name = "async-trait" -version = "0.1.51" +version = "0.1.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44318e776df68115a881de9a8fd1b9e53368d7a4a5ce4cc48517da3393233a5e" +checksum = "061a7acccaa286c011ddc30970520b98fa40e00c9d644633fb26b5fc63a265e3" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", @@ -965,7 +965,7 @@ checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" dependencies = [ "cfg-if 1.0.0", "libc", - "wasi", + "wasi 0.10.2+wasi-snapshot-preview1", ] [[package]] @@ -1253,15 +1253,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.108" +version = "0.2.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119" +checksum = "ad5c14e80759d0939d013e6ca49930e59fc53dd8e5009132f76240c179380c09" [[package]] name = "lock_api" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712a4d093c9976e24e7dbca41db895dabcbac38eb5f4045393d17a95bdfb1109" +checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b" dependencies = [ "scopeguard", ] @@ -1350,14 +1350,15 @@ dependencies = [ [[package]] name = "mio" -version = "0.7.14" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8067b404fe97c70829f082dec8bcf4f71225d7eaea1d8645349cb76fa06205cc" +checksum = "7ba42135c6a5917b9db9cd7b293e5409e1c6b041e6f9825e92e55a894c63b6f8" dependencies = [ "libc", "log", "miow", "ntapi", + "wasi 0.11.0+wasi-snapshot-preview1", "winapi", ] @@ -1670,7 +1671,17 @@ checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" dependencies = [ "instant", "lock_api", - "parking_lot_core", + "parking_lot_core 0.8.5", +] + +[[package]] +name = "parking_lot" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f5ec2493a61ac0506c0f4199f99070cbe83857b0337006a30f3e6719b8ef58" +dependencies = [ + "lock_api", + "parking_lot_core 0.9.1", ] [[package]] @@ -1688,6 +1699,19 @@ dependencies = [ ] [[package]] +name = "parking_lot_core" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28141e0cc4143da2443301914478dc976a61ffdb3f043058310c70df2fed8954" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + +[[package]] name = "pem" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2389,9 +2413,9 @@ checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012" [[package]] name = "serde" -version = "1.0.130" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" dependencies = [ "serde_derive", ] @@ -2408,9 +2432,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.130" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", @@ -2447,7 +2471,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0bccbcf40c8938196944a3da0e133e031a33f4d6b72db3bda3cc556e361905d" dependencies = [ "lazy_static", - "parking_lot", + "parking_lot 0.11.2", "serial_test_derive", ] @@ -2507,9 +2531,9 @@ checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309" [[package]] name = "socket2" -version = "0.4.2" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dc90fe6c7be1a323296982db1836d1ea9e47b6839496dde9a541bc496df3516" +checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0" dependencies = [ "libc", "winapi", @@ -2522,6 +2546,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] name = "stats_alloc" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2657,6 +2687,20 @@ dependencies = [ ] [[package]] +name = "tedge_api" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "serde", + "static_assertions", + "thiserror", + "tokio", + "toml", + "uuid", +] + +[[package]] name = "tedge_apt_plugin" version = "0.5.2" dependencies = [ @@ -2912,29 +2956,29 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.14.0" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70e992e41e0d2fb9f755b37446f20900f64446ef54874f40a60c78f021ac6144" +checksum = "2af73ac49756f3f7c01172e34a23e5d0216f6c32333757c2c61feb2bbff5a5ee" dependencies = [ - "autocfg", "bytes", "libc", "memchr", "mio", "num_cpus", "once_cell", - "parking_lot", + "parking_lot 0.12.0", "pin-project-lite", "signal-hook-registry", + "socket2", "tokio-macros", "winapi", ] [[package]] name = "tokio-macros" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9efc1aba077437943f7515666aa2b882dfabfbfdf89c819ea75a8d6e9eaba5e" +checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", @@ -3228,6 +3272,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" [[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" +dependencies = [ + "getrandom", + "serde", +] + +[[package]] name = "value-bag" version = "1.0.0-alpha.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3316,6 +3370,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] name = "wasm-bindgen" version = "0.2.78" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3463,6 +3523,49 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] +name = "windows-sys" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3df6e476185f92a12c072be4a189a0210dcdcf512a1891d6dff9edb874deadc6" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8e92753b1c443191654ec532f14c199742964a061be25d77d7a96f09db20bf5" + +[[package]] +name = "windows_i686_gnu" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a711c68811799e017b6038e0922cb27a5e2f43a2ddb609fe0b6f3eeda9de615" + +[[package]] +name = "windows_i686_msvc" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c11bb1a02615db74680b32a68e2d61f553cc24c4eb5b4ca10311740e44172" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c912b12f7454c6620635bbff3450962753834be2a594819bd5e945af18ec64bc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "504a2476202769977a040c6364301a3f65d0cc9e3fb08600b2bda150a0488316" + +[[package]] name = "winreg" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/crates/core/tedge_api/examples/heartbeat.rs b/crates/core/tedge_api/examples/heartbeat.rs index 5c96a0fc..10c1a6c6 100644 --- a/crates/core/tedge_api/examples/heartbeat.rs +++ b/crates/core/tedge_api/examples/heartbeat.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; use tedge_api::{ - address::EndpointKind, Address, Comms, Message, MessageKind, Plugin, PluginBuilder, + address::EndpointKind, Address, CoreCommunication, Message, MessageKind, Plugin, PluginBuilder, PluginConfiguration, PluginError, }; @@ -15,14 +15,14 @@ impl PluginBuilder for HeartbeatServiceBuilder { async fn verify_configuration( &self, _config: &PluginConfiguration, - ) -> Result<(), tedge_api::errors::PluginError> { + ) -> Result<(), tedge_api::error::PluginError> { Ok(()) } async fn instantiate( &self, config: PluginConfiguration, - tedge_comms: tedge_api::plugins::Comms, + tedge_comms: tedge_api::plugin::CoreCommunication, ) -> Result<Box<dyn Plugin>, PluginError> { let hb_config: HeartbeatConfig = toml::Value::try_into(config.into_inner())?; Ok(Box::new(HeartbeatService::new(tedge_comms, hb_config))) @@ -35,12 +35,12 @@ struct HeartbeatConfig { } struct HeartbeatService { - comms: tedge_api::plugins::Comms, + comms: tedge_api::plugin::CoreCommunication, config: HeartbeatConfig, } impl HeartbeatService { - fn new(comms: tedge_api::plugins::Comms, config: HeartbeatConfig) -> Self { + fn new(comms: tedge_api::plugin::CoreCommunication, config: HeartbeatConfig) -> Self { Self { comms, config } } } @@ -61,8 +61,7 @@ impl Plugin for HeartbeatService { let kind = MessageKind::SignalPluginState { state: String::from("Ok"), }; - let msg = self.comms.new_message(message.origin().clone(), kind); - self.comms.send(msg).await?; + self.comms.send(kind, message.origin().clone()).await?; } msg => println!("Does not handle: {:#?}", msg), } @@ -81,7 +80,8 @@ async fn main() { let hsb = HeartbeatServiceBuilder; let (sender, mut receiver) = tokio::sync::mpsc::channel(10); - let comms = Comms::new("heartbeat-service".to_string(), sender); + let plugin_name = "heartbeat-service".to_string(); + let comms = CoreCommunication::new(plugin_name.clone(), sender); let config = toml::from_str( r#" @@ -97,10 +97,9 @@ async fn main() { let handle = tokio::task::spawn(async move { let hb = heartbeat; - hb.handle_message(comms.new_message( - Address::new(EndpointKind::Plugin { - id: "heartbeat-service".to_string(), - }), + hb.handle_message(Message::new( + Address::new(EndpointKind::Plugin { id: plugin_name }), + Address::new(EndpointKind::Core), MessageKind::CheckReadyness, )) .await diff --git a/crates/core/tedge_api/src/lib.rs b/crates/core/tedge_api/src/lib.rs index e37cb82a..99af771b 100644 --- a/crates/core/tedge_api/src/lib.rs +++ b/crates/core/tedge_api/src/lib.rs @@ -1,5 +1,5 @@ pub mod plugin; -pub use plugin::{Comms, Plugin, PluginBuilder, PluginConfiguration}; +pub use plugin::{CoreCommunication, Plugin, PluginBuilder, PluginConfiguration}; pub mod address; pub use address::Address; diff --git a/crates/core/tedge_api/src/message.rs b/crates/core/tedge_api/src/message.rs index 6ae74219..7ebceba5 100644 --- a/crates/core/tedge_api/src/message.rs +++ b/crates/core/tedge_api/src/message.rs @@ -10,7 +10,8 @@ pub struct Message { } impl Message { - pub(crate) fn new(origin: Address, destination: Address, kind: MessageKind) -> Self { + #[doc(hidden)] + pub fn new(origin: Address, destination: Address, kind: MessageKind) -> Self { Self { origin, destination, diff --git a/crates/core/tedge_api/src/plugin.rs b/crates/core/tedge_api/src/plugin.rs index 6a55c344..fc881498 100644 --- a/crates/core/tedge_api/src/plugin.rs +++ b/crates/core/tedge_api/src/plugin.rs @@ -6,23 +6,28 @@ use async_trait::async_trait; -use crate::{error::PluginError, message::Message}; +use crate::{error::PluginError, message::Message, Address, MessageKind}; +/// The communication struct to interface with the core of ThinEdge +/// +/// It's main purpose is the [`send`](CoreCommunication::send) method, through which one plugin +/// can communicate with another. #[derive(Clone)] -pub struct Comms { +pub struct CoreCommunication { plugin_name: String, sender: tokio::sync::mpsc::Sender<Message>, } -impl std::fmt::Debug for Comms { +impl std::fmt::Debug for CoreCommunication { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.debug_struct("Comms") .field("plugin_name", &self.plugin_name) - .finish() + .finish_non_exhaustive() } } -impl Comms { +impl CoreCommunication { + #[doc(hidden)] pub fn new(plugin_name: String, sender: tokio::sync::mpsc::Sender<Message>) -> Self { Self { plugin_name, @@ -30,14 +35,15 @@ impl Comms { } } - pub fn new_message(&self, destination: crate::Address, kind: crate::MessageKind) -> Message { - let addr = crate::Address::new(crate::address::EndpointKind::Plugin { + pub async fn send( + &self, + msg_kind: MessageKind, + destination: Address, + ) -> Result<(), PluginError> { + let origin = Address::new(crate::address::EndpointKind::Plugin { id: self.plugin_name.clone(), }); - Message::new(addr, destination, kind) - } - - pub async fn send<T: Into<Message>>(&self, msg: T) -> Result<(), PluginError> { + let msg = Message::new(origin, destination, msg_kind); self.sender.send(msg.into()).await?; Ok(()) @@ -67,7 +73,7 @@ pub trait PluginBuilder: Sync + Send + 'static { async fn instantiate( &self, config: PluginConfiguration, - tedge_comms: Comms, + core_comms: CoreCommunication, ) -> Result<Box<dyn Plugin + 'static>, PluginError>; } @@ -86,7 +92,7 @@ pub trait Plugin: Sync + Send { #[cfg(test)] mod tests { - use super::{Comms, Plugin, PluginBuilder}; + use super::{CoreCommunication, Plugin, PluginBuilder}; use static_assertions::{assert_impl_all, assert_obj_safe}; // Object Safety @@ -94,5 +100,5 @@ mod tests { assert_obj_safe!(Plugin); // Sync + Send - assert_impl_all!(Comms: Send, Clone); + assert_impl_all!(CoreCommunication: Send, Clone); } |