From 8b032a5fa21146bb037a57ddde1990050faaea3d Mon Sep 17 00:00:00 2001 From: Kai Hudalla Date: Wed, 2 Oct 2024 17:05:29 +0200 Subject: [PATCH] Use StaticUriProvider in examples The examples have been changed to use StaticUriProvider instead of having LocalTransport implement LocalUriProvider. --- examples/simple_notify.rs | 8 ++--- examples/simple_publish.rs | 6 ++-- examples/simple_rpc.rs | 8 ++--- src/local_transport.rs | 61 ++++++++++++-------------------------- 4 files changed, 30 insertions(+), 53 deletions(-) diff --git a/examples/simple_notify.rs b/examples/simple_notify.rs index ca0ce4d..d538477 100644 --- a/examples/simple_notify.rs +++ b/examples/simple_notify.rs @@ -17,7 +17,7 @@ use protobuf::well_known_types::wrappers::StringValue; use up_rust::{ communication::{CallOptions, Notifier, SimpleNotifier, UPayload}, local_transport::LocalTransport, - LocalUriProvider, UListener, UMessage, + LocalUriProvider, StaticUriProvider, UListener, UMessage, }; struct ConsolePrinter {} @@ -35,9 +35,9 @@ impl UListener for ConsolePrinter { pub async fn main() -> Result<(), Box> { const ORIGIN_RESOURCE_ID: u16 = 0xd100; - let transport = Arc::new(LocalTransport::new("my-vehicle", 0xa34b, 0x01)); - let uri_provider: Arc = transport.clone(); - let notifier = SimpleNotifier::new(transport.clone(), uri_provider.clone()); + let uri_provider = Arc::new(StaticUriProvider::new("my-vehicle", 0xa34b, 0x01)); + let transport = Arc::new(LocalTransport::default()); + let notifier = SimpleNotifier::new(transport, uri_provider.clone()); let topic = uri_provider.get_resource_uri(ORIGIN_RESOURCE_ID); let listener = Arc::new(ConsolePrinter {}); diff --git a/examples/simple_publish.rs b/examples/simple_publish.rs index 89d39b1..c657e0d 100644 --- a/examples/simple_publish.rs +++ b/examples/simple_publish.rs @@ -17,7 +17,7 @@ use protobuf::well_known_types::wrappers::StringValue; use up_rust::{ communication::{CallOptions, Publisher, SimplePublisher, UPayload}, local_transport::LocalTransport, - LocalUriProvider, UListener, UMessage, UTransport, + LocalUriProvider, StaticUriProvider, UListener, UMessage, UTransport, }; struct ConsolePrinter {} @@ -34,8 +34,8 @@ impl UListener for ConsolePrinter { #[tokio::main] pub async fn main() -> Result<(), Box> { const ORIGIN_RESOURCE_ID: u16 = 0xb4c1; - let transport = Arc::new(LocalTransport::new("my-vehicle", 0xa34b, 0x01)); - let uri_provider: Arc = transport.clone(); + let uri_provider = Arc::new(StaticUriProvider::new("my-vehicle", 0xa34b, 0x01)); + let transport = Arc::new(LocalTransport::default()); let publisher = SimplePublisher::new(transport.clone(), uri_provider.clone()); let listener = Arc::new(ConsolePrinter {}); diff --git a/examples/simple_rpc.rs b/examples/simple_rpc.rs index 493ccbb..f88627c 100644 --- a/examples/simple_rpc.rs +++ b/examples/simple_rpc.rs @@ -24,7 +24,7 @@ use up_rust::{ ServiceInvocationError, UPayload, }, local_transport::LocalTransport, - LocalUriProvider, + LocalUriProvider, StaticUriProvider, }; struct EchoOperation {} @@ -49,8 +49,8 @@ impl RequestHandler for EchoOperation { #[tokio::main] pub async fn main() -> Result<(), Box> { const METHOD_RESOURCE_ID: u16 = 0x00a0; - let transport = Arc::new(LocalTransport::new("my-vehicle", 0xa34b, 0x01)); - let uri_provider: Arc = transport.clone(); + let uri_provider = Arc::new(StaticUriProvider::new("my-vehicle", 0xa34b, 0x01)); + let transport = Arc::new(LocalTransport::default()); // create the RpcServer using the local transport let rpc_server = InMemoryRpcServer::new(transport.clone(), uri_provider.clone()); @@ -62,7 +62,7 @@ pub async fn main() -> Result<(), Box> { .await?; // now create an RpcClient attached to the same local transport - let rpc_client = InMemoryRpcClient::new(transport.clone(), uri_provider.clone()).await?; + let rpc_client = InMemoryRpcClient::new(transport, uri_provider.clone()).await?; // and invoke the service operation without any payload match rpc_client .invoke_method( diff --git a/src/local_transport.rs b/src/local_transport.rs index ff11c15..385ae4a 100644 --- a/src/local_transport.rs +++ b/src/local_transport.rs @@ -20,7 +20,7 @@ use std::{collections::HashSet, sync::Arc}; use tokio::sync::RwLock; -use crate::{ComparableListener, LocalUriProvider, UListener, UMessage, UStatus, UTransport, UUri}; +use crate::{ComparableListener, UListener, UMessage, UStatus, UTransport, UUri}; #[derive(Eq, PartialEq, Hash)] struct RegisteredListener { @@ -66,22 +66,12 @@ impl RegisteredListener { /// /// A message sent via [`UTransport::send`] will be dispatched to all registered listeners that /// match the message's source and sink filters. +#[derive(Default)] pub struct LocalTransport { listeners: RwLock>, - authority_name: String, - entity_id: u32, - entity_version: u8, } impl LocalTransport { - pub fn new(authority_name: &str, entity_id: u32, entity_version: u8) -> Self { - LocalTransport { - listeners: RwLock::new(HashSet::new()), - authority_name: authority_name.to_string(), - entity_id, - entity_version, - } - } async fn dispatch(&self, message: UMessage) { let listeners = self.listeners.read().await; for listener in listeners.iter() { @@ -92,25 +82,6 @@ impl LocalTransport { } } -impl LocalUriProvider for LocalTransport { - fn get_authority(&self) -> String { - self.authority_name.clone() - } - - fn get_resource_uri(&self, resource_id: u16) -> UUri { - UUri::try_from_parts( - &self.authority_name, - self.entity_id, - self.entity_version, - resource_id, - ) - .unwrap() - } - fn get_source_uri(&self) -> UUri { - self.get_resource_uri(0x0000) - } -} - #[async_trait::async_trait] impl UTransport for LocalTransport { async fn send(&self, message: UMessage) -> Result<(), UStatus> { @@ -167,7 +138,7 @@ impl UTransport for LocalTransport { #[cfg(test)] mod tests { use super::*; - use crate::{utransport::MockUListener, UMessageBuilder}; + use crate::{utransport::MockUListener, LocalUriProvider, StaticUriProvider, UMessageBuilder}; #[tokio::test] async fn test_send_dispatches_to_matching_listener() { @@ -175,11 +146,12 @@ mod tests { let mut listener = MockUListener::new(); listener.expect_on_receive().once().return_const(()); let listener_ref = Arc::new(listener); - let transport = LocalTransport::new("my-vehicle", 0x100d, 0x02); + let uri_provider = StaticUriProvider::new("my-vehicle", 0x100d, 0x02); + let transport = LocalTransport::default(); transport .register_listener( - &transport.get_resource_uri(RESOURCE_ID), + &uri_provider.get_resource_uri(RESOURCE_ID), None, listener_ref.clone(), ) @@ -187,19 +159,23 @@ mod tests { .unwrap(); let _ = transport .send( - UMessageBuilder::publish(transport.get_resource_uri(RESOURCE_ID)) + UMessageBuilder::publish(uri_provider.get_resource_uri(RESOURCE_ID)) .build() .unwrap(), ) .await; transport - .unregister_listener(&transport.get_resource_uri(RESOURCE_ID), None, listener_ref) + .unregister_listener( + &uri_provider.get_resource_uri(RESOURCE_ID), + None, + listener_ref, + ) .await .unwrap(); let _ = transport .send( - UMessageBuilder::publish(transport.get_resource_uri(RESOURCE_ID)) + UMessageBuilder::publish(uri_provider.get_resource_uri(RESOURCE_ID)) .build() .unwrap(), ) @@ -212,11 +188,12 @@ mod tests { let mut listener = MockUListener::new(); listener.expect_on_receive().never().return_const(()); let listener_ref = Arc::new(listener); - let transport = LocalTransport::new("my-vehicle", 0x100d, 0x02); + let uri_provider = StaticUriProvider::new("my-vehicle", 0x100d, 0x02); + let transport = LocalTransport::default(); transport .register_listener( - &transport.get_resource_uri(RESOURCE_ID + 10), + &uri_provider.get_resource_uri(RESOURCE_ID + 10), None, listener_ref.clone(), ) @@ -224,7 +201,7 @@ mod tests { .unwrap(); let _ = transport .send( - UMessageBuilder::publish(transport.get_resource_uri(RESOURCE_ID)) + UMessageBuilder::publish(uri_provider.get_resource_uri(RESOURCE_ID)) .build() .unwrap(), ) @@ -232,7 +209,7 @@ mod tests { transport .unregister_listener( - &transport.get_resource_uri(RESOURCE_ID + 10), + &uri_provider.get_resource_uri(RESOURCE_ID + 10), None, listener_ref, ) @@ -240,7 +217,7 @@ mod tests { .unwrap(); let _ = transport .send( - UMessageBuilder::publish(transport.get_resource_uri(RESOURCE_ID)) + UMessageBuilder::publish(uri_provider.get_resource_uri(RESOURCE_ID)) .build() .unwrap(), )