Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions src/communication/default_notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,12 @@ mod tests {
use protobuf::well_known_types::wrappers::StringValue;

use crate::{
utransport::{MockLocalUriProvider, MockTransport, MockUListener},
UCode, UPriority, UStatus, UUri, UUID,
utransport::{MockTransport, MockUListener},
StaticUriProvider, UCode, UPriority, UStatus, UUri, UUID,
};

fn new_uri_provider() -> Arc<dyn LocalUriProvider> {
let mut mock_uri_locator = MockLocalUriProvider::new();
mock_uri_locator
.expect_get_resource_uri()
.returning(|resource_id| UUri {
ue_id: 0x0005,
ue_version_major: 0x02,
resource_id: resource_id as u32,
..Default::default()
});
mock_uri_locator.expect_get_source_uri().returning(|| UUri {
ue_id: 0x0005,
ue_version_major: 0x02,
resource_id: 0x0000,
..Default::default()
});
Arc::new(mock_uri_locator)
Arc::new(StaticUriProvider::new("", 0x0005, 0x02))
}

#[tokio::test]
Expand Down
21 changes: 3 additions & 18 deletions src/communication/default_pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,27 +467,12 @@ mod tests {

use crate::{
communication::{notification::MockNotifier, pubsub::MockSubscriptionChangeHandler},
utransport::{MockLocalUriProvider, MockTransport, MockUListener},
UAttributes, UCode, UMessageType, UPriority, UStatus, UUri, UUID,
utransport::{MockTransport, MockUListener},
StaticUriProvider, UAttributes, UCode, UMessageType, UPriority, UStatus, UUri, UUID,
};

fn new_uri_provider() -> Arc<dyn LocalUriProvider> {
let mut mock_uri_locator = MockLocalUriProvider::new();
mock_uri_locator
.expect_get_resource_uri()
.returning(|resource_id| UUri {
ue_id: 0x0005,
ue_version_major: 0x02,
resource_id: resource_id as u32,
..Default::default()
});
mock_uri_locator.expect_get_source_uri().returning(|| UUri {
ue_id: 0x0005,
ue_version_major: 0x02,
resource_id: 0x0000,
..Default::default()
});
Arc::new(mock_uri_locator)
Arc::new(StaticUriProvider::new("", 0x0005, 0x02))
}

fn succeding_notifier() -> Arc<dyn Notifier> {
Expand Down
14 changes: 2 additions & 12 deletions src/communication/in_memory_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,20 +298,10 @@ mod tests {
use protobuf::{well_known_types::wrappers::StringValue, Enum};
use tokio::{join, sync::Notify};

use crate::{
utransport::{MockLocalUriProvider, MockTransport},
UMessageBuilder, UPriority, UUri,
};
use crate::{utransport::MockTransport, StaticUriProvider, UMessageBuilder, UPriority, UUri};

fn new_uri_provider() -> Arc<dyn LocalUriProvider> {
let mut mock_uri_locator = MockLocalUriProvider::new();
mock_uri_locator.expect_get_source_uri().returning(|| UUri {
ue_id: 0x0005,
ue_version_major: 0x02,
resource_id: 0x0000,
..Default::default()
});
Arc::new(mock_uri_locator)
Arc::new(StaticUriProvider::new("", 0x0005, 0x02))
}

fn service_method_uri() -> UUri {
Expand Down
14 changes: 2 additions & 12 deletions src/communication/in_memory_rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,22 +291,12 @@ mod tests {
use tokio::sync::Notify;

use crate::{
communication::rpc::MockRequestHandler,
utransport::{MockLocalUriProvider, MockTransport},
communication::rpc::MockRequestHandler, utransport::MockTransport, StaticUriProvider,
UAttributes, UMessageType, UPriority, UUri, UUID,
};

fn new_uri_provider() -> Arc<dyn LocalUriProvider> {
let mut mock_uri_provider = MockLocalUriProvider::new();
mock_uri_provider
.expect_get_resource_uri()
.returning(|resource_id| UUri {
ue_id: 0x0005,
ue_version_major: 0x02,
resource_id: resource_id as u32,
..Default::default()
});
Arc::new(mock_uri_provider)
Arc::new(StaticUriProvider::new("", 0x0005, 0x02))
}

#[test_case(None, 0x4A10; "for empty origin filter")]
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ mod ustatus;
pub use ustatus::{UCode, UStatus};

mod utransport;
pub use utransport::{ComparableListener, LocalUriProvider, UListener, UTransport};
pub use utransport::{
ComparableListener, LocalUriProvider, StaticUriProvider, UListener, UTransport,
};
mod uuid;
pub use uuid::UUID;

Expand Down
125 changes: 125 additions & 0 deletions src/utransport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::num::TryFromIntError;
use std::ops::Deref;
use std::sync::Arc;

Expand All @@ -26,6 +27,7 @@ use crate::{UCode, UMessage, UStatus, UUri};
///
/// Implementations may use arbitrary mechanisms to determine the information that
/// is necessary for creating URIs, e.g. environment variables, configuration files etc.
// [impl->req~up-language-transport-api~1]
#[cfg_attr(test, automock)]
pub trait LocalUriProvider: Send + Sync {
/// Gets the _authority_ used for URIs representing this uEntity's resources.
Expand All @@ -37,6 +39,109 @@ pub trait LocalUriProvider: Send + Sync {
fn get_source_uri(&self) -> UUri;
}

/// A URI provider that is statically configured with the uEntity's authority, entity ID and version.
pub struct StaticUriProvider {
local_uri: UUri,
}

impl StaticUriProvider {
/// Creates a new URI provider from static information.
///
/// # Arguments
///
/// * `authority` - The uEntity's authority name.
/// * `entity_id` - The entity identifier.
/// * `major_version` - The uEntity's major version.
///
/// # Examples
///
/// ```rust
/// use up_rust::{LocalUriProvider, StaticUriProvider};
///
/// let provider = StaticUriProvider::new("my-vehicle", 0x4210, 0x05);
/// assert_eq!(provider.get_authority(), "my-vehicle");
/// ```
pub fn new(authority: impl Into<String>, entity_id: u32, major_version: u8) -> Self {
let local_uri = UUri {
authority_name: authority.into(),
ue_id: entity_id,
ue_version_major: major_version as u32,
resource_id: 0x0000,
..Default::default()
};
StaticUriProvider { local_uri }
}
}

impl LocalUriProvider for StaticUriProvider {
fn get_authority(&self) -> String {
self.local_uri.authority_name.clone()
}

fn get_resource_uri(&self, resource_id: u16) -> UUri {
let mut uri = self.local_uri.clone();
uri.resource_id = resource_id as u32;
uri
}

fn get_source_uri(&self) -> UUri {
self.local_uri.clone()
}
}

impl TryFrom<UUri> for StaticUriProvider {
type Error = TryFromIntError;
fn try_from(value: UUri) -> Result<Self, Self::Error> {
Self::try_from(&value)
}
}

impl TryFrom<&UUri> for StaticUriProvider {
type Error = TryFromIntError;
/// Creates a URI provider from a UUri.
///
/// # Arguments
///
/// * `source_uri` - The UUri to take the entity's authority, entity ID and version information from.
/// The UUri's resource ID is ignored.
///
/// # Errors
///
/// Returns an error if the given UUri's major version property is not a `u8`.
///
/// # Examples
///
/// ```rust
/// use up_rust::{LocalUriProvider, StaticUriProvider, UUri};
///
/// let source_uri = UUri::try_from("//my-vehicle/4210/5/0").unwrap();
/// assert!(StaticUriProvider::try_from(&source_uri).is_ok());
/// ```
///
/// ## Invalid Major Version
///
/// ```rust
/// use up_rust::{LocalUriProvider, StaticUriProvider, UUri};
///
/// let uuri_with_invalid_version = UUri {
/// authority_name: "".to_string(),
/// ue_id: 0x5430,
/// ue_version_major: 0x1234, // not a u8
/// resource_id: 0x0000,
/// ..Default::default()
/// };
/// assert!(StaticUriProvider::try_from(uuri_with_invalid_version).is_err());
/// ```
fn try_from(source_uri: &UUri) -> Result<Self, Self::Error> {
let major_version = u8::try_from(source_uri.ue_version_major)?;
Ok(StaticUriProvider::new(
&source_uri.authority_name,
source_uri.ue_id,
major_version,
))
}
}

/// A handler for processing uProtocol messages.
///
/// Implementations contain the details for what should occur when a message is received.
Expand Down Expand Up @@ -294,6 +399,26 @@ mod tests {

use super::*;

#[test]
fn test_static_uri_provider_get_source() {
let provider = StaticUriProvider::new("my-vehicle", 0x4210, 0x05);
let source_uri = provider.get_source_uri();
assert_eq!(source_uri.authority_name, "my-vehicle");
assert_eq!(source_uri.ue_id, 0x4210);
assert_eq!(source_uri.ue_version_major, 0x05);
assert_eq!(source_uri.resource_id, 0x0000);
}

#[test]
fn test_static_uri_provider_get_resource() {
let provider = StaticUriProvider::new("my-vehicle", 0x4210, 0x05);
let resource_uri = provider.get_resource_uri(0x1234);
assert_eq!(resource_uri.authority_name, "my-vehicle");
assert_eq!(resource_uri.ue_id, 0x4210);
assert_eq!(resource_uri.ue_version_major, 0x05);
assert_eq!(resource_uri.resource_id, 0x1234);
}

#[tokio::test]
async fn test_deref_returns_wrapped_listener() {
let mut mock_listener = MockUListener::new();
Expand Down