settings.rs 2.8 KB

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