Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/dto #9

Merged
merged 2 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jarust/src/demux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Demux {
{
self.write().unwrap().channels.insert(namespace.into(), tx);
}
log::trace!("Namespace created: {{ id: {namespace} }}");
log::trace!("Namespace created {{ id: {namespace} }}");
rx
}

Expand Down
1 change: 1 addition & 0 deletions jarust/src/dto/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod response;
21 changes: 21 additions & 0 deletions jarust/src/dto/response/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use serde::Deserialize;

#[derive(Deserialize)]
pub struct AttachResponse {
pub data: AttachInnerResponse,
}

#[derive(Deserialize)]
pub struct AttachInnerResponse {
pub id: u64,
}

#[derive(Deserialize)]
pub struct CreateSessionResponse {
pub data: CreateSessionInnerResponse,
}

#[derive(Deserialize)]
pub struct CreateSessionInnerResponse {
pub id: u64,
}
34 changes: 13 additions & 21 deletions jarust/src/jaconnection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::demux::Demux;
use crate::dto::response::CreateSessionResponse;
use crate::jaconfig::JaConfig;
use crate::japrotocol::JaConnectionRequestProtocol;
use crate::jasession::JaSession;
Expand All @@ -9,7 +10,6 @@ use crate::transport::trans::TransportProtocol;
use crate::utils::generate_transaction;
use crate::utils::get_subnamespace_from_request;
use crate::utils::get_subnamespace_from_response;
use serde::Deserialize;
use serde_json::json;
use serde_json::Value;
use std::collections::HashMap;
Expand Down Expand Up @@ -105,16 +105,18 @@ impl JaConnection {
let (transport_protocol, receiver) =
TransportProtocol::connect(transport, &config.uri).await?;

let demux_clone = demux.clone();
let transaction_manager_clone = transaction_manager.clone();
tokio::runtime::Handle::current().spawn(async move {
JaConnection::demux_task(
receiver,
demux_clone,
transaction_manager_clone,
&root_namespace.clone(),
)
.await
tokio::spawn({
let demux = demux.clone();
let transaction_manager = transaction_manager.clone();
async move {
JaConnection::demux_task(
receiver,
demux,
transaction_manager,
&root_namespace.clone(),
)
.await
}
});

let shared = Shared { config };
Expand Down Expand Up @@ -209,13 +211,3 @@ impl JaConnection {
))
}
}

#[derive(Deserialize)]
struct CreateSessionResponse {
data: CreateSessionInnerResponse,
}

#[derive(Deserialize)]
struct CreateSessionInnerResponse {
id: u64,
}
2 changes: 1 addition & 1 deletion jarust/src/jahandle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl JaHandle {
let (ack_sender, ack_receiver) = mpsc::channel(100);
let (event_sender, event_receiver) = mpsc::channel(100);

tokio::runtime::Handle::current().spawn(async move {
tokio::spawn(async move {
while let Some(item) = receiver.recv().await {
let response_type = serde_json::from_str::<JaResponse>(&item).unwrap();
match response_type.janus {
Expand Down
25 changes: 5 additions & 20 deletions jarust/src/jasession.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::dto::response::AttachResponse;
use crate::jaconnection::WeakJaConnection;
use crate::jahandle::JaHandle;
use crate::japrotocol::JaSessionRequestProtocol;
use crate::prelude::*;
use serde::Deserialize;
use serde_json::json;
use serde_json::Value;
use std::collections::HashMap;
Expand Down Expand Up @@ -67,8 +67,7 @@ impl JaSession {

let this = session.clone();

let handle = tokio::runtime::Handle::current();
let _join_handle = handle.spawn(async move {
let _join_handle = tokio::spawn(async move {
let _ = this.keep_alive(ka_interval).await;
});

Expand Down Expand Up @@ -108,7 +107,7 @@ impl JaSession {
.handles
.insert(handle_id, handle.clone());

log::info!("Handle created {{ id: {} }}", handle_id);
log::info!("Handle created {{ id: {handle_id} }}");

Ok((handle, event_receiver))
}
Expand All @@ -126,31 +125,17 @@ impl JaSession {
let id = { self.shared.id };
loop {
interval.tick().await;
log::trace!(
"Sending keep-alive {{ id: {}, timeout: {}s }}",
id,
ka_interval
);
log::trace!("Sending keep-alive {{ id: {id}, timeout: {ka_interval}s }}");
self.send_request(json!({
"janus": JaSessionRequestProtocol::KeepAlive,
}))
.await?;
self.safe.lock().await.receiver.recv().await.unwrap();
log::trace!("keep-alive OK {{ id: {} }}", id);
log::trace!("keep-alive OK {{ id: {id} }}");
}
}

pub(crate) fn downgrade(&self) -> WeakJaSession {
WeakJaSession(Arc::downgrade(self))
}
}

#[derive(Deserialize)]
struct AttachResponse {
data: AttachInnerResponse,
}

#[derive(Deserialize)]
struct AttachInnerResponse {
id: u64,
}
1 change: 1 addition & 0 deletions jarust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod prelude;
pub mod transport;

mod demux;
mod dto;
mod error;
mod jaconnection;
mod jahandle;
Expand Down
Loading