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

bin/ircd: build ring buffer struct & remove ringbuffer crate

ghassmo 3 лет назад
Родитель
Сommit
f74b636e63

+ 0 - 1
bin/ircd/Cargo.toml

@@ -34,7 +34,6 @@ simplelog = "0.12.0"
 fxhash = "0.2.1"
 ctrlc = { version = "3.2.3", features = ["termination"] }
 url = "2.2.2"
-ringbuffer = "0.8.5"
 
 # Encoding and parsing
 serde_json = "1.0.85"

+ 72 - 0
bin/ircd/src/buffers.rs

@@ -0,0 +1,72 @@
+use async_std::sync::{Arc, Mutex};
+use std::collections::VecDeque;
+
+use crate::{Privmsg, SIZE_OF_MSGS_BUFFER};
+
+pub struct RingBuffer<T> {
+    pub items: VecDeque<T>,
+    pub size: usize,
+}
+
+impl<T: Eq + PartialEq> RingBuffer<T> {
+    pub fn new(capacity: usize) -> Self {
+        let items = VecDeque::with_capacity(capacity);
+        let size = items.capacity();
+        Self { items, size }
+    }
+
+    pub fn push(&mut self, val: T) {
+        if self.items.len() == self.size {
+            self.items.pop_front();
+        }
+        self.items.push_back(val);
+    }
+
+    pub fn contains(&self, val: &T) -> bool {
+        self.items.contains(val)
+    }
+}
+
+pub type SeenMsgIds = Arc<Mutex<RingBuffer<u64>>>;
+
+pub type ArcPrivmsgsBuffer = Arc<Mutex<PrivmsgsBuffer>>;
+
+pub struct PrivmsgsBuffer(RingBuffer<Privmsg>);
+
+impl PrivmsgsBuffer {
+    pub fn new() -> ArcPrivmsgsBuffer {
+        Arc::new(Mutex::new(Self(RingBuffer::new(SIZE_OF_MSGS_BUFFER))))
+    }
+
+    pub fn push(&mut self, _privmsg: &Privmsg) {
+        // TODO
+    }
+
+    pub fn last_term(&self) -> u64 {
+        match self.0.items.len() {
+            0 => 0,
+            n => self.0.items[n - 1].term,
+        }
+    }
+
+    pub fn to_vec(&self) -> Vec<Privmsg> {
+        self.0.items.clone().into()
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    #[test]
+    fn test_ring_buffer() {
+        let mut b = RingBuffer::<&str>::new(3);
+        b.push("h1");
+        b.push("h2");
+        b.push("h3");
+        assert_eq!(b.items, vec!["h1", "h2", "h3"]);
+        assert_eq!(b.items.capacity(), b.size);
+        b.push("h4");
+        assert_eq!(b.items, vec!["h2", "h3", "h4"]);
+        assert_eq!(b.items.capacity(), b.size);
+    }
+}

+ 4 - 3
bin/ircd/src/main.rs

@@ -28,6 +28,7 @@ use darkfi::{
     Error, Result,
 };
 
+pub mod buffers;
 pub mod crypto;
 pub mod privmsg;
 pub mod protocol_privmsg;
@@ -36,7 +37,8 @@ pub mod server;
 pub mod settings;
 
 use crate::{
-    privmsg::{ArcPrivmsgsBuffer, Privmsg, PrivmsgsBuffer, SeenMsgIds},
+    buffers::{ArcPrivmsgsBuffer, PrivmsgsBuffer, RingBuffer, SeenMsgIds},
+    privmsg::Privmsg,
     protocol_privmsg::ProtocolPrivmsg,
     rpc::JsonRpcInterface,
     server::IrcServerConnection,
@@ -180,8 +182,7 @@ impl Ircd {
 
 async_daemonize!(realmain);
 async fn realmain(settings: Args, executor: Arc<Executor<'_>>) -> Result<()> {
-    let seen_msg_ids =
-        Arc::new(Mutex::new(ringbuffer::AllocRingBuffer::with_capacity(SIZE_OF_MSG_IDSS_BUFFER)));
+    let seen_msg_ids = Arc::new(Mutex::new(RingBuffer::new(SIZE_OF_MSG_IDSS_BUFFER)));
     let privmsgs_buffer = PrivmsgsBuffer::new();
 
     if settings.gen_secret {

+ 1 - 36
bin/ircd/src/privmsg.rs

@@ -1,48 +1,13 @@
-use async_std::sync::{Arc, Mutex};
-
 use rand::{rngs::OsRng, RngCore};
-use ringbuffer::{AllocRingBuffer, RingBufferExt, RingBufferWrite};
 
 use darkfi::util::{
     serial::{SerialDecodable, SerialEncodable},
     Timestamp,
 };
 
-use crate::SIZE_OF_MSGS_BUFFER;
-
 pub type PrivmsgId = u64;
 
-pub type SeenMsgIds = Arc<Mutex<AllocRingBuffer<u64>>>;
-
-pub type ArcPrivmsgsBuffer = Arc<Mutex<PrivmsgsBuffer>>;
-
-pub struct PrivmsgsBuffer(AllocRingBuffer<Privmsg>);
-
-impl PrivmsgsBuffer {
-    pub fn new() -> ArcPrivmsgsBuffer {
-        Arc::new(Mutex::new(Self(ringbuffer::AllocRingBuffer::with_capacity(SIZE_OF_MSGS_BUFFER))))
-    }
-
-    pub fn push(&mut self, privmsg: &Privmsg) {
-        if privmsg.timestamp > Timestamp::current_time() {
-            return
-        }
-
-        if let Some(last_msg) = self.0.get(-1) {
-            if privmsg.timestamp > last_msg.timestamp {
-                self.0.push(privmsg.clone());
-            }
-        } else {
-            self.0.push(privmsg.clone());
-        }
-    }
-
-    pub fn to_vec(&self) -> Vec<Privmsg> {
-        self.0.to_vec()
-    }
-}
-
-#[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
+#[derive(Debug, Clone, SerialEncodable, SerialDecodable, Eq, PartialEq)]
 pub struct Privmsg {
     pub id: PrivmsgId,
     pub nickname: String,

+ 4 - 2
bin/ircd/src/protocol_privmsg.rs

@@ -3,11 +3,13 @@ use async_std::sync::Arc;
 use async_executor::Executor;
 use async_trait::async_trait;
 use log::debug;
-use ringbuffer::{RingBufferExt, RingBufferWrite};
 
 use darkfi::{net, Result};
 
-use crate::privmsg::{ArcPrivmsgsBuffer, Privmsg, SeenMsgIds};
+use crate::{
+    buffers::{ArcPrivmsgsBuffer, SeenMsgIds},
+    Privmsg,
+};
 
 pub struct ProtocolPrivmsg {
     jobsman: net::ProtocolJobsManagerPtr,

+ 0 - 1
bin/ircd/src/server/command.rs

@@ -1,6 +1,5 @@
 use futures::{AsyncRead, AsyncWrite};
 use log::{debug, info, warn};
-use ringbuffer::RingBufferWrite;
 
 use darkfi::{Error, Result};
 

+ 2 - 2
bin/ircd/src/server/mod.rs

@@ -7,9 +7,9 @@ use log::{debug, info, warn};
 use darkfi::{net::P2pPtr, system::SubscriberPtr, Error, Result};
 
 use crate::{
+    buffers::{ArcPrivmsgsBuffer, SeenMsgIds},
     crypto::{decrypt_privmsg, decrypt_target},
-    privmsg::{ArcPrivmsgsBuffer, Privmsg, SeenMsgIds},
-    ChannelInfo, MAXIMUM_LENGTH_OF_MESSAGE,
+    ChannelInfo, Privmsg, MAXIMUM_LENGTH_OF_MESSAGE,
 };
 
 mod command;