model.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::collections::HashMap;
  19. use async_std::sync::{Arc, Mutex};
  20. use serde::{Deserialize, Serialize};
  21. use darkfi::util::time::NanoTimestamp;
  22. type MsgLog = Vec<(NanoTimestamp, String, String)>;
  23. type MsgMap = Mutex<HashMap<String, MsgLog>>;
  24. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  25. pub enum Session {
  26. Inbound,
  27. Outbound,
  28. Manual,
  29. Offline,
  30. }
  31. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  32. pub enum SelectableObject {
  33. Node(NodeInfo),
  34. Lilith(LilithInfo),
  35. Network(NetworkInfo),
  36. Session(SessionInfo),
  37. Connect(ConnectInfo),
  38. }
  39. #[derive(Debug)]
  40. pub struct Model {
  41. pub msg_map: MsgMap,
  42. pub msg_log: Mutex<MsgLog>,
  43. pub selectables: Mutex<HashMap<String, SelectableObject>>,
  44. }
  45. impl Model {
  46. pub fn new() -> Arc<Self> {
  47. let selectables = Mutex::new(HashMap::new());
  48. let msg_map = Mutex::new(HashMap::new());
  49. let msg_log = Mutex::new(Vec::new());
  50. Arc::new(Model { msg_map, msg_log, selectables })
  51. }
  52. }
  53. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  54. pub struct NodeInfo {
  55. pub id: String,
  56. pub name: String,
  57. pub state: String,
  58. pub children: Vec<SessionInfo>,
  59. pub external_addr: Option<String>,
  60. pub is_offline: bool,
  61. }
  62. impl NodeInfo {
  63. pub fn new(
  64. id: String,
  65. name: String,
  66. state: String,
  67. children: Vec<SessionInfo>,
  68. external_addr: Option<String>,
  69. is_offline: bool,
  70. ) -> Self {
  71. Self { id, name, state, children, external_addr, is_offline }
  72. }
  73. }
  74. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  75. pub struct SessionInfo {
  76. pub id: String,
  77. pub name: String,
  78. pub parent: String,
  79. pub is_empty: bool,
  80. pub children: Vec<ConnectInfo>,
  81. pub accept_addr: Option<String>,
  82. pub hosts: Option<Vec<String>>,
  83. }
  84. impl SessionInfo {
  85. pub fn new(
  86. id: String,
  87. name: String,
  88. is_empty: bool,
  89. parent: String,
  90. children: Vec<ConnectInfo>,
  91. accept_addr: Option<String>,
  92. hosts: Option<Vec<String>>,
  93. ) -> Self {
  94. Self { id, name, is_empty, parent, children, accept_addr, hosts }
  95. }
  96. }
  97. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  98. pub struct ConnectInfo {
  99. pub id: String,
  100. pub addr: String,
  101. pub state: String,
  102. pub parent: String,
  103. pub msg_log: Vec<(NanoTimestamp, String, String)>,
  104. pub is_empty: bool,
  105. pub last_msg: String,
  106. pub last_status: String,
  107. pub remote_node_id: String,
  108. }
  109. impl ConnectInfo {
  110. #[allow(clippy::too_many_arguments)]
  111. pub fn new(
  112. id: String,
  113. addr: String,
  114. state: String,
  115. parent: String,
  116. msg_log: Vec<(NanoTimestamp, String, String)>,
  117. is_empty: bool,
  118. last_msg: String,
  119. last_status: String,
  120. remote_node_id: String,
  121. ) -> Self {
  122. Self { id, addr, state, parent, msg_log, is_empty, last_msg, last_status, remote_node_id }
  123. }
  124. }
  125. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  126. pub struct LilithInfo {
  127. pub id: String,
  128. pub name: String,
  129. pub urls: Vec<String>,
  130. pub networks: Vec<NetworkInfo>,
  131. }
  132. impl LilithInfo {
  133. pub fn new(id: String, name: String, urls: Vec<String>, networks: Vec<NetworkInfo>) -> Self {
  134. Self { id, name, urls, networks }
  135. }
  136. }
  137. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  138. pub struct NetworkInfo {
  139. pub id: String,
  140. pub name: String,
  141. pub urls: Vec<String>,
  142. pub nodes: Vec<String>,
  143. }
  144. impl NetworkInfo {
  145. pub fn new(id: String, name: String, urls: Vec<String>, nodes: Vec<String>) -> Self {
  146. Self { id, name, urls, nodes }
  147. }
  148. }