Skip to content

Commit

Permalink
chore: modify transport method return values
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDhejavu committed Aug 12, 2024
1 parent 7506bbe commit 6f6ff3d
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 95 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ debug = true
[dependencies]
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
if-addrs = "0.13.1"
anyhow = { version = "1.0.56", features = ["backtrace"] }
tokio = { version = "1.0", features = ["full"] }
futures = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Asynchronous SWIM(Scalable Weakly-consistent Infection-style Process Group Me
- [x] Extensible design allowing for future custom behaviors
- [x] Configurable failure detection parameters
- [x] Inbuilt Support for both TCP & UDP
- [x] Configurable Transport layer (QUINN, UDP)
- [x] Pluggable Transport layer (QUIC, UDP)
- [ ] Basic encryption of data packets for secure communication
- [ ] Support `io_uring` runtime via Tokio
- [ ] Configurable Observability (Logging and Metrics)
Expand Down
2 changes: 1 addition & 1 deletion examples/gossipod_with_custom_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn main() -> Result<()> {

// Use Default broadcast Queue
let broadcast_queue = Arc::new(DefaultBroadcastQueue::new(1));
let transport = Arc::new(DefaultTransport::new(config.addr(), config.port()).await?);
let transport = Arc::new(DefaultTransport::new(config.ip_addr(), config.port()).await?);
let gossipod = Gossipod::with_custom(config, metadata, broadcast_queue, transport, None)
.await
.context("Failed to initialize Gossipod with custom metadata")?;
Expand Down
1 change: 0 additions & 1 deletion examples/ping_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ impl SwimNode {
.await
.context("Failed to initialize Gossipod with custom metadata")?;


Ok(SwimNode {
gossipod: gossipod.into(),
receiver,
Expand Down
5 changes: 2 additions & 3 deletions src/codec.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use anyhow::{anyhow, bail, Result};
use anyhow::{anyhow, Result};
use serde::de::DeserializeOwned;
use tokio_util::{bytes::{Buf, BufMut, BytesMut}, codec::{Decoder, Encoder}};
use crate::{message::{AckPayload, AppMsgPayload, Broadcast, Message, MessagePayload, MessageType, NoAckPayload, PingPayload, PingReqPayload, RemoteNode, SyncReqPayload}, state::NodeState};

/// MessageCodec: Append-Only, Length-Prefixed Encoding Scheme
/// [`MessageCodec`]: Append-Only, Length-Prefixed Encoding Scheme
///
/// This codec uses an append-only format where each piece of data is prefixed
/// with its length, followed by the data itself. This allows for efficient
Expand Down Expand Up @@ -63,7 +63,6 @@ impl MessageCodec {
dst.put_u16(addr_v6.port());
return Ok(())
},
_ => Err(bail!("address does not match v4 or v6"))
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) const MAX_UDP_PACKET_SIZE: usize = 1400;
pub(crate) const BROADCAST_FANOUT: usize = 2;
pub(crate) const INDIRECT_REQ: usize = 2;

/// Represents the type of network environment the gossip protocol is operating in.
/// [`NetworkType`] Represents the type of network environment the gossip protocol is operating in.
/// This affects various timing and timeout calculations.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum NetworkType {
Expand All @@ -43,7 +43,7 @@ impl Default for NetworkType {
}
}

/// Main configuration structure for the Gossipod protocol
/// [`GossipodConfig`] configuration structure for the Gossipod protocol
pub struct GossipodConfig {
/// Name of the node, used for identification in the cluster
pub(crate) name: String,
Expand Down Expand Up @@ -85,6 +85,8 @@ pub struct GossipodConfig {
/// allowing them an opportunity to refute their dead status if they're actually alive.
pub(crate) dead_node_gossip_window: Duration,

// Disable tcp listener. When TCP is disabled, connection-oriented messages like AppMsg
// automatically never gets processed.
pub(crate) disable_tcp: bool,
}

Expand All @@ -108,7 +110,7 @@ impl Clone for GossipodConfig {
}

impl GossipodConfig {
pub fn addr(&self) -> IpAddr {
pub fn ip_addr(&self) -> IpAddr {
self.ip_addr
}

Expand Down
2 changes: 1 addition & 1 deletion src/dispatch_event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{node::Node, NodeMetadata};
use std::net::SocketAddr;
use std::error::Error;

/// A trait for dispatching events.
/// [`DispatchEventHandler`] trait is used for dispatching events.
///
/// This trait defines methods for handling core SWIM events:
/// node death detection, node departure, node joining, and message handling.
Expand Down
21 changes: 14 additions & 7 deletions src/ip_addr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
use anyhow::{anyhow, Result};
use if_addrs::get_if_addrs;
use sysinfo::Networks;

/// A wrapper around `std::net::IpAddr` providing additional functionality.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -38,14 +38,21 @@ impl IpAddress {
///
/// Returns the first non-loopback IPv4 address found, or an error if none is available.
pub fn find_system_ip() -> Result<IpAddr> {
get_if_addrs()?
.into_iter()
.find(|iface| !iface.is_loopback() && iface.addr.ip().is_ipv4())
.map(|iface| iface.addr.ip())
.ok_or_else(|| anyhow!("No suitable IP address found"))
let networks = Networks::new_with_refreshed_list();
for (_, data) in &networks {
for ip in data.ip_networks() {
println!("DR {}", ip.addr);
if let IpAddr::V4(ipv4) = ip.addr {
if !ipv4.is_loopback() {
return Ok(IpAddr::V4(ipv4));
}
}
}
}

Err(anyhow!("No suitable IPv4 address found"))
}
}

impl AsRef<IpAddr> for IpAddress {
fn as_ref(&self) -> &IpAddr {
&self.0
Expand Down
Loading

0 comments on commit 6f6ff3d

Please sign in to comment.