|
@@ -29,7 +29,6 @@ use darkfi::{
|
|
|
server::{listen_and_serve, RequestHandler},
|
|
server::{listen_and_serve, RequestHandler},
|
|
|
},
|
|
},
|
|
|
system::{StoppableTask, StoppableTaskPtr},
|
|
system::{StoppableTask, StoppableTaskPtr},
|
|
|
- util::path::get_config_path,
|
|
|
|
|
Error, Result,
|
|
Error, Result,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -45,8 +44,8 @@ pub mod dchatmsg;
|
|
|
pub mod protocol_dchat;
|
|
pub mod protocol_dchat;
|
|
|
pub mod rpc;
|
|
pub mod rpc;
|
|
|
|
|
|
|
|
-const CONFIG_FILE: &str = "dchat_config.toml";
|
|
|
|
|
-const CONFIG_FILE_CONTENTS: &str = include_str!("../dchat_config.toml");
|
|
|
|
|
|
|
+const CONFIG_FILE: &str = "dchatd_config.toml";
|
|
|
|
|
+const CONFIG_FILE_CONTENTS: &str = include_str!("../dchatd_config.toml");
|
|
|
|
|
|
|
|
// ANCHOR: args
|
|
// ANCHOR: args
|
|
|
#[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
|
|
#[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
|
|
@@ -98,68 +97,65 @@ impl Dchat {
|
|
|
// ANCHOR: main
|
|
// ANCHOR: main
|
|
|
async_daemonize!(realmain);
|
|
async_daemonize!(realmain);
|
|
|
async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
|
|
async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
|
|
|
- let cfg_path = get_config_path(args.config, CONFIG_FILE)?;
|
|
|
|
|
- let _toml_contents = std::fs::read_to_string(cfg_path)?;
|
|
|
|
|
-
|
|
|
|
|
let p2p = net::P2p::new(args.net.into(), ex.clone()).await;
|
|
let p2p = net::P2p::new(args.net.into(), ex.clone()).await;
|
|
|
|
|
|
|
|
- // ANCHOR: dnet
|
|
|
|
|
- info!("Starting dnet subs task");
|
|
|
|
|
- let dnet_sub = JsonSubscriber::new("dnet.subscribe_events");
|
|
|
|
|
- let dnet_sub_ = dnet_sub.clone();
|
|
|
|
|
- let p2p_ = p2p.clone();
|
|
|
|
|
- let dnet_task = StoppableTask::new();
|
|
|
|
|
- dnet_task.clone().start(
|
|
|
|
|
- async move {
|
|
|
|
|
- let dnet_sub = p2p_.dnet_subscribe().await;
|
|
|
|
|
- loop {
|
|
|
|
|
- let event = dnet_sub.receive().await;
|
|
|
|
|
- debug!("Got dnet event: {:?}", event);
|
|
|
|
|
- dnet_sub_.notify(vec![event.into()].into()).await;
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
- |res| async {
|
|
|
|
|
- match res {
|
|
|
|
|
- Ok(()) | Err(Error::DetachedTaskStopped) => { /* Do nothing */ }
|
|
|
|
|
- Err(e) => panic!("{}", e),
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
- Error::DetachedTaskStopped,
|
|
|
|
|
- ex.clone(),
|
|
|
|
|
- );
|
|
|
|
|
- // ANCHOR_end: dnet
|
|
|
|
|
-
|
|
|
|
|
- // ANCHOR: rpc
|
|
|
|
|
- info!("Starting JSON-RPC server on port {}", args.rpc_listen);
|
|
|
|
|
- let msgs: DchatMsgsBuffer = Arc::new(Mutex::new(vec![DchatMsg { msg: String::new() }]));
|
|
|
|
|
- let rpc_connections = Mutex::new(HashSet::new());
|
|
|
|
|
- let dchat = Arc::new(Dchat::new(p2p.clone(), msgs.clone(), rpc_connections, dnet_sub));
|
|
|
|
|
- let _ex = ex.clone();
|
|
|
|
|
-
|
|
|
|
|
- let rpc_task = StoppableTask::new();
|
|
|
|
|
- rpc_task.clone().start(
|
|
|
|
|
- listen_and_serve(args.rpc_listen, dchat.clone(), None, ex.clone()),
|
|
|
|
|
- |res| async move {
|
|
|
|
|
- match res {
|
|
|
|
|
- Ok(()) | Err(Error::RpcServerStopped) => dchat.stop_connections().await,
|
|
|
|
|
- Err(e) => error!("Failed stopping JSON-RPC server: {}", e),
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
- Error::RpcServerStopped,
|
|
|
|
|
- ex.clone(),
|
|
|
|
|
- );
|
|
|
|
|
- // ANCHOR_end: rpc
|
|
|
|
|
-
|
|
|
|
|
- // ANCHOR: register_protocol
|
|
|
|
|
- info!("Registering Dchat protocol");
|
|
|
|
|
- let registry = p2p.protocol_registry();
|
|
|
|
|
- registry
|
|
|
|
|
- .register(!net::session::SESSION_SEED, move |channel, _p2p| {
|
|
|
|
|
- let msgs_ = msgs.clone();
|
|
|
|
|
- async move { ProtocolDchat::init(channel, msgs_).await }
|
|
|
|
|
- })
|
|
|
|
|
- .await;
|
|
|
|
|
- // ANCHOR_END: register_protocol
|
|
|
|
|
|
|
+ // // ANCHOR: dnet
|
|
|
|
|
+ // info!("Starting dnet subs task");
|
|
|
|
|
+ // let dnet_sub = JsonSubscriber::new("dnet.subscribe_events");
|
|
|
|
|
+ // let dnet_sub_ = dnet_sub.clone();
|
|
|
|
|
+ // let p2p_ = p2p.clone();
|
|
|
|
|
+ // let dnet_task = StoppableTask::new();
|
|
|
|
|
+ // dnet_task.clone().start(
|
|
|
|
|
+ // async move {
|
|
|
|
|
+ // let dnet_sub = p2p_.dnet_subscribe().await;
|
|
|
|
|
+ // loop {
|
|
|
|
|
+ // let event = dnet_sub.receive().await;
|
|
|
|
|
+ // debug!("Got dnet event: {:?}", event);
|
|
|
|
|
+ // dnet_sub_.notify(vec![event.into()].into()).await;
|
|
|
|
|
+ // }
|
|
|
|
|
+ // },
|
|
|
|
|
+ // |res| async {
|
|
|
|
|
+ // match res {
|
|
|
|
|
+ // Ok(()) | Err(Error::DetachedTaskStopped) => { /* Do nothing */ }
|
|
|
|
|
+ // Err(e) => panic!("{}", e),
|
|
|
|
|
+ // }
|
|
|
|
|
+ // },
|
|
|
|
|
+ // Error::DetachedTaskStopped,
|
|
|
|
|
+ // ex.clone(),
|
|
|
|
|
+ // );
|
|
|
|
|
+ // // ANCHOR_end: dnet
|
|
|
|
|
+
|
|
|
|
|
+ // // ANCHOR: rpc
|
|
|
|
|
+ // info!("Starting JSON-RPC server on port {}", args.rpc_listen);
|
|
|
|
|
+ // let msgs: DchatMsgsBuffer = Arc::new(Mutex::new(vec![DchatMsg { msg: String::new() }]));
|
|
|
|
|
+ // let rpc_connections = Mutex::new(HashSet::new());
|
|
|
|
|
+ // let dchat = Arc::new(Dchat::new(p2p.clone(), msgs.clone(), rpc_connections, dnet_sub));
|
|
|
|
|
+ // let _ex = ex.clone();
|
|
|
|
|
+
|
|
|
|
|
+ // let rpc_task = StoppableTask::new();
|
|
|
|
|
+ // rpc_task.clone().start(
|
|
|
|
|
+ // listen_and_serve(args.rpc_listen, dchat.clone(), None, ex.clone()),
|
|
|
|
|
+ // |res| async move {
|
|
|
|
|
+ // match res {
|
|
|
|
|
+ // Ok(()) | Err(Error::RpcServerStopped) => dchat.stop_connections().await,
|
|
|
|
|
+ // Err(e) => error!("Failed stopping JSON-RPC server: {}", e),
|
|
|
|
|
+ // }
|
|
|
|
|
+ // },
|
|
|
|
|
+ // Error::RpcServerStopped,
|
|
|
|
|
+ // ex.clone(),
|
|
|
|
|
+ // );
|
|
|
|
|
+ // // ANCHOR_end: rpc
|
|
|
|
|
+
|
|
|
|
|
+ // // ANCHOR: register_protocol
|
|
|
|
|
+ // info!("Registering Dchat protocol");
|
|
|
|
|
+ // let registry = p2p.protocol_registry();
|
|
|
|
|
+ // registry
|
|
|
|
|
+ // .register(!net::session::SESSION_SEED, move |channel, _p2p| {
|
|
|
|
|
+ // let msgs_ = msgs.clone();
|
|
|
|
|
+ // async move { ProtocolDchat::init(channel, msgs_).await }
|
|
|
|
|
+ // })
|
|
|
|
|
+ // .await;
|
|
|
|
|
+ // // ANCHOR_END: register_protocol
|
|
|
|
|
|
|
|
// ANCHOR: p2p_start
|
|
// ANCHOR: p2p_start
|
|
|
info!("Starting P2P network");
|
|
info!("Starting P2P network");
|
|
@@ -174,11 +170,11 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
|
|
|
info!("Stopping P2P network");
|
|
info!("Stopping P2P network");
|
|
|
p2p.stop().await;
|
|
p2p.stop().await;
|
|
|
|
|
|
|
|
- info!("Stopping JSON-RPC server");
|
|
|
|
|
- rpc_task.stop().await;
|
|
|
|
|
- dnet_task.stop().await;
|
|
|
|
|
|
|
+ //info!("Stopping JSON-RPC server");
|
|
|
|
|
+ //rpc_task.stop().await;
|
|
|
|
|
+ //dnet_task.stop().await;
|
|
|
|
|
|
|
|
- info!("Shut down successfully");
|
|
|
|
|
|
|
+ //info!("Shut down successfully");
|
|
|
// ANCHOR_END: shutdown
|
|
// ANCHOR_END: shutdown
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|