p2p_method.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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 async_trait::async_trait;
  19. use super::{
  20. jsonrpc::{JsonResponse, JsonResult},
  21. util::*,
  22. };
  23. use crate::net;
  24. #[async_trait]
  25. pub trait HandlerP2p: Sync + Send {
  26. async fn p2p_get_info(&self, id: u16, _params: JsonValue) -> JsonResult {
  27. let mut channels = Vec::new();
  28. for channel in self.p2p().hosts().channels().await {
  29. let session = match channel.session_type_id() {
  30. net::session::SESSION_INBOUND => "inbound",
  31. net::session::SESSION_OUTBOUND => "outbound",
  32. net::session::SESSION_MANUAL => "manual",
  33. net::session::SESSION_SEED => "seed",
  34. _ => panic!("invalid result from channel.session_type_id()"),
  35. };
  36. channels.push(json_map([
  37. ("url", JsonStr(channel.address().clone().into())),
  38. ("session", json_str(session)),
  39. ("id", JsonNum(channel.info.id.into())),
  40. ]));
  41. }
  42. let mut slots = Vec::new();
  43. for channel_id in self.p2p().session_outbound().slot_info().await {
  44. slots.push(JsonNum(channel_id.into()));
  45. }
  46. let result =
  47. json_map([("channels", JsonArray(channels)), ("outbound_slots", JsonArray(slots))]);
  48. JsonResponse::new(result, id).into()
  49. }
  50. fn p2p(&self) -> net::P2pPtr;
  51. }