|
|
@@ -1,7 +1,7 @@
|
|
|
use async_executor::Executor;
|
|
|
use async_std::sync::Mutex;
|
|
|
use log::*;
|
|
|
-use std::collections::HashMap;
|
|
|
+use std::collections::{HashSet, HashMap};
|
|
|
use std::net::SocketAddr;
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
@@ -10,12 +10,14 @@ use crate::net::sessions::{InboundSession, OutboundSession, SeedSession};
|
|
|
use crate::net::{Channel, ChannelPtr, Hosts, HostsPtr, Settings, SettingsPtr};
|
|
|
use crate::system::{Subscriber, SubscriberPtr, Subscription};
|
|
|
|
|
|
-pub type Pending<T> = Mutex<HashMap<SocketAddr, Arc<T>>>;
|
|
|
+pub type PendingChannels = Mutex<HashSet<SocketAddr>>;
|
|
|
+pub type ConnectedChannels<T> = Mutex<HashMap<SocketAddr, Arc<T>>>;
|
|
|
|
|
|
pub type P2pPtr = Arc<P2p>;
|
|
|
|
|
|
pub struct P2p {
|
|
|
- pending_channels: Pending<Channel>,
|
|
|
+ pending: PendingChannels,
|
|
|
+ channels: ConnectedChannels<Channel>,
|
|
|
// Used internally
|
|
|
stop_subscriber: SubscriberPtr<NetError>,
|
|
|
hosts: HostsPtr,
|
|
|
@@ -26,7 +28,8 @@ impl P2p {
|
|
|
pub fn new(settings: Settings) -> Arc<Self> {
|
|
|
let settings = Arc::new(settings);
|
|
|
Arc::new(Self {
|
|
|
- pending_channels: Mutex::new(HashMap::new()),
|
|
|
+ pending: Mutex::new(HashSet::new()),
|
|
|
+ channels: Mutex::new(HashMap::new()),
|
|
|
stop_subscriber: Subscriber::new(),
|
|
|
hosts: Hosts::new(settings.clone()),
|
|
|
settings,
|
|
|
@@ -70,21 +73,32 @@ impl P2p {
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
- pub async fn store(self: Arc<Self>, channel: ChannelPtr) {
|
|
|
- self.pending_channels
|
|
|
+ pub async fn store(&self, channel: ChannelPtr) {
|
|
|
+ self.channels
|
|
|
.lock()
|
|
|
.await
|
|
|
.insert(channel.address(), channel);
|
|
|
}
|
|
|
- pub async fn remove(self: Arc<Self>, channel: ChannelPtr) {
|
|
|
- self.pending_channels
|
|
|
+ pub async fn remove(&self, channel: ChannelPtr) {
|
|
|
+ self.channels
|
|
|
.lock()
|
|
|
.await
|
|
|
.remove(&channel.address());
|
|
|
}
|
|
|
|
|
|
+ pub async fn exists(&self, addr: &SocketAddr) -> bool {
|
|
|
+ self.channels.lock().await.contains_key(addr)
|
|
|
+ }
|
|
|
+
|
|
|
+ pub async fn add_pending(&self, addr: SocketAddr) -> bool {
|
|
|
+ self.pending.lock().await.insert(addr)
|
|
|
+ }
|
|
|
+ pub async fn remove_pending(&self, addr: &SocketAddr) {
|
|
|
+ self.pending.lock().await.remove(addr);
|
|
|
+ }
|
|
|
+
|
|
|
pub async fn connections_count(&self) -> usize {
|
|
|
- self.pending_channels.lock().await.len()
|
|
|
+ self.channels.lock().await.len()
|
|
|
}
|
|
|
|
|
|
pub fn settings(&self) -> SettingsPtr {
|