model.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. use async_std::sync::{Arc, Mutex};
  2. use fxhash::FxHashMap;
  3. use serde::{Deserialize, Serialize};
  4. use darkfi::util::time::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. Lilith(LilithInfo),
  18. Network(NetworkInfo),
  19. Session(SessionInfo),
  20. Connect(ConnectInfo),
  21. }
  22. #[derive(Debug)]
  23. pub struct Model {
  24. pub msg_map: MsgMap,
  25. pub msg_log: Mutex<MsgLog>,
  26. pub selectables: Mutex<FxHashMap<String, SelectableObject>>,
  27. }
  28. impl Model {
  29. pub fn new() -> Arc<Self> {
  30. let selectables = Mutex::new(FxHashMap::default());
  31. let msg_map = Mutex::new(FxHashMap::default());
  32. let msg_log = Mutex::new(Vec::new());
  33. Arc::new(Model { msg_map, msg_log, selectables })
  34. }
  35. }
  36. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  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. ) -> Self {
  54. Self { id, name, state, children, external_addr, is_offline }
  55. }
  56. }
  57. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  58. pub struct SessionInfo {
  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. pub hosts: Option<Vec<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. hosts: Option<Vec<String>>,
  76. ) -> Self {
  77. Self { id, name, is_empty, parent, children, accept_addr, hosts }
  78. }
  79. }
  80. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  81. pub struct ConnectInfo {
  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. ) -> Self {
  105. Self { id, addr, state, parent, msg_log, is_empty, last_msg, last_status, remote_node_id }
  106. }
  107. }
  108. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  109. pub struct LilithInfo {
  110. pub id: String,
  111. pub name: String,
  112. pub urls: Vec<String>,
  113. pub networks: Vec<NetworkInfo>,
  114. }
  115. impl LilithInfo {
  116. pub fn new(id: String, name: String, urls: Vec<String>, networks: Vec<NetworkInfo>) -> Self {
  117. Self { id, name, urls, networks }
  118. }
  119. }
  120. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  121. pub struct NetworkInfo {
  122. pub id: String,
  123. pub name: String,
  124. pub urls: Vec<String>,
  125. pub nodes: Vec<String>,
  126. }
  127. impl NetworkInfo {
  128. pub fn new(id: String, name: String, urls: Vec<String>, nodes: Vec<String>) -> Self {
  129. Self { id, name, urls, nodes }
  130. }
  131. }