From 562b476857d2d7bb9028313d70dd55ef4293fd7d Mon Sep 17 00:00:00 2001 From: Ghamza-Jd Date: Fri, 11 Oct 2024 22:56:17 +0300 Subject: [PATCH] refactor(interface): removed structs with timeouts in favour of another param --- jarust/src/jahandle.rs | 40 ++++++++++--------- jarust/tests/mocks/mock_interface.rs | 11 ++--- jarust_interface/src/handle_msg.rs | 16 -------- jarust_interface/src/janus_interface.rs | 16 ++++---- .../src/restful/restful_interface.rs | 16 ++++---- .../src/websocket/websocket_interface.rs | 17 ++++---- 6 files changed, 55 insertions(+), 61 deletions(-) diff --git a/jarust/src/jahandle.rs b/jarust/src/jahandle.rs index a377d31..e345d11 100644 --- a/jarust/src/jahandle.rs +++ b/jarust/src/jahandle.rs @@ -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; @@ -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) } @@ -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) } @@ -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) } diff --git a/jarust/tests/mocks/mock_interface.rs b/jarust/tests/mocks/mock_interface.rs index 9af91a7..0a3297e 100644 --- a/jarust/tests/mocks/mock_interface.rs +++ b/jarust/tests/mocks/mock_interface.rs @@ -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; @@ -169,14 +167,16 @@ impl JanusInterface for MockInterface { async fn send_msg_waiton_ack( &self, - _message: HandleMessageWithTimeout, + _message: HandleMessage, + _timeout: Duration, ) -> Result { todo!("Send message wait on ack is not implemented"); } async fn internal_send_msg_waiton_rsp( &self, - _message: HandleMessageWithTimeout, + _message: HandleMessage, + _timeout: Duration, ) -> Result { todo!("Internal send message wait on response is not implemented"); } @@ -190,7 +190,8 @@ impl JanusInterface for MockInterface { async fn send_msg_waiton_ack_with_est( &self, - _message: HandleMessageWithEstAndTimeout, + _message: HandleMessageWithEst, + _timeout: Duration, ) -> Result { todo!("Send message wait on ack with establishment is not implemented"); } diff --git a/jarust_interface/src/handle_msg.rs b/jarust_interface/src/handle_msg.rs index a80f444..290d2a7 100644 --- a/jarust_interface/src/handle_msg.rs +++ b/jarust_interface/src/handle_msg.rs @@ -1,6 +1,5 @@ use super::japrotocol::EstProto; use serde_json::Value; -use std::time::Duration; pub struct HandleMessage { pub session_id: u64, @@ -8,24 +7,9 @@ pub struct HandleMessage { 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, -} diff --git a/jarust_interface/src/janus_interface.rs b/jarust_interface/src/janus_interface.rs index 9533f35..384853c 100644 --- a/jarust_interface/src/janus_interface.rs +++ b/jarust_interface/src/janus_interface.rs @@ -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; @@ -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; /// Internal method to send a message and wait for the response. Ideally, this shouldn't be internal, @@ -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; /// Sends a one-shot message with establishment. @@ -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; /// Returns the name of the interface (for the debug trait) @@ -129,12 +130,13 @@ impl JanusInterfaceImpl { #[tracing::instrument(level = tracing::Level::TRACE, skip_all)] pub async fn send_msg_waiton_rsp( &self, - message: HandleMessageWithTimeout, + message: HandleMessage, + timeout: Duration, ) -> Result 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 { diff --git a/jarust_interface/src/restful/restful_interface.rs b/jarust_interface/src/restful/restful_interface.rs index 1bd78e9..da9ddc8 100644 --- a/jarust_interface/src/restful/restful_interface.rs +++ b/jarust_interface/src/restful/restful_interface.rs @@ -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; @@ -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 { let url = &self.inner.shared.url; let session_id = message.session_id; @@ -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::() @@ -290,7 +289,8 @@ impl JanusInterface for RestfulInterface { async fn internal_send_msg_waiton_rsp( &self, - message: HandleMessageWithTimeout, + message: HandleMessage, + timeout: Duration, ) -> Result { let url = &self.inner.shared.url; let session_id = message.session_id; @@ -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::() @@ -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 { let url = &self.inner.shared.url; let session_id = message.session_id; @@ -375,6 +376,7 @@ impl JanusInterface for RestfulInterface { .client .post(format!("{url}/{session_id}/{handle_id}")) .json(&request) + .timeout(timeout) .send() .await? .json::() diff --git a/jarust_interface/src/websocket/websocket_interface.rs b/jarust_interface/src/websocket/websocket_interface.rs index ec17a25..c814230 100644 --- a/jarust_interface/src/websocket/websocket_interface.rs +++ b/jarust_interface/src/websocket/websocket_interface.rs @@ -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; @@ -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 { let request = json!({ "janus": "message", @@ -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 { let request = json!({ "janus": "message", @@ -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)] @@ -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 { let mut request = json!({ "janus": "message", @@ -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 {