lib.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. pub mod communicator;
  2. pub mod tcp;
  3. pub mod unix;
  4. use bincode::error::{DecodeError, EncodeError};
  5. use std::collections::HashMap;
  6. use std::io::Error as IoError;
  7. use std::sync::mpsc::{RecvError, SendError};
  8. pub trait Serializable: Clone + Send + 'static + bincode::Encode + bincode::Decode {}
  9. impl<T> Serializable for T where T: Clone + Send + 'static + bincode::Encode + bincode::Decode {}
  10. /// Represent data of type T that we expect to receive
  11. pub trait Fut<T> {
  12. /// Wait until the data has arrived and obtain it.
  13. fn get(self) -> Result<T, Error>;
  14. }
  15. #[derive(Debug, Clone, Copy)]
  16. pub struct CommunicationStats {
  17. pub num_msgs_received: usize,
  18. pub num_bytes_received: usize,
  19. pub num_msgs_sent: usize,
  20. pub num_bytes_sent: usize,
  21. }
  22. /// Abstract communication interface between multiple parties
  23. pub trait AbstractCommunicator {
  24. type Fut<T: Serializable>: Fut<T>;
  25. /// How many parties N there are in total
  26. fn get_num_parties(&self) -> usize;
  27. /// My party id in [0, N)
  28. fn get_my_id(&self) -> usize;
  29. /// Send a message of type T to given party
  30. fn send<T: Serializable>(&mut self, party_id: usize, val: T) -> Result<(), Error>;
  31. /// Send a message of multiple elements of type T to given party
  32. fn send_slice<T: Serializable>(&mut self, party_id: usize, val: &[T]) -> Result<(), Error>;
  33. /// Send a message of type T to next party
  34. fn send_next<T: Serializable>(&mut self, val: T) -> Result<(), Error> {
  35. self.send((self.get_my_id() + 1) % self.get_num_parties(), val)
  36. }
  37. /// Send a message of multiple elements of type T to next party
  38. fn send_slice_next<T: Serializable>(&mut self, val: &[T]) -> Result<(), Error> {
  39. self.send_slice((self.get_my_id() + 1) % self.get_num_parties(), val)
  40. }
  41. /// Send a message of type T to previous party
  42. fn send_previous<T: Serializable>(&mut self, val: T) -> Result<(), Error> {
  43. self.send(
  44. (self.get_num_parties() + self.get_my_id() - 1) % self.get_num_parties(),
  45. val,
  46. )
  47. }
  48. /// Send a message of multiple elements of type T to previous party
  49. fn send_slice_previous<T: Serializable>(&mut self, val: &[T]) -> Result<(), Error> {
  50. self.send_slice(
  51. (self.get_num_parties() + self.get_my_id() - 1) % self.get_num_parties(),
  52. val,
  53. )
  54. }
  55. /// Send a message of type T all parties
  56. fn broadcast<T: Serializable>(&mut self, val: T) -> Result<(), Error> {
  57. let my_id = self.get_my_id();
  58. for party_id in 0..self.get_num_parties() {
  59. if party_id == my_id {
  60. continue;
  61. }
  62. self.send(party_id, val.clone())?;
  63. }
  64. Ok(())
  65. }
  66. /// Expect to receive message of type T from given party. Use the returned future to obtain
  67. /// the message once it has arrived.
  68. fn receive<T: Serializable>(&mut self, party_id: usize) -> Result<Self::Fut<T>, Error>;
  69. /// Expect to receive message of type T from the next party. Use the returned future to obtain
  70. /// the message once it has arrived.
  71. fn receive_next<T: Serializable>(&mut self) -> Result<Self::Fut<T>, Error> {
  72. self.receive((self.get_my_id() + 1) % self.get_num_parties())
  73. }
  74. /// Expect to receive message of type T from the previous party. Use the returned future to obtain
  75. /// the message once it has arrived.
  76. fn receive_previous<T: Serializable>(&mut self) -> Result<Self::Fut<T>, Error> {
  77. self.receive((self.get_num_parties() + self.get_my_id() - 1) % self.get_num_parties())
  78. }
  79. /// Shutdown the communication system
  80. fn shutdown(&mut self) -> HashMap<usize, CommunicationStats>;
  81. }
  82. /// Custom error type
  83. #[derive(Debug)]
  84. pub enum Error {
  85. /// The connection has not been established
  86. ConnectionSetupError,
  87. /// The API was not used correctly
  88. LogicError(String),
  89. /// Some std::io::Error appeared
  90. IoError(IoError),
  91. /// Some std::sync::mpsc::RecvError appeared
  92. RecvError(RecvError),
  93. /// Some std::sync::mpsc::SendError appeared
  94. SendError(String),
  95. /// Some bincode::error::DecodeError appeared
  96. EncodeError(EncodeError),
  97. /// Some bincode::error::DecodeError appeared
  98. DecodeError(DecodeError),
  99. /// Serialization of data failed
  100. SerializationError(String),
  101. /// Deserialization of data failed
  102. DeserializationError(String),
  103. }
  104. /// Enable automatic conversions from std::io::Error
  105. impl From<IoError> for Error {
  106. fn from(e: IoError) -> Error {
  107. Error::IoError(e)
  108. }
  109. }
  110. /// Enable automatic conversions from std::sync::mpsc::RecvError
  111. impl From<RecvError> for Error {
  112. fn from(e: RecvError) -> Error {
  113. Error::RecvError(e)
  114. }
  115. }
  116. /// Enable automatic conversions from std::sync::mpsc::SendError
  117. impl<T> From<SendError<T>> for Error {
  118. fn from(e: SendError<T>) -> Error {
  119. Error::SendError(e.to_string())
  120. }
  121. }
  122. /// Enable automatic conversions from bincode::error::EncodeError
  123. impl From<EncodeError> for Error {
  124. fn from(e: EncodeError) -> Error {
  125. Error::EncodeError(e)
  126. }
  127. }
  128. /// Enable automatic conversions from bincode::error::DecodeError
  129. impl From<DecodeError> for Error {
  130. fn from(e: DecodeError) -> Error {
  131. Error::DecodeError(e)
  132. }
  133. }