#![allow(dead_code)]
use foca::Identity;
fn main() {
use std::net::{Ipv4Addr, SocketAddrV4};
let basic_identity = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
assert_eq!(None, basic_identity.renew());
#[derive(Clone, Debug, PartialEq, Eq)]
struct FatIdentity {
addr: SocketAddrV4,
extra: u16,
}
impl From<SocketAddrV4> for FatIdentity {
fn from(addr: SocketAddrV4) -> Self {
Self {
addr,
extra: rand::random(),
}
}
}
impl Identity for FatIdentity {
type Addr = SocketAddrV4;
fn renew(&self) -> Option<Self> {
Some(Self {
addr: self.addr,
extra: self.extra.wrapping_add(1),
})
}
fn addr(&self) -> SocketAddrV4 {
self.addr
}
fn win_addr_conflict(&self, adversary: &Self) -> bool {
self.extra > adversary.extra
}
}
assert!(FatIdentity::from(basic_identity).renew().is_some());
#[derive(Clone, Debug, PartialEq, Eq)]
struct SubnetFixedPortId {
addr: (u8, u8),
extra: u16,
}
impl From<Ipv4Addr> for SubnetFixedPortId {
fn from(src: Ipv4Addr) -> Self {
let octets = src.octets();
Self {
addr: (octets[2], octets[3]),
extra: rand::random(),
}
}
}
impl SubnetFixedPortId {
const PORTNR: u16 = 8080;
pub fn as_socket_addr_v4(&self) -> SocketAddrV4 {
SocketAddrV4::new(
Ipv4Addr::new(192, 168, self.addr.0, self.addr.1),
Self::PORTNR,
)
}
}
impl Identity for SubnetFixedPortId {
type Addr = (u8, u8);
fn renew(&self) -> Option<Self> {
Some(Self {
addr: self.addr,
extra: self.extra.wrapping_add(1),
})
}
fn addr(&self) -> (u8, u8) {
self.addr
}
fn win_addr_conflict(&self, adversary: &Self) -> bool {
self.extra > adversary.extra
}
}
}