1use std::sync::Arc;
5
6use async_trait::async_trait;
7use bytes::Bytes;
8use pim_core::NodeId;
9use pim_plugin::{DaemonPlugin, PluginContext};
10
11use crate::service::MessagingService;
12use crate::wire::{KIND_ACK, KIND_MESSAGE};
13
14const KINDS: &[&str] = &[KIND_MESSAGE, KIND_ACK];
15
16pub struct MessagingPlugin {
23 service: Arc<MessagingService>,
24}
25
26impl MessagingPlugin {
27 pub fn new(service: Arc<MessagingService>) -> Arc<Self> {
29 Arc::new(Self { service })
30 }
31}
32
33#[async_trait]
34impl DaemonPlugin for MessagingPlugin {
35 fn name(&self) -> &'static str {
36 "messaging"
37 }
38
39 fn payload_kinds(&self) -> &'static [&'static str] {
40 KINDS
41 }
42
43 async fn handle_payload(&self, src: NodeId, kind: &str, body: Bytes) {
44 match kind {
45 KIND_MESSAGE => self.service.handle_incoming_message(src, body).await,
46 KIND_ACK => self.service.handle_incoming_message_ack(src, body).await,
47 _ => {}
48 }
49 }
50
51 async fn start(self: Arc<Self>, _ctx: PluginContext) -> anyhow::Result<()> {
52 Ok(())
55 }
56
57 async fn on_peer_forgotten(&self, peer: NodeId) {
58 self.service.on_peer_forgotten(peer).await;
59 }
60
61 async fn shutdown(&self) {}
62}