model.rs 3.2 KB

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