Просмотр исходного кода

bin/ircd/model: create EventQueue to handle communications between View
and Model

ghassmo 3 лет назад
Родитель
Сommit
043a50a03f
2 измененных файлов с 32 добавлено и 7 удалено
  1. 23 1
      bin/ircd/src/model.rs
  2. 9 6
      bin/ircd/src/view.rs

+ 23 - 1
bin/ircd/src/model.rs

@@ -1,13 +1,35 @@
+use async_std::sync::Arc;
 use std::{fmt, io};
 
 use fxhash::FxHashMap;
 use ripemd::{Digest, Ripemd256};
 
-use darkfi::serial::{Decodable, Encodable, ReadExt, SerialDecodable, SerialEncodable};
+use darkfi::{
+    serial::{Decodable, Encodable, ReadExt, SerialDecodable, SerialEncodable},
+    Error, Result,
+};
 
 use crate::settings::get_current_time;
 
 pub type EventId = [u8; 32];
+pub type EventQueueArc = Arc<EventQueue>;
+
+pub struct EventQueue(async_channel::Sender<Event>, async_channel::Receiver<Event>);
+
+impl EventQueue {
+    pub fn new() -> EventQueueArc {
+        let (sn, rv) = async_channel::unbounded();
+        Arc::new(Self(sn, rv))
+    }
+
+    pub async fn fetch(&self) -> Result<Event> {
+        self.1.recv().await.map_err(Error::from)
+    }
+
+    pub async fn dispatch(&self, event: &Event) -> Result<()> {
+        self.0.send(event.clone()).await.map_err(Error::from)
+    }
+}
 
 const MAX_DEPTH: u32 = 300;
 const MAX_HEIGHT: u32 = 300;

+ 9 - 6
bin/ircd/src/view.rs

@@ -1,6 +1,8 @@
 use fxhash::FxHashMap;
 
-use crate::model::{Event, EventId, Model};
+use darkfi::Result;
+
+use crate::model::{Event, EventId, EventQueueArc, Model};
 
 struct View {
     seen: FxHashMap<EventId, Event>,
@@ -11,10 +13,11 @@ impl View {
         Self { seen: FxHashMap::default() }
     }
 
-    fn process(_model: &Model) {
-        // This does 2 passes:
-        // 1. Walk down all chains and get unseen events
-        // 2. Order those events according to timestamp
-        // Then the events are replayed to the IRC client
+    pub async fn process(&mut self, event_queue: EventQueueArc) -> Result<()>  {
+        loop {
+            let new_event = event_queue.fetch().await?;
+            // TODO sort the events
+            self.seen.insert(new_event.hash(), new_event);
+        }
     }
 }