net.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #[macro_use]
  2. extern crate clap;
  3. use async_executor::Executor;
  4. //use easy_parallel::Parallel;
  5. use std::{net::SocketAddr, sync::Arc};
  6. use drk::{net, Result};
  7. async fn start(executor: Arc<Executor<'_>>, options: ProgramOptions) -> Result<()> {
  8. let p2p = net::P2p::new(options.network_settings);
  9. p2p.clone().start(executor.clone()).await?;
  10. p2p.run(executor).await?;
  11. Ok(())
  12. }
  13. struct ProgramOptions {
  14. network_settings: net::Settings,
  15. log_path: Box<std::path::PathBuf>,
  16. }
  17. impl ProgramOptions {
  18. fn load() -> Result<ProgramOptions> {
  19. let app = clap_app!(dfi =>
  20. (version: "0.1.0")
  21. (author: "Amir Taaki <amir@dyne.org>")
  22. (about: "Dark node")
  23. (@arg ACCEPT: -a --accept +takes_value "Accept address")
  24. (@arg SEED_NODES: -s --seeds ... "Seed nodes")
  25. (@arg CONNECTS: -c --connect ... "Manual connections")
  26. (@arg CONNECT_SLOTS: --slots +takes_value "Connection slots")
  27. (@arg LOG_PATH: --log +takes_value "Logfile path")
  28. (@arg RPC_PORT: -r --rpc +takes_value "RPC port")
  29. )
  30. .get_matches();
  31. let accept_addr = if let Some(accept_addr) = app.value_of("ACCEPT") {
  32. Some(accept_addr.parse()?)
  33. } else {
  34. None
  35. };
  36. let mut seed_addrs: Vec<SocketAddr> = vec![];
  37. if let Some(seeds) = app.values_of("SEED_NODES") {
  38. for seed in seeds {
  39. seed_addrs.push(seed.parse()?);
  40. }
  41. }
  42. let mut manual_connects: Vec<SocketAddr> = vec![];
  43. if let Some(connections) = app.values_of("CONNECTS") {
  44. for connect in connections {
  45. manual_connects.push(connect.parse()?);
  46. }
  47. }
  48. let connection_slots = if let Some(connection_slots) = app.value_of("CONNECT_SLOTS") {
  49. connection_slots.parse()?
  50. } else {
  51. 0
  52. };
  53. let log_path = Box::new(
  54. if let Some(log_path) = app.value_of("LOG_PATH") {
  55. std::path::Path::new(log_path)
  56. } else {
  57. std::path::Path::new("/tmp/darkfid.log")
  58. }
  59. .to_path_buf(),
  60. );
  61. Ok(ProgramOptions {
  62. network_settings: net::Settings {
  63. inbound: accept_addr,
  64. outbound_connections: connection_slots,
  65. external_addr: accept_addr,
  66. peers: manual_connects,
  67. seeds: seed_addrs,
  68. ..Default::default()
  69. },
  70. log_path,
  71. })
  72. }
  73. }
  74. fn main() -> Result<()> {
  75. use simplelog::*;
  76. let options = ProgramOptions::load()?;
  77. let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
  78. CombinedLogger::init(vec![
  79. TermLogger::new(LevelFilter::Debug, logger_config, TerminalMode::Mixed, ColorChoice::Auto),
  80. WriteLogger::new(
  81. LevelFilter::Debug,
  82. Config::default(),
  83. std::fs::File::create(options.log_path.as_path()).unwrap(),
  84. ),
  85. ])
  86. .unwrap();
  87. let ex = Arc::new(Executor::new());
  88. smol::block_on(ex.run(start(ex.clone(), options)))
  89. /*
  90. let (_, result) = Parallel::new()
  91. // Run four executor threads.
  92. .each(0..3, |_| smol::future::block_on(ex.run(shutdown.recv())))
  93. // Run the main future on the current thread.
  94. .finish(|| {
  95. smol::future::block_on(async move {
  96. start(ex2, options).await?;
  97. drop(signal);
  98. Ok::<(), drk::Error>(())
  99. })
  100. });
  101. result
  102. */
  103. }