use std::sync::Arc;
use async_trait::async_trait;
use bytes::Bytes;
use pim_core::NodeId;
use pim_plugin::{DaemonPlugin, PluginContext};
use crate::service::MessagingService;
use crate::wire::{KIND_ACK, KIND_MESSAGE};
const KINDS: &[&str] = &[KIND_MESSAGE, KIND_ACK];
pub struct MessagingPlugin {
service: Arc<MessagingService>,
}
impl MessagingPlugin {
pub fn new(service: Arc<MessagingService>) -> Arc<Self> {
Arc::new(Self { service })
}
}
#[async_trait]
impl DaemonPlugin for MessagingPlugin {
fn name(&self) -> &'static str {
"messaging"
}
fn payload_kinds(&self) -> &'static [&'static str] {
KINDS
}
async fn handle_payload(&self, src: NodeId, kind: &str, body: Bytes) {
match kind {
KIND_MESSAGE => self.service.handle_incoming_message(src, body).await,
KIND_ACK => self.service.handle_incoming_message_ack(src, body).await,
_ => {}
}
}
async fn start(self: Arc<Self>, _ctx: PluginContext) -> anyhow::Result<()> {
Ok(())
}
async fn on_peer_forgotten(&self, peer: NodeId) {
self.service.on_peer_forgotten(peer).await;
}
async fn shutdown(&self) {}
}