aboutsummaryrefslogtreecommitdiff
path: root/src/membership.rs
blob: c14d370b0361490a300f020844902fd399e1d97f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::sync::Arc;
use std::collections::HashMap;
use std::time::Duration;
use std::net::SocketAddr;

use hyper::client::Client;
use tokio::sync::RwLock;

use crate::Config;
use crate::error::Error;
use crate::data::*;
use crate::proto::*;
use crate::rpc::*;

const PING_INTERVAL: Duration = Duration::from_secs(10);
const PING_TIMEOUT: Duration = Duration::from_secs(2);
const MAX_FAILED_PINGS: usize = 3;

pub struct System {
	pub config: Config,
	pub id: UUID,

	pub rpc_client: Client<hyper::client::HttpConnector, hyper::Body>,

	pub members: RwLock<Members>,
}

pub struct Members {
	pub present: Vec<UUID>,
	pub status: HashMap<UUID, NodeStatus>,

	pub config: HashMap<UUID, NodeConfig>,
	pub config_version: u64,
}

pub struct NodeStatus {
	pub addr: SocketAddr,
	remaining_ping_attempts: usize,
}

pub struct NodeConfig {
	pub n_tokens: u32,
}

impl System {
	pub fn new(config: Config, id: UUID) -> Self {
		System{
			config,
			id,
			rpc_client: Client::new(),
			members: RwLock::new(Members{
				present: Vec::new(),
				status: HashMap::new(),
				config: HashMap::new(),
				config_version: 0,
			}),
		}
	}

	pub async fn broadcast(&self) -> Vec<UUID> {
		self.members.read().await.present.clone()
	}
}

pub async fn bootstrap(system: Arc<System>) {
	rpc_call_many_addr(system.clone(),
					   &system.config.bootstrap_peers,
					   &Message::Ping(PingMessage{
						   id: system.id,
						   rpc_port: system.config.rpc_port,
						   present_hash: [0u8; 32],
						   config_version: 0,
					   }),
					   None,
					   PING_TIMEOUT).await;

	unimplemented!() //TODO
}

pub async fn handle_ping(sys: Arc<System>, from: &SocketAddr, ping: &PingMessage) -> Result<Message, Error> {
	unimplemented!() //TODO
}

pub async fn handle_advertise_node(sys: Arc<System>, ping: &AdvertiseNodeMessage) -> Result<Message, Error> {
	unimplemented!() //TODO
}