dchat: deleted more code and cleaned up warnings

This commit is contained in:
lunar-mining
2022-07-25 20:03:52 +02:00
parent e03c8e54ec
commit 90af9667d6
3 changed files with 35 additions and 102 deletions

View File

@@ -1,44 +1,29 @@
use async_channel::{Receiver, Sender};
use async_executor::Executor;
use async_std::{net::TcpListener, sync::Arc};
use easy_parallel::Parallel;
//use futures::{AsyncRead, AsyncWrite};
use log::{error, info};
use simplelog::WriteLogger;
//use std::net::SocketAddr;
use std::{
fs::File,
io::{self, Write},
net::{IpAddr, Ipv4Addr, SocketAddr},
};
use structopt_toml::StructOptToml;
use termion::{event::Key, input::TermRead};
use url::Url;
use darkfi::{
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,
};
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 protocol_dchat;
pub mod settings;
struct Dchat {
//accept_addr: String,
//connect_addr: String,
//dchatmsgs_buffer: DchatmsgsBuffer,
p2p: net::P2pPtr,
}
@@ -86,36 +71,25 @@ impl Dchat {
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");
let registry = self.p2p.protocol_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;
info!("DCHAT::register_protocol()::stop");
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");
let dchat = Dchat::new(self.p2p.clone());
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;
@@ -128,8 +102,8 @@ impl Dchat {
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
.spawn(async move {
@@ -160,33 +134,32 @@ impl Dchat {
#[async_std::main]
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();
// 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 log_path = "/tmp/dchat.log";
let file = File::create(log_path).unwrap();
WriteLogger::init(log_level, log_config, file)?;
let args = Args::from_args_with_toml(&std::fs::read_to_string(cfg_path)?).unwrap();
let url = Url::parse("tcp://127.0.0.1:55555").unwrap();
let settings = args;
let net_settings = settings.net.clone();
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(net_settings.into()).await;
//let (p2p_send_channel, p2p_recv_channel) = async_channel::unbounded::<Dchatmsg>();
let p2p = net::P2p::new(settings).await;
let p2p = p2p.clone();
let nthreads = num_cpus::get();
@@ -201,7 +174,7 @@ async fn main() -> Result<()> {
.each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
.finish(|| {
smol::future::block_on(async move {
dchat.start(ex2, settings.clone(), p2p_recv_channel, p2p_send_channel).await?;
dchat.start(ex2).await?;
drop(signal);
Ok(())
})

View File

@@ -8,18 +8,12 @@ use crate::dchatmsg::Dchatmsg;
pub struct ProtocolDchat {
jobsman: net::ProtocolJobsManagerPtr,
//sender: async_channel::Sender<Dchatmsg>,
msg_sub: net::MessageSubscription<Dchatmsg>,
p2p: net::P2pPtr,
channel: net::ChannelPtr,
}
impl ProtocolDchat {
pub async fn init(
channel: net::ChannelPtr,
//sender: async_channel::Sender<Dchatmsg>,
p2p: net::P2pPtr,
) -> net::ProtocolBasePtr {
pub async fn init(channel: net::ChannelPtr, p2p: net::P2pPtr) -> net::ProtocolBasePtr {
debug!(target: "dchat", "ProtocolDchat::init() [START]");
let message_subsytem = channel.get_message_subsystem();
@@ -30,10 +24,8 @@ impl ProtocolDchat {
Arc::new(Self {
jobsman: net::ProtocolJobsManager::new("ProtocolDchat", channel.clone()),
//sender,
msg_sub,
p2p,
channel,
})
}

View File

@@ -1,32 +0,0 @@
use darkfi::net::settings::SettingsOpt;
use serde::Deserialize;
use structopt::StructOpt;
use structopt_toml::StructOptToml;
use url::Url;
pub const CONFIG_FILE: &str = "dchat_config.toml";
pub const CONFIG_FILE_CONTENTS: &str = include_str!("../dchat_config.toml");
#[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
#[serde(default)]
#[structopt(name = "chatapp")]
pub struct Args {
/// Sets a custom config file
#[structopt(long)]
pub config: Option<String>,
/// Sets a custom log path
#[structopt(long)]
pub log_path: Option<String>,
/// IRC listen URL
#[structopt(long = "listen", default_value = "tcp://127.0.0.1:11066")]
pub listen: Url,
#[structopt(flatten)]
pub net: SettingsOpt,
/// Increase verbosity
#[structopt(short, parse(from_occurrences))]
pub verbose: u64,
}