Skip to content

Commit

Permalink
feat: added server info response parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghamza-Jd committed Jun 24, 2024
1 parent 3910664 commit 3978417
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
8 changes: 7 additions & 1 deletion jarust/examples/secure_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ use jarust::japrotocol::Jsep;
use jarust::japrotocol::JsepType;
use jarust::TransactionGenerationStrategy;
use serde_json::json;
use std::path::Path;
use std::time::Duration;
use tokio::time;
use tracing_subscriber::EnvFilter;

#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
let env_filter = EnvFilter::from_default_env().add_directive("jarust=trace".parse()?);
let filename = Path::new(file!()).file_stem().unwrap().to_str().unwrap();
let env_filter = EnvFilter::from_default_env()
.add_directive("jarust=trace".parse()?)
.add_directive(format!("{filename}=trace").parse()?);
tracing_subscriber::fmt().with_env_filter(env_filter).init();

let capacity = 32;
Expand All @@ -28,6 +32,8 @@ async fn main() -> anyhow::Result<()> {
.await?;
let timeout = Duration::from_secs(10);

tracing::info!("server info: {:#?}", connection.server_info(timeout).await?);

let session = connection.create(10, timeout).await?;
let (handle, mut event_receiver) = session.attach("janus.plugin.echotest", capacity).await?;

Expand Down
13 changes: 11 additions & 2 deletions jarust/src/jaconnection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::nw::nwconn::NwConn;
use crate::nw::transaction_gen::GenerateTransaction;
use crate::nw::transaction_gen::TransactionGenerator;
use crate::prelude::*;
use crate::respones::ServerInfoRsp;
use jarust_rt::JaTask;
use jarust_transport::trans::TransportProtocol;
use serde_json::json;
Expand Down Expand Up @@ -196,14 +197,22 @@ impl JaConnection {
}

impl JaConnection {
/// Returns janus server info
#[tracing::instrument(level = tracing::Level::TRACE, skip_all)]
pub async fn server_info(&mut self, timeout: Duration) -> JaResult<JaResponse> {
pub async fn server_info(&mut self, timeout: Duration) -> JaResult<ServerInfoRsp> {
let request = json!({
"janus": "info"
});
let transaction = self.send_request(request).await?;
let response = self.poll_response(&transaction, timeout).await?;
Ok(response)
match response.janus {
ResponseType::ServerInfo(info) => Ok(*info),
ResponseType::Error { error } => Err(JaError::JanusError {
code: error.code,
reason: error.reason,
}),
_ => Err(JaError::IncompletePacket),
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion jarust/src/japrotocol.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::respones::ServerInfoRsp;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
Expand All @@ -19,8 +20,10 @@ pub struct JaResponse {
pub enum ResponseType {
#[serde(rename = "error")]
Error { error: ErrorResponse },
// Wrapped in a box so the entire enum isn't as large as this varient
// e.g: Ack that doesn't contain any data will have allocate the same space as ServerInfo
#[serde(rename = "server_info")]
ServerInfo,
ServerInfo(Box<ServerInfoRsp>),
#[serde(rename = "ack")]
Ack,
#[serde(rename = "success")]
Expand Down
1 change: 1 addition & 0 deletions jarust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod japlugin;
pub mod japrotocol;
pub mod jasession;
pub mod prelude;
pub mod respones;

mod napmap;
mod nw;
Expand Down
4 changes: 1 addition & 3 deletions jarust/src/nw/transaction_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ impl TransactionManager {

#[tracing::instrument(parent = None, skip(self))]
pub async fn insert(&self, id: &str, transaction: &str) {
let mut guard = self.inner.write().await;
guard.put(id.into(), transaction.into());
tracing::trace!("Transaction manager {:#?}", guard);
self.inner.write().await.put(id.into(), transaction.into());
}
}
56 changes: 56 additions & 0 deletions jarust/src/respones.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;

#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ServerInfoRsp {
pub name: String,
pub version: u64,
#[serde(rename = "version_string")]
pub version_string: String,
pub author: String,
pub commit_hash: String,
pub compile_time: String,
pub log_to_stdout: bool,
pub log_to_file: bool,
#[serde(rename = "data_channels")]
pub data_channels: bool,
pub accepting_new_sessions: bool,
pub session_timeout: u64,
pub reclaim_session_timeout: u64,
pub candidates_timeout: u64,
pub server_name: String,
pub local_ip: String,
pub ipv6: bool,
pub ice_lite: bool,
pub ice_tcp: bool,
pub ice_nomination: String, /* could be enum when we know the varients */
pub ice_keepalive_conncheck: bool,
pub full_trickle: bool,
pub mdns_enabled: bool,
pub min_nack_queue: u64,
pub twcc_period: u64,
pub dtls_mtu: u64,
pub static_event_loops: u64,
#[serde(rename = "api_secret")]
pub api_secret: bool,
#[serde(rename = "auth_token")]
pub auth_token: bool,
#[serde(rename = "event_handlers")]
pub event_handlers: bool,
#[serde(rename = "opaqueid_in_api")]
pub opaqueid_in_api: bool,
pub dependencies: HashMap<String, String>,
pub transports: HashMap<String, MetaData>,
pub plugins: HashMap<String, MetaData>,
}

#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
pub struct MetaData {
name: String,
author: String,
description: String,
version_string: String,
version: u64,
}

0 comments on commit 3978417

Please sign in to comment.