Skip to content

Commit

Permalink
Migrate proxies to signal store model (#106)
Browse files Browse the repository at this point in the history
Closes #18 by migrating proxies to the signal store model for sharing data.

Also moves some macros related to axum to a shared place since I otherwise needed to copy one of them for the http proxy

Co-authored-by: jorchiu <[email protected]>
  • Loading branch information
wilyle and jorchiu authored Jan 10, 2024
1 parent 6f4547b commit 4c54cf6
Show file tree
Hide file tree
Showing 29 changed files with 295 additions and 397 deletions.
64 changes: 0 additions & 64 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ async-trait = "0.1.77"
axum = "0.6.12"
config = "0.13.4"
convert_case = "0.6.0"
crossbeam = "0.8.3"
env_logger = "0.10.1"
futures = "0.3.30"
home = "0.5.9"
Expand Down
2 changes: 0 additions & 2 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ license = "MIT"
[dependencies]
async-trait = { workspace = true }
config = { workspace = true }
crossbeam = { workspace = true }
home = { workspace = true }
log = { workspace = true }
proc-macros = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
strum = { workspace = true }
strum_macros = { workspace = true }
tokio = { workspace = true }
43 changes: 43 additions & 0 deletions common/src/http_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

#[macro_export]
macro_rules! response {
($status_code:ident) => {
(axum::http::StatusCode::$status_code, axum::Json("")).into_response()
};
($status_code:ident, $body:expr) => {
(axum::http::StatusCode::$status_code, axum::Json($body)).into_response()
};
}

#[macro_export]
macro_rules! ok {
() => {
freyja_common::response!(OK)
};
($body:expr) => {
freyja_common::response!(OK, $body)
};
}

#[macro_export]
macro_rules! not_found {
() => {
freyja_common::response!(NOT_FOUND)
};
($body:expr) => {
freyja_common::response!(NOT_FOUND, $body)
};
}

#[macro_export]
macro_rules! server_error {
() => {
freyja_common::response!(INTERNAL_SERVER_ERROR)
};
($body:expr) => {
freyja_common::response!(INTERNAL_SERVER_ERROR, $body)
};
}
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod conversion;
pub mod digital_twin_adapter;
pub mod digital_twin_map_entry;
pub mod entity;
pub mod http_utils;
pub mod mapping_client;
pub mod message_utils;
pub mod provider_proxy;
Expand Down
25 changes: 9 additions & 16 deletions common/src/provider_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,12 @@
use std::{fmt::Debug, sync::Arc};

use async_trait::async_trait;
use crossbeam::queue::SegQueue;
use strum_macros::Display;

use crate::entity::{Entity, EntityEndpoint};

/// Represents a signal value
pub struct SignalValue {
/// The entity's id
pub entity_id: String,

/// The entity's value
pub value: String,
}
use crate::{
entity::{Entity, EntityEndpoint},
signal_store::SignalStore,
};

#[derive(Clone, Debug, Display, Eq, PartialEq)]
/// Return options for when a proxy attempts to register an entity
Expand All @@ -35,10 +28,10 @@ pub trait ProviderProxy {
///
/// # Arguments
/// - `provider_uri`: the provider uri for accessing an entity's information
/// - `signal_values_queue`: shared queue for all provider proxies to push new signal values of entities
/// - `signal_store`: the shared signal store
fn create_new(
provider_uri: &str,
signal_values_queue: Arc<SegQueue<SignalValue>>,
signals: Arc<SignalStore>,
) -> Result<Self, ProviderProxyError>
where
Self: Sized;
Expand Down Expand Up @@ -85,12 +78,12 @@ pub trait ProviderProxyFactory {
/// Create a new proxy
///
/// # Arguments
/// - `provider_uri`: The provider URI to associate with this proxy
/// - `signal_values_queue`: The queue into which new signal values will be published
/// - `provider_uri`: the provider URI to associate with this proxy
/// - `signals`: the shared signal store
fn create_proxy(
&self,
provider_uri: &str,
signal_values_queue: Arc<SegQueue<SignalValue>>,
signals: Arc<SignalStore>,
) -> Result<Arc<dyn ProviderProxy + Send + Sync>, ProviderProxyError>;
}

Expand Down
1 change: 0 additions & 1 deletion freyja/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ edition = "2021"
license = "MIT"

[dependencies]
crossbeam = { workspace = true }
env_logger = { workspace = true }
freyja-common = { workspace = true }
log = { workspace = true }
Expand Down
Loading

0 comments on commit 4c54cf6

Please sign in to comment.