model.rs 2.9 KB

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