|
@@ -14,7 +14,7 @@ use crate::{
|
|
|
|
|
|
|
|
use super::{
|
|
use super::{
|
|
|
dht::DhtPtr,
|
|
dht::DhtPtr,
|
|
|
- messages::{KeyRequest, KeyResponse, LookupRequest},
|
|
|
|
|
|
|
+ messages::{KeyRequest, KeyResponse, LookupMapRequest, LookupMapResponse, LookupRequest},
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
pub struct Protocol {
|
|
pub struct Protocol {
|
|
@@ -23,6 +23,7 @@ pub struct Protocol {
|
|
|
req_sub: MessageSubscription<KeyRequest>,
|
|
req_sub: MessageSubscription<KeyRequest>,
|
|
|
resp_sub: MessageSubscription<KeyResponse>,
|
|
resp_sub: MessageSubscription<KeyResponse>,
|
|
|
lookup_sub: MessageSubscription<LookupRequest>,
|
|
lookup_sub: MessageSubscription<LookupRequest>,
|
|
|
|
|
+ lookup_map_sub: MessageSubscription<LookupMapRequest>,
|
|
|
jobsman: ProtocolJobsManagerPtr,
|
|
jobsman: ProtocolJobsManagerPtr,
|
|
|
dht: DhtPtr,
|
|
dht: DhtPtr,
|
|
|
p2p: P2pPtr,
|
|
p2p: P2pPtr,
|
|
@@ -40,10 +41,12 @@ impl Protocol {
|
|
|
msg_subsystem.add_dispatch::<KeyRequest>().await;
|
|
msg_subsystem.add_dispatch::<KeyRequest>().await;
|
|
|
msg_subsystem.add_dispatch::<KeyResponse>().await;
|
|
msg_subsystem.add_dispatch::<KeyResponse>().await;
|
|
|
msg_subsystem.add_dispatch::<LookupRequest>().await;
|
|
msg_subsystem.add_dispatch::<LookupRequest>().await;
|
|
|
|
|
+ msg_subsystem.add_dispatch::<LookupMapRequest>().await;
|
|
|
|
|
|
|
|
let req_sub = channel.subscribe_msg::<KeyRequest>().await?;
|
|
let req_sub = channel.subscribe_msg::<KeyRequest>().await?;
|
|
|
let resp_sub = channel.subscribe_msg::<KeyResponse>().await?;
|
|
let resp_sub = channel.subscribe_msg::<KeyResponse>().await?;
|
|
|
let lookup_sub = channel.subscribe_msg::<LookupRequest>().await?;
|
|
let lookup_sub = channel.subscribe_msg::<LookupRequest>().await?;
|
|
|
|
|
+ let lookup_map_sub = channel.subscribe_msg::<LookupMapRequest>().await?;
|
|
|
|
|
|
|
|
Ok(Arc::new(Self {
|
|
Ok(Arc::new(Self {
|
|
|
channel: channel.clone(),
|
|
channel: channel.clone(),
|
|
@@ -51,6 +54,7 @@ impl Protocol {
|
|
|
req_sub,
|
|
req_sub,
|
|
|
resp_sub,
|
|
resp_sub,
|
|
|
lookup_sub,
|
|
lookup_sub,
|
|
|
|
|
+ lookup_map_sub,
|
|
|
jobsman: ProtocolJobsManager::new("Protocol", channel),
|
|
jobsman: ProtocolJobsManager::new("Protocol", channel),
|
|
|
dht,
|
|
dht,
|
|
|
p2p,
|
|
p2p,
|
|
@@ -205,6 +209,40 @@ impl Protocol {
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ async fn handle_receive_lookup_map_request(self: Arc<Self>) -> Result<()> {
|
|
|
|
|
+ debug!("Protocol::handle_receive_lookup_map_request() [START]");
|
|
|
|
|
+ loop {
|
|
|
|
|
+ let req = match self.lookup_map_sub.receive().await {
|
|
|
|
|
+ Ok(v) => v,
|
|
|
|
|
+ Err(e) => {
|
|
|
|
|
+ error!("Protocol::handle_receive_lookup_map_request(): recv fail: {}", e);
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ debug!("Protocol::handle_receive_lookup_map_request(): req: {:?}", req);
|
|
|
|
|
+
|
|
|
|
|
+ {
|
|
|
|
|
+ let dht = &mut self.dht.write().await;
|
|
|
|
|
+ if dht.seen.contains_key(&req.id) {
|
|
|
|
|
+ debug!(
|
|
|
|
|
+ "Protocol::handle_receive_lookup_map_request(): We have already seen this request."
|
|
|
|
|
+ );
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ dht.seen.insert(req.id.clone(), Utc::now().timestamp());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Extra validations can be added here.
|
|
|
|
|
+ let lookup = self.dht.read().await.lookup.clone();
|
|
|
|
|
+ let response = LookupMapResponse::new(lookup);
|
|
|
|
|
+ if let Err(e) = self.channel.send(response).await {
|
|
|
|
|
+ error!("Protocol::handle_receive_lookup_map_request() channel send fail: {}", e);
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
#[async_trait]
|
|
@@ -218,6 +256,10 @@ impl ProtocolBase for Protocol {
|
|
|
.clone()
|
|
.clone()
|
|
|
.spawn(self.clone().handle_receive_lookup_request(), executor.clone())
|
|
.spawn(self.clone().handle_receive_lookup_request(), executor.clone())
|
|
|
.await;
|
|
.await;
|
|
|
|
|
+ self.jobsman
|
|
|
|
|
+ .clone()
|
|
|
|
|
+ .spawn(self.clone().handle_receive_lookup_map_request(), executor.clone())
|
|
|
|
|
+ .await;
|
|
|
debug!("Protocol::start() [END]");
|
|
debug!("Protocol::start() [END]");
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|