model.rs 3.3 KB

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