model.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. use async_std::sync::Mutex;
  2. use serde::Deserialize;
  3. use std::{
  4. collections::{HashMap, HashSet},
  5. net::SocketAddr,
  6. };
  7. use tui::widgets::ListState;
  8. pub struct Model {
  9. pub id_list: IdList,
  10. pub info_list: InfoList,
  11. }
  12. impl Model {
  13. pub fn new(id_list: IdList, info_list: InfoList) -> Model {
  14. Model { id_list, info_list }
  15. }
  16. }
  17. pub struct IdList {
  18. pub state: Mutex<ListState>,
  19. pub node_id: Mutex<HashSet<String>>,
  20. }
  21. impl IdList {
  22. pub fn new(node_id: HashSet<String>) -> IdList {
  23. let node_id = Mutex::new(node_id);
  24. IdList { state: Mutex::new(ListState::default()), node_id }
  25. }
  26. }
  27. pub struct InfoList {
  28. pub index: Mutex<usize>,
  29. pub infos: Mutex<HashMap<String, NodeInfo>>,
  30. }
  31. impl InfoList {
  32. pub fn new() -> InfoList {
  33. let index = 0;
  34. let index = Mutex::new(index);
  35. let infos = Mutex::new(HashMap::new());
  36. InfoList { index, infos }
  37. }
  38. }
  39. #[derive(Clone, Debug, PartialEq, Eq, Hash)]
  40. pub struct NodeInfo {
  41. pub outbound: Vec<OutboundInfo>,
  42. pub manual: Vec<ManualInfo>,
  43. pub inbound: Vec<InboundInfo>,
  44. }
  45. impl NodeInfo {
  46. pub fn new() -> NodeInfo {
  47. NodeInfo { outbound: Vec::new(), manual: Vec::new(), inbound: Vec::new() }
  48. }
  49. }
  50. #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
  51. pub struct ManualInfo {
  52. pub key: u64,
  53. }
  54. impl ManualInfo {
  55. pub fn new(key: u64) -> ManualInfo {
  56. ManualInfo { key }
  57. }
  58. }
  59. #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
  60. pub struct OutboundInfo {
  61. pub slots: Vec<Slot>,
  62. }
  63. impl OutboundInfo {
  64. pub fn new(slots: Vec<Slot>) -> OutboundInfo {
  65. OutboundInfo { slots }
  66. }
  67. }
  68. #[derive(Clone, Debug, PartialEq, Deserialize, Eq, Hash)]
  69. pub struct Slot {
  70. pub addr: String,
  71. pub channel: Channel,
  72. pub state: String,
  73. }
  74. impl Slot {
  75. pub fn new(addr: String, channel: Channel, state: String) -> Slot {
  76. Slot { addr, channel, state }
  77. }
  78. }
  79. #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)]
  80. pub struct Channel {
  81. pub last_msg: String,
  82. pub last_status: String,
  83. }
  84. impl Channel {
  85. pub fn new(last_msg: String, last_status: String) -> Channel {
  86. Channel { last_msg, last_status }
  87. }
  88. }
  89. #[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
  90. pub struct InboundInfo {
  91. pub connected: String,
  92. pub channel: Channel,
  93. }
  94. impl InboundInfo {
  95. pub fn new(connected: String, channel: Channel) -> InboundInfo {
  96. InboundInfo { connected, channel }
  97. }
  98. }