aboutsummaryrefslogtreecommitdiff
path: root/p2p/examples/monitor.rs
blob: cda897281f8e4c5bf458dd8300f12d0f28a1897f (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
mod shared;

use std::sync::Arc;

use clap::Parser;
use log::error;
use serde::{Deserialize, Serialize};
use smol::{channel, Executor};

use karyon_p2p::{
    endpoint::{Endpoint, Port},
    keypair::{KeyPair, KeyPairType},
    monitor::{ConnEvent, DiscoveryEvent, PeerPoolEvent},
    ArcBackend, Backend, Config,
};

use karyon_jsonrpc::{rpc_impl, rpc_pubsub_impl, ArcChannel, Server, SubscriptionID};

use shared::run_executor;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
    /// Optional list of bootstrap peers to start the seeding process.
    #[arg(short)]
    bootstrap_peers: Vec<Endpoint>,

    /// RPC server endpoint.
    #[arg(short)]
    rpc_endpoint: Endpoint,

    /// Optional list of peer endpoints for manual connections.
    #[arg(short)]
    peer_endpoints: Vec<Endpoint>,

    /// Optional endpoint for accepting incoming connections.
    #[arg(short)]
    listen_endpoint: Option<Endpoint>,

    /// Optional TCP/UDP port for the discovery service.
    #[arg(short)]
    discovery_port: Option<Port>,
}

struct MonitorRPC {
    backend: ArcBackend,
}

#[rpc_impl]
impl MonitorRPC {
    async fn peer_id(
        &self,
        _params: serde_json::Value,
    ) -> Result<serde_json::Value, karyon_jsonrpc::Error> {
        Ok(serde_json::json!(self.backend.peer_id().to_string()))
    }

    async fn inbound_connection(
        &self,
        _params: serde_json::Value,
    ) -> Result<serde_json::Value, karyon_jsonrpc::Error> {
        Ok(serde_json::json!(self.backend.inbound_slots()))
    }

    async fn outbound_connection(
        &self,
        _params: serde_json::Value,
    ) -> Result<serde_json::Value, karyon_jsonrpc::Error> {
        Ok(serde_json::json!(self.backend.inbound_slots()))
    }
}

#[rpc_pubsub_impl]
impl MonitorRPC {
    async fn conn_subscribe(
        &self,
        chan: ArcChannel,
        method: String,
        _params: serde_json::Value,
    ) -> Result<serde_json::Value, karyon_jsonrpc::Error> {
        let sub = chan.new_subscription(&method).await;
        let sub_id = sub.id.clone();
        let conn_events = self.backend.monitor().conn_events().await;
        smol::spawn(async move {
            loop {
                let event = conn_events.recv().await.unwrap();
                let event: ConnEventJson = event.into();
                if let Err(err) = sub.notify(serde_json::json!(event)).await {
                    error!("Failed to notify: {err}");
                    break;
                }
            }
        })
        .detach();

        Ok(serde_json::json!(sub_id))
    }

    async fn peer_pool_subscribe(
        &self,
        chan: ArcChannel,
        method: String,
        _params: serde_json::Value,
    ) -> Result<serde_json::Value, karyon_jsonrpc::Error> {
        let sub = chan.new_subscription(&method).await;
        let sub_id = sub.id.clone();
        let peer_pool_events = self.backend.monitor().peer_pool_events().await;
        smol::spawn(async move {
            loop {
                let event = peer_pool_events.recv().await.unwrap();
                let event: PeerPoolEventJson = event.into();
                if let Err(err) = sub.notify(serde_json::json!(event)).await {
                    error!("Failed to notify: {err}");
                    break;
                }
            }
        })
        .detach();

        Ok(serde_json::json!(sub_id))
    }

    async fn discovery_subscribe(
        &self,
        chan: ArcChannel,
        method: String,
        _params: serde_json::Value,
    ) -> Result<serde_json::Value, karyon_jsonrpc::Error> {
        let sub = chan.new_subscription(&method).await;
        let sub_id = sub.id.clone();
        let discovery_events = self.backend.monitor().discovery_events().await;
        smol::spawn(async move {
            loop {
                let event = discovery_events.recv().await.unwrap();
                let event: DiscoveryEventJson = event.into();
                if let Err(err) = sub.notify(serde_json::json!(event)).await {
                    error!("Failed to notify: {err}");
                    break;
                }
            }
        })
        .detach();

        Ok(serde_json::json!(sub_id))
    }

    async fn conn_unsubscribe(
        &self,
        chan: ArcChannel,
        _method: String,
        params: serde_json::Value,
    ) -> Result<serde_json::Value, karyon_jsonrpc::Error> {
        let sub_id: SubscriptionID = serde_json::from_value(params)?;
        chan.remove_subscription(&sub_id).await;
        Ok(serde_json::json!(true))
    }

    async fn peer_pool_unsubscribe(
        &self,
        chan: ArcChannel,
        _method: String,
        params: serde_json::Value,
    ) -> Result<serde_json::Value, karyon_jsonrpc::Error> {
        let sub_id: SubscriptionID = serde_json::from_value(params)?;
        chan.remove_subscription(&sub_id).await;
        Ok(serde_json::json!(true))
    }

    async fn discovery_unsubscribe(
        &self,
        chan: ArcChannel,
        _method: String,
        params: serde_json::Value,
    ) -> Result<serde_json::Value, karyon_jsonrpc::Error> {
        let sub_id: SubscriptionID = serde_json::from_value(params)?;
        chan.remove_subscription(&sub_id).await;
        Ok(serde_json::json!(true))
    }
}

fn main() {
    env_logger::init();
    let cli = Cli::parse();

    let key_pair = KeyPair::generate(&KeyPairType::Ed25519);

    // Create the configuration for the backend.
    let config = Config {
        listen_endpoint: cli.listen_endpoint,
        peer_endpoints: cli.peer_endpoints,
        bootstrap_peers: cli.bootstrap_peers,
        discovery_port: cli.discovery_port.unwrap_or(0),
        enable_monitor: true,
        ..Default::default()
    };

    // Create a new Executor
    let ex = Arc::new(Executor::new());

    // Create a new Backend
    let backend = Backend::new(&key_pair, config, ex.clone().into());

    let (ctrlc_s, ctrlc_r) = channel::unbounded();
    let handle = move || ctrlc_s.try_send(()).unwrap();
    ctrlc::set_handler(handle).unwrap();

    let exc = ex.clone();
    run_executor(
        async {
            // RPC service
            let service = Arc::new(MonitorRPC {
                backend: backend.clone(),
            });

            // Create rpc server
            let server = Server::builder(cli.rpc_endpoint)
                .expect("Create server builder")
                .service(service.clone())
                .pubsub_service(service)
                .build_with_executor(exc.clone().into())
                .await
                .expect("Build rpc server");

            // Run the RPC server
            server.start().await;

            // Run the backend
            backend.run().await.expect("Run p2p backend");

            // Wait for ctrlc signal
            ctrlc_r.recv().await.expect("Wait for ctrlc signal");

            // Shutdown the backend
            backend.shutdown().await;

            // Shutdown the RPC server
            server.shutdown().await;
        },
        ex,
    );
}

#[derive(Debug, Serialize, Deserialize)]
struct ConnEventJson {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    endpoint: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
struct PeerPoolEventJson {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    peer_id: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
struct DiscoveryEventJson {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    endpoint: Option<String>,
}

impl From<ConnEvent> for ConnEventJson {
    fn from(item: ConnEvent) -> Self {
        match item {
            ConnEvent::Connected(ref e) => ConnEventJson {
                name: "Connected".into(),
                endpoint: Some(e.to_string()),
            },
            ConnEvent::Disconnected(e) => ConnEventJson {
                name: "Disconnected".into(),
                endpoint: Some(e.to_string()),
            },
            ConnEvent::ConnectFailed(e) => ConnEventJson {
                name: "ConnectFailed".into(),
                endpoint: Some(e.to_string()),
            },
            ConnEvent::ConnectRetried(e) => ConnEventJson {
                name: "ConnectRetried".into(),
                endpoint: Some(e.to_string()),
            },
            ConnEvent::Accepted(e) => ConnEventJson {
                name: "Accepted".into(),
                endpoint: Some(e.to_string()),
            },
            ConnEvent::AcceptFailed => ConnEventJson {
                name: "AcceptFailed".into(),
                endpoint: None,
            },
            ConnEvent::Listening(e) => ConnEventJson {
                name: "Listening".into(),
                endpoint: Some(e.to_string()),
            },
            ConnEvent::ListenFailed(e) => ConnEventJson {
                name: "ListenFailed".into(),
                endpoint: Some(e.to_string()),
            },
        }
    }
}

impl From<PeerPoolEvent> for PeerPoolEventJson {
    fn from(item: PeerPoolEvent) -> Self {
        match item {
            PeerPoolEvent::NewPeer(id) => PeerPoolEventJson {
                name: "NewPeer".into(),
                peer_id: Some(id.to_string()),
            },
            PeerPoolEvent::RemovePeer(id) => PeerPoolEventJson {
                name: "RemovePeer".into(),
                peer_id: Some(id.to_string()),
            },
        }
    }
}

impl From<DiscoveryEvent> for DiscoveryEventJson {
    fn from(item: DiscoveryEvent) -> Self {
        match item {
            DiscoveryEvent::RefreshStarted => DiscoveryEventJson {
                name: "RefreshStarted".into(),
                endpoint: None,
            },
            DiscoveryEvent::LookupStarted(e) => DiscoveryEventJson {
                name: "LookupStarted".into(),
                endpoint: Some(e.to_string()),
            },
            DiscoveryEvent::LookupFailed(e) => DiscoveryEventJson {
                name: "LookupFailed".into(),
                endpoint: Some(e.to_string()),
            },
            DiscoveryEvent::LookupSucceeded(e, _) => DiscoveryEventJson {
                name: "LookupSucceeded".into(),
                endpoint: Some(e.to_string()),
            },
        }
    }
}