settings.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use std::sync::Arc;
  2. use serde::Deserialize;
  3. use structopt::StructOpt;
  4. use structopt_toml::StructOptToml;
  5. use url::Url;
  6. /// Atomic pointer to network settings.
  7. pub type SettingsPtr = Arc<Settings>;
  8. /// Defines the network settings.
  9. #[derive(Clone, Debug)]
  10. pub struct Settings {
  11. pub inbound: Option<Url>,
  12. pub outbound_connections: u32,
  13. pub manual_attempt_limit: u32,
  14. pub seed_query_timeout_seconds: u32,
  15. pub connect_timeout_seconds: u32,
  16. pub channel_handshake_seconds: u32,
  17. pub channel_heartbeat_seconds: u32,
  18. pub external_addr: Option<Url>,
  19. pub peers: Vec<Url>,
  20. pub seeds: Vec<Url>,
  21. }
  22. impl Default for Settings {
  23. fn default() -> Self {
  24. Self {
  25. inbound: None,
  26. outbound_connections: 0,
  27. manual_attempt_limit: 0,
  28. seed_query_timeout_seconds: 8,
  29. connect_timeout_seconds: 10,
  30. channel_handshake_seconds: 4,
  31. channel_heartbeat_seconds: 10,
  32. external_addr: None,
  33. peers: Vec::new(),
  34. seeds: Vec::new(),
  35. }
  36. }
  37. }
  38. /// Defines the network settings.
  39. #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
  40. #[structopt()]
  41. pub struct SettingsOpt {
  42. /// P2P accept address
  43. #[structopt(long = "accept")]
  44. pub inbound: Option<Url>,
  45. /// Connection slots
  46. #[structopt(long = "slots")]
  47. pub outbound_connections: Option<u32>,
  48. /// P2P external address
  49. #[structopt(long)]
  50. pub external_addr: Option<Url>,
  51. /// Peer nodes to connect to
  52. #[serde(default)]
  53. #[structopt(long)]
  54. pub peers: Vec<Url>,
  55. /// Seed nodes to connect to
  56. #[serde(default)]
  57. #[structopt(long)]
  58. pub seeds: Vec<Url>,
  59. #[structopt(skip)]
  60. pub manual_attempt_limit: Option<u32>,
  61. #[structopt(skip)]
  62. pub seed_query_timeout_seconds: Option<u32>,
  63. #[structopt(skip)]
  64. pub connect_timeout_seconds: Option<u32>,
  65. #[structopt(skip)]
  66. pub channel_handshake_seconds: Option<u32>,
  67. #[structopt(skip)]
  68. pub channel_heartbeat_seconds: Option<u32>,
  69. }
  70. impl From<SettingsOpt> for Settings {
  71. fn from(settings_opt: SettingsOpt) -> Self {
  72. Self {
  73. inbound: settings_opt.inbound,
  74. outbound_connections: settings_opt.outbound_connections.unwrap_or(0),
  75. manual_attempt_limit: settings_opt.manual_attempt_limit.unwrap_or(0),
  76. seed_query_timeout_seconds: settings_opt.seed_query_timeout_seconds.unwrap_or(8),
  77. connect_timeout_seconds: settings_opt.connect_timeout_seconds.unwrap_or(10),
  78. channel_handshake_seconds: settings_opt.channel_handshake_seconds.unwrap_or(4),
  79. channel_heartbeat_seconds: settings_opt.channel_heartbeat_seconds.unwrap_or(10),
  80. external_addr: settings_opt.external_addr,
  81. peers: settings_opt.peers,
  82. seeds: settings_opt.seeds,
  83. }
  84. }
  85. }