main.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. use async_executor::Executor;
  2. use async_std::sync::Arc;
  3. use easy_parallel::Parallel;
  4. use log::{error, info};
  5. use simplelog::WriteLogger;
  6. use std::{
  7. fs::File,
  8. io::{self, Read, Write},
  9. };
  10. use termion::{async_stdin, event::Key, input::TermRead};
  11. use url::Url;
  12. use darkfi::{
  13. net,
  14. net::Settings,
  15. util::cli::{get_log_config, get_log_level},
  16. Result,
  17. };
  18. use crate::{dchatmsg::Dchatmsg, protocol_dchat::ProtocolDchat};
  19. pub mod dchatmsg;
  20. pub mod protocol_dchat;
  21. struct Dchat {
  22. p2p: net::P2pPtr,
  23. }
  24. impl Dchat {
  25. fn new(p2p: net::P2pPtr) -> Arc<Self> {
  26. Arc::new(Self { p2p })
  27. }
  28. async fn render(&self, ex: Arc<Executor<'_>>) -> Result<()> {
  29. info!("DCHAT::render()::start");
  30. let mut stdout = io::stdout().lock();
  31. let mut stdin = async_stdin();
  32. stdout.write_all(
  33. b"Welcome to dchat
  34. s: send message
  35. i. inbox
  36. q: quit \n",
  37. )?;
  38. loop {
  39. for k in stdin.by_ref().keys() {
  40. match k.unwrap() {
  41. Key::Char('q') => {
  42. info!("DCHAT::Q pressed.... exiting");
  43. return Ok(())
  44. }
  45. Key::Char('i') => {}
  46. Key::Char('s') => {
  47. let msg = self.get_input().await?;
  48. self.send(msg).await?;
  49. }
  50. _ => {}
  51. }
  52. }
  53. }
  54. }
  55. async fn get_input(&self) -> Result<String> {
  56. let mut stdout = io::stdout().lock();
  57. stdout.write_all(b"type your message and then press enter\n")?;
  58. let mut input = String::new();
  59. io::stdin().read_line(&mut input)?;
  60. stdout.write_all(b"you entered:")?;
  61. stdout.write_all(input.as_bytes())?;
  62. return Ok(input)
  63. }
  64. async fn register_protocol(&self) -> Result<()> {
  65. info!("DCHAT::register_protocol()::start");
  66. let registry = self.p2p.protocol_registry();
  67. registry
  68. .register(net::SESSION_ALL, move |channel, p2p| async move {
  69. ProtocolDchat::init(channel, p2p).await
  70. })
  71. .await;
  72. info!("DCHAT::register_protocol()::stop");
  73. Ok(())
  74. }
  75. async fn start(&self, ex: Arc<Executor<'_>>) -> Result<()> {
  76. info!("DCHAT::start()::start");
  77. let ex2 = ex.clone();
  78. let dchat = Dchat::new(self.p2p.clone());
  79. dchat.register_protocol().await?;
  80. self.p2p.clone().start(ex.clone()).await?;
  81. ex2.spawn(self.p2p.clone().run(ex.clone())).detach();
  82. info!("DCHAT::start()::stop");
  83. Ok(())
  84. }
  85. async fn send(&self, message: String) -> Result<()> {
  86. let mut stdout = io::stdout().lock();
  87. stdout.write_all(b"sending: ")?;
  88. stdout.write_all(message.as_bytes())?;
  89. let dchatmsg = Dchatmsg { message };
  90. self.p2p.broadcast(dchatmsg).await?;
  91. Ok(())
  92. }
  93. }
  94. #[async_std::main]
  95. async fn main() -> Result<()> {
  96. let log_level = get_log_level(1);
  97. let log_config = get_log_config();
  98. let log_path = "/tmp/dchat.log";
  99. let file = File::create(log_path).unwrap();
  100. WriteLogger::init(log_level, log_config, file)?;
  101. let seed = Url::parse("tcp://127.0.0.1:55555").unwrap();
  102. let inbound = Url::parse("tcp://127.0.0.1:55554").unwrap();
  103. let ext_addr = Url::parse("tcp://127.0.0.1:55544").unwrap();
  104. let settings = Settings {
  105. inbound: Some(inbound),
  106. outbound_connections: 0,
  107. manual_attempt_limit: 0,
  108. seed_query_timeout_seconds: 8,
  109. connect_timeout_seconds: 10,
  110. channel_handshake_seconds: 4,
  111. channel_heartbeat_seconds: 10,
  112. outbound_retry_seconds: 1200,
  113. external_addr: Some(ext_addr),
  114. peers: Vec::new(),
  115. seeds: vec![seed],
  116. node_id: String::new(),
  117. };
  118. let p2p = net::P2p::new(settings).await;
  119. let p2p = p2p.clone();
  120. let nthreads = num_cpus::get();
  121. let (signal, shutdown) = async_channel::unbounded::<()>();
  122. let ex = Arc::new(Executor::new());
  123. let ex2 = ex.clone();
  124. let ex3 = ex.clone();
  125. let dchat = Dchat::new(p2p.clone());
  126. let (_, result) = Parallel::new()
  127. .each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  128. .finish(|| {
  129. smol::future::block_on(async move {
  130. dchat.start(ex3).await?;
  131. dchat.render(ex2).await?;
  132. drop(signal);
  133. Ok(())
  134. })
  135. });
  136. result
  137. }