model.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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. Null,
  31. }
  32. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  33. pub enum SelectableObject {
  34. Node(NodeInfo),
  35. Lilith(LilithInfo),
  36. Network(NetworkInfo),
  37. Session(SessionInfo),
  38. Slot(SlotInfo),
  39. }
  40. #[derive(Debug)]
  41. pub struct Model {
  42. pub msg_map: MsgMap,
  43. pub log: Mutex<MsgLog>,
  44. pub selectables: Mutex<HashMap<String, SelectableObject>>,
  45. }
  46. impl Model {
  47. pub fn new() -> Arc<Self> {
  48. let selectables = Mutex::new(HashMap::new());
  49. let msg_map = Mutex::new(HashMap::new());
  50. let log = Mutex::new(Vec::new());
  51. Arc::new(Model { msg_map, log, selectables })
  52. }
  53. }
  54. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  55. pub struct NodeInfo {
  56. pub dnet_id: String,
  57. pub name: String,
  58. pub hosts: Vec<String>,
  59. pub inbound: Vec<SessionInfo>,
  60. pub outbound: Vec<SessionInfo>,
  61. pub is_offline: bool,
  62. pub dnet_enabled: bool,
  63. }
  64. impl NodeInfo {
  65. pub fn new(
  66. dnet_id: String,
  67. name: String,
  68. hosts: Vec<String>,
  69. inbound: Vec<SessionInfo>,
  70. outbound: Vec<SessionInfo>,
  71. is_offline: bool,
  72. dnet_enabled: bool,
  73. ) -> Self {
  74. Self { dnet_id, name, hosts, inbound, outbound, is_offline, dnet_enabled }
  75. }
  76. }
  77. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  78. pub struct SessionInfo {
  79. pub dnet_id: String,
  80. pub node_id: String,
  81. pub addr: String,
  82. pub state: Option<String>,
  83. pub info: SlotInfo,
  84. pub sort: Session,
  85. pub is_empty: bool,
  86. }
  87. impl SessionInfo {
  88. pub fn new(
  89. dnet_id: String,
  90. node_id: String,
  91. addr: String,
  92. state: Option<String>,
  93. info: SlotInfo,
  94. sort: Session,
  95. is_empty: bool,
  96. ) -> Self {
  97. Self { dnet_id, node_id, addr, state, info, sort, is_empty }
  98. }
  99. }
  100. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  101. pub struct SlotInfo {
  102. pub dnet_id: String,
  103. pub node_id: String,
  104. pub addr: String,
  105. pub random_id: u64,
  106. pub remote_id: String,
  107. pub log: Vec<(NanoTimestamp, String, String)>,
  108. pub is_empty: bool,
  109. }
  110. impl SlotInfo {
  111. #[allow(clippy::too_many_arguments)]
  112. pub fn new(
  113. dnet_id: String,
  114. node_id: String,
  115. addr: String,
  116. random_id: u64,
  117. remote_id: String,
  118. log: Vec<(NanoTimestamp, String, String)>,
  119. is_empty: bool,
  120. ) -> Self {
  121. Self { dnet_id, addr, random_id, remote_id, log, node_id, is_empty }
  122. }
  123. }
  124. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  125. pub struct LilithInfo {
  126. pub id: String,
  127. pub name: String,
  128. pub networks: Vec<NetworkInfo>,
  129. }
  130. impl LilithInfo {
  131. pub fn new(id: String, name: String, networks: Vec<NetworkInfo>) -> Self {
  132. Self { id, name, networks }
  133. }
  134. }
  135. #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq)]
  136. pub struct NetworkInfo {
  137. pub id: String,
  138. pub name: String,
  139. pub urls: Vec<String>,
  140. pub nodes: Vec<String>,
  141. }
  142. impl NetworkInfo {
  143. pub fn new(id: String, name: String, urls: Vec<String>, nodes: Vec<String>) -> Self {
  144. Self { id, name, urls, nodes }
  145. }
  146. }