~qaul/community

[PATCH 2/2] Exposing `send` interface on Router structure

Details
Message ID
<20191024162604.7994-2-kookie@spacekookie.de>
DKIM signature
missing
Download raw message
Patch: +53 -13
---
 ratman/netmod/src/frame.rs |  1 +
 ratman/netmod/src/seq.rs   |  1 +
 ratman/src/core.rs         | 15 ++++++------
 ratman/src/lib.rs          | 49 ++++++++++++++++++++++++++++++++++----
 4 files changed, 53 insertions(+), 13 deletions(-)

diff --git a/ratman/netmod/src/frame.rs b/ratman/netmod/src/frame.rs
index 97e92dc..c832d56 100644
--- a/ratman/netmod/src/frame.rs
+++ b/ratman/netmod/src/frame.rs
@@ -22,6 +22,7 @@ pub enum Recipient {
 /// Because a `Frame` is usually created in a sequence, the
 /// constructors assume chainable operations, such as a `Vec<Frame>`
 /// can be returned with all sequence ID information correctly setup.
+#[derive(Clone)]
 pub struct Frame {
     /// Sender information
     pub sender: Identity,
diff --git a/ratman/netmod/src/seq.rs b/ratman/netmod/src/seq.rs
index 456e147..8d9afd6 100644
--- a/ratman/netmod/src/seq.rs
+++ b/ratman/netmod/src/seq.rs
@@ -23,6 +23,7 @@ pub struct XxSignature {
 /// was transmitted without error.
 ///
 /// Check the `netmod` crate documentation for more details.
+#[derive(Clone)]
 pub struct SeqId {
     /// A hash signature of the payload
     pub sig: XxSignature,
diff --git a/ratman/src/core.rs b/ratman/src/core.rs
index 4792a13..a45bc92 100644
--- a/ratman/src/core.rs
+++ b/ratman/src/core.rs
@@ -53,13 +53,7 @@ impl Worker {
             });
         });
 
-        (
-            Self {
-                _thread,
-                to_send,
-            },
-            received,
-        )
+        (Self { _thread, to_send }, received)
     }
 }
 
@@ -116,7 +110,12 @@ impl Core {
 
     /// Remove a list of interface names and their unique IDs
     pub(crate) fn get_ifs(&self) -> Vec<(u8, String)> {
-        vec![]
+        self.ifs
+            .lock()
+            .unwrap()
+            .keys()
+            .map(|k| (*k, "".into()))
+            .collect()
     }
 
     pub(crate) fn id_reachable(&self, id: Identity, ifid: u8) {
diff --git a/ratman/src/lib.rs b/ratman/src/lib.rs
index 2005134..a6bb571 100644
--- a/ratman/src/lib.rs
+++ b/ratman/src/lib.rs
@@ -1,5 +1,6 @@
-//! RATMAN decentralised routing protocol
+//! `R.A.T.M.A.N.` decentralised routing protocol
 //!
+//! <small> Nananananananana Nananananananana RAT MAN!</small>
 //! A modern approach to fully delay-tolerant mesh routing,
 //! implemented network agnostically and entirely in userspace.
 
@@ -15,18 +16,35 @@ pub use crate::{
 };
 pub use netmod;
 
-use crate::{core::Core, journal::Journal};
-use netmod::Endpoint;
+use crate::{
+    core::{Core, Envelope},
+    journal::Journal,
+    slicer::Slicer,
+};
+use netmod::{Endpoint, Recipient};
+use std::sync::{mpsc::Receiver, Arc, Mutex};
 
-use std::sync::{Arc, Mutex};
+/// A temporary structure used to initialise a `R.A.T.M.A.N.` `Router`
+///
+/// Use this structure only for destructuring, it has no useful
+/// attributes beyond named fields.
+pub struct RouterInit {
+    router: Router,
+    channel: Receiver<Message>,
+}
 
-/// A `RATMAN` router context
+/// A `R.A.T.M.A.N.` routing context
+///
+/// Handles `Message` splicing, `Frame` sequencing, routing tables,
+/// journal keeping, re-transmissions of non-local frames, as well as
+/// the discovery protocol.
 pub struct Router {
     core: Arc<Mutex<Core>>,
     journal: Arc<Journal>,
 }
 
 impl Router {
+    /// Create a new `Router` with internal mutability
     pub fn new() -> Self {
         let (core, j_rcv) = Some(Core::new())
             .map(|(c, r)| (Arc::new(Mutex::new(c)), r))
@@ -42,4 +60,25 @@ impl Router {
     pub fn add_ep(&self, ep: impl Endpoint + 'static + Send) {
         self.core.lock().unwrap().add_if(ep);
     }
+
+    /// Queue a `R.A.T.M.A.N.` message for sending
+    pub fn send(&self, msg: Message) {
+        // FIXME: This is so pointless...
+        let recp = msg.recipient.clone();
+
+        let frames = Slicer::slice(0, msg);
+        let core = self.core.lock().unwrap();
+
+        core.send(match recp {
+            Recipient::User(ref id) => core.lookup(id, frames),
+            Recipient::Flood => core
+                .get_ifs()
+                .into_iter()
+                .fold(vec![], |mut vec, (ifid, _)| {
+                    let mut set = frames.iter().cloned().map(|f| Envelope(ifid, f)).collect();
+                    vec.append(&mut set);
+                    vec
+                }),
+        });
+    }
 }
-- 
2.23.0