Skip to content

Commit

Permalink
refactor: peer manager, sink, stream, messages and fused
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Aug 11, 2024
1 parent 236271e commit 380b624
Show file tree
Hide file tree
Showing 8 changed files with 466 additions and 432 deletions.
7 changes: 4 additions & 3 deletions packages/peer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ mod protocol;
pub use codec::PeerProtocolCodec;

pub use crate::manager::builder::PeerManagerBuilder;
pub use crate::manager::messages::{IPeerManagerMessage, ManagedMessage, MessageId, OPeerManagerMessage};
pub use crate::manager::peer_info::PeerInfo;
pub use crate::manager::{
IPeerManagerMessage, ManagedMessage, MessageId, OPeerManagerMessage, PeerManager, PeerManagerSink, PeerManagerStream,
};
pub use crate::manager::sink::PeerManagerSink;
pub use crate::manager::stream::PeerManagerStream;
pub use crate::manager::PeerManager;
pub use crate::protocol::{NestedPeerProtocol, PeerProtocol};

/// Serializable and deserializable protocol messages.
Expand Down
File renamed without changes.
54 changes: 54 additions & 0 deletions packages/peer/src/manager/messages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use futures::Sink;

use crate::PeerInfo;

/// Trait for giving `PeerManager` message information it needs.
///
/// For any `PeerProtocol` (or plain `Codec`), that wants to be managed
/// by `PeerManager`, it must ensure that it's message type implements
/// this trait so that we have the hooks necessary to manage the peer.
pub trait ManagedMessage {
/// Retrieve a keep alive message variant.
fn keep_alive() -> Self;

/// Whether or not this message is a keep alive message.
fn is_keep_alive(&self) -> bool;
}

//----------------------------------------------------------------------------//

/// Identifier for matching sent messages with received messages.
pub type MessageId = u64;

/// Message that can be sent to the `PeerManager`.
pub enum IPeerManagerMessage<P>
where
P: Sink,
{
/// Add a peer to the peer manager.
AddPeer(PeerInfo, P),
/// Remove a peer from the peer manager.
RemovePeer(PeerInfo),
/// Send a message to a peer.
SendMessage(PeerInfo, MessageId, P::SinkItem), // TODO: Support querying for statistics
}

/// Message that can be received from the `PeerManager`.
pub enum OPeerManagerMessage<M> {
/// Message indicating a peer has been added to the peer manager.
PeerAdded(PeerInfo),
/// Message indicating a peer has been removed from the peer manager.
PeerRemoved(PeerInfo),
/// Message indicating a message has been sent to the given peer.
SentMessage(PeerInfo, MessageId),
/// Message indicating we have received a message from a peer.
ReceivedMessage(PeerInfo, M),
/// Message indicating a peer has disconnected from us.
///
/// Same semantics as `PeerRemoved`, but the peer is not returned.
PeerDisconnect(PeerInfo),
/// Message indicating a peer errored out.
///
/// Same semantics as `PeerRemoved`, but the peer is not returned.
PeerError(PeerInfo, std::io::Error),
}
Loading

0 comments on commit 380b624

Please sign in to comment.