|
|
@ -35,14 +35,14 @@ use crate::utils::spawn_and_log_error; |
|
|
|
use crate::utils::{Receiver, ResultSend, Sender}; |
|
|
|
use crate::utils::{Receiver, ResultSend, Sender}; |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
#[derive(Debug)] |
|
|
|
pub enum PeerConnection { |
|
|
|
enum PeerConnection { |
|
|
|
Core(BindAddress), |
|
|
|
Core(BindAddress), |
|
|
|
Client(ConnectionBase), |
|
|
|
Client(ConnectionBase), |
|
|
|
NONE, |
|
|
|
NONE, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
#[derive(Debug)] |
|
|
|
pub struct BrokerPeerInfo { |
|
|
|
struct BrokerPeerInfo { |
|
|
|
#[allow(dead_code)] |
|
|
|
#[allow(dead_code)] |
|
|
|
last_peer_advert: Option<PeerAdvert>, //FIXME: remove Option
|
|
|
|
last_peer_advert: Option<PeerAdvert>, //FIXME: remove Option
|
|
|
|
connected: PeerConnection, |
|
|
|
connected: PeerConnection, |
|
|
@ -50,7 +50,7 @@ pub struct BrokerPeerInfo { |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
#[derive(Debug)] |
|
|
|
#[allow(dead_code)] |
|
|
|
#[allow(dead_code)] |
|
|
|
pub struct DirectConnection { |
|
|
|
struct DirectConnection { |
|
|
|
addr: BindAddress, |
|
|
|
addr: BindAddress, |
|
|
|
remote_peer_id: X25519PrivKey, |
|
|
|
remote_peer_id: X25519PrivKey, |
|
|
|
tp: TransportProtocol, |
|
|
|
tp: TransportProtocol, |
|
|
@ -69,6 +69,7 @@ pub struct ServerConfig { |
|
|
|
pub bootstrap: BootstrapContent, |
|
|
|
pub bootstrap: BootstrapContent, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)] |
|
|
|
#[async_trait::async_trait] |
|
|
|
#[async_trait::async_trait] |
|
|
|
pub trait ILocalBroker: Send + Sync + EActor { |
|
|
|
pub trait ILocalBroker: Send + Sync + EActor { |
|
|
|
async fn deliver(&mut self, event: Event, overlay: OverlayId, user: UserId); |
|
|
|
async fn deliver(&mut self, event: Event, overlay: OverlayId, user: UserId); |
|
|
@ -122,28 +123,30 @@ impl Broker { |
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_config(&self) -> Option<&ServerConfig> { |
|
|
|
pub(crate) fn get_config(&self) -> Option<&ServerConfig> { |
|
|
|
self.config.as_ref() |
|
|
|
self.config.as_ref() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_registration_url(&self) -> Option<&String> { |
|
|
|
pub(crate) fn get_registration_url(&self) -> Option<&String> { |
|
|
|
self.config |
|
|
|
self.config |
|
|
|
.as_ref() |
|
|
|
.as_ref() |
|
|
|
.and_then(|c| c.registration_url.as_ref()) |
|
|
|
.and_then(|c| c.registration_url.as_ref()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_bootstrap(&self) -> Result<&BootstrapContent, ProtocolError> { |
|
|
|
pub(crate) fn get_bootstrap(&self) -> Result<&BootstrapContent, ProtocolError> { |
|
|
|
self.config |
|
|
|
self.config |
|
|
|
.as_ref() |
|
|
|
.as_ref() |
|
|
|
.map(|c| &c.bootstrap) |
|
|
|
.map(|c| &c.bootstrap) |
|
|
|
.ok_or(ProtocolError::BrokerError) |
|
|
|
.ok_or(ProtocolError::BrokerError) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)] |
|
|
|
pub fn set_server_broker(&mut self, broker: impl IServerBroker + 'static) { |
|
|
|
pub fn set_server_broker(&mut self, broker: impl IServerBroker + 'static) { |
|
|
|
//log_debug!("set_server_broker");
|
|
|
|
//log_debug!("set_server_broker");
|
|
|
|
self.server_broker = Some(Box::new(broker)); |
|
|
|
self.server_broker = Some(Box::new(broker)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)] |
|
|
|
pub fn set_local_broker(&mut self, broker: Arc<RwLock<dyn ILocalBroker>>) { |
|
|
|
pub fn set_local_broker(&mut self, broker: Arc<RwLock<dyn ILocalBroker>>) { |
|
|
|
//log_debug!("set_local_broker");
|
|
|
|
//log_debug!("set_local_broker");
|
|
|
|
self.local_broker = Some(broker); |
|
|
|
self.local_broker = Some(broker); |
|
|
@ -171,7 +174,7 @@ impl Broker { |
|
|
|
(copy_listeners, copy_bind_addresses) |
|
|
|
(copy_listeners, copy_bind_addresses) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_server_broker( |
|
|
|
pub(crate) fn get_server_broker( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
) -> Result<&Box<dyn IServerBroker + Send + Sync>, ProtocolError> { |
|
|
|
) -> Result<&Box<dyn IServerBroker + Send + Sync>, ProtocolError> { |
|
|
|
//log_debug!("GET STORAGE {:?}", self.server_storage);
|
|
|
|
//log_debug!("GET STORAGE {:?}", self.server_storage);
|
|
|
@ -180,7 +183,7 @@ impl Broker { |
|
|
|
.ok_or(ProtocolError::BrokerError) |
|
|
|
.ok_or(ProtocolError::BrokerError) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_server_broker_mut( |
|
|
|
pub(crate) fn get_server_broker_mut( |
|
|
|
&mut self, |
|
|
|
&mut self, |
|
|
|
) -> Result<&mut Box<dyn IServerBroker + Send + Sync>, ProtocolError> { |
|
|
|
) -> Result<&mut Box<dyn IServerBroker + Send + Sync>, ProtocolError> { |
|
|
|
//log_debug!("GET STORAGE {:?}", self.server_storage);
|
|
|
|
//log_debug!("GET STORAGE {:?}", self.server_storage);
|
|
|
@ -199,7 +202,7 @@ impl Broker { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))] |
|
|
|
#[cfg(not(target_arch = "wasm32"))] |
|
|
|
pub fn authorize( |
|
|
|
pub(crate) fn authorize( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
bind_addresses: &(BindAddress, BindAddress), |
|
|
|
bind_addresses: &(BindAddress, BindAddress), |
|
|
|
auth: Authorization, |
|
|
|
auth: Authorization, |
|
|
@ -306,7 +309,7 @@ impl Broker { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn reconnecting(&mut self, peer_id: X25519PrivKey, user: Option<PubKey>) { |
|
|
|
fn reconnecting(&mut self, peer_id: X25519PrivKey, user: Option<PubKey>) { |
|
|
|
let peerinfo = self.peers.get_mut(&(user, peer_id)); |
|
|
|
let peerinfo = self.peers.get_mut(&(user, peer_id)); |
|
|
|
match peerinfo { |
|
|
|
match peerinfo { |
|
|
|
Some(info) => match &info.connected { |
|
|
|
Some(info) => match &info.connected { |
|
|
@ -351,7 +354,8 @@ impl Broker { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn remove_anonymous( |
|
|
|
#[cfg(not(target_arch = "wasm32"))] |
|
|
|
|
|
|
|
fn remove_anonymous( |
|
|
|
&mut self, |
|
|
|
&mut self, |
|
|
|
remote_bind_address: BindAddress, |
|
|
|
remote_bind_address: BindAddress, |
|
|
|
local_bind_address: BindAddress, |
|
|
|
local_bind_address: BindAddress, |
|
|
@ -380,7 +384,7 @@ impl Broker { |
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
pub fn new() -> Self { |
|
|
|
fn new() -> Self { |
|
|
|
let (shutdown_sender, shutdown_receiver) = mpsc::unbounded::<ProtocolError>(); |
|
|
|
let (shutdown_sender, shutdown_receiver) = mpsc::unbounded::<ProtocolError>(); |
|
|
|
let mut random_buf = [0u8; 4]; |
|
|
|
let mut random_buf = [0u8; 4]; |
|
|
|
getrandom::getrandom(&mut random_buf).unwrap(); |
|
|
|
getrandom::getrandom(&mut random_buf).unwrap(); |
|
|
@ -496,7 +500,9 @@ impl Broker { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub async fn shutdown(&mut self) { |
|
|
|
#[allow(dead_code)] |
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))] |
|
|
|
|
|
|
|
async fn shutdown(&mut self) { |
|
|
|
if self.closing { |
|
|
|
if self.closing { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -505,6 +511,7 @@ impl Broker { |
|
|
|
let _ = self.shutdown_sender.send(ProtocolError::Closing).await; |
|
|
|
let _ = self.shutdown_sender.send(ProtocolError::Closing).await; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)] |
|
|
|
#[cfg(not(target_arch = "wasm32"))] |
|
|
|
#[cfg(not(target_arch = "wasm32"))] |
|
|
|
pub async fn accept( |
|
|
|
pub async fn accept( |
|
|
|
&mut self, |
|
|
|
&mut self, |
|
|
@ -600,7 +607,7 @@ impl Broker { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))] |
|
|
|
#[cfg(not(target_arch = "wasm32"))] |
|
|
|
pub async fn attach_and_authorize_peer_id( |
|
|
|
pub(crate) async fn attach_and_authorize_peer_id( |
|
|
|
&mut self, |
|
|
|
&mut self, |
|
|
|
remote_bind_address: BindAddress, |
|
|
|
remote_bind_address: BindAddress, |
|
|
|
local_bind_address: BindAddress, |
|
|
|
local_bind_address: BindAddress, |
|
|
@ -883,7 +890,7 @@ impl Broker { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))] |
|
|
|
#[cfg(not(target_arch = "wasm32"))] |
|
|
|
pub async fn dispatch_event( |
|
|
|
pub(crate) async fn dispatch_event( |
|
|
|
&mut self, |
|
|
|
&mut self, |
|
|
|
overlay: &OverlayId, |
|
|
|
overlay: &OverlayId, |
|
|
|
event: Event, |
|
|
|
event: Event, |
|
|
@ -951,7 +958,7 @@ impl Broker { |
|
|
|
.await |
|
|
|
.await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub async fn close_anonymous( |
|
|
|
async fn close_anonymous( |
|
|
|
&mut self, |
|
|
|
&mut self, |
|
|
|
remote_bind_address: BindAddress, |
|
|
|
remote_bind_address: BindAddress, |
|
|
|
local_bind_address: BindAddress, |
|
|
|
local_bind_address: BindAddress, |
|
|
@ -964,6 +971,7 @@ impl Broker { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)] |
|
|
|
pub fn print_status(&self) { |
|
|
|
pub fn print_status(&self) { |
|
|
|
self.peers.iter().for_each(|(peer_id, peer_info)| { |
|
|
|
self.peers.iter().for_each(|(peer_id, peer_info)| { |
|
|
|
log_info!("PEER in BROKER {:?} {:?}", peer_id, peer_info); |
|
|
|
log_info!("PEER in BROKER {:?} {:?}", peer_id, peer_info); |
|
|
|