Skip to content

Commit

Permalink
Implemented Broadcast Options for NonBlockingSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
bushrat011899 committed Nov 9, 2023
1 parent d062ead commit fb6f11d
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 101 deletions.
42 changes: 42 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@ where
/// This method should return all messages received since the last time this method was called.
/// The pairs `(A, Message)` indicate from which address each packet was received.
fn receive_all_messages(&mut self) -> Vec<(A, Message)>;

/// Broadcast a single [`Message`] to all provided addresses.
fn send_to_many(&mut self, message: &Message, addresses: &[&A]) {
for address in addresses {
self.send_to(message, address);
}
}

/// Send many [`Messages`](`Message`) to a single addresses.
fn send_many_to(&mut self, messages: &[Message], address: &A) {
for message in messages {
self.send_to(message, address);
}
}

/// Send many [`Messages`](`Message`) to all provided addresses.
fn send_many_to_many(&mut self, messages: &[Message], addresses: &[&A]) {
for message in messages {
self.send_to_many(message, addresses);
}
}
}

/// Compile time parameterization for sessions.
Expand Down Expand Up @@ -276,4 +297,25 @@ where
/// This method should return all messages received since the last time this method was called.
/// The pairs `(A, Message)` indicate from which address each packet was received.
fn receive_all_messages(&mut self) -> Vec<(A, Message)>;

/// Broadcast a single [`Message`] to all provided addresses.
fn send_to_many(&mut self, message: &Message, addresses: &[&A]) {
for address in addresses {
self.send_to(message, address);
}
}

/// Send many [`Messages`](`Message`) to a single addresses.
fn send_many_to(&mut self, messages: &[Message], address: &A) {
for message in messages {
self.send_to(message, address);
}
}

/// Send many [`Messages`](`Message`) to all provided addresses.
fn send_many_to_many(&mut self, messages: &[Message], addresses: &[&A]) {
for message in messages {
self.send_to_many(message, addresses);
}
}
}
36 changes: 36 additions & 0 deletions src/network/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ pub(crate) struct MessageHeader {
pub magic: u16,
}

impl MessageHeader {
pub(crate) const UNINITIALIZED: Self = Self { magic: 0 };
pub(crate) const BROADCAST: Self = Self { magic: u16::MAX };
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum MessageBody {
SyncRequest(SyncRequest),
Expand All @@ -104,3 +109,34 @@ pub struct Message {
pub(crate) header: MessageHeader,
pub(crate) body: MessageBody,
}

/// Trait describing how to react to the receipt of the message `M`.
pub(crate) trait HandleMessage<M> {
fn handle(&mut self, body: &M, message: &Message);
}

// For types implementing `HandleMessage` for all `MessageBody` variants, we can
// automatically implement this behavior.
impl<T> HandleMessage<MessageBody> for T
where
T: HandleMessage<SyncRequest>,
T: HandleMessage<SyncReply>,
T: HandleMessage<Input>,
T: HandleMessage<InputAck>,
T: HandleMessage<QualityReport>,
T: HandleMessage<QualityReply>,
T: HandleMessage<ChecksumReport>,
{
fn handle(&mut self, body: &MessageBody, message: &Message) {
match &body {
MessageBody::SyncRequest(body) => self.handle(body, message),
MessageBody::SyncReply(body) => self.handle(body, message),
MessageBody::Input(body) => self.handle(body, message),
MessageBody::InputAck(body) => self.handle(body, message),
MessageBody::QualityReport(body) => self.handle(body, message),
MessageBody::QualityReply(body) => self.handle(body, message),
MessageBody::ChecksumReport(body) => self.handle(body, message),
MessageBody::KeepAlive => (),
}
}
}
Loading

0 comments on commit fb6f11d

Please sign in to comment.