model.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. use async_std::sync::Mutex;
  2. use fxhash::{FxHashMap, FxHashSet};
  3. use serde::{Deserialize, Serialize};
  4. use darkfi::util::NanoTimestamp;
  5. type MsgLogMutex = Mutex<FxHashMap<String, Vec<(NanoTimestamp, String, String)>>>;
  6. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
  7. pub enum Session {
  8. Inbound,
  9. Outbound,
  10. Manual,
  11. Offline,
  12. }
  13. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
  14. pub enum SelectableObject {
  15. Node(NodeInfo),
  16. Session(SessionInfo),
  17. Connect(ConnectInfo),
  18. }
  19. pub struct Model {
  20. pub ids: Mutex<FxHashSet<String>>,
  21. pub nodes: Mutex<FxHashMap<String, NodeInfo>>,
  22. pub msg_log: MsgLogMutex,
  23. pub selectables: Mutex<FxHashMap<String, SelectableObject>>,
  24. }
  25. impl Model {
  26. pub fn new(
  27. ids: Mutex<FxHashSet<String>>,
  28. nodes: Mutex<FxHashMap<String, NodeInfo>>,
  29. msg_log: MsgLogMutex,
  30. selectables: Mutex<FxHashMap<String, SelectableObject>>,
  31. ) -> Model {
  32. Model { ids, nodes, msg_log, selectables }
  33. }
  34. }
  35. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
  36. pub struct NodeInfo {
  37. pub id: String,
  38. pub name: String,
  39. pub state: String,
  40. pub children: Vec<SessionInfo>,
  41. pub external_addr: Option<String>,
  42. pub is_offline: bool,
  43. }
  44. impl NodeInfo {
  45. pub fn new(
  46. id: String,
  47. name: String,
  48. state: String,
  49. children: Vec<SessionInfo>,
  50. external_addr: Option<String>,
  51. is_offline: bool,
  52. ) -> NodeInfo {
  53. NodeInfo { id, name, state, children, external_addr, is_offline }
  54. }
  55. }
  56. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
  57. pub struct SessionInfo {
  58. // TODO: make all values optional to handle empty sessions
  59. pub id: String,
  60. pub name: String,
  61. pub parent: String,
  62. pub is_empty: bool,
  63. pub children: Vec<ConnectInfo>,
  64. pub accept_addr: Option<String>,
  65. }
  66. impl SessionInfo {
  67. pub fn new(
  68. id: String,
  69. name: String,
  70. is_empty: bool,
  71. parent: String,
  72. children: Vec<ConnectInfo>,
  73. accept_addr: Option<String>,
  74. ) -> SessionInfo {
  75. SessionInfo { id, name, is_empty, parent, children, accept_addr }
  76. }
  77. }
  78. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
  79. pub struct ConnectInfo {
  80. // TODO: make all values optional to handle empty connections
  81. pub id: String,
  82. pub addr: String,
  83. pub state: String,
  84. pub parent: String,
  85. pub msg_log: Vec<(NanoTimestamp, String, String)>,
  86. pub is_empty: bool,
  87. pub last_msg: String,
  88. pub last_status: String,
  89. }
  90. impl ConnectInfo {
  91. #[allow(clippy::too_many_arguments)]
  92. pub fn new(
  93. id: String,
  94. addr: String,
  95. state: String,
  96. parent: String,
  97. msg_log: Vec<(NanoTimestamp, String, String)>,
  98. is_empty: bool,
  99. last_msg: String,
  100. last_status: String,
  101. ) -> ConnectInfo {
  102. ConnectInfo { id, addr, state, parent, msg_log, is_empty, last_msg, last_status }
  103. }
  104. }