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
10 changes: 5 additions & 5 deletions s2energy-connection/examples/communication-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ use uuid::uuid;
use rustls::pki_types::{CertificateDer, pem::PemObject};
use s2energy_common::S2Transport;
use s2energy_connection::{
AccessToken, MessageVersion, S2NodeId,
AccessToken, MessageVersion, NodeId,
communication::{Client, ClientConfig, ClientPairing, NodeConfig},
};
use tracing_subscriber::{EnvFilter, fmt, prelude::*};

struct MemoryPairing {
communication_url: String,
tokens: Vec<AccessToken>,
server: S2NodeId,
client: S2NodeId,
server: NodeId,
client: NodeId,
}

impl ClientPairing for &mut MemoryPairing {
type Error = Infallible;

fn client_id(&self) -> S2NodeId {
fn client_id(&self) -> NodeId {
self.client.clone()
}

fn server_id(&self) -> S2NodeId {
fn server_id(&self) -> NodeId {
self.server.clone()
}

Expand Down
6 changes: 3 additions & 3 deletions s2energy-connection/examples/communication-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ use uuid::uuid;
use axum_server::tls_rustls::RustlsConfig;
use s2energy_common::S2Transport;
use s2energy_connection::{
AccessToken, MessageVersion, S2NodeId,
AccessToken, MessageVersion, NodeId,
communication::{NodeConfig, PairingLookupResult, Server, ServerConfig, ServerPairing, ServerPairingStore},
};
use tracing_subscriber::{EnvFilter, fmt, prelude::*};

struct MemoryPairingStoreInner {
token: AccessToken,
config: Arc<NodeConfig>,
server: S2NodeId,
client: S2NodeId,
server: NodeId,
client: NodeId,
// indication of whether the client has unpaired with us.
unpaired: bool,
}
Expand Down
4 changes: 2 additions & 2 deletions s2energy-connection/examples/discovery-client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use s2energy_connection::{S2Role, discovery::S2Discoverer};
use s2energy_connection::{Role, discovery::S2Discoverer};

#[tokio::main(flavor = "current_thread")]
async fn main() {
let mut discoverer = S2Discoverer::new(S2Role::Cem).await.unwrap();
let mut discoverer = S2Discoverer::new(Role::Cem).await.unwrap();

while let Ok(event) = discoverer.next_event().await {
match event {
Expand Down
4 changes: 2 additions & 2 deletions s2energy-connection/examples/discovery-server.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use s2energy_connection::{
S2Role,
Role,
discovery::{DiscoverableS2Endpoint, advertise},
};

#[tokio::main(flavor = "current_thread")]
async fn main() {
let endpoint = DiscoverableS2Endpoint::build_with_pairing(vec![S2Role::Cem], "https://example.com/".into())
let endpoint = DiscoverableS2Endpoint::build_with_pairing(vec![Role::Cem], "https://example.com/".into())
.unwrap()
.with_endpoint_name("test endpoint".into())
.build();
Expand Down
14 changes: 7 additions & 7 deletions s2energy-connection/examples/pairing-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::path::PathBuf;
use uuid::uuid;

use s2energy_connection::{
Deployment, MessageVersion, S2EndpointDescription, S2NodeDescription, S2Role,
pairing::{Client, ClientConfig, NodeConfig, PairingRemote, PairingS2NodeId},
Deployment, EndpointDescription, MessageVersion, NodeDescription, Role,
pairing::{Client, ClientConfig, NodeConfig, NodeIdAlias, PairingRemote},
};
use tracing_subscriber::{EnvFilter, fmt, prelude::*};

Expand All @@ -18,14 +18,14 @@ async fn main() {
.init();

let config = NodeConfig::builder(
S2NodeDescription {
NodeDescription {
id: uuid!("67e55044-10b1-426f-9247-bb680e5fe0c7").into(),
brand: String::from("super-reliable-corp"),
logo_uri: None,
logo_url: None,
type_: String::from("fancy"),
model_name: String::from("the best"),
user_defined_name: None,
role: S2Role::Rm,
role: Role::Rm,
},
vec![MessageVersion("v1".into())],
)
Expand All @@ -37,7 +37,7 @@ async fn main() {
additional_certificates: vec![
CertificateDer::from_pem_file(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("testdata").join("root.pem")).unwrap(),
],
endpoint_description: S2EndpointDescription::default(),
endpoint_description: EndpointDescription::default(),
pairing_deployment: Deployment::Lan,
})
.unwrap();
Expand All @@ -48,7 +48,7 @@ async fn main() {
&config,
PairingRemote {
url: "https://localhost:8005".into(),
id: Some(PairingS2NodeId("ninechars".into())),
id: Some(NodeIdAlias("ninechars".into())),
},
PAIRING_TOKEN,
async |pairing| {
Expand Down
14 changes: 7 additions & 7 deletions s2energy-connection/examples/pairing-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::{net::SocketAddr, path::PathBuf, sync::Arc};
use uuid::uuid;

use s2energy_connection::{
MessageVersion, S2EndpointDescription, S2NodeDescription, S2Role,
pairing::{NodeConfig, PairingS2NodeId, PairingToken, Server, ServerConfig},
EndpointDescription, MessageVersion, NodeDescription, Role,
pairing::{NodeConfig, NodeIdAlias, PairingToken, Server, ServerConfig},
};
use tracing_subscriber::{EnvFilter, fmt, prelude::*};

Expand All @@ -20,18 +20,18 @@ async fn main() {

let server = Server::new(ServerConfig {
leaf_certificate: None,
endpoint_description: S2EndpointDescription::default(),
endpoint_description: EndpointDescription::default(),
advertised_nodes: vec![],
});
let config = NodeConfig::builder(
S2NodeDescription {
NodeDescription {
id: uuid!("67e55044-10b1-426f-9247-bb680e5fe0c8").into(),
brand: String::from("super-reliable-corp"),
logo_uri: None,
logo_url: None,
type_: String::from("fancy"),
model_name: String::from("the best"),
user_defined_name: None,
role: S2Role::Cem,
role: Role::Cem,
},
vec![MessageVersion("v1".into())],
)
Expand Down Expand Up @@ -59,7 +59,7 @@ async fn main() {
.unwrap();
});

let pairing_node_id = PairingS2NodeId("ninechars".into());
let pairing_node_id = NodeIdAlias("ninechars".into());

let server_clone = server.clone();
server
Expand Down
48 changes: 24 additions & 24 deletions s2energy-connection/src/common/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ pub struct MessageVersion(pub String);
/// Information about the pairing endpoint of a S2 node.
#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct S2EndpointDescription {
pub struct EndpointDescription {
/// Name of the endpoint.
#[serde(default)]
pub name: Option<String>,
/// URI of a logo to be used for the endpoint in GUIs.
#[serde(default)]
pub logo_uri: Option<String>,
pub logo_url: Option<String>,
/// Type of deployment used by the endpoint (local or globally routable).
#[serde(default)]
pub deployment: Option<Deployment>,
Expand Down Expand Up @@ -130,57 +130,57 @@ impl From<uuid::Error> for InvalidNodeId {

/// Unique identifier of the S2 node.
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct S2NodeId(Uuid);
pub struct NodeId(Uuid);

impl From<Uuid> for S2NodeId {
impl From<Uuid> for NodeId {
fn from(value: Uuid) -> Self {
Self(value)
}
}

impl From<S2NodeId> for Uuid {
fn from(value: S2NodeId) -> Self {
impl From<NodeId> for Uuid {
fn from(value: NodeId) -> Self {
value.0
}
}

impl TryFrom<String> for S2NodeId {
impl TryFrom<String> for NodeId {
type Error = InvalidNodeId;

fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(Self(Uuid::try_from(value)?))
}
}

impl TryFrom<&str> for S2NodeId {
impl TryFrom<&str> for NodeId {
type Error = InvalidNodeId;

fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(Self(Uuid::try_from(value)?))
}
}

impl std::ops::Deref for S2NodeId {
impl std::ops::Deref for NodeId {
type Target = Uuid;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl std::ops::DerefMut for S2NodeId {
impl std::ops::DerefMut for NodeId {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl core::fmt::Display for S2NodeId {
impl core::fmt::Display for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.as_hyphenated().fmt(f)
}
}

impl S2NodeId {
impl NodeId {
/// Generate a new random node id.
#[expect(clippy::new_without_default, reason = "New uses non-trivial randomness")]
pub fn new() -> Self {
Expand All @@ -191,14 +191,14 @@ impl S2NodeId {
/// Information about the S2 node.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct S2NodeDescription {
pub struct NodeDescription {
/// Unique identifier of the node.
pub id: S2NodeId,
pub id: NodeId,
/// Brandname used for the node.
pub brand: String,
/// URI of a logo to be used for the node in GUIs.
#[serde(default)]
pub logo_uri: Option<String>,
pub logo_url: Option<String>,
/// The type of this node.
pub type_: String,
/// Model name of the device this node belongs to.
Expand All @@ -207,7 +207,7 @@ pub struct S2NodeDescription {
#[serde(default)]
pub user_defined_name: Option<String>,
/// The S2 role this device has (e.g. CEM or RM).
pub role: S2Role,
pub role: Role,
}

/// Identifier of a protocol that can be used for communication of S2 messages between nodes.
Expand All @@ -220,14 +220,14 @@ pub struct CommunicationProtocol(pub String);
/// Role within the S2 standard.
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash)]
#[serde(rename_all = "UPPERCASE")]
pub enum S2Role {
pub enum Role {
/// Customer Energy Manager.
Cem,
/// Resource Manager.
Rm,
}

impl S2Role {
impl Role {
pub(crate) fn service_subtype(self) -> &'static str {
match self {
Self::Cem => "cem",
Expand Down Expand Up @@ -267,20 +267,20 @@ impl TryFrom<url::Host<&str>> for Deployment {
pub(crate) mod test {
use uuid::{Uuid, uuid};

use crate::{S2NodeDescription, S2Role, pairing::PairingS2NodeId};
use crate::{NodeDescription, Role, pairing::NodeIdAlias};

pub(crate) const UUID_A: Uuid = uuid!("67e55044-10b1-426f-9247-bb680e5fe0c8");
pub(crate) const UUID_B: Uuid = uuid!("67e55044-10b1-426f-9247-bb680e5fe0c7");

pub(crate) fn pairing_s2_node_id() -> PairingS2NodeId {
PairingS2NodeId("test_pairing_id".into())
pub(crate) fn pairing_s2_node_id() -> NodeIdAlias {
NodeIdAlias("test_pairing_id".into())
}

pub(crate) fn basic_node_description(uuid: Uuid, role: S2Role) -> S2NodeDescription {
S2NodeDescription {
pub(crate) fn basic_node_description(uuid: Uuid, role: Role) -> NodeDescription {
NodeDescription {
id: uuid.into(),
brand: String::from("super-reliable-corp"),
logo_uri: None,
logo_url: None,
type_: String::from("fancy"),
model_name: String::from("the best"),
user_defined_name: None,
Expand Down
Loading
Loading