|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + handlers::{WorkflowHandler, WorkflowPayload}, |
| 5 | + payloads::{TaskRequestPayload, TaskResponsePayload}, |
| 6 | + utils::DriaMessage, |
| 7 | +}; |
| 8 | +use dkn_p2p::{ |
| 9 | + libp2p::{ |
| 10 | + gossipsub::{Message, MessageId}, |
| 11 | + PeerId, |
| 12 | + }, |
| 13 | + DriaP2PCommander, |
| 14 | +}; |
| 15 | +use eyre::Result; |
| 16 | +use tokio::sync::mpsc; |
| 17 | +use tokio_util::sync::CancellationToken; |
| 18 | + |
| 19 | +const TASK_PRINT_INTERVAL_SECS: u64 = 20; |
| 20 | + |
| 21 | +pub struct DriaMonitorNode { |
| 22 | + pub p2p: DriaP2PCommander, |
| 23 | + pub msg_rx: mpsc::Receiver<(PeerId, MessageId, Message)>, |
| 24 | + |
| 25 | + // task monitoring |
| 26 | + pub tasks: HashMap<String, TaskRequestPayload<WorkflowPayload>>, |
| 27 | + pub results: HashMap<String, TaskResponsePayload>, |
| 28 | +} |
| 29 | + |
| 30 | +impl DriaMonitorNode { |
| 31 | + pub fn new( |
| 32 | + p2p: DriaP2PCommander, |
| 33 | + msg_rx: mpsc::Receiver<(PeerId, MessageId, Message)>, |
| 34 | + ) -> Self { |
| 35 | + Self { |
| 36 | + p2p, |
| 37 | + msg_rx, |
| 38 | + tasks: HashMap::new(), |
| 39 | + results: HashMap::new(), |
| 40 | + } |
| 41 | + } |
| 42 | + pub async fn setup(&self) -> Result<()> { |
| 43 | + self.p2p.subscribe(WorkflowHandler::LISTEN_TOPIC).await?; |
| 44 | + self.p2p.subscribe(WorkflowHandler::RESPONSE_TOPIC).await?; |
| 45 | + |
| 46 | + Ok(()) |
| 47 | + } |
| 48 | + |
| 49 | + pub async fn shutdown(&mut self) -> Result<()> { |
| 50 | + log::info!("Shutting down monitor"); |
| 51 | + self.p2p.unsubscribe(WorkflowHandler::LISTEN_TOPIC).await?; |
| 52 | + self.p2p |
| 53 | + .unsubscribe(WorkflowHandler::RESPONSE_TOPIC) |
| 54 | + .await?; |
| 55 | + |
| 56 | + self.p2p.shutdown().await?; |
| 57 | + self.msg_rx.close(); |
| 58 | + |
| 59 | + Ok(()) |
| 60 | + } |
| 61 | + |
| 62 | + pub async fn run(&mut self, token: CancellationToken) { |
| 63 | + let mut task_print_interval = |
| 64 | + tokio::time::interval(tokio::time::Duration::from_secs(TASK_PRINT_INTERVAL_SECS)); |
| 65 | + |
| 66 | + loop { |
| 67 | + tokio::select! { |
| 68 | + message = self.msg_rx.recv() => match message { |
| 69 | + Some(message) => match self.handle_message(message).await { |
| 70 | + Ok(_) => {} |
| 71 | + Err(e) => log::error!("Error handling message: {:?}", e), |
| 72 | + } |
| 73 | + None => break, // channel closed |
| 74 | + }, |
| 75 | + _ = task_print_interval.tick() => { |
| 76 | + log::info!("Current seen tasks: {:#?}", self.tasks.keys().collect::<Vec<_>>()); |
| 77 | + } |
| 78 | + _ = token.cancelled() => break, |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + async fn handle_message( |
| 84 | + &mut self, |
| 85 | + (peer_id, message_id, gossipsub_message): (PeerId, MessageId, Message), |
| 86 | + ) -> Result<()> { |
| 87 | + log::info!( |
| 88 | + "Received {} message {} from {}", |
| 89 | + gossipsub_message.topic, |
| 90 | + message_id, |
| 91 | + peer_id |
| 92 | + ); |
| 93 | + |
| 94 | + // accept all message regardless immediately |
| 95 | + self.p2p |
| 96 | + .validate_message( |
| 97 | + &message_id, |
| 98 | + &peer_id, |
| 99 | + dkn_p2p::libp2p::gossipsub::MessageAcceptance::Accept, |
| 100 | + ) |
| 101 | + .await?; |
| 102 | + |
| 103 | + // parse message, ignore signatures |
| 104 | + let message: DriaMessage = serde_json::from_slice(&gossipsub_message.data)?; |
| 105 | + |
| 106 | + match message.topic.as_str() { |
| 107 | + WorkflowHandler::LISTEN_TOPIC => { |
| 108 | + let payload: TaskRequestPayload<WorkflowPayload> = message.parse_payload(true)?; |
| 109 | + self.tasks.insert(payload.task_id.clone(), payload); |
| 110 | + } |
| 111 | + WorkflowHandler::RESPONSE_TOPIC => { |
| 112 | + let payload: TaskResponsePayload = message.parse_payload(false)?; |
| 113 | + self.results.insert(payload.task_id.clone(), payload); |
| 114 | + } |
| 115 | + _ => { /* ignore */ } |
| 116 | + } |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | +} |
0 commit comments