Skip to content

Commit

Permalink
refactor(interface): removed structs with timeouts in favour of anoth…
Browse files Browse the repository at this point in the history
…er param
  • Loading branch information
Ghamza-Jd committed Oct 11, 2024
1 parent 993dba8 commit 562b476
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 61 deletions.
40 changes: 22 additions & 18 deletions jarust/src/jahandle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use jarust_interface::handle_msg::HandleMessage;
use jarust_interface::handle_msg::HandleMessageWithEst;
use jarust_interface::handle_msg::HandleMessageWithEstAndTimeout;
use jarust_interface::handle_msg::HandleMessageWithTimeout;
use jarust_interface::janus_interface::JanusInterfaceImpl;
use jarust_interface::japrotocol::Candidate;
use jarust_interface::japrotocol::EstProto;
Expand Down Expand Up @@ -67,12 +65,14 @@ impl JaHandle {
let res = self
.inner
.interface
.send_msg_waiton_rsp(HandleMessageWithTimeout {
session_id: self.inner.session_id,
handle_id: self.inner.id,
body,
.send_msg_waiton_rsp(
HandleMessage {
session_id: self.inner.session_id,
handle_id: self.inner.id,
body,
},
timeout,
})
)
.await?;
Ok(res)
}
Expand All @@ -88,12 +88,14 @@ impl JaHandle {
let ack = self
.inner
.interface
.send_msg_waiton_ack(HandleMessageWithTimeout {
session_id: self.inner.session_id,
handle_id: self.inner.id,
body,
.send_msg_waiton_ack(
HandleMessage {
session_id: self.inner.session_id,
handle_id: self.inner.id,
body,
},
timeout,
})
)
.await?;
Ok(ack)
}
Expand All @@ -110,13 +112,15 @@ impl JaHandle {
let ack = self
.inner
.interface
.send_msg_waiton_ack_with_est(HandleMessageWithEstAndTimeout {
session_id: self.inner.session_id,
handle_id: self.inner.id,
body,
estproto,
.send_msg_waiton_ack_with_est(
HandleMessageWithEst {
session_id: self.inner.session_id,
handle_id: self.inner.id,
body,
estproto,
},
timeout,
})
)
.await?;
Ok(ack)
}
Expand Down
11 changes: 6 additions & 5 deletions jarust/tests/mocks/mock_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use jarust::GenerateTransaction;
use jarust_interface::error::Error;
use jarust_interface::handle_msg::HandleMessage;
use jarust_interface::handle_msg::HandleMessageWithEst;
use jarust_interface::handle_msg::HandleMessageWithEstAndTimeout;
use jarust_interface::handle_msg::HandleMessageWithTimeout;
use jarust_interface::janus_interface::ConnectionParams;
use jarust_interface::janus_interface::JanusInterface;
use jarust_interface::japrotocol::JaSuccessProtocol;
Expand Down Expand Up @@ -169,14 +167,16 @@ impl JanusInterface for MockInterface {

async fn send_msg_waiton_ack(
&self,
_message: HandleMessageWithTimeout,
_message: HandleMessage,
_timeout: Duration,
) -> Result<JaResponse, jarust_interface::Error> {
todo!("Send message wait on ack is not implemented");
}

async fn internal_send_msg_waiton_rsp(
&self,
_message: HandleMessageWithTimeout,
_message: HandleMessage,
_timeout: Duration,
) -> Result<JaResponse, jarust_interface::Error> {
todo!("Internal send message wait on response is not implemented");
}
Expand All @@ -190,7 +190,8 @@ impl JanusInterface for MockInterface {

async fn send_msg_waiton_ack_with_est(
&self,
_message: HandleMessageWithEstAndTimeout,
_message: HandleMessageWithEst,
_timeout: Duration,
) -> Result<JaResponse, jarust_interface::Error> {
todo!("Send message wait on ack with establishment is not implemented");
}
Expand Down
16 changes: 0 additions & 16 deletions jarust_interface/src/handle_msg.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
use super::japrotocol::EstProto;
use serde_json::Value;
use std::time::Duration;

pub struct HandleMessage {
pub session_id: u64,
pub handle_id: u64,
pub body: Value,
}

pub struct HandleMessageWithTimeout {
pub session_id: u64,
pub handle_id: u64,
pub body: Value,
pub timeout: Duration,
}

pub struct HandleMessageWithEst {
pub session_id: u64,
pub handle_id: u64,
pub body: Value,
pub estproto: EstProto,
}

pub struct HandleMessageWithEstAndTimeout {
pub session_id: u64,
pub handle_id: u64,
pub body: Value,
pub timeout: Duration,
pub estproto: EstProto,
}
16 changes: 9 additions & 7 deletions jarust_interface/src/janus_interface.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::handle_msg::HandleMessage;
use crate::handle_msg::HandleMessageWithEst;
use crate::handle_msg::HandleMessageWithEstAndTimeout;
use crate::handle_msg::HandleMessageWithTimeout;
use crate::japrotocol::JaResponse;
use crate::japrotocol::JaSuccessProtocol;
use crate::japrotocol::PluginInnerData;
Expand Down Expand Up @@ -70,7 +68,8 @@ pub trait JanusInterface: Debug + Send + Sync + 'static {
/// Sends a message and waits for acknowledgment.
async fn send_msg_waiton_ack(
&self,
message: HandleMessageWithTimeout,
message: HandleMessage,
timeout: Duration,
) -> Result<JaResponse, Error>;

/// Internal method to send a message and wait for the response. Ideally, this shouldn't be internal,
Expand All @@ -84,7 +83,8 @@ pub trait JanusInterface: Debug + Send + Sync + 'static {
/// [Why are trait methods with generic type parameters are object unsafe](https://stackoverflow.com/questions/67767207/why-are-trait-methods-with-generic-type-parameters-object-unsafe)
async fn internal_send_msg_waiton_rsp(
&self,
message: HandleMessageWithTimeout,
message: HandleMessage,
timeout: Duration,
) -> Result<JaResponse, Error>;

/// Sends a one-shot message with establishment.
Expand All @@ -96,7 +96,8 @@ pub trait JanusInterface: Debug + Send + Sync + 'static {
/// Sends a message and waits for acknowledgment with establishment.
async fn send_msg_waiton_ack_with_est(
&self,
message: HandleMessageWithEstAndTimeout,
message: HandleMessageWithEst,
timeout: Duration,
) -> Result<JaResponse, Error>;

/// Returns the name of the interface (for the debug trait)
Expand Down Expand Up @@ -129,12 +130,13 @@ impl JanusInterfaceImpl {
#[tracing::instrument(level = tracing::Level::TRACE, skip_all)]
pub async fn send_msg_waiton_rsp<R>(
&self,
message: HandleMessageWithTimeout,
message: HandleMessage,
timeout: Duration,
) -> Result<R, Error>
where
R: DeserializeOwned,
{
let response = self.internal_send_msg_waiton_rsp(message).await?;
let response = self.internal_send_msg_waiton_rsp(message, timeout).await?;
let result = match response.janus {
ResponseType::Success(JaSuccessProtocol::Plugin { plugin_data }) => {
match plugin_data.data {
Expand Down
16 changes: 9 additions & 7 deletions jarust_interface/src/restful/restful_interface.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::handle_msg::HandleMessage;
use crate::handle_msg::HandleMessageWithEst;
use crate::handle_msg::HandleMessageWithEstAndTimeout;
use crate::handle_msg::HandleMessageWithTimeout;
use crate::janus_interface::ConnectionParams;
use crate::janus_interface::JanusInterface;
use crate::japrotocol::EstProto;
Expand Down Expand Up @@ -263,7 +261,8 @@ impl JanusInterface for RestfulInterface {
#[tracing::instrument(level = tracing::Level::TRACE, skip_all)]
async fn send_msg_waiton_ack(
&self,
message: HandleMessageWithTimeout,
message: HandleMessage,
timeout: Duration,
) -> Result<JaResponse, Error> {
let url = &self.inner.shared.url;
let session_id = message.session_id;
Expand All @@ -280,7 +279,7 @@ impl JanusInterface for RestfulInterface {
.client
.post(format!("{url}/{session_id}/{handle_id}"))
.json(&request)
.timeout(message.timeout)
.timeout(timeout)
.send()
.await?
.json::<JaResponse>()
Expand All @@ -290,7 +289,8 @@ impl JanusInterface for RestfulInterface {

async fn internal_send_msg_waiton_rsp(
&self,
message: HandleMessageWithTimeout,
message: HandleMessage,
timeout: Duration,
) -> Result<JaResponse, Error> {
let url = &self.inner.shared.url;
let session_id = message.session_id;
Expand All @@ -307,7 +307,7 @@ impl JanusInterface for RestfulInterface {
.client
.post(format!("{url}/{session_id}/{handle_id}"))
.json(&request)
.timeout(message.timeout)
.timeout(timeout)
.send()
.await?
.json::<JaResponse>()
Expand Down Expand Up @@ -350,7 +350,8 @@ impl JanusInterface for RestfulInterface {
#[tracing::instrument(level = tracing::Level::TRACE, skip_all)]
async fn send_msg_waiton_ack_with_est(
&self,
message: HandleMessageWithEstAndTimeout,
message: HandleMessageWithEst,
timeout: Duration,
) -> Result<JaResponse, Error> {
let url = &self.inner.shared.url;
let session_id = message.session_id;
Expand All @@ -375,6 +376,7 @@ impl JanusInterface for RestfulInterface {
.client
.post(format!("{url}/{session_id}/{handle_id}"))
.json(&request)
.timeout(timeout)
.send()
.await?
.json::<JaResponse>()
Expand Down
17 changes: 9 additions & 8 deletions jarust_interface/src/websocket/websocket_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use super::tmanager::TransactionManager;
use super::websocket_client::WebSocketClient;
use crate::handle_msg::HandleMessage;
use crate::handle_msg::HandleMessageWithEst;
use crate::handle_msg::HandleMessageWithEstAndTimeout;
use crate::handle_msg::HandleMessageWithTimeout;
use crate::janus_interface::ConnectionParams;
use crate::janus_interface::JanusInterface;
use crate::japrotocol::EstProto;
Expand Down Expand Up @@ -342,7 +340,8 @@ impl JanusInterface for WebSocketInterface {
#[tracing::instrument(level = tracing::Level::TRACE, skip_all)]
async fn send_msg_waiton_ack(
&self,
message: HandleMessageWithTimeout,
message: HandleMessage,
timeout: Duration,
) -> Result<JaResponse, Error> {
let request = json!({
"janus": "message",
Expand All @@ -351,12 +350,13 @@ impl JanusInterface for WebSocketInterface {
"body": message.body
});
let transaction = self.send(request).await?;
self.poll_ack(&transaction, message.timeout).await
self.poll_ack(&transaction, timeout).await
}

async fn internal_send_msg_waiton_rsp(
&self,
message: HandleMessageWithTimeout,
message: HandleMessage,
timeout: Duration,
) -> Result<JaResponse, Error> {
let request = json!({
"janus": "message",
Expand All @@ -365,7 +365,7 @@ impl JanusInterface for WebSocketInterface {
"body": message.body
});
let transaction = self.send(request).await?;
self.poll_response(&transaction, message.timeout).await
self.poll_response(&transaction, timeout).await
}

#[tracing::instrument(level = tracing::Level::TRACE, skip_all)]
Expand Down Expand Up @@ -394,7 +394,8 @@ impl JanusInterface for WebSocketInterface {
#[tracing::instrument(level = tracing::Level::TRACE, skip_all)]
async fn send_msg_waiton_ack_with_est(
&self,
message: HandleMessageWithEstAndTimeout,
message: HandleMessageWithEst,
timeout: Duration,
) -> Result<JaResponse, Error> {
let mut request = json!({
"janus": "message",
Expand All @@ -411,7 +412,7 @@ impl JanusInterface for WebSocketInterface {
}
};
let transaction = self.send(request).await?;
self.poll_ack(&transaction, message.timeout).await
self.poll_ack(&transaction, timeout).await
}

fn name(&self) -> Box<str> {
Expand Down

0 comments on commit 562b476

Please sign in to comment.