Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fm 324 relay client service mode #344

Draft
wants to merge 1 commit into
base: FM-321-refactor-relay-client
Choose a base branch
from
Draft
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
68 changes: 58 additions & 10 deletions relay-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ pub enum Auth {
enum Mode {
Orb,
App,
Service,
}

#[derive(Debug, Clone)]
struct Config {
src_id: String,
dst_id: String,
dst_id: Option<String>,
namespace: String,

url: String,
Expand Down Expand Up @@ -122,15 +123,18 @@ impl Client {
let (src_t, dst_t) = match self.config.mode {
Mode::Orb => (EntityType::Orb as i32, EntityType::App as i32),
Mode::App => (EntityType::App as i32, EntityType::Orb as i32),
Mode::Service => {
(EntityType::Service as i32, EntityType::Unspecified as i32)
}
};
RelayPayload {
src: Some(Entity {
id: self.config.src_id.clone(),
entity_type: src_t,
namespace: self.config.namespace.clone(),
}),
dst: Some(Entity {
id: self.config.dst_id.clone(),
dst: self.config.dst_id.as_ref().map(|id| Entity {
id: id.clone(),
entity_type: dst_t,
namespace: self.config.namespace.clone(),
}),
Expand All @@ -145,7 +149,7 @@ impl Client {
url: String,
auth: Auth,
src_id: String,
dst_id: String,
dst_id: Option<String>,
namespace: String,
mode: Mode,
) -> Self {
Expand Down Expand Up @@ -188,7 +192,7 @@ impl Client {
token: token.into(),
}),
orb_id,
dest_id,
Some(dest_id),
namespace,
Mode::Orb,
)
Expand All @@ -209,7 +213,7 @@ impl Client {
token: token.into(),
}),
session_id,
orb_id,
Some(orb_id),
namespace,
Mode::App,
)
Expand All @@ -236,12 +240,54 @@ impl Client {
proof: proof.into(),
}),
session_id,
orb_id,
Some(orb_id),
namespace,
Mode::App,
)
}

/// Create a new service-based client which can send to any destination
#[must_use]
pub fn new_as_service(
url: String,
token: String,
service_id: String,
namespace: String,
) -> Self {
Self::new(
url,
Auth::Token(TokenAuth {
token: token.into(),
}),
service_id,
None,
namespace,
Mode::Service,
)
}

/// Create a new service-based client which sends to a specific destination
/// Use this when you expect a response to the same service instance
#[must_use]
pub fn new_session_as_service(
url: String,
token: String,
session_id: String,
dest_id: String,
namespace: String,
) -> Self {
Self::new(
url,
Auth::Token(TokenAuth {
token: token.into(),
}),
session_id,
Some(dest_id),
namespace,
Mode::Service,
)
}

/// Connect to the Orb-Relay server
pub async fn connect(&mut self) -> Result<()> {
let shutdown_token = CancellationToken::new();
Expand All @@ -264,7 +310,7 @@ impl Client {
let no_state = self.no_state();

info!(
"Connecting with: src_id: {}, dst_id: {}",
"Connecting with: src_id: {}, dst_id: {:?}",
config.src_id, config.dst_id
);
tokio::spawn(async move {
Expand Down Expand Up @@ -572,7 +618,7 @@ impl<'a> PollerAgent<'a> {
.send(self.last_message.clone())
.await
.wrap_err("Failed to send outgoing message")?;
} else if src.id != self.config.dst_id {
} else if self.config.dst_id.is_some() && (src.id != self.config.dst_id.clone().unwrap()) {
error!(
"Skipping received message from unexpected source: {:?}: {payload:?}",
src.id
Expand Down Expand Up @@ -615,10 +661,11 @@ impl<'a> PollerAgent<'a> {
let (src_t, dst_t) = match self.config.mode {
Mode::Orb => (EntityType::Orb as i32, EntityType::App as i32),
Mode::App => (EntityType::App as i32, EntityType::Orb as i32),
Mode::Service => (EntityType::Service as i32, EntityType::Unspecified as i32),
};
let relay_message = RelayPayload {
src: Some(Entity { id: self.config.src_id.clone(), entity_type: src_t, namespace: self.config.namespace.clone() }),
dst: Some(Entity { id: self.config.dst_id.clone(), entity_type: dst_t, namespace: self.config.namespace.clone() }),
dst: self.config.dst_id.as_ref().map(|id| Entity { id: id.clone(), entity_type: dst_t, namespace: self.config.namespace.clone() }),
seq: self.seq.into(),
payload: Some(payload),
};
Expand Down Expand Up @@ -728,6 +775,7 @@ impl<'a> PollerAgent<'a> {
entity_type: match self.config.mode {
Mode::Orb => EntityType::Orb as i32,
Mode::App => EntityType::App as i32,
Mode::Service => EntityType::Service as i32,
},
namespace: self.config.namespace.clone(),
}),
Expand Down
Loading