util.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 darkfi::Result;
  19. use crate::model::{ConnectInfo, Session};
  20. pub fn make_node_id(node_name: &String) -> Result<String> {
  21. let mut id = hex::encode(node_name);
  22. id.insert_str(0, "NODE");
  23. Ok(id)
  24. }
  25. pub fn make_session_id(node_id: &str, session: &Session) -> Result<String> {
  26. let mut num = 0_u64;
  27. let session_chars = match session {
  28. Session::Inbound => vec!['i', 'n'],
  29. Session::Outbound => vec!['o', 'u', 't'],
  30. Session::Manual => vec!['m', 'a', 'n'],
  31. Session::Offline => vec!['o', 'f', 'f'],
  32. };
  33. for i in session_chars {
  34. num += i as u64
  35. }
  36. for i in node_id.chars() {
  37. num += i as u64
  38. }
  39. let mut id = hex::encode(num.to_ne_bytes());
  40. id.insert_str(0, "SESSION");
  41. Ok(id)
  42. }
  43. pub fn make_connect_id(id: &u64) -> Result<String> {
  44. let mut id = hex::encode(id.to_ne_bytes());
  45. id.insert_str(0, "CONNECT");
  46. Ok(id)
  47. }
  48. pub fn make_empty_id(node_id: &str, session: &Session, count: u64) -> Result<String> {
  49. let count = count * 2;
  50. let mut num = 0_u64;
  51. let id = match session {
  52. Session::Inbound => {
  53. let session_chars = vec!['i', 'n'];
  54. for i in session_chars {
  55. num += i as u64
  56. }
  57. for i in node_id.chars() {
  58. num += i as u64
  59. }
  60. num += count;
  61. let mut id = hex::encode(num.to_ne_bytes());
  62. id.insert_str(0, "EMPTYIN");
  63. id
  64. }
  65. Session::Outbound => {
  66. let session_chars = vec!['o', 'u', 't'];
  67. for i in session_chars {
  68. num += i as u64
  69. }
  70. for i in node_id.chars() {
  71. num += i as u64
  72. }
  73. num += count;
  74. let mut id = hex::encode(num.to_ne_bytes());
  75. id.insert_str(0, "EMPTYOUT");
  76. id
  77. }
  78. Session::Manual => {
  79. let session_chars = vec!['m', 'a', 'n'];
  80. for i in session_chars {
  81. num += i as u64
  82. }
  83. for i in node_id.chars() {
  84. num += i as u64
  85. }
  86. num += count;
  87. let mut id = hex::encode(num.to_ne_bytes());
  88. id.insert_str(0, "EMPTYMAN");
  89. id
  90. }
  91. Session::Offline => {
  92. let session_chars = vec!['o', 'f', 'f'];
  93. for i in session_chars {
  94. num += i as u64
  95. }
  96. for i in node_id.chars() {
  97. num += i as u64
  98. }
  99. num += count;
  100. let mut id = hex::encode(num.to_ne_bytes());
  101. id.insert_str(0, "EMPTYOFF");
  102. id
  103. }
  104. };
  105. Ok(id)
  106. }
  107. pub fn is_empty_session(connects: &[ConnectInfo]) -> bool {
  108. return connects.iter().all(|conn| conn.is_empty)
  109. }