|
@@ -1,44 +1,29 @@
|
|
|
-use async_channel::{Receiver, Sender};
|
|
|
|
|
use async_executor::Executor;
|
|
use async_executor::Executor;
|
|
|
use async_std::{net::TcpListener, sync::Arc};
|
|
use async_std::{net::TcpListener, sync::Arc};
|
|
|
-
|
|
|
|
|
use easy_parallel::Parallel;
|
|
use easy_parallel::Parallel;
|
|
|
-//use futures::{AsyncRead, AsyncWrite};
|
|
|
|
|
use log::{error, info};
|
|
use log::{error, info};
|
|
|
use simplelog::WriteLogger;
|
|
use simplelog::WriteLogger;
|
|
|
-//use std::net::SocketAddr;
|
|
|
|
|
use std::{
|
|
use std::{
|
|
|
fs::File,
|
|
fs::File,
|
|
|
io::{self, Write},
|
|
io::{self, Write},
|
|
|
|
|
+ net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
-use structopt_toml::StructOptToml;
|
|
|
|
|
use termion::{event::Key, input::TermRead};
|
|
use termion::{event::Key, input::TermRead};
|
|
|
|
|
+use url::Url;
|
|
|
|
|
|
|
|
use darkfi::{
|
|
use darkfi::{
|
|
|
net,
|
|
net,
|
|
|
- system::{Subscriber, SubscriberPtr},
|
|
|
|
|
- util::{
|
|
|
|
|
- cli::{get_log_config, get_log_level, spawn_config},
|
|
|
|
|
- path::get_config_path,
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ net::Settings,
|
|
|
|
|
+ util::cli::{get_log_config, get_log_level},
|
|
|
Result,
|
|
Result,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-use crate::{
|
|
|
|
|
- dchatmsg::Dchatmsg,
|
|
|
|
|
- protocol_dchat::ProtocolDchat,
|
|
|
|
|
- settings::{Args, CONFIG_FILE, CONFIG_FILE_CONTENTS},
|
|
|
|
|
-};
|
|
|
|
|
|
|
+use crate::{dchatmsg::Dchatmsg, protocol_dchat::ProtocolDchat};
|
|
|
|
|
|
|
|
pub mod dchatmsg;
|
|
pub mod dchatmsg;
|
|
|
pub mod protocol_dchat;
|
|
pub mod protocol_dchat;
|
|
|
-pub mod settings;
|
|
|
|
|
|
|
|
|
|
struct Dchat {
|
|
struct Dchat {
|
|
|
- //accept_addr: String,
|
|
|
|
|
- //connect_addr: String,
|
|
|
|
|
- //dchatmsgs_buffer: DchatmsgsBuffer,
|
|
|
|
|
p2p: net::P2pPtr,
|
|
p2p: net::P2pPtr,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -86,36 +71,25 @@ impl Dchat {
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async fn register_protocol(
|
|
|
|
|
- &self,
|
|
|
|
|
- p2p_send_channel: Sender<Dchatmsg>,
|
|
|
|
|
- //p2p: net::P2pPtr,
|
|
|
|
|
- ) -> Result<()> {
|
|
|
|
|
|
|
+ async fn register_protocol(&self) -> Result<()> {
|
|
|
info!("DCHAT::register_protocol()::start");
|
|
info!("DCHAT::register_protocol()::start");
|
|
|
let registry = self.p2p.protocol_registry();
|
|
let registry = self.p2p.protocol_registry();
|
|
|
registry
|
|
registry
|
|
|
- .register(net::SESSION_ALL, move |channel, p2p| {
|
|
|
|
|
- let sender = p2p_send_channel.clone();
|
|
|
|
|
- async move { ProtocolDchat::init(channel, p2p).await }
|
|
|
|
|
|
|
+ .register(net::SESSION_ALL, move |channel, p2p| async move {
|
|
|
|
|
+ ProtocolDchat::init(channel, p2p).await
|
|
|
})
|
|
})
|
|
|
.await;
|
|
.await;
|
|
|
info!("DCHAT::register_protocol()::stop");
|
|
info!("DCHAT::register_protocol()::stop");
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async fn start(
|
|
|
|
|
- &self,
|
|
|
|
|
- executor: Arc<Executor<'_>>,
|
|
|
|
|
- settings: Args,
|
|
|
|
|
- p2p_recv_channel: Receiver<Dchatmsg>,
|
|
|
|
|
- p2p_send_channel: Sender<Dchatmsg>,
|
|
|
|
|
- ) -> Result<()> {
|
|
|
|
|
|
|
+ async fn start(&self, executor: Arc<Executor<'_>>) -> Result<()> {
|
|
|
info!("DCHAT::start()::start");
|
|
info!("DCHAT::start()::start");
|
|
|
let dchat = Dchat::new(self.p2p.clone());
|
|
let dchat = Dchat::new(self.p2p.clone());
|
|
|
|
|
|
|
|
self.p2p.clone().start(executor.clone()).await?;
|
|
self.p2p.clone().start(executor.clone()).await?;
|
|
|
|
|
|
|
|
- dchat.register_protocol(p2p_send_channel, self.p2p.clone()).await?;
|
|
|
|
|
|
|
+ dchat.register_protocol().await?;
|
|
|
|
|
|
|
|
let result = dchat.render(executor.clone()).await;
|
|
let result = dchat.render(executor.clone()).await;
|
|
|
|
|
|
|
@@ -128,8 +102,8 @@ impl Dchat {
|
|
|
|
|
|
|
|
self.send().await?;
|
|
self.send().await?;
|
|
|
|
|
|
|
|
- let listenaddr = settings.listen.socket_addrs(|| None)?[0];
|
|
|
|
|
- let listener = TcpListener::bind(listenaddr).await?;
|
|
|
|
|
|
|
+ let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
|
|
|
|
|
+ let listener = TcpListener::bind(socket).await?;
|
|
|
|
|
|
|
|
executor
|
|
executor
|
|
|
.spawn(async move {
|
|
.spawn(async move {
|
|
@@ -160,33 +134,32 @@ impl Dchat {
|
|
|
|
|
|
|
|
#[async_std::main]
|
|
#[async_std::main]
|
|
|
async fn main() -> Result<()> {
|
|
async fn main() -> Result<()> {
|
|
|
- //let options = ProgramOptions::load()?;
|
|
|
|
|
- //let verbosity_level = options.app.occurrences_of("verbose");
|
|
|
|
|
-
|
|
|
|
|
- let args = Args::from_args_with_toml("").unwrap();
|
|
|
|
|
- let cfg_path = get_config_path(args.config, CONFIG_FILE)?;
|
|
|
|
|
- spawn_config(&cfg_path, CONFIG_FILE_CONTENTS.as_bytes())?;
|
|
|
|
|
-
|
|
|
|
|
- let log_level = get_log_level(args.verbose);
|
|
|
|
|
|
|
+ let log_level = get_log_level(1);
|
|
|
let log_config = get_log_config();
|
|
let log_config = get_log_config();
|
|
|
|
|
|
|
|
- // TODO: clean up
|
|
|
|
|
- if args.log_path.is_none() {
|
|
|
|
|
- let log_path = "/tmp/dchat.log";
|
|
|
|
|
- let file = File::create(log_path).unwrap();
|
|
|
|
|
- WriteLogger::init(log_level, log_config, file)?;
|
|
|
|
|
- } else {
|
|
|
|
|
- let file = File::create(args.log_path.unwrap()).unwrap();
|
|
|
|
|
- WriteLogger::init(log_level, log_config, file)?;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- let args = Args::from_args_with_toml(&std::fs::read_to_string(cfg_path)?).unwrap();
|
|
|
|
|
-
|
|
|
|
|
- let settings = args;
|
|
|
|
|
- let net_settings = settings.net.clone();
|
|
|
|
|
-
|
|
|
|
|
- let (p2p_send_channel, p2p_recv_channel) = async_channel::unbounded::<Dchatmsg>();
|
|
|
|
|
- let p2p = net::P2p::new(net_settings.into()).await;
|
|
|
|
|
|
|
+ let log_path = "/tmp/dchat.log";
|
|
|
|
|
+ let file = File::create(log_path).unwrap();
|
|
|
|
|
+ WriteLogger::init(log_level, log_config, file)?;
|
|
|
|
|
+
|
|
|
|
|
+ let url = Url::parse("tcp://127.0.0.1:55555").unwrap();
|
|
|
|
|
+
|
|
|
|
|
+ let settings = Settings {
|
|
|
|
|
+ inbound: Some(url),
|
|
|
|
|
+ outbound_connections: 0,
|
|
|
|
|
+ manual_attempt_limit: 0,
|
|
|
|
|
+ seed_query_timeout_seconds: 8,
|
|
|
|
|
+ connect_timeout_seconds: 10,
|
|
|
|
|
+ channel_handshake_seconds: 4,
|
|
|
|
|
+ channel_heartbeat_seconds: 10,
|
|
|
|
|
+ outbound_retry_seconds: 1200,
|
|
|
|
|
+ external_addr: None,
|
|
|
|
|
+ peers: Vec::new(),
|
|
|
|
|
+ seeds: Vec::new(),
|
|
|
|
|
+ node_id: String::new(),
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ //let (p2p_send_channel, p2p_recv_channel) = async_channel::unbounded::<Dchatmsg>();
|
|
|
|
|
+ let p2p = net::P2p::new(settings).await;
|
|
|
let p2p = p2p.clone();
|
|
let p2p = p2p.clone();
|
|
|
|
|
|
|
|
let nthreads = num_cpus::get();
|
|
let nthreads = num_cpus::get();
|
|
@@ -201,7 +174,7 @@ async fn main() -> Result<()> {
|
|
|
.each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
|
|
.each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
|
|
|
.finish(|| {
|
|
.finish(|| {
|
|
|
smol::future::block_on(async move {
|
|
smol::future::block_on(async move {
|
|
|
- dchat.start(ex2, settings.clone(), p2p_recv_channel, p2p_send_channel).await?;
|
|
|
|
|
|
|
+ dchat.start(ex2).await?;
|
|
|
drop(signal);
|
|
drop(signal);
|
|
|
Ok(())
|
|
Ok(())
|
|
|
})
|
|
})
|