-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
253 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::collections::HashMap; | ||
use std::net::SocketAddr; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NodeMeta { | ||
pub id: u32, | ||
pub address: SocketAddr, | ||
} | ||
|
||
impl NodeMeta { | ||
fn new(id: u32, address: SocketAddr) -> NodeMeta { | ||
Self { id, address } | ||
} | ||
} | ||
|
||
impl From<(u32, SocketAddr)> for NodeMeta { | ||
fn from((id, address): (u32, SocketAddr)) -> Self { | ||
Self::new(id, address) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ClusterConfig { | ||
peers: Vec<NodeMeta>, | ||
id_node_map: HashMap<u32, NodeMeta>, | ||
} | ||
|
||
impl ClusterConfig { | ||
pub fn new(peers: Vec<NodeMeta>) -> ClusterConfig { | ||
let id_node_map = peers | ||
.clone() | ||
.into_iter() | ||
.map(|x| (x.id, x)) | ||
.collect::<HashMap<u32, NodeMeta>>(); | ||
ClusterConfig { peers, id_node_map } | ||
} | ||
|
||
pub fn peers(&self) -> &[NodeMeta] { | ||
&self.peers | ||
} | ||
|
||
// Return meta of peers for a node | ||
pub fn peers_for(&self, id: u32) -> Vec<&NodeMeta> { | ||
self.peers.iter().filter(|x| x.id != id).collect::<Vec<_>>() | ||
} | ||
|
||
// Return address of peers for a node | ||
pub fn peer_address_for(&self, id: u32) -> Vec<SocketAddr> { | ||
self.peers | ||
.iter() | ||
.filter(|x| x.id != id) | ||
.map(|x| x.address) | ||
.collect::<Vec<_>>() | ||
} | ||
|
||
pub fn address(&self, id: u32) -> Option<SocketAddr> { | ||
self.id_node_map.get(&id).map(|x| x.address) | ||
} | ||
pub fn meta(&self, node_id: u32) -> Option<&NodeMeta> { | ||
self.id_node_map.get(&node_id) | ||
} | ||
|
||
pub fn contains_server(&self, node_id: u32) -> bool { | ||
self.id_node_map.contains_key(&node_id) | ||
} | ||
|
||
pub fn add_server(&mut self, n: NodeMeta) { | ||
self.peers.push(n.clone()); | ||
self.id_node_map.insert(n.id, n); | ||
} | ||
|
||
pub fn peer_count(&self, id: u32) -> usize { | ||
self.peers_for(id).len() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,9 @@ | ||
// organization : SpacewalkHq | ||
// License : MIT License | ||
|
||
pub mod cluster; | ||
pub mod error; | ||
pub mod log; | ||
pub mod network; | ||
pub mod server; | ||
pub mod storage; | ||
|
||
/// Helper function to parse the IP address delimited by ':' and return tuple of (ip, port) | ||
fn parse_ip_address(addr: &str) -> (&str, &str) { | ||
let tokens = addr.split(':').collect::<Vec<&str>>(); | ||
(tokens[0], tokens[1]) | ||
} |
Oops, something went wrong.