Skip to content
Open
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
60 changes: 60 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ pub enum Litep2pEvent {
},
}

/// Configuration for adding protocols at runtime (protocols that are started after the Litep2p
/// instance is created).
struct RuntimeConfigs {
/// The keep-alive timeout for protocols.
///
/// Needed for supporting protocols at runtime.
keep_alive_timeout: std::time::Duration,

/// Executor used to run protocols.
executor: Arc<dyn crate::executor::Executor>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: IMO executor deserves a dedicated field in Litep2p, and may be we don't need a new struct then.

}

/// [`Litep2p`] object.
pub struct Litep2p {
/// Local peer ID.
Expand All @@ -149,6 +161,11 @@ pub struct Litep2p {

/// Bandwidth sink.
bandwidth_sink: BandwidthSink,

/// The keep-alive timeout for protocols.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The keep-alive timeout for protocols.
/// The executor & keep-alive timeout for protocols.

///
/// Needed for supporting protocols at runtime.
runtime_configs: RuntimeConfigs,
}

impl Litep2p {
Expand Down Expand Up @@ -415,6 +432,10 @@ impl Litep2p {
bandwidth_sink,
listen_addresses,
transport_manager,
runtime_configs: RuntimeConfigs {
keep_alive_timeout: litep2p_config.keep_alive_timeout,
executor: litep2p_config.executor,
},
})
}

Expand Down Expand Up @@ -491,6 +512,45 @@ impl Litep2p {
self.transport_manager.add_known_address(peer, address)
}

/// Register a request-response protocol at runtime.
pub fn register_request_response(
&mut self,
config: crate::protocol::request_response::Config,
) -> crate::Result<()> {
let service = self.transport_manager.register_protocol(
config.protocol_name.clone(),
config.fallback_names.clone(),
config.codec,
self.runtime_configs.keep_alive_timeout,
);

self.runtime_configs.executor.run(Box::pin(async move {
RequestResponseProtocol::new(service, config).run().await
}));

Ok(())
}

/// Register a notification protocol at runtime.
pub fn register_notification(
&mut self,
config: crate::protocol::notification::Config,
) -> crate::Result<()> {
let service = self.transport_manager.register_protocol(
config.protocol_name.clone(),
config.fallback_names.clone(),
config.codec,
self.runtime_configs.keep_alive_timeout,
);

let executor = Arc::clone(&self.runtime_configs.executor);
self.runtime_configs.executor.run(Box::pin(async move {
NotificationProtocol::new(service, config, executor).run().await
}));

Ok(())
}

/// Poll next event.
///
/// This function must be called in order for litep2p to make progress.
Expand Down
Loading