Skip to content

Commit

Permalink
refactor(rust): remove the supervised node manager
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Oct 2, 2023
1 parent 7fb1fc2 commit 59f52a6
Show file tree
Hide file tree
Showing 43 changed files with 288 additions and 417 deletions.
111 changes: 48 additions & 63 deletions implementations/rust/ockam/ockam_api/src/nodes/service.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Node Manager (Node Man, the superhero that we deserve)
use miette::IntoDiagnostic;
use std::collections::BTreeMap;
use std::error::Error as _;
use std::net::SocketAddr;
use std::ops::Deref;
use std::path::PathBuf;
use std::time::Duration;

Expand Down Expand Up @@ -48,9 +48,7 @@ use crate::nodes::models::portal::{OutletList, OutletStatus};
use crate::nodes::models::transport::{TransportMode, TransportType};
use crate::nodes::models::workers::{WorkerList, WorkerStatus};
use crate::nodes::registry::KafkaServiceKind;
use crate::nodes::NODEMANAGER_ADDR;
use crate::session::sessions::{Key, Session};
use crate::session::MedicHandle;
use crate::nodes::{InMemoryNode, NODEMANAGER_ADDR};
use crate::DefaultAddress;

use super::registry::Registry;
Expand Down Expand Up @@ -89,56 +87,11 @@ pub(crate) fn encode_request_result<T: Encode<()>>(
Ok(v)
}

/// Node manager provides a messaging API to interact with the current node
pub struct SupervisedNodeManager {
node_manager: Arc<NodeManager>,
medic_handle: MedicHandle,
}

impl Deref for SupervisedNodeManager {
type Target = Arc<NodeManager>;

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

impl SupervisedNodeManager {
/// Create a new NodeManager with the node name from the ockam CLI
pub async fn create(
ctx: &Context,
general_options: NodeManagerGeneralOptions,
transport_options: NodeManagerTransportOptions,
trust_options: NodeManagerTrustOptions,
) -> Result<Self> {
let node_manager =
NodeManager::create(general_options, transport_options, trust_options).await?;
debug!("start the Medic");
let medic_handle = MedicHandle::start_medic(ctx).await?;
Ok(Self {
node_manager: Arc::new(node_manager),
medic_handle,
})
}

pub fn node_manager(&self) -> Arc<NodeManager> {
self.node_manager.clone()
}

pub fn add_session(&self, session: Session) -> Key {
self.medic_handle.add_session(session)
}

pub async fn stop(&self, ctx: &Context) -> Result<()> {
self.medic_handle.stop_medic(ctx).await?;
for addr in DefaultAddress::iter() {
ctx.stop_worker(addr).await?;
}
Ok(())
}
}

/// Node manager provides a messaging API to interact with the current node
/// Node manager provides high-level operations to
/// - send messages
/// - create secure channels, inlet, outlet
/// - configure the trust context
/// - manage persistent data
pub struct NodeManager {
pub(crate) cli_state: CliState,
node_name: String,
Expand Down Expand Up @@ -213,7 +166,43 @@ impl NodeManager {
}

impl NodeManager {
pub async fn make_controller_node_client(&self) -> Result<Controller> {
pub async fn create_authority_client(
&self,
authority_identifier: &Identifier,
authority_multiaddr: &MultiAddr,
caller_identity_name: Option<String>,
) -> miette::Result<AuthorityNode> {
self.make_authority_node_client(
authority_identifier,
authority_multiaddr,
&self
.get_identifier(caller_identity_name)
.await
.into_diagnostic()?,
)
.await
.into_diagnostic()
}

pub async fn create_project_client(
&self,
project_identifier: &Identifier,
project_multiaddr: &MultiAddr,
caller_identity_name: Option<String>,
) -> miette::Result<ProjectNode> {
self.make_project_node_client(
project_identifier,
project_multiaddr,
&self
.get_identifier(caller_identity_name)
.await
.into_diagnostic()?,
)
.await
.into_diagnostic()
}

pub(crate) async fn make_controller_node_client(&self) -> Result<Controller> {
SecureClients::controller(
&self.tcp_transport,
self.secure_channels.clone(),
Expand All @@ -222,7 +211,7 @@ impl NodeManager {
.await
}

pub async fn make_authority_node_client(
async fn make_authority_node_client(
&self,
authority_identifier: &Identifier,
authority_multiaddr: &MultiAddr,
Expand All @@ -238,7 +227,7 @@ impl NodeManager {
.await
}

pub async fn make_project_node_client(
async fn make_project_node_client(
&self,
project_identifier: &Identifier,
project_multiaddr: &MultiAddr,
Expand Down Expand Up @@ -273,11 +262,11 @@ impl NodeManager {

#[derive(Clone)]
pub struct NodeManagerWorker {
pub node_manager: Arc<SupervisedNodeManager>,
pub node_manager: Arc<InMemoryNode>,
}

impl NodeManagerWorker {
pub fn new(node_manager: Arc<SupervisedNodeManager>) -> Self {
pub fn new(node_manager: Arc<InMemoryNode>) -> Self {
NodeManagerWorker { node_manager }
}

Expand All @@ -286,10 +275,6 @@ impl NodeManagerWorker {
ctx.stop_worker(NODEMANAGER_ADDR).await?;
Ok(())
}

pub async fn make_controller_node_client(&self) -> Result<Controller> {
self.node_manager.make_controller_node_client().await
}
}

pub struct IdentityOverride {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::nodes::models::forwarder::{CreateForwarder, ForwarderInfo};
use crate::nodes::models::secure_channel::{
CreateSecureChannelRequest, CreateSecureChannelResponse,
};
use crate::nodes::service::SupervisedNodeManager;
use crate::nodes::{BackgroundNode, InMemoryNode};
use crate::nodes::service::in_memory_node::InMemoryNode;
use crate::nodes::BackgroundNode;
use crate::session::sessions::{Replacer, Session};
use crate::session::sessions::{MAX_CONNECT_TIME, MAX_RECOVERY_TIME};

Expand Down Expand Up @@ -200,7 +200,7 @@ impl NodeManager {
}
}

impl SupervisedNodeManager {
impl InMemoryNode {
pub async fn create_forwarder(
&self,
ctx: &Context,
Expand Down
Loading

0 comments on commit 59f52a6

Please sign in to comment.