model.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. pub struct Model {
  21. pub ids: Mutex<FxHashSet<String>>,
  22. pub nodes: Mutex<FxHashMap<String, NodeInfo>>,
  23. pub msg_map: MsgMap,
  24. pub msg_log: Mutex<MsgLog>,
  25. pub selectables: Mutex<FxHashMap<String, SelectableObject>>,
  26. }
  27. impl Model {
  28. pub fn new(
  29. ids: Mutex<FxHashSet<String>>,
  30. nodes: Mutex<FxHashMap<String, NodeInfo>>,
  31. msg_map: MsgMap,
  32. msg_log: Mutex<MsgLog>,
  33. selectables: Mutex<FxHashMap<String, SelectableObject>>,
  34. ) -> Model {
  35. Model { ids, nodes, msg_map, msg_log, selectables }
  36. }
  37. }
  38. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  39. pub struct NodeInfo {
  40. pub id: String,
  41. pub name: String,
  42. pub state: String,
  43. pub children: Vec<SessionInfo>,
  44. pub external_addr: Option<String>,
  45. pub is_offline: bool,
  46. }
  47. impl NodeInfo {
  48. pub fn new(
  49. id: String,
  50. name: String,
  51. state: String,
  52. children: Vec<SessionInfo>,
  53. external_addr: Option<String>,
  54. is_offline: bool,
  55. ) -> NodeInfo {
  56. NodeInfo { id, name, state, children, external_addr, is_offline }
  57. }
  58. }
  59. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  60. pub struct SessionInfo {
  61. // TODO: make all values optional to handle empty sessions
  62. pub id: String,
  63. pub name: String,
  64. pub parent: String,
  65. pub is_empty: bool,
  66. pub children: Vec<ConnectInfo>,
  67. pub accept_addr: Option<String>,
  68. }
  69. impl SessionInfo {
  70. pub fn new(
  71. id: String,
  72. name: String,
  73. is_empty: bool,
  74. parent: String,
  75. children: Vec<ConnectInfo>,
  76. accept_addr: Option<String>,
  77. ) -> SessionInfo {
  78. SessionInfo { id, name, is_empty, parent, children, accept_addr }
  79. }
  80. }
  81. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  82. pub struct ConnectInfo {
  83. // TODO: make all values optional to handle empty connections
  84. pub id: String,
  85. pub addr: String,
  86. pub state: String,
  87. pub parent: String,
  88. pub msg_log: Vec<(NanoTimestamp, String, String)>,
  89. pub is_empty: bool,
  90. pub last_msg: String,
  91. pub last_status: String,
  92. pub remote_node_id: String,
  93. }
  94. impl ConnectInfo {
  95. #[allow(clippy::too_many_arguments)]
  96. pub fn new(
  97. id: String,
  98. addr: String,
  99. state: String,
  100. parent: String,
  101. msg_log: Vec<(NanoTimestamp, String, String)>,
  102. is_empty: bool,
  103. last_msg: String,
  104. last_status: String,
  105. remote_node_id: String,
  106. ) -> ConnectInfo {
  107. ConnectInfo {
  108. id,
  109. addr,
  110. state,
  111. parent,
  112. msg_log,
  113. is_empty,
  114. last_msg,
  115. last_status,
  116. remote_node_id,
  117. }
  118. }
  119. }