provider: move presence notifications into its own module
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
use crate::provider::presence;
|
||||
use crate::provider::ServiceProvider;
|
||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||
use curve25519_dalek::scalar::Scalar;
|
||||
use std::net::ToSocketAddrs;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
use std::thread;
|
||||
|
||||
mod provider;
|
||||
|
||||
@@ -121,6 +123,12 @@ fn run(matches: &ArgMatches) {
|
||||
client_socket_address
|
||||
);
|
||||
|
||||
// Start sending presence notifications in a separate thread
|
||||
thread::spawn(|| {
|
||||
let notifier = presence::Notifier::new();
|
||||
notifier.run();
|
||||
});
|
||||
|
||||
// make sure our socket_address is equal to our predefined-hardcoded value
|
||||
// assert_eq!("127.0.0.1:8081", mix_socket_address.to_string());
|
||||
|
||||
|
||||
+2
-32
@@ -2,8 +2,6 @@ use std::net::{Shutdown, SocketAddr};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::RwLock;
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
use curve25519_dalek::scalar::Scalar;
|
||||
use tokio::prelude::*;
|
||||
@@ -12,16 +10,10 @@ use tokio::runtime::Runtime;
|
||||
use crate::provider::client_handling::{ClientProcessingData, ClientRequestProcessor};
|
||||
use crate::provider::mix_handling::{MixPacketProcessor, MixProcessingData};
|
||||
use crate::provider::storage::ClientStorage;
|
||||
use nym_client::clients::directory;
|
||||
use nym_client::clients::directory::presence::MixProviderPresence;
|
||||
use nym_client::clients::directory::requests::presence_mixnodes_post::PresenceMixNodesPoster;
|
||||
use nym_client::clients::directory::DirectoryClient;
|
||||
|
||||
use tokio::time::{interval_at, Instant, Interval};
|
||||
use nym_client::clients::directory::requests::presence_providers_post::PresenceMixProviderPoster;
|
||||
|
||||
mod client_handling;
|
||||
mod mix_handling;
|
||||
pub mod presence;
|
||||
mod storage;
|
||||
|
||||
// TODO: if we ever create config file, this should go there
|
||||
@@ -169,42 +161,20 @@ impl ServiceProvider {
|
||||
futures::future::join(self.start_mixnet_listening(), self.start_client_listening()).await
|
||||
}
|
||||
|
||||
|
||||
pub fn start_listening(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Create the runtime, probably later move it to Provider struct itself?
|
||||
// TODO: figure out the difference between Runtime and Handle
|
||||
let mut rt = Runtime::new()?;
|
||||
// let mut h = rt.handle();
|
||||
// let mut h = rt.handle();
|
||||
|
||||
// Spawn the root task
|
||||
rt.block_on(async {
|
||||
let future_results = self.start_listeners().await;
|
||||
assert!(future_results.0.is_ok() && future_results.1.is_ok());
|
||||
|
||||
// DH: This blows at runtime, we need to stick it in its own task
|
||||
presence_timer.tick().await;
|
||||
let response = directory.presence_providers_post.post(&presence);
|
||||
println!("response: {:?}", response.unwrap().text());
|
||||
// DH: end blow
|
||||
});
|
||||
|
||||
// this line in theory should never be reached as the runtime should be permanently blocked on listeners
|
||||
eprintln!("The server went kaput...");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn setup_presence_timer(&self) -> (directory::Client, MixProviderPresence, Interval) {
|
||||
let presence_notifications_start = Instant::now() + Duration::from_nanos(5000);
|
||||
let presence_notifications_interval =
|
||||
interval_at(presence_notifications_start, Duration::from_millis(5000));
|
||||
let config = directory::Config {
|
||||
base_url: "https://directory.nymtech.net/".to_string(),
|
||||
};
|
||||
let directory = directory::Client::new(config);
|
||||
let presence = MixProviderPresence {
|
||||
host: "halpin.org:6666".to_string(),
|
||||
pub_key: "superkey".to_string(),
|
||||
};
|
||||
(directory, presence, presence_notifications_interval)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
use nym_client::clients::directory;
|
||||
use nym_client::clients::directory::DirectoryClient;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use nym_client::clients::directory::presence::MixProviderPresence;
|
||||
|
||||
use nym_client::clients::directory::requests::presence_providers_post::PresenceMixProviderPoster;
|
||||
|
||||
pub struct Notifier {
|
||||
pub net_client: directory::Client,
|
||||
presence: MixProviderPresence,
|
||||
}
|
||||
|
||||
impl Notifier {
|
||||
pub fn new() -> Notifier {
|
||||
let config = directory::Config {
|
||||
base_url: "https://directory.nymtech.net/".to_string(),
|
||||
};
|
||||
let net_client = directory::Client::new(config);
|
||||
let presence = MixProviderPresence {
|
||||
host: "halpin.org:6666".to_string(),
|
||||
pub_key: "superkey".to_string(),
|
||||
registered_clients: vec![],
|
||||
};
|
||||
Notifier {
|
||||
net_client,
|
||||
presence,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn notify(&self) {
|
||||
self.net_client
|
||||
.presence_providers_post
|
||||
.post(&self.presence)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn run(&self) {
|
||||
loop {
|
||||
self.notify();
|
||||
thread::sleep(Duration::from_secs(5));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user