1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* This file is part of DarkFi (https://dark.fi)
 *
 * Copyright (C) 2020-2024 Dyne.org foundation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use async_trait::async_trait;

use super::{
    jsonrpc::{JsonResponse, JsonResult},
    util::*,
};
use crate::net;

#[async_trait]
pub trait HandlerP2p: Sync + Send {
    async fn p2p_get_info(&self, id: u16, _params: JsonValue) -> JsonResult {
        let mut channels = Vec::new();
        for channel in self.p2p().hosts().channels() {
            let session = match channel.session_type_id() {
                net::session::SESSION_INBOUND => "inbound",
                net::session::SESSION_OUTBOUND => "outbound",
                net::session::SESSION_MANUAL => "manual",
                net::session::SESSION_SEED => "seed",
                _ => panic!("invalid result from channel.session_type_id()"),
            };
            channels.push(json_map([
                ("url", JsonStr(channel.address().clone().into())),
                ("session", json_str(session)),
                ("id", JsonNum(channel.info.id.into())),
            ]));
        }

        let mut slots = Vec::new();
        for channel_id in self.p2p().session_outbound().slot_info().await {
            slots.push(JsonNum(channel_id.into()));
        }

        let result =
            json_map([("channels", JsonArray(channels)), ("outbound_slots", JsonArray(slots))]);
        JsonResponse::new(result, id).into()
    }

    fn p2p(&self) -> net::P2pPtr;
}