2021-02-18 11:07:04 +00:00
|
|
|
use std::io::Write;
|
2023-01-31 13:37:09 +00:00
|
|
|
use std::num::NonZeroUsize;
|
2021-02-18 11:07:04 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use log::{debug, info, warn};
|
|
|
|
|
2021-10-13 15:12:13 +00:00
|
|
|
use async_trait::async_trait;
|
2021-02-18 11:07:04 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
|
|
use sodiumoxide::crypto::auth;
|
|
|
|
use sodiumoxide::crypto::sign::ed25519;
|
|
|
|
|
2021-10-13 15:30:41 +00:00
|
|
|
use tokio::sync::watch;
|
|
|
|
|
2021-10-13 10:33:14 +00:00
|
|
|
use netapp::endpoint::*;
|
2022-07-21 15:34:53 +00:00
|
|
|
use netapp::message::*;
|
2021-02-18 11:07:04 +00:00
|
|
|
use netapp::peering::basalt::*;
|
2021-10-14 09:58:09 +00:00
|
|
|
use netapp::util::parse_peer_addr;
|
2021-10-13 10:33:14 +00:00
|
|
|
use netapp::{NetApp, NodeID};
|
2021-02-18 11:07:04 +00:00
|
|
|
|
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
#[structopt(name = "netapp")]
|
|
|
|
pub struct Opt {
|
|
|
|
#[structopt(long = "network-key", short = "n")]
|
|
|
|
network_key: Option<String>,
|
|
|
|
|
|
|
|
#[structopt(long = "private-key", short = "p")]
|
|
|
|
private_key: Option<String>,
|
|
|
|
|
|
|
|
#[structopt(long = "bootstrap-peer", short = "b")]
|
|
|
|
bootstrap_peers: Vec<String>,
|
|
|
|
|
|
|
|
#[structopt(long = "listen-addr", short = "l", default_value = "127.0.0.1:1980")]
|
|
|
|
listen_addr: String,
|
|
|
|
|
|
|
|
#[structopt(long = "public-addr", short = "a")]
|
|
|
|
public_addr: Option<String>,
|
|
|
|
|
|
|
|
#[structopt(long = "view-size", short = "v", default_value = "100")]
|
|
|
|
view_size: usize,
|
|
|
|
|
|
|
|
#[structopt(long = "cache-size", short = "c", default_value = "1000")]
|
2023-01-31 13:37:09 +00:00
|
|
|
cache_size: NonZeroUsize,
|
2021-02-18 11:07:04 +00:00
|
|
|
|
|
|
|
#[structopt(long = "exchange-interval-secs", short = "x", default_value = "1")]
|
|
|
|
exchange_interval: u64,
|
|
|
|
|
|
|
|
#[structopt(long = "reset-interval-secs", short = "r", default_value = "10")]
|
|
|
|
reset_interval: u64,
|
|
|
|
|
|
|
|
#[structopt(long = "reset-count", short = "k", default_value = "20")]
|
|
|
|
reset_count: usize,
|
|
|
|
}
|
|
|
|
|
2021-10-13 10:33:14 +00:00
|
|
|
struct Example {
|
|
|
|
netapp: Arc<NetApp>,
|
|
|
|
basalt: Arc<Basalt>,
|
|
|
|
example_endpoint: Arc<Endpoint<ExampleMessage, Self>>,
|
|
|
|
}
|
|
|
|
|
2021-02-18 11:07:04 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
env_logger::Builder::new()
|
|
|
|
.parse_env("RUST_LOG")
|
|
|
|
.format(|buf, record| {
|
|
|
|
writeln!(
|
|
|
|
buf,
|
|
|
|
"{} {} {} {}",
|
|
|
|
chrono::Local::now().format("%s%.6f"),
|
|
|
|
record.module_path().unwrap_or("_"),
|
|
|
|
record.level(),
|
|
|
|
record.args()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.init();
|
|
|
|
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
|
|
|
let netid = match &opt.network_key {
|
|
|
|
Some(k) => auth::Key::from_slice(&hex::decode(k).unwrap()).unwrap(),
|
|
|
|
None => auth::gen_key(),
|
|
|
|
};
|
|
|
|
info!("KYEV NK {}", hex::encode(&netid));
|
|
|
|
|
|
|
|
let privkey = match &opt.private_key {
|
|
|
|
Some(k) => ed25519::SecretKey::from_slice(&hex::decode(k).unwrap()).unwrap(),
|
|
|
|
None => {
|
|
|
|
let (_pk, sk) = ed25519::gen_keypair();
|
|
|
|
sk
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
info!("KYEV SK {}", hex::encode(&privkey));
|
|
|
|
info!("KYEV PK {}", hex::encode(&privkey.public_key()));
|
|
|
|
|
2022-02-21 15:43:17 +00:00
|
|
|
let netapp = NetApp::new(0u64, netid, privkey);
|
2021-02-18 11:07:04 +00:00
|
|
|
|
|
|
|
let mut bootstrap_peers = vec![];
|
|
|
|
for peer in opt.bootstrap_peers.iter() {
|
2021-10-14 09:58:09 +00:00
|
|
|
bootstrap_peers.push(parse_peer_addr(peer).expect("Invalid peer address"));
|
2021-02-18 11:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let basalt_params = BasaltParams {
|
|
|
|
view_size: opt.view_size,
|
|
|
|
cache_size: opt.cache_size,
|
|
|
|
exchange_interval: Duration::from_secs(opt.exchange_interval),
|
|
|
|
reset_interval: Duration::from_secs(opt.reset_interval),
|
|
|
|
reset_count: opt.reset_count,
|
|
|
|
};
|
2021-10-13 10:33:14 +00:00
|
|
|
let basalt = Basalt::new(netapp.clone(), bootstrap_peers, basalt_params);
|
|
|
|
|
|
|
|
let example = Arc::new(Example {
|
|
|
|
netapp: netapp.clone(),
|
|
|
|
basalt,
|
|
|
|
example_endpoint: netapp.endpoint("__netapp/examples/basalt.rs/Example".into()),
|
|
|
|
});
|
|
|
|
example.example_endpoint.set_handler(example.clone());
|
2021-02-18 11:07:04 +00:00
|
|
|
|
|
|
|
let listen_addr = opt.listen_addr.parse().unwrap();
|
|
|
|
let public_addr = opt.public_addr.map(|x| x.parse().unwrap());
|
2021-10-13 15:12:13 +00:00
|
|
|
|
|
|
|
let watch_cancel = netapp::util::watch_ctrl_c();
|
|
|
|
|
2021-02-18 11:07:04 +00:00
|
|
|
tokio::join!(
|
2021-10-13 15:30:41 +00:00
|
|
|
example.clone().sampling_loop(watch_cancel.clone()),
|
2021-10-13 15:12:13 +00:00
|
|
|
example
|
|
|
|
.netapp
|
|
|
|
.clone()
|
2021-10-13 15:30:41 +00:00
|
|
|
.listen(listen_addr, public_addr, watch_cancel.clone()),
|
|
|
|
example.basalt.clone().run(watch_cancel.clone()),
|
2021-02-18 11:07:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-13 10:33:14 +00:00
|
|
|
impl Example {
|
2021-10-13 15:30:41 +00:00
|
|
|
async fn sampling_loop(self: Arc<Self>, must_exit: watch::Receiver<bool>) {
|
|
|
|
while !*must_exit.borrow() {
|
2021-10-13 10:33:14 +00:00
|
|
|
tokio::time::sleep(Duration::from_secs(10)).await;
|
|
|
|
|
|
|
|
let peers = self.basalt.sample(10);
|
|
|
|
for p in peers {
|
|
|
|
debug!("kyev S {}", hex::encode(p));
|
|
|
|
|
|
|
|
let self2 = self.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
match self2
|
2021-10-13 15:12:13 +00:00
|
|
|
.example_endpoint
|
2022-07-21 17:25:07 +00:00
|
|
|
.call(&p, ExampleMessage { example_field: 42 }, PRIO_NORMAL)
|
2021-10-13 10:33:14 +00:00
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(resp) => debug!("Got example response: {:?}", resp),
|
|
|
|
Err(e) => warn!("Error with example request: {}", e),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl EndpointHandler<ExampleMessage> for Example {
|
2022-07-22 08:55:37 +00:00
|
|
|
async fn handle(self: &Arc<Self>, msg: &ExampleMessage, _from: NodeID) -> ExampleResponse {
|
2021-10-13 10:33:14 +00:00
|
|
|
debug!("Got example message: {:?}, sending example response", msg);
|
2022-07-22 08:55:37 +00:00
|
|
|
ExampleResponse {
|
2021-10-13 10:33:14 +00:00
|
|
|
example_field: false,
|
2022-07-22 08:55:37 +00:00
|
|
|
}
|
2021-02-18 11:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
struct ExampleMessage {
|
|
|
|
example_field: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
struct ExampleResponse {
|
|
|
|
example_field: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Message for ExampleMessage {
|
|
|
|
type Response = ExampleResponse;
|
|
|
|
}
|