Browse Source

communicator: add send_slice{,_previous,_next}

Lennart Braun 2 years ago
parent
commit
cddd02ce0a
2 changed files with 36 additions and 0 deletions
  1. 20 0
      communicator/src/communicator.rs
  2. 16 0
      communicator/src/lib.rs

+ 20 - 0
communicator/src/communicator.rs

@@ -126,6 +126,13 @@ impl SenderThread {
         Ok(())
         Ok(())
     }
     }
 
 
+    pub fn send_slice<T: Serializable>(&mut self, data: &[T]) -> Result<(), Error> {
+        let buf =
+            bincode::encode_to_vec(data, bincode::config::standard().skip_fixed_array_length())?;
+        self.buf_tx.send(buf)?;
+        Ok(())
+    }
+
     pub fn join(self) -> Result<(usize, usize), Error> {
     pub fn join(self) -> Result<(usize, usize), Error> {
         drop(self.buf_tx);
         drop(self.buf_tx);
         self.join_handle.join().expect("join failed")
         self.join_handle.join().expect("join failed")
@@ -202,6 +209,19 @@ impl AbstractCommunicator for Communicator {
         }
         }
     }
     }
 
 
+    fn send_slice<T: Serializable>(&mut self, party_id: usize, val: &[T]) -> Result<(), Error> {
+        match self.sender_threads.get_mut(&party_id) {
+            Some(t) => {
+                t.send_slice(val)?;
+                Ok(())
+            }
+            None => Err(Error::LogicError(format!(
+                "SenderThread for party {} not found",
+                party_id
+            ))),
+        }
+    }
+
     fn receive<T: Serializable>(&mut self, party_id: usize) -> Result<Self::Fut<T>, Error> {
     fn receive<T: Serializable>(&mut self, party_id: usize) -> Result<Self::Fut<T>, Error> {
         match self.receiver_threads.get_mut(&party_id) {
         match self.receiver_threads.get_mut(&party_id) {
             Some(t) => t.receive::<T>(),
             Some(t) => t.receive::<T>(),

+ 16 - 0
communicator/src/lib.rs

@@ -38,11 +38,19 @@ pub trait AbstractCommunicator {
     /// Send a message of type T to given party
     /// Send a message of type T to given party
     fn send<T: Serializable>(&mut self, party_id: usize, val: T) -> Result<(), Error>;
     fn send<T: Serializable>(&mut self, party_id: usize, val: T) -> Result<(), Error>;
 
 
+    /// Send a message of multiple elements of type T to given party
+    fn send_slice<T: Serializable>(&mut self, party_id: usize, val: &[T]) -> Result<(), Error>;
+
     /// Send a message of type T to next party
     /// Send a message of type T to next party
     fn send_next<T: Serializable>(&mut self, val: T) -> Result<(), Error> {
     fn send_next<T: Serializable>(&mut self, val: T) -> Result<(), Error> {
         self.send((self.get_my_id() + 1) % self.get_num_parties(), val)
         self.send((self.get_my_id() + 1) % self.get_num_parties(), val)
     }
     }
 
 
+    /// Send a message of multiple elements of type T to next party
+    fn send_slice_next<T: Serializable>(&mut self, val: &[T]) -> Result<(), Error> {
+        self.send_slice((self.get_my_id() + 1) % self.get_num_parties(), val)
+    }
+
     /// Send a message of type T to previous party
     /// Send a message of type T to previous party
     fn send_previous<T: Serializable>(&mut self, val: T) -> Result<(), Error> {
     fn send_previous<T: Serializable>(&mut self, val: T) -> Result<(), Error> {
         self.send(
         self.send(
@@ -51,6 +59,14 @@ pub trait AbstractCommunicator {
         )
         )
     }
     }
 
 
+    /// Send a message of multiple elements of type T to previous party
+    fn send_slice_previous<T: Serializable>(&mut self, val: &[T]) -> Result<(), Error> {
+        self.send_slice(
+            (self.get_num_parties() + self.get_my_id() - 1) % self.get_num_parties(),
+            val,
+        )
+    }
+
     /// Send a message of type T all parties
     /// Send a message of type T all parties
     fn broadcast<T: Serializable>(&mut self, val: T) -> Result<(), Error> {
     fn broadcast<T: Serializable>(&mut self, val: T) -> Result<(), Error> {
         let my_id = self.get_my_id();
         let my_id = self.get_my_id();