Skip to content

Commit

Permalink
feat: serialize echotest events
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghamza-Jd committed Dec 30, 2023
1 parent 1f68758 commit 1f955b2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
7 changes: 6 additions & 1 deletion client/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(())
Expand Down
9 changes: 7 additions & 2 deletions jarust/src/japrotocol.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;

#[derive(Serialize)]
pub enum JaConnectionRequestProtocol {
Expand Down Expand Up @@ -55,7 +56,7 @@ pub enum JaResponseProtocol {
#[serde(rename = "ack")]
Ack,
#[serde(untagged)]
Event { janus: JaEventProtocol },
Event(JaEventProtocol),
}

#[derive(Debug, Deserialize, Clone)]
Expand All @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions jarust/src/plugins/echotest/events.rs
Original file line number Diff line number Diff line change
@@ -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 },
}
22 changes: 18 additions & 4 deletions jarust/src/plugins/echotest/handle.rs
Original file line number Diff line number Diff line change
@@ -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<String>)>;
async fn attach_echotest(
&self,
) -> JaResult<(EchoTestHandle, mpsc::Receiver<EchoTestPluginData>)>;
}

pub struct EchoTestHandle {
Expand All @@ -22,12 +29,19 @@ impl From<JaHandle> for EchoTestHandle {

#[async_trait]
impl EchoTest for JaSession {
async fn attach_echotest(&self) -> JaResult<(EchoTestHandle, mpsc::Receiver<String>)> {
let (handle, mut receiver) = self.attach("janus.plugin.echotest").await?;
async fn attach_echotest(
&self,
) -> JaResult<(EchoTestHandle, mpsc::Receiver<EchoTestPluginData>)> {
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::<EchoTestPluginData>(plugin_data).unwrap()
}
_ => continue,
};
let _ = tx.send(msg).await;
}
});
Expand Down

0 comments on commit 1f955b2

Please sign in to comment.