From 1f955b22c0efc483cacbd0c14483d18a98299851 Mon Sep 17 00:00:00 2001 From: Hamza Jadid Date: Sat, 30 Dec 2023 12:13:16 +0200 Subject: [PATCH] feat: serialize echotest events --- client/src/main.rs | 7 ++++++- jarust/src/japrotocol.rs | 9 +++++++-- jarust/src/plugins/echotest/events.rs | 14 ++++++++++++++ jarust/src/plugins/echotest/handle.rs | 22 ++++++++++++++++++---- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/client/src/main.rs b/client/src/main.rs index e0b3b90..74c97ee 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,5 +1,6 @@ use jarust::jaconfig::JaConfig; use jarust::jaconfig::TransportType; +use jarust::plugins::echotest::events::EchoTestPluginEvent; use jarust::plugins::echotest::handle::EchoTest; use jarust::plugins::echotest::messages::EchoTestStartMsg; use log::LevelFilter; @@ -31,7 +32,11 @@ async fn main() -> anyhow::Result<()> { .await?; while let Some(event) = event_receiver.recv().await { - log::info!("{event}"); + match event.event { + EchoTestPluginEvent::Result { result, .. } => { + log::info!("result: {result}"); + } + } } Ok(()) diff --git a/jarust/src/japrotocol.rs b/jarust/src/japrotocol.rs index 5ae6fd8..95bc3aa 100644 --- a/jarust/src/japrotocol.rs +++ b/jarust/src/japrotocol.rs @@ -1,5 +1,6 @@ use serde::Deserialize; use serde::Serialize; +use serde_json::Value; #[derive(Serialize)] pub enum JaConnectionRequestProtocol { @@ -55,7 +56,7 @@ pub enum JaResponseProtocol { #[serde(rename = "ack")] Ack, #[serde(untagged)] - Event { janus: JaEventProtocol }, + Event(JaEventProtocol), } #[derive(Debug, Deserialize, Clone)] @@ -70,9 +71,13 @@ pub struct JaResponseError { } #[derive(Debug, Deserialize, Clone)] +#[serde(tag = "janus")] pub enum JaEventProtocol { #[serde(rename = "event")] - Event, + Event { + #[serde(rename = "plugindata")] + plugin_data: Value, + }, #[serde(rename = "detached")] Detached, /// The PeerConnection was closed, either by Janus or by the user/application, and as such cannot be used anymore. diff --git a/jarust/src/plugins/echotest/events.rs b/jarust/src/plugins/echotest/events.rs index e69de29..bc757c2 100644 --- a/jarust/src/plugins/echotest/events.rs +++ b/jarust/src/plugins/echotest/events.rs @@ -0,0 +1,14 @@ +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct EchoTestPluginData { + pub plugin: String, + #[serde(rename = "data")] + pub event: EchoTestPluginEvent, +} + +#[derive(Debug, Deserialize)] +pub enum EchoTestPluginEvent { + #[serde(untagged)] + Result { echotest: String, result: String }, +} diff --git a/jarust/src/plugins/echotest/handle.rs b/jarust/src/plugins/echotest/handle.rs index 1114385..a28ae16 100644 --- a/jarust/src/plugins/echotest/handle.rs +++ b/jarust/src/plugins/echotest/handle.rs @@ -1,13 +1,20 @@ +use super::events::EchoTestPluginData; use super::messages::EchoTestStartMsg; use crate::jahandle::JaHandle; +use crate::japrotocol::JaEventProtocol; +use crate::japrotocol::JaResponseProtocol; use crate::jasession::JaSession; use crate::prelude::*; use async_trait::async_trait; use tokio::sync::mpsc; +const PLUGIN_ID: &str = "janus.plugin.echotest"; + #[async_trait] pub trait EchoTest { - async fn attach_echotest(&self) -> JaResult<(EchoTestHandle, mpsc::Receiver)>; + async fn attach_echotest( + &self, + ) -> JaResult<(EchoTestHandle, mpsc::Receiver)>; } pub struct EchoTestHandle { @@ -22,12 +29,19 @@ impl From for EchoTestHandle { #[async_trait] impl EchoTest for JaSession { - async fn attach_echotest(&self) -> JaResult<(EchoTestHandle, mpsc::Receiver)> { - let (handle, mut receiver) = self.attach("janus.plugin.echotest").await?; + async fn attach_echotest( + &self, + ) -> JaResult<(EchoTestHandle, mpsc::Receiver)> { + let (handle, mut receiver) = self.attach(PLUGIN_ID).await?; let (tx, rx) = mpsc::channel(100); tokio::spawn(async move { while let Some(msg) = receiver.recv().await { - let msg = format!("Todo: parse properly {msg:?}"); + let msg = match msg.janus { + JaResponseProtocol::Event(JaEventProtocol::Event { plugin_data, .. }) => { + serde_json::from_value::(plugin_data).unwrap() + } + _ => continue, + }; let _ = tx.send(msg).await; } });