app.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. use crate::{
  2. list::IdList,
  3. node_info::{NodeInfo, InfoList},
  4. };
  5. //use smol::Timer;
  6. //use std::{collections::HashMap, time::Duration};
  7. // make a structure to be able to modify and read them
  8. // protect using a mutex
  9. // arc reference
  10. #[derive(Clone)]
  11. pub struct App {
  12. pub node_list: IdList,
  13. pub node_info: InfoList,
  14. }
  15. impl App {
  16. pub fn new() -> App {
  17. let infos = vec![
  18. NodeInfo {
  19. id: "0385048034sodisofjhosd1111q3434".to_string(),
  20. connections: 10,
  21. is_active: true,
  22. last_message: "hey how are you?".to_string(),
  23. },
  24. NodeInfo {
  25. id: "09w30we9wsnfksdfkdjflsjkdfjdfsd".to_string(),
  26. connections: 5,
  27. is_active: false,
  28. last_message: "lmao".to_string(),
  29. },
  30. NodeInfo {
  31. id: "038043325alsdlasjfrsdfsdfsdjsdf".to_string(),
  32. connections: 7,
  33. is_active: true,
  34. last_message: "gm".to_string(),
  35. },
  36. NodeInfo {
  37. id: "04985034953ldflsdjflsdjflsdjfii".to_string(),
  38. connections: 2,
  39. is_active: true,
  40. last_message: "hihi".to_string(),
  41. },
  42. NodeInfo {
  43. id: "09850249352asdjapsdikalskasdkas".to_string(),
  44. connections: 10,
  45. is_active: true,
  46. last_message: "wtf".to_string(),
  47. },
  48. ];
  49. let node_info = InfoList::new(infos.clone());
  50. let ids = vec![
  51. infos[0].id.clone(),
  52. infos[1].id.clone(),
  53. infos[2].id.clone(),
  54. infos[3].id.clone(),
  55. infos[4].id.clone(),
  56. ];
  57. let node_list = IdList::new(ids);
  58. //let infos = Vec::new();
  59. //let ids = Vec::new();
  60. //let node_info = InfoList::new(infos);
  61. //let node_list = IdList::new(ids);
  62. App { node_list, node_info }
  63. }
  64. // TODO: implement this
  65. //async fn sleep(self, dur: Duration) {
  66. // Timer::after(dur).await;
  67. //}
  68. pub async fn update(mut self, node_vec: Vec<NodeInfo>) -> App {
  69. let ids = vec![node_vec[0].id.clone()];
  70. for id in ids {
  71. self.node_list.node_id.push(id);
  72. }
  73. let node_list = self.node_list;
  74. for info in node_vec {
  75. self.node_info.infos.push(info);
  76. }
  77. let node_info = self.node_info;
  78. App { node_list, node_info }
  79. }
  80. }
  81. impl Default for App {
  82. fn default() -> Self {
  83. Self::new()
  84. }
  85. }