Swapped Id's to be Unix Timestamps instead of UUID's
This commit is contained in:
parent
a3612ba55c
commit
b9208c190e
13 changed files with 352 additions and 291 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
use crate::data::communication::{CommunicationType, CommunicationValue, DataTypes};
|
use crate::data::communication::{CommunicationType, CommunicationValue, DataTypes};
|
||||||
use crate::util::config_util::CONFIG;
|
use crate::util::config_util::CONFIG;
|
||||||
|
use crate::util::print::{PrintType, line};
|
||||||
|
use json::number::Number;
|
||||||
use reqwest::{Client, Response};
|
use reqwest::{Client, Response};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
@ -64,7 +66,7 @@ pub async fn get_user(user_id: Uuid) -> Option<AuthUser> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_iota_id(user_id: Uuid) -> Option<Uuid> {
|
pub async fn get_iota_id(user_id: i64) -> Option<i64> {
|
||||||
let url = format!("https://auth.tensamin.net/api/get/iota-id/{}", user_id);
|
let url = format!("https://auth.tensamin.net/api/get/iota-id/{}", user_id);
|
||||||
|
|
||||||
let client = client();
|
let client = client();
|
||||||
|
|
@ -81,13 +83,17 @@ pub async fn get_iota_id(user_id: Uuid) -> Option<Uuid> {
|
||||||
|
|
||||||
let cv = CommunicationValue::from_json(&json);
|
let cv = CommunicationValue::from_json(&json);
|
||||||
if cv.comm_type != CommunicationType::success {
|
if cv.comm_type != CommunicationType::success {
|
||||||
|
line(PrintType::IotaIn, &cv.to_json().to_string());
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let iota_id_str = cv.get_data(DataTypes::iota_id)?.to_string();
|
let iota_id = cv.get_data(DataTypes::iota_id)?.as_i64().unwrap_or(0);
|
||||||
Uuid::parse_str(&iota_id_str).ok()
|
if iota_id == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(iota_id)
|
||||||
}
|
}
|
||||||
pub async fn is_private_key_valid(user_id: Uuid, pk_hash: &str) -> bool {
|
pub async fn is_private_key_valid(user_id: i64, pk_hash: &str) -> bool {
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"https://auth.tensamin.net/api/get/private-key-hash/{}/",
|
"https://auth.tensamin.net/api/get/private-key-hash/{}/",
|
||||||
user_id
|
user_id
|
||||||
|
|
@ -120,7 +126,7 @@ pub async fn is_private_key_valid(user_id: Uuid, pk_hash: &str) -> bool {
|
||||||
None => false,
|
None => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub async fn get_public_key(user_id: Uuid) -> Option<String> {
|
pub async fn get_public_key(user_id: i64) -> Option<String> {
|
||||||
let url = format!("https://auth.tensamin.net/api/{}/public-key", user_id);
|
let url = format!("https://auth.tensamin.net/api/{}/public-key", user_id);
|
||||||
|
|
||||||
let client = client();
|
let client = client();
|
||||||
|
|
@ -141,14 +147,16 @@ pub async fn get_public_key(user_id: Uuid) -> Option<String> {
|
||||||
Some(cv.get_data(DataTypes::ping_clients)?.to_string())
|
Some(cv.get_data(DataTypes::ping_clients)?.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_register() -> Option<Uuid> {
|
pub async fn get_register() -> Option<i64> {
|
||||||
let url = "https://auth.tensamin.net/api/register/init".to_string();
|
let url = "https://auth.tensamin.net/api/register/init".to_string();
|
||||||
let client = client();
|
let client = client();
|
||||||
let res = client.get(&url).send().await.ok()?;
|
let res = client.get(&url).send().await.ok()?;
|
||||||
let json = res.text().await.ok()?;
|
let json = res.text().await.ok()?;
|
||||||
|
|
||||||
let cv = CommunicationValue::from_json(&json);
|
let cv = CommunicationValue::from_json(&json);
|
||||||
Uuid::parse_str(&*cv.get_data(DataTypes::user_id).unwrap().to_string()).ok()
|
cv.get_data(DataTypes::user_id)
|
||||||
|
.unwrap_or(&json::JsonValue::Number(Number::from(0)))
|
||||||
|
.as_i64()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_response(resp: Response) -> bool {
|
async fn handle_response(resp: Response) -> bool {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use crate::calls::caller::Caller;
|
||||||
pub struct CallGroup {
|
pub struct CallGroup {
|
||||||
pub call_id: Uuid,
|
pub call_id: Uuid,
|
||||||
pub members: RwLock<Vec<Arc<Caller>>>,
|
pub members: RwLock<Vec<Arc<Caller>>>,
|
||||||
|
pub show: RwLock<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CallGroup {
|
impl CallGroup {
|
||||||
|
|
@ -15,13 +16,15 @@ impl CallGroup {
|
||||||
CallGroup {
|
CallGroup {
|
||||||
call_id,
|
call_id,
|
||||||
members: RwLock::new(vec![user]),
|
members: RwLock::new(vec![user]),
|
||||||
|
show: RwLock::new(true),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_member(self: Arc<Self>, member: Uuid, inviter: Uuid) {
|
pub async fn add_member(self: Arc<Self>, member: i64, inviter: i64) {
|
||||||
|
*self.show.write().await = true;
|
||||||
self.members
|
self.members
|
||||||
.write()
|
.write()
|
||||||
.await
|
.await
|
||||||
.push(Arc::new(Caller::new(member, inviter, self.call_id)));
|
.push(Arc::new(Caller::new(member, self.call_id, inviter)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,17 @@
|
||||||
|
use livekit_api::services::room::RoomClient;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use std::sync::Arc;
|
use std::{str::FromStr, sync::Arc, time::Duration};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::calls::{call_group::CallGroup, caller::Caller};
|
use crate::{
|
||||||
|
calls::{call_group::CallGroup, caller::Caller},
|
||||||
|
util::print::{PrintType, line},
|
||||||
|
};
|
||||||
|
|
||||||
static CALL_GROUPS: Lazy<RwLock<Vec<Arc<CallGroup>>>> = Lazy::new(|| RwLock::new(Vec::new()));
|
static CALL_GROUPS: Lazy<RwLock<Vec<Arc<CallGroup>>>> = Lazy::new(|| RwLock::new(Vec::new()));
|
||||||
|
|
||||||
pub async fn get_call_invites(user_id: Uuid) -> Vec<Arc<Caller>> {
|
pub async fn get_call_invites(user_id: i64) -> Vec<Arc<Caller>> {
|
||||||
let mut callers = Vec::new();
|
let mut callers = Vec::new();
|
||||||
for cg in CALL_GROUPS.read().await.iter() {
|
for cg in CALL_GROUPS.read().await.iter() {
|
||||||
let members = cg.members.read().await;
|
let members = cg.members.read().await;
|
||||||
|
|
@ -20,7 +24,7 @@ pub async fn get_call_invites(user_id: Uuid) -> Vec<Arc<Caller>> {
|
||||||
callers
|
callers
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_call_groups(user_id: Uuid) -> Vec<Arc<CallGroup>> {
|
pub async fn get_call_groups(user_id: i64) -> Vec<Arc<CallGroup>> {
|
||||||
let mut call_groups = Vec::new();
|
let mut call_groups = Vec::new();
|
||||||
for cg in CALL_GROUPS.read().await.iter() {
|
for cg in CALL_GROUPS.read().await.iter() {
|
||||||
let is_member = {
|
let is_member = {
|
||||||
|
|
@ -35,7 +39,7 @@ pub async fn get_call_groups(user_id: Uuid) -> Vec<Arc<CallGroup>> {
|
||||||
call_groups
|
call_groups
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_call_token(user_id: Uuid, call_id: Uuid) -> Option<String> {
|
pub async fn get_call_token(user_id: i64, call_id: Uuid) -> Option<String> {
|
||||||
let existing_group = {
|
let existing_group = {
|
||||||
let call_groups = CALL_GROUPS.read().await;
|
let call_groups = CALL_GROUPS.read().await;
|
||||||
call_groups.iter().find(|g| g.call_id == call_id).cloned()
|
call_groups.iter().find(|g| g.call_id == call_id).cloned()
|
||||||
|
|
@ -80,11 +84,13 @@ pub async fn get_call_token(user_id: Uuid, call_id: Uuid) -> Option<String> {
|
||||||
Some(caller.create_token())
|
Some(caller.create_token())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_invite(call_id: Uuid, inviter_id: Uuid, invitee_id: Uuid) -> bool {
|
pub async fn add_invite(call_id: Uuid, inviter_id: i64, invitee_id: i64) -> bool {
|
||||||
let target_group = {
|
let target_group: Option<Arc<CallGroup>> = CALL_GROUPS
|
||||||
let call_groups = CALL_GROUPS.read().await;
|
.read()
|
||||||
call_groups.iter().find(|g| g.call_id == call_id).cloned()
|
.await
|
||||||
};
|
.iter()
|
||||||
|
.find(|g| g.call_id == call_id)
|
||||||
|
.cloned();
|
||||||
|
|
||||||
if let Some(cg) = target_group {
|
if let Some(cg) = target_group {
|
||||||
let mut members = cg.members.write().await;
|
let mut members = cg.members.write().await;
|
||||||
|
|
@ -100,18 +106,45 @@ pub async fn add_invite(call_id: Uuid, inviter_id: Uuid, invitee_id: Uuid) -> bo
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
pub fn garbage_collect_calls() {
|
||||||
pub async fn get_call_group_by_user(user_id: Uuid) -> Option<Arc<CallGroup>> {
|
tokio::spawn(async move {
|
||||||
let call_groups = CALL_GROUPS.read().await;
|
loop {
|
||||||
for cg in call_groups.iter() {
|
clean_calls().await;
|
||||||
let is_member = {
|
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||||
let members = cg.members.read().await;
|
}
|
||||||
members.iter().any(|m| m.user_id == user_id)
|
});
|
||||||
};
|
}
|
||||||
|
pub async fn clean_calls() {
|
||||||
if is_member {
|
let room_service = RoomClient::new("https://call.tensamin.net").unwrap();
|
||||||
return Some(cg.clone());
|
let rooms = room_service.list_rooms(Vec::new()).await.unwrap();
|
||||||
|
let mut call_ids: Vec<Uuid> = Vec::new();
|
||||||
|
let mut no_users: Vec<Uuid> = Vec::new();
|
||||||
|
for room in rooms {
|
||||||
|
if let Ok(id) = Uuid::from_str(&room.name) {
|
||||||
|
if room.num_participants == 0 {
|
||||||
|
no_users.push(id);
|
||||||
|
}
|
||||||
|
call_ids.push(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
let mut call_groups = CALL_GROUPS.write().await;
|
||||||
|
let size_pre = call_groups.len();
|
||||||
|
call_groups.retain(|cg| call_ids.contains(&cg.call_id));
|
||||||
|
|
||||||
|
for cg in call_groups.iter() {
|
||||||
|
*cg.show.write().await = !no_users.contains(&cg.call_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
let size_post = call_groups.len();
|
||||||
|
drop(call_groups);
|
||||||
|
if size_pre - size_post != 0 {
|
||||||
|
line(
|
||||||
|
PrintType::CallIn,
|
||||||
|
&format!(
|
||||||
|
"Cleaned {} calls, {} remaining",
|
||||||
|
size_pre - size_post,
|
||||||
|
size_post
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,7 @@ use livekit_api::access_token;
|
||||||
use std::env;
|
use std::env;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub fn create_token(
|
pub fn create_token(user_id: i64, call_id: Uuid) -> Result<String, access_token::AccessTokenError> {
|
||||||
user_id: Uuid,
|
|
||||||
call_id: Uuid,
|
|
||||||
) -> Result<String, access_token::AccessTokenError> {
|
|
||||||
let api_key = env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
|
let api_key = env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
|
||||||
let api_secret = env::var("LIVEKIT_API_SECRET").expect("LIVEKIT_API_SECRET is not set");
|
let api_secret = env::var("LIVEKIT_API_SECRET").expect("LIVEKIT_API_SECRET is not set");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,13 @@ use uuid::Uuid;
|
||||||
use crate::calls::call_util;
|
use crate::calls::call_util;
|
||||||
|
|
||||||
pub struct Caller {
|
pub struct Caller {
|
||||||
pub user_id: Uuid,
|
pub user_id: i64,
|
||||||
pub call_id: Uuid,
|
pub call_id: Uuid,
|
||||||
pub inviters: Vec<Uuid>,
|
pub inviters: Vec<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Caller {
|
impl Caller {
|
||||||
pub fn new(user_id: Uuid, call_id: Uuid, inviter_id: Uuid) -> Self {
|
pub fn new(user_id: i64, call_id: Uuid, inviter_id: i64) -> Self {
|
||||||
Caller {
|
Caller {
|
||||||
user_id,
|
user_id,
|
||||||
call_id,
|
call_id,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
use json::number::Number;
|
use json::number::Number;
|
||||||
use json::{Array, JsonValue, object, parse};
|
use json::{Array, JsonValue, object, parse};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::str::FromStr;
|
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
|
@ -20,6 +19,7 @@ pub enum DataTypes {
|
||||||
user_state,
|
user_state,
|
||||||
user_states,
|
user_states,
|
||||||
user_pings,
|
user_pings,
|
||||||
|
call_state,
|
||||||
screen_share,
|
screen_share,
|
||||||
private_key_hash,
|
private_key_hash,
|
||||||
accepted,
|
accepted,
|
||||||
|
|
@ -73,11 +73,12 @@ pub enum DataTypes {
|
||||||
challenge,
|
challenge,
|
||||||
community_title,
|
community_title,
|
||||||
communities,
|
communities,
|
||||||
|
|
||||||
|
user,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DataTypes {
|
impl DataTypes {
|
||||||
pub fn parse(p0: String) -> DataTypes {
|
pub fn parse(p0: String) -> DataTypes {
|
||||||
// normalize: lowercase + remove underscores
|
|
||||||
let normalized = p0.to_lowercase().replace('_', "");
|
let normalized = p0.to_lowercase().replace('_', "");
|
||||||
|
|
||||||
match normalized.as_str() {
|
match normalized.as_str() {
|
||||||
|
|
@ -92,6 +93,7 @@ impl DataTypes {
|
||||||
"userstate" => DataTypes::user_state,
|
"userstate" => DataTypes::user_state,
|
||||||
"userstates" => DataTypes::user_states,
|
"userstates" => DataTypes::user_states,
|
||||||
"userpings" => DataTypes::user_pings,
|
"userpings" => DataTypes::user_pings,
|
||||||
|
"callstate" => DataTypes::call_state,
|
||||||
"screenshare" => DataTypes::screen_share,
|
"screenshare" => DataTypes::screen_share,
|
||||||
"privatekeyhash" => DataTypes::private_key_hash,
|
"privatekeyhash" => DataTypes::private_key_hash,
|
||||||
"accepted" => DataTypes::accepted,
|
"accepted" => DataTypes::accepted,
|
||||||
|
|
@ -145,6 +147,8 @@ impl DataTypes {
|
||||||
"challenge" => DataTypes::challenge,
|
"challenge" => DataTypes::challenge,
|
||||||
"communitytitle" => DataTypes::community_title,
|
"communitytitle" => DataTypes::community_title,
|
||||||
"communities" => DataTypes::communities,
|
"communities" => DataTypes::communities,
|
||||||
|
|
||||||
|
"user" => DataTypes::user,
|
||||||
_ => DataTypes::error_type, // fallback if unknown
|
_ => DataTypes::error_type, // fallback if unknown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -160,6 +164,9 @@ pub enum CommunicationType {
|
||||||
error_invalid_challenge,
|
error_invalid_challenge,
|
||||||
error_invalid_secret,
|
error_invalid_secret,
|
||||||
error_invalid_private_key,
|
error_invalid_private_key,
|
||||||
|
error_no_user_id,
|
||||||
|
error_no_call_id,
|
||||||
|
error_invalid_call_id,
|
||||||
success,
|
success,
|
||||||
settings_save,
|
settings_save,
|
||||||
settings_load,
|
settings_load,
|
||||||
|
|
@ -178,11 +185,11 @@ pub enum CommunicationType {
|
||||||
add_community,
|
add_community,
|
||||||
remove_community,
|
remove_community,
|
||||||
get_communities,
|
get_communities,
|
||||||
|
challenge,
|
||||||
|
challenge_response,
|
||||||
register,
|
register,
|
||||||
register_response,
|
register_response,
|
||||||
identification,
|
identification,
|
||||||
challenge,
|
|
||||||
challenge_response,
|
|
||||||
identification_response,
|
identification_response,
|
||||||
ping,
|
ping,
|
||||||
pong,
|
pong,
|
||||||
|
|
@ -203,25 +210,42 @@ pub enum CommunicationType {
|
||||||
watch_stream,
|
watch_stream,
|
||||||
call_token,
|
call_token,
|
||||||
call_invite,
|
call_invite,
|
||||||
|
end_call,
|
||||||
function,
|
function,
|
||||||
update,
|
update,
|
||||||
|
create_user,
|
||||||
}
|
}
|
||||||
impl CommunicationType {
|
impl CommunicationType {
|
||||||
pub fn parse(p0: String) -> CommunicationType {
|
pub fn parse(p0: String) -> CommunicationType {
|
||||||
let normalized = p0.to_lowercase().replace('_', "");
|
let normalized = p0.to_lowercase().replace('_', "");
|
||||||
|
|
||||||
match normalized.as_str() {
|
match normalized.as_str() {
|
||||||
"error" => CommunicationType::error,
|
"watchstream" => CommunicationType::watch_stream,
|
||||||
|
"calltoken" => CommunicationType::call_token,
|
||||||
|
"callinvite" => CommunicationType::call_invite,
|
||||||
|
"endcall" => CommunicationType::end_call,
|
||||||
|
"function" => CommunicationType::function,
|
||||||
|
"update" => CommunicationType::update,
|
||||||
|
"createuser" => CommunicationType::create_user,
|
||||||
|
"errorinvaliduserid" => CommunicationType::error_invalid_user_id,
|
||||||
|
"errornotfound" => CommunicationType::error_not_found,
|
||||||
|
"errornoiota" => CommunicationType::error_no_iota,
|
||||||
|
"errorinvalidchallenge" => CommunicationType::error_invalid_challenge,
|
||||||
|
"errorinvalidsecret" => CommunicationType::error_invalid_secret,
|
||||||
|
"errorinvalidprivatekey" => CommunicationType::error_invalid_private_key,
|
||||||
|
"errornouserid" => CommunicationType::error_no_user_id,
|
||||||
|
"errornocallid" => CommunicationType::error_no_call_id,
|
||||||
|
"errorinvalidcallid" => CommunicationType::error_invalid_call_id,
|
||||||
|
"success" => CommunicationType::success,
|
||||||
"settingssave" => CommunicationType::settings_save,
|
"settingssave" => CommunicationType::settings_save,
|
||||||
"settingsload" => CommunicationType::settings_load,
|
"settingsload" => CommunicationType::settings_load,
|
||||||
"settingslist" => CommunicationType::settings_list,
|
"settingslist" => CommunicationType::settings_list,
|
||||||
"success" => CommunicationType::success,
|
|
||||||
"message" => CommunicationType::message,
|
"message" => CommunicationType::message,
|
||||||
|
"messagesend" => CommunicationType::message_send,
|
||||||
"messagelive" => CommunicationType::message_live,
|
"messagelive" => CommunicationType::message_live,
|
||||||
"messageotheriota" => CommunicationType::message_other_iota,
|
"messageother_iota" => CommunicationType::message_other_iota,
|
||||||
"messagechunk" => CommunicationType::message_chunk,
|
"messagechunk" => CommunicationType::message_chunk,
|
||||||
"messagesget" => CommunicationType::messages_get,
|
"messagesget" => CommunicationType::messages_get,
|
||||||
"messagesend" => CommunicationType::message_send,
|
|
||||||
"changeconfirm" => CommunicationType::change_confirm,
|
"changeconfirm" => CommunicationType::change_confirm,
|
||||||
"confirmreceive" => CommunicationType::confirm_receive,
|
"confirmreceive" => CommunicationType::confirm_receive,
|
||||||
"confirmread" => CommunicationType::confirm_read,
|
"confirmread" => CommunicationType::confirm_read,
|
||||||
|
|
@ -230,11 +254,11 @@ impl CommunicationType {
|
||||||
"addcommunity" => CommunicationType::add_community,
|
"addcommunity" => CommunicationType::add_community,
|
||||||
"removecommunity" => CommunicationType::remove_community,
|
"removecommunity" => CommunicationType::remove_community,
|
||||||
"getcommunities" => CommunicationType::get_communities,
|
"getcommunities" => CommunicationType::get_communities,
|
||||||
|
"challenge" => CommunicationType::challenge,
|
||||||
|
"challengeresponse" => CommunicationType::challenge_response,
|
||||||
"register" => CommunicationType::register,
|
"register" => CommunicationType::register,
|
||||||
"registerresponse" => CommunicationType::register_response,
|
"registerresponse" => CommunicationType::register_response,
|
||||||
"identification" => CommunicationType::identification,
|
"identification" => CommunicationType::identification,
|
||||||
"challenge" => CommunicationType::challenge,
|
|
||||||
"challengeresponse" => CommunicationType::challenge_response,
|
|
||||||
"identificationresponse" => CommunicationType::identification_response,
|
"identificationresponse" => CommunicationType::identification_response,
|
||||||
"ping" => CommunicationType::ping,
|
"ping" => CommunicationType::ping,
|
||||||
"pong" => CommunicationType::pong,
|
"pong" => CommunicationType::pong,
|
||||||
|
|
@ -252,11 +276,7 @@ impl CommunicationType {
|
||||||
"webrtcice" => CommunicationType::webrtc_ice,
|
"webrtcice" => CommunicationType::webrtc_ice,
|
||||||
"startstream" => CommunicationType::start_stream,
|
"startstream" => CommunicationType::start_stream,
|
||||||
"endstream" => CommunicationType::end_stream,
|
"endstream" => CommunicationType::end_stream,
|
||||||
"watchstream" => CommunicationType::watch_stream,
|
|
||||||
"calltoken" => CommunicationType::call_token,
|
|
||||||
"callinvite" => CommunicationType::call_invite,
|
|
||||||
"function" => CommunicationType::function,
|
|
||||||
"update" => CommunicationType::update,
|
|
||||||
_ => CommunicationType::error,
|
_ => CommunicationType::error,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -266,8 +286,8 @@ impl CommunicationType {
|
||||||
pub struct CommunicationValue {
|
pub struct CommunicationValue {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub comm_type: CommunicationType,
|
pub comm_type: CommunicationType,
|
||||||
pub sender: Uuid,
|
pub sender: i64,
|
||||||
pub receiver: Uuid,
|
pub receiver: i64,
|
||||||
pub data: HashMap<DataTypes, JsonValue>,
|
pub data: HashMap<DataTypes, JsonValue>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -277,8 +297,8 @@ impl CommunicationValue {
|
||||||
Self {
|
Self {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
comm_type,
|
comm_type,
|
||||||
sender: Uuid::new_v4(),
|
sender: 0,
|
||||||
receiver: Uuid::new_v4(),
|
receiver: 0,
|
||||||
data: HashMap::new(),
|
data: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -289,18 +309,18 @@ impl CommunicationValue {
|
||||||
pub fn get_id(&self) -> Uuid {
|
pub fn get_id(&self) -> Uuid {
|
||||||
self.id.clone()
|
self.id.clone()
|
||||||
}
|
}
|
||||||
pub fn with_sender(mut self, sender: Uuid) -> Self {
|
pub fn with_sender(mut self, sender: i64) -> Self {
|
||||||
self.sender = sender;
|
self.sender = sender;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn get_sender(&self) -> Uuid {
|
pub fn get_sender(&self) -> i64 {
|
||||||
self.sender.clone()
|
self.sender.clone()
|
||||||
}
|
}
|
||||||
pub fn with_receiver(mut self, receiver: Uuid) -> Self {
|
pub fn with_receiver(mut self, receiver: i64) -> Self {
|
||||||
self.receiver = receiver;
|
self.receiver = receiver;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn get_receiver(&self) -> Uuid {
|
pub fn get_receiver(&self) -> i64 {
|
||||||
self.receiver.clone()
|
self.receiver.clone()
|
||||||
}
|
}
|
||||||
pub fn add_data_num(mut self, key: DataTypes, value: Number) -> Self {
|
pub fn add_data_num(mut self, key: DataTypes, value: Number) -> Self {
|
||||||
|
|
@ -331,72 +351,104 @@ impl CommunicationValue {
|
||||||
for (k, v) in &self.data {
|
for (k, v) in &self.data {
|
||||||
jdata[&format!("{:?}", k)] = JsonValue::from(v.clone());
|
jdata[&format!("{:?}", k)] = JsonValue::from(v.clone());
|
||||||
}
|
}
|
||||||
|
if self.sender > 0 && self.receiver > 0 {
|
||||||
object! {
|
object! {
|
||||||
id: self.id.to_string(),
|
id: self.id.to_string(),
|
||||||
type: format!("{:?}", self.comm_type),
|
type: format!("{:?}", self.comm_type),
|
||||||
sender: self.sender.to_string(),
|
sender: self.sender.to_string(),
|
||||||
receiver: self.receiver.to_string(),
|
receiver: self.receiver.to_string(),
|
||||||
data: jdata
|
data: jdata
|
||||||
|
}
|
||||||
|
} else if self.sender > 0 {
|
||||||
|
object! {
|
||||||
|
id: self.id.to_string(),
|
||||||
|
type: format!("{:?}", self.comm_type),
|
||||||
|
sender: self.sender.to_string(),
|
||||||
|
data: jdata
|
||||||
|
}
|
||||||
|
} else if self.receiver > 0 {
|
||||||
|
object! {
|
||||||
|
id: self.id.to_string(),
|
||||||
|
type: format!("{:?}", self.comm_type),
|
||||||
|
receiver: self.receiver.to_string(),
|
||||||
|
data: jdata
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
object! {
|
||||||
|
id: self.id.to_string(),
|
||||||
|
type: format!("{:?}", self.comm_type),
|
||||||
|
data: jdata
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_json(json_str: &str) -> Self {
|
pub fn from_json(json_str: &str) -> Self {
|
||||||
let parsed = parse(json_str).unwrap();
|
if let Ok(parsed) = parse(json_str) {
|
||||||
|
let comm_type = CommunicationType::parse(parsed["type"].to_string());
|
||||||
let comm_type = CommunicationType::parse(parsed["type"].to_string());
|
let mut sender: i64 = 0;
|
||||||
|
if parsed.has_key("sender") {
|
||||||
let sender: Uuid = parsed["sender"]
|
sender = parsed["sender"].as_i64().unwrap_or(0);
|
||||||
.as_str()
|
}
|
||||||
.and_then(|s| Uuid::parse_str(s).ok())
|
let mut receiver: i64 = 0;
|
||||||
.unwrap_or(Uuid::new_v4());
|
if parsed.has_key("receiver") {
|
||||||
let receiver: Uuid = parsed["receiver"]
|
receiver = parsed["receiver"].as_i64().unwrap_or(0);
|
||||||
.as_str()
|
|
||||||
.and_then(|s| Uuid::parse_str(s).ok())
|
|
||||||
.unwrap_or(Uuid::new_v4());
|
|
||||||
|
|
||||||
let uuid = Uuid::parse_str(parsed["id"].as_str().unwrap_or("")).unwrap_or(Uuid::new_v4());
|
|
||||||
let mut data = HashMap::new();
|
|
||||||
if parsed["data"].is_object() {
|
|
||||||
for (k, v) in parsed["data"].entries() {
|
|
||||||
data.insert(DataTypes::parse(k.to_string()), v.clone());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Self {
|
let uuid =
|
||||||
id: uuid,
|
Uuid::parse_str(parsed["id"].as_str().unwrap_or("")).unwrap_or(Uuid::new_v4());
|
||||||
comm_type,
|
let mut data = HashMap::new();
|
||||||
sender,
|
if parsed["data"].is_object() {
|
||||||
receiver,
|
for (k, v) in parsed["data"].entries() {
|
||||||
data,
|
data.insert(DataTypes::parse(k.to_string()), v.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Self {
|
||||||
|
id: uuid,
|
||||||
|
comm_type,
|
||||||
|
sender,
|
||||||
|
receiver,
|
||||||
|
data,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Self {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
comm_type: CommunicationType::error,
|
||||||
|
sender: 0,
|
||||||
|
receiver: 0,
|
||||||
|
data: HashMap::new(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn forward_to_other_iota(original: &mut CommunicationValue) -> CommunicationValue {
|
pub fn forward_to_other_iota(original: &mut CommunicationValue) -> CommunicationValue {
|
||||||
let receiver = Uuid::from_str(
|
let receiver = original
|
||||||
&*original
|
.get_data(DataTypes::receiver_id)
|
||||||
.get_data(DataTypes::receiver_id)
|
.unwrap_or(&JsonValue::Number(Number::from(0)))
|
||||||
.unwrap()
|
.as_i64()
|
||||||
.to_string(),
|
.unwrap_or(0);
|
||||||
)
|
|
||||||
.ok()
|
|
||||||
.or(Option::from(Uuid::nil()));
|
|
||||||
|
|
||||||
let now_ms = SystemTime::now()
|
let now_ms = SystemTime::now()
|
||||||
.duration_since(UNIX_EPOCH)
|
.duration_since(UNIX_EPOCH)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_millis() as i64;
|
.as_millis() as i64;
|
||||||
|
|
||||||
let cv = CommunicationValue::new(CommunicationType::message_other_iota)
|
let sender = original.get_sender();
|
||||||
|
CommunicationValue::new(CommunicationType::message_other_iota)
|
||||||
.with_id(original.get_id())
|
.with_id(original.get_id())
|
||||||
.with_receiver(receiver.unwrap())
|
.with_receiver(receiver)
|
||||||
|
.add_data(
|
||||||
|
DataTypes::receiver_id,
|
||||||
|
JsonValue::Number(Number::from(receiver)),
|
||||||
|
)
|
||||||
|
.with_sender(sender)
|
||||||
.add_data(DataTypes::send_time, JsonValue::String(now_ms.to_string()))
|
.add_data(DataTypes::send_time, JsonValue::String(now_ms.to_string()))
|
||||||
|
.add_data(
|
||||||
|
DataTypes::sender_id,
|
||||||
|
JsonValue::Number(Number::from(sender)),
|
||||||
|
)
|
||||||
.add_data(
|
.add_data(
|
||||||
DataTypes::content,
|
DataTypes::content,
|
||||||
JsonValue::String(original.get_data(DataTypes::content).unwrap().to_string()),
|
JsonValue::String(original.get_data(DataTypes::content).unwrap().to_string()),
|
||||||
);
|
)
|
||||||
|
|
||||||
// include sender_id if the original had one
|
|
||||||
let sender = original.get_sender();
|
|
||||||
cv.add_data(DataTypes::sender_id, JsonValue::String(sender.to_string()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub iota_id: Uuid,
|
pub iota_id: i64,
|
||||||
pub user_id: Uuid,
|
pub user_id: i64,
|
||||||
pub status: UserStatus,
|
pub status: UserStatus,
|
||||||
}
|
}
|
||||||
impl User {
|
impl User {
|
||||||
pub fn new(iota_id: Uuid, user_id: Uuid, status: UserStatus) -> Self {
|
pub fn new(iota_id: i64, user_id: i64, status: UserStatus) -> Self {
|
||||||
User {
|
User {
|
||||||
iota_id,
|
iota_id,
|
||||||
user_id,
|
user_id,
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ use tokio_util::compat::TokioAsyncReadCompatExt;
|
||||||
use tungstenite::handshake::server::{Request, Response};
|
use tungstenite::handshake::server::{Request, Response};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
calls::call_manager::garbage_collect_calls,
|
||||||
omega::omega_connection::OmegaConnection,
|
omega::omega_connection::OmegaConnection,
|
||||||
rho::{client_connection::ClientConnection, iota_connection::IotaConnection},
|
rho::{client_connection::ClientConnection, iota_connection::IotaConnection},
|
||||||
util::{
|
util::{
|
||||||
|
|
@ -36,6 +37,8 @@ async fn main() {
|
||||||
&format!("WebSocket server listening on {}", &address),
|
&format!("WebSocket server listening on {}", &address),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
garbage_collect_calls();
|
||||||
|
|
||||||
while let Ok((stream, _)) = listener.accept().await {
|
while let Ok((stream, _)) = listener.accept().await {
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut path: String = "/".to_string();
|
let mut path: String = "/".to_string();
|
||||||
|
|
|
||||||
|
|
@ -104,14 +104,16 @@ impl OmegaConnection {
|
||||||
|
|
||||||
// Handle CLIENT_CHANGED
|
// Handle CLIENT_CHANGED
|
||||||
if cv.is_type(CommunicationType::client_changed) {
|
if cv.is_type(CommunicationType::client_changed) {
|
||||||
let iota_id = Uuid::parse_str(
|
let iota_id = cv
|
||||||
cv.get_data(DataTypes::iota_id).unwrap().as_str().unwrap(),
|
.get_data(DataTypes::iota_id)
|
||||||
)
|
.unwrap()
|
||||||
.unwrap();
|
.as_i64()
|
||||||
let user_id = Uuid::parse_str(
|
.unwrap_or(0);
|
||||||
cv.get_data(DataTypes::user_id).unwrap().as_str().unwrap(),
|
let user_id = cv
|
||||||
)
|
.get_data(DataTypes::user_id)
|
||||||
.unwrap();
|
.unwrap()
|
||||||
|
.as_i64()
|
||||||
|
.unwrap_or(0);
|
||||||
let status_str = cv
|
let status_str = cv
|
||||||
.get_data(DataTypes::user_state)
|
.get_data(DataTypes::user_state)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
@ -147,7 +149,7 @@ impl OmegaConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_iota(iota_id: Uuid, user_ids: Vec<Uuid>) {
|
pub async fn connect_iota(iota_id: i64, user_ids: Vec<i64>) {
|
||||||
let user_ids_str = user_ids
|
let user_ids_str = user_ids
|
||||||
.iter()
|
.iter()
|
||||||
.map(|id| id.to_string())
|
.map(|id| id.to_string())
|
||||||
|
|
@ -159,21 +161,21 @@ impl OmegaConnection {
|
||||||
OmegaConnection::send_global(cv).await;
|
OmegaConnection::send_global(cv).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn close_iota(iota_id: Uuid) {
|
pub async fn close_iota(iota_id: i64) {
|
||||||
let cv = CommunicationValue::new(CommunicationType::iota_closed)
|
let cv = CommunicationValue::new(CommunicationType::iota_closed)
|
||||||
.add_data(DataTypes::iota_id, JsonValue::from(iota_id.to_string()));
|
.add_data(DataTypes::iota_id, JsonValue::from(iota_id.to_string()));
|
||||||
OmegaConnection::send_global(cv).await;
|
OmegaConnection::send_global(cv).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn client_changed(iota_id: Uuid, user_id: Uuid, state: UserStatus) {
|
pub async fn client_changed(iota_id: i64, user_id: i64, state: UserStatus) {
|
||||||
let cv = CommunicationValue::new(CommunicationType::client_changed)
|
let cv = CommunicationValue::new(CommunicationType::client_changed)
|
||||||
.add_data(DataTypes::iota_id, JsonValue::from(iota_id.to_string()))
|
.add_data(DataTypes::iota_id, JsonValue::from(iota_id))
|
||||||
.add_data(DataTypes::user_id, JsonValue::from(user_id.to_string()))
|
.add_data(DataTypes::user_id, JsonValue::from(user_id))
|
||||||
.add_data(DataTypes::user_state, JsonValue::from(state.to_string()));
|
.add_data(DataTypes::user_state, JsonValue::from(state.to_string()));
|
||||||
OmegaConnection::send_global(cv).await;
|
OmegaConnection::send_global(cv).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn user_states(user_id: Uuid, user_ids: Vec<Uuid>) {
|
pub async fn user_states(user_id: i64, user_ids: Vec<i64>) {
|
||||||
let user_ids_str = user_ids
|
let user_ids_str = user_ids
|
||||||
.iter()
|
.iter()
|
||||||
.map(|id| id.to_string())
|
.map(|id| id.to_string())
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use async_tungstenite::tungstenite::Message;
|
use async_tungstenite::tungstenite::Message;
|
||||||
use async_tungstenite::{WebSocketReceiver, WebSocketSender};
|
use async_tungstenite::{WebSocketReceiver, WebSocketSender};
|
||||||
|
use json::number::Number;
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
use tokio_util::compat::Compat;
|
use tokio_util::compat::Compat;
|
||||||
|
|
@ -27,7 +28,7 @@ pub struct ClientConnection {
|
||||||
pub sender: Arc<RwLock<WebSocketSender<Compat<tokio::net::TcpStream>>>>,
|
pub sender: Arc<RwLock<WebSocketSender<Compat<tokio::net::TcpStream>>>>,
|
||||||
pub receiver: Arc<RwLock<WebSocketReceiver<Compat<tokio::net::TcpStream>>>>,
|
pub receiver: Arc<RwLock<WebSocketReceiver<Compat<tokio::net::TcpStream>>>>,
|
||||||
/// User ID associated with this client
|
/// User ID associated with this client
|
||||||
pub user_id: Arc<RwLock<Option<Uuid>>>,
|
pub user_id: Arc<RwLock<i64>>,
|
||||||
/// Whether this connection has been identified/authenticated
|
/// Whether this connection has been identified/authenticated
|
||||||
pub identified: Arc<RwLock<bool>>,
|
pub identified: Arc<RwLock<bool>>,
|
||||||
/// Ping latency tracking
|
/// Ping latency tracking
|
||||||
|
|
@ -35,7 +36,7 @@ pub struct ClientConnection {
|
||||||
/// Weak reference to RhoConnection to avoid circular references
|
/// Weak reference to RhoConnection to avoid circular references
|
||||||
pub rho_connection: Arc<RwLock<Option<Weak<RhoConnection>>>>,
|
pub rho_connection: Arc<RwLock<Option<Weak<RhoConnection>>>>,
|
||||||
/// List of user IDs this client is interested in receiving updates about
|
/// List of user IDs this client is interested in receiving updates about
|
||||||
pub interested_users: Arc<RwLock<Vec<Uuid>>>,
|
pub interested_users: Arc<RwLock<Vec<i64>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientConnection {
|
impl ClientConnection {
|
||||||
|
|
@ -47,7 +48,7 @@ impl ClientConnection {
|
||||||
Arc::new(Self {
|
Arc::new(Self {
|
||||||
sender: Arc::new(RwLock::new(sender)),
|
sender: Arc::new(RwLock::new(sender)),
|
||||||
receiver: Arc::new(RwLock::new(receiver)),
|
receiver: Arc::new(RwLock::new(receiver)),
|
||||||
user_id: Arc::new(RwLock::new(None)),
|
user_id: Arc::new(RwLock::new(0)),
|
||||||
identified: Arc::new(RwLock::new(false)),
|
identified: Arc::new(RwLock::new(false)),
|
||||||
ping: Arc::new(RwLock::new(-1)),
|
ping: Arc::new(RwLock::new(-1)),
|
||||||
rho_connection: Arc::new(RwLock::new(None)),
|
rho_connection: Arc::new(RwLock::new(None)),
|
||||||
|
|
@ -56,7 +57,7 @@ impl ClientConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the user ID
|
/// Get the user ID
|
||||||
pub async fn get_user_id(&self) -> Option<Uuid> {
|
pub async fn get_user_id(&self) -> i64 {
|
||||||
*self.user_id.read().await
|
*self.user_id.read().await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -155,18 +156,8 @@ impl ClientConnection {
|
||||||
/// Handle identification message
|
/// Handle identification message
|
||||||
async fn handle_identification(&self, sarc: Arc<ClientConnection>, cv: CommunicationValue) {
|
async fn handle_identification(&self, sarc: Arc<ClientConnection>, cv: CommunicationValue) {
|
||||||
// Extract user ID
|
// Extract user ID
|
||||||
let user_id = match cv.get_data(DataTypes::user_id) {
|
let user_id: i64 = match cv.get_data(DataTypes::user_id) {
|
||||||
Some(id_str) => match Uuid::parse_str(&id_str.to_string()) {
|
Some(id_str) => id_str.as_i64().unwrap_or(0),
|
||||||
Ok(id) => id,
|
|
||||||
Err(_) => {
|
|
||||||
self.send_error_response(
|
|
||||||
&cv.get_id(),
|
|
||||||
CommunicationType::error_invalid_user_id,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => {
|
None => {
|
||||||
self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_user_id)
|
self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_user_id)
|
||||||
.await;
|
.await;
|
||||||
|
|
@ -209,7 +200,7 @@ impl ClientConnection {
|
||||||
// Set identification data
|
// Set identification data
|
||||||
{
|
{
|
||||||
let mut user_id_guard = self.user_id.write().await;
|
let mut user_id_guard = self.user_id.write().await;
|
||||||
*user_id_guard = Some(user_id);
|
*user_id_guard = user_id;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let mut identified_guard = self.identified.write().await;
|
let mut identified_guard = self.identified.write().await;
|
||||||
|
|
@ -253,61 +244,53 @@ impl ClientConnection {
|
||||||
|
|
||||||
/// Handle client status change
|
/// Handle client status change
|
||||||
async fn handle_client_changed(&self, cv: CommunicationValue) {
|
async fn handle_client_changed(&self, cv: CommunicationValue) {
|
||||||
if let Some(user_id) = self.get_user_id().await {
|
let user_id = self.get_user_id().await;
|
||||||
if let Some(_status_str) = cv.get_data(DataTypes::user_state) {
|
if let Some(_status_str) = cv.get_data(DataTypes::user_state) {
|
||||||
// Parse user status - this would need to be implemented properly
|
// Parse user status - this would need to be implemented properly
|
||||||
let user_status = UserStatus::online; // placeholder
|
let user_status = UserStatus::online; // placeholder
|
||||||
if let Some(rho_conn) = self.get_rho_connection().await {
|
if let Some(rho_conn) = self.get_rho_connection().await {
|
||||||
OmegaConnection::client_changed(
|
OmegaConnection::client_changed(rho_conn.get_iota_id().await, user_id, user_status)
|
||||||
rho_conn.get_iota_id().await,
|
|
||||||
user_id,
|
|
||||||
user_status,
|
|
||||||
)
|
|
||||||
.await;
|
.await;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle call invite
|
/// Handle call invite
|
||||||
async fn handle_call_invite(&self, cv: CommunicationValue) {
|
async fn handle_call_invite(&self, cv: CommunicationValue) {
|
||||||
let receiver_id = match cv.get_data(DataTypes::receiver_id) {
|
let receiver_id: i64 = cv
|
||||||
Some(id_str) => match Uuid::parse_str(&id_str.to_string()) {
|
.get_data(DataTypes::receiver_id)
|
||||||
Ok(id) => id,
|
.unwrap_or(&json::JsonValue::Number(Number::from(0)))
|
||||||
Err(_) => {
|
.as_i64()
|
||||||
self.send_error_response(&cv.get_id(), CommunicationType::error)
|
.unwrap_or(0);
|
||||||
.await;
|
if receiver_id == 0 {
|
||||||
return;
|
self.send_error_response(&cv.get_id(), CommunicationType::error_no_user_id)
|
||||||
}
|
.await;
|
||||||
},
|
return;
|
||||||
None => {
|
}
|
||||||
self.send_error_response(&cv.get_id(), CommunicationType::error)
|
|
||||||
.await;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let call_id = match cv.get_data(DataTypes::call_id) {
|
let call_id = match cv.get_data(DataTypes::call_id) {
|
||||||
Some(id_str) => match Uuid::parse_str(&id_str.to_string()) {
|
Some(id_str) => match Uuid::parse_str(&id_str.to_string()) {
|
||||||
Ok(id) => id,
|
Ok(id) => id,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
self.send_error_response(&cv.get_id(), CommunicationType::error)
|
self.send_error_response(
|
||||||
.await;
|
&cv.get_id(),
|
||||||
|
CommunicationType::error_invalid_call_id,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
self.send_error_response(&cv.get_id(), CommunicationType::error)
|
self.send_error_response(&cv.get_id(), CommunicationType::error_no_call_id)
|
||||||
.await;
|
.await;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let invited =
|
let invited =
|
||||||
call_manager::add_invite(call_id, self.user_id.read().await.unwrap(), receiver_id)
|
call_manager::add_invite(call_id, *self.user_id.read().await, receiver_id).await;
|
||||||
.await;
|
if !invited {
|
||||||
if invited {
|
self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_call_id)
|
||||||
self.send_error_response(&cv.get_id(), CommunicationType::error)
|
|
||||||
.await;
|
.await;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -323,10 +306,7 @@ impl ClientConnection {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get sender user ID
|
// Get sender user ID
|
||||||
let sender_id = match self.get_user_id().await {
|
let sender_id = self.get_user_id().await;
|
||||||
Some(id) => id,
|
|
||||||
None => return,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create and send call distribution message
|
// Create and send call distribution message
|
||||||
let forward = CommunicationValue::new(CommunicationType::call_invite)
|
let forward = CommunicationValue::new(CommunicationType::call_invite)
|
||||||
|
|
@ -344,10 +324,7 @@ impl ClientConnection {
|
||||||
|
|
||||||
/// Handle get call request
|
/// Handle get call request
|
||||||
async fn handle_get_call(&self, cv: CommunicationValue) {
|
async fn handle_get_call(&self, cv: CommunicationValue) {
|
||||||
let user_id = match self.get_user_id().await {
|
let user_id = self.get_user_id().await;
|
||||||
Some(id) => id,
|
|
||||||
None => return,
|
|
||||||
};
|
|
||||||
|
|
||||||
let call_id = match cv.get_data(DataTypes::call_id) {
|
let call_id = match cv.get_data(DataTypes::call_id) {
|
||||||
Some(id_str) => match Uuid::parse_str(&id_str.to_string()) {
|
Some(id_str) => match Uuid::parse_str(&id_str.to_string()) {
|
||||||
|
|
@ -380,11 +357,10 @@ impl ClientConnection {
|
||||||
|
|
||||||
/// Forward message to Iota
|
/// Forward message to Iota
|
||||||
async fn forward_to_iota(&self, cv: CommunicationValue) {
|
async fn forward_to_iota(&self, cv: CommunicationValue) {
|
||||||
if let Some(user_id) = self.get_user_id().await {
|
let user_id = self.get_user_id().await;
|
||||||
if let Some(rho_conn) = self.get_rho_connection().await {
|
if let Some(rho_conn) = self.get_rho_connection().await {
|
||||||
let updated_cv = cv.with_sender(user_id);
|
let updated_cv = cv.with_sender(user_id);
|
||||||
rho_conn.message_to_iota(updated_cv).await;
|
rho_conn.message_to_iota(updated_cv).await;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -401,7 +377,7 @@ impl ClientConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set interested users list
|
/// Set interested users list
|
||||||
pub async fn set_interested_users(&self, interested_ids: Vec<Uuid>) {
|
pub async fn set_interested_users(&self, interested_ids: Vec<i64>) {
|
||||||
let mut interested_guard = self.interested_users.write().await;
|
let mut interested_guard = self.interested_users.write().await;
|
||||||
*interested_guard = interested_ids;
|
*interested_guard = interested_ids;
|
||||||
}
|
}
|
||||||
|
|
@ -424,12 +400,11 @@ impl ClientConnection {
|
||||||
/// Handle connection close
|
/// Handle connection close
|
||||||
pub async fn handle_close(&self) {
|
pub async fn handle_close(&self) {
|
||||||
if self.is_identified().await {
|
if self.is_identified().await {
|
||||||
if let Some(user_id) = self.get_user_id().await {
|
let user_id = self.get_user_id().await;
|
||||||
if let Some(rho_conn) = rho_manager::get_rho_con_for_user(user_id).await {
|
if let Some(rho_conn) = rho_manager::get_rho_con_for_user(user_id).await {
|
||||||
rho_conn
|
rho_conn
|
||||||
.close_client_connection(Arc::new(self.clone()))
|
.close_client_connection(Arc::new(self.clone()))
|
||||||
.await;
|
.await;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ use async_tungstenite::WebSocketReceiver;
|
||||||
use async_tungstenite::WebSocketSender;
|
use async_tungstenite::WebSocketSender;
|
||||||
use async_tungstenite::tungstenite::Message;
|
use async_tungstenite::tungstenite::Message;
|
||||||
use json::JsonValue;
|
use json::JsonValue;
|
||||||
|
use json::number::Number;
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
sync::{Arc, Weak},
|
sync::{Arc, Weak},
|
||||||
|
|
@ -27,8 +28,8 @@ use crate::{
|
||||||
pub struct IotaConnection {
|
pub struct IotaConnection {
|
||||||
pub sender: Arc<RwLock<WebSocketSender<Compat<tokio::net::TcpStream>>>>,
|
pub sender: Arc<RwLock<WebSocketSender<Compat<tokio::net::TcpStream>>>>,
|
||||||
pub receiver: Arc<RwLock<WebSocketReceiver<Compat<tokio::net::TcpStream>>>>,
|
pub receiver: Arc<RwLock<WebSocketReceiver<Compat<tokio::net::TcpStream>>>>,
|
||||||
pub iota_id: Arc<RwLock<Uuid>>,
|
pub iota_id: Arc<RwLock<i64>>,
|
||||||
pub user_ids: Arc<RwLock<Vec<Uuid>>>,
|
pub user_ids: Arc<RwLock<Vec<i64>>>,
|
||||||
pub identified: Arc<RwLock<bool>>,
|
pub identified: Arc<RwLock<bool>>,
|
||||||
pub ping: Arc<RwLock<i64>>,
|
pub ping: Arc<RwLock<i64>>,
|
||||||
pub rho_connection: Arc<RwLock<Option<Weak<RhoConnection>>>>,
|
pub rho_connection: Arc<RwLock<Option<Weak<RhoConnection>>>>,
|
||||||
|
|
@ -43,7 +44,7 @@ impl IotaConnection {
|
||||||
Arc::new(Self {
|
Arc::new(Self {
|
||||||
sender: Arc::new(RwLock::new(sender)),
|
sender: Arc::new(RwLock::new(sender)),
|
||||||
receiver: Arc::new(RwLock::new(receiver)),
|
receiver: Arc::new(RwLock::new(receiver)),
|
||||||
iota_id: Arc::new(RwLock::new(Uuid::nil())),
|
iota_id: Arc::new(RwLock::new(0)),
|
||||||
user_ids: Arc::new(RwLock::new(Vec::new())),
|
user_ids: Arc::new(RwLock::new(Vec::new())),
|
||||||
identified: Arc::new(RwLock::new(false)),
|
identified: Arc::new(RwLock::new(false)),
|
||||||
ping: Arc::new(RwLock::new(0)),
|
ping: Arc::new(RwLock::new(0)),
|
||||||
|
|
@ -52,12 +53,12 @@ impl IotaConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the Iota ID
|
/// Get the Iota ID
|
||||||
pub async fn get_iota_id(&self) -> Uuid {
|
pub async fn get_iota_id(&self) -> i64 {
|
||||||
*self.iota_id.read().await
|
*self.iota_id.read().await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the user IDs
|
/// Get the user IDs
|
||||||
pub async fn get_user_ids(&self) -> Vec<Uuid> {
|
pub async fn get_user_ids(&self) -> Vec<i64> {
|
||||||
self.user_ids.read().await.clone()
|
self.user_ids.read().await.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -115,6 +116,7 @@ impl IotaConnection {
|
||||||
|
|
||||||
// Handle identification
|
// Handle identification
|
||||||
if cv.is_type(CommunicationType::identification) && !self.is_identified().await {
|
if cv.is_type(CommunicationType::identification) && !self.is_identified().await {
|
||||||
|
line(PrintType::IotaIn, &cv.to_json().to_string());
|
||||||
self.handle_identification(cv).await;
|
self.handle_identification(cv).await;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -151,35 +153,47 @@ impl IotaConnection {
|
||||||
|
|
||||||
/// Handle identification message
|
/// Handle identification message
|
||||||
async fn handle_identification(self: Arc<Self>, cv: CommunicationValue) {
|
async fn handle_identification(self: Arc<Self>, cv: CommunicationValue) {
|
||||||
// Parse Iota ID
|
let iota_id: i64 = cv
|
||||||
let iota_id = match cv.get_data(DataTypes::iota_id) {
|
.get_data(DataTypes::iota_id)
|
||||||
Some(id_str) => match Uuid::parse_str(&id_str.to_string()) {
|
.unwrap_or(&JsonValue::Number(Number::from(0)))
|
||||||
Ok(id) => id,
|
.as_i64()
|
||||||
Err(_) => {
|
.unwrap_or(0);
|
||||||
self.send_error_response(&cv.get_id()).await;
|
|
||||||
return;
|
if iota_id == 0 {
|
||||||
}
|
let error = CommunicationValue::new(CommunicationType::error).with_id(cv.get_id());
|
||||||
},
|
self.send_message(error).await;
|
||||||
None => {
|
return;
|
||||||
self.send_error_response(&cv.get_id()).await;
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Parse user IDs
|
// Parse user IDs
|
||||||
let mut validated_user_ids: Vec<Uuid> = Vec::new();
|
let mut validated_user_ids: Vec<i64> = Vec::new();
|
||||||
if let Some(user_ids_str) = cv.get_data(DataTypes::user_ids) {
|
if let Some(user_ids_str) = cv.get_data(DataTypes::user_ids) {
|
||||||
for id_str in user_ids_str.to_string().split(',') {
|
for id_str in user_ids_str.to_string().split(',') {
|
||||||
if id_str.is_empty() {
|
match id_str.parse::<i64>() {
|
||||||
continue;
|
Ok(user_id) => {
|
||||||
}
|
if let Some(auth_iota_id) = auth_connector::get_iota_id(user_id).await {
|
||||||
if let Ok(user_id) = Uuid::parse_str(id_str.trim()) {
|
line(
|
||||||
if let Some(auth_iota_id) = auth_connector::get_iota_id(user_id).await {
|
PrintType::IotaIn,
|
||||||
if auth_iota_id == iota_id {
|
&format!(
|
||||||
validated_user_ids.push(user_id);
|
"auth for {} should be {} is {}",
|
||||||
|
user_id, iota_id, auth_iota_id
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if auth_iota_id == iota_id {
|
||||||
|
validated_user_ids.push(user_id);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
line(
|
||||||
|
PrintType::IotaIn,
|
||||||
|
&format!("User ID {} not parsed", id_str.trim()),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
line(PrintType::IotaIn, "User ID not found");
|
Err(e) => {
|
||||||
|
line(
|
||||||
|
PrintType::IotaIn,
|
||||||
|
&format!("Failed to parse '{}' as i64: {:?}", id_str, e),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -280,10 +294,10 @@ impl IotaConnection {
|
||||||
/// Handle GET_CHATS message
|
/// Handle GET_CHATS message
|
||||||
async fn handle_get_chats(&self, cv: CommunicationValue) {
|
async fn handle_get_chats(&self, cv: CommunicationValue) {
|
||||||
let receiver_id = cv.get_receiver();
|
let receiver_id = cv.get_receiver();
|
||||||
let mut interested_ids: Vec<Uuid> = Vec::new();
|
let mut interested_ids: Vec<i64> = Vec::new();
|
||||||
|
|
||||||
let calls: Vec<Arc<CallGroup>> = call_manager::get_call_groups(receiver_id).await;
|
let calls: Vec<Arc<CallGroup>> = call_manager::get_call_groups(receiver_id).await;
|
||||||
let mut invites: HashMap<Uuid, Vec<JsonValue>> = HashMap::new();
|
let mut invites: HashMap<i64, Vec<JsonValue>> = HashMap::new();
|
||||||
let empty = &calls.is_empty();
|
let empty = &calls.is_empty();
|
||||||
for call in calls {
|
for call in calls {
|
||||||
for inviter in call.members.read().await.iter() {
|
for inviter in call.members.read().await.iter() {
|
||||||
|
|
@ -309,22 +323,19 @@ impl IotaConnection {
|
||||||
JsonValue::new_array()
|
JsonValue::new_array()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
line(PrintType::CallIn, &format!("not empty: {:?}", invites));
|
|
||||||
let mut enrc_contacts = JsonValue::new_array();
|
let mut enrc_contacts = JsonValue::new_array();
|
||||||
if let Some(contacts_data) = cv.get_data(DataTypes::user_ids) {
|
if let Some(contacts_data) = cv.get_data(DataTypes::user_ids) {
|
||||||
if let JsonValue::Array(user_ids) = contacts_data {
|
if let JsonValue::Array(user_ids) = contacts_data {
|
||||||
for user_json in user_ids {
|
for user_json in user_ids {
|
||||||
let user_id_str = user_json["user_id"].as_str().unwrap_or("");
|
let user_id = user_json["user_id"].as_i64().unwrap_or(0);
|
||||||
if let Ok(user_id) = Uuid::parse_str(&user_id_str) {
|
interested_ids.push(user_id);
|
||||||
interested_ids.push(user_id);
|
let mut enriched_contact = JsonValue::new_object();
|
||||||
let mut enriched_contact = JsonValue::new_object();
|
let _ = enriched_contact.insert("user_id", user_id.to_string());
|
||||||
let _ = enriched_contact.insert("user_id", user_id.to_string());
|
let _ = enriched_contact.insert(
|
||||||
let _ = enriched_contact.insert(
|
"calls",
|
||||||
"calls",
|
JsonValue::Array(invites.get(&user_id).unwrap_or(&vec![]).clone()),
|
||||||
JsonValue::Array(invites.get(&user_id).unwrap_or(&vec![]).clone()),
|
);
|
||||||
);
|
let _ = enrc_contacts.push(enriched_contact);
|
||||||
let _ = enrc_contacts.push(enriched_contact);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
enrc_contacts = contacts_data.clone();
|
enrc_contacts = contacts_data.clone();
|
||||||
|
|
@ -355,11 +366,6 @@ impl IotaConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_error_response(&self, message_id: &Uuid) {
|
|
||||||
let error = CommunicationValue::new(CommunicationType::error).with_id(*message_id);
|
|
||||||
self.send_message(error).await;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn handle_close(&self) {
|
pub async fn handle_close(&self) {
|
||||||
if self.is_identified().await {
|
if self.is_identified().await {
|
||||||
if let Some(rho_conn) = self.get_rho_connection().await {
|
if let Some(rho_conn) = self.get_rho_connection().await {
|
||||||
|
|
|
||||||
|
|
@ -4,20 +4,20 @@ use crate::data::{
|
||||||
user::UserStatus,
|
user::UserStatus,
|
||||||
};
|
};
|
||||||
use crate::omega::omega_connection::OmegaConnection;
|
use crate::omega::omega_connection::OmegaConnection;
|
||||||
|
use json::{JsonValue, number::Number};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub struct RhoConnection {
|
pub struct RhoConnection {
|
||||||
iota_connection: Arc<IotaConnection>,
|
iota_connection: Arc<IotaConnection>,
|
||||||
user_ids: Vec<Uuid>,
|
user_ids: Vec<i64>,
|
||||||
client_connections: Arc<RwLock<Vec<Arc<ClientConnection>>>>,
|
client_connections: Arc<RwLock<Vec<Arc<ClientConnection>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RhoConnection {
|
impl RhoConnection {
|
||||||
/// Create a new RhoConnection
|
/// Create a new RhoConnection
|
||||||
pub async fn new(iota_connection: Arc<IotaConnection>, user_ids: Vec<Uuid>) -> Self {
|
pub async fn new(iota_connection: Arc<IotaConnection>, user_ids: Vec<i64>) -> Self {
|
||||||
let rho_connection = Self {
|
let rho_connection = Self {
|
||||||
iota_connection,
|
iota_connection,
|
||||||
user_ids: user_ids.clone(),
|
user_ids: user_ids.clone(),
|
||||||
|
|
@ -30,11 +30,11 @@ impl RhoConnection {
|
||||||
rho_connection
|
rho_connection
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_iota_id(&self) -> Uuid {
|
pub async fn get_iota_id(&self) -> i64 {
|
||||||
self.iota_connection.get_iota_id().await
|
self.iota_connection.get_iota_id().await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_user_ids(&self) -> &Vec<Uuid> {
|
pub fn get_user_ids(&self) -> &Vec<i64> {
|
||||||
&self.user_ids
|
&self.user_ids
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,12 +50,12 @@ impl RhoConnection {
|
||||||
/// Get client connections for a specific user
|
/// Get client connections for a specific user
|
||||||
pub async fn get_client_connections_for_user(
|
pub async fn get_client_connections_for_user(
|
||||||
&self,
|
&self,
|
||||||
user_id: Uuid,
|
user_id: i64,
|
||||||
) -> Vec<Arc<ClientConnection>> {
|
) -> Vec<Arc<ClientConnection>> {
|
||||||
let connections = self.client_connections.read().await;
|
let connections = self.client_connections.read().await;
|
||||||
let mut collections = Vec::new();
|
let mut collections = Vec::new();
|
||||||
for con in connections.iter() {
|
for con in connections.iter() {
|
||||||
if con.get_user_id().await.unwrap() == user_id {
|
if con.get_user_id().await == user_id {
|
||||||
collections.push(con.clone());
|
collections.push(con.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -64,15 +64,10 @@ impl RhoConnection {
|
||||||
|
|
||||||
/// Add a client connection
|
/// Add a client connection
|
||||||
pub async fn add_client_connection(&self, connection: Arc<ClientConnection>) {
|
pub async fn add_client_connection(&self, connection: Arc<ClientConnection>) {
|
||||||
let notification = CommunicationValue::new(CommunicationType::client_connected)
|
let notification = CommunicationValue::new(CommunicationType::client_connected).add_data(
|
||||||
.add_data_str(
|
DataTypes::user_id,
|
||||||
DataTypes::user_id,
|
JsonValue::Number(Number::from(connection.get_user_id().await)),
|
||||||
connection
|
);
|
||||||
.get_user_id()
|
|
||||||
.await
|
|
||||||
.unwrap_or(Uuid::nil())
|
|
||||||
.to_string(),
|
|
||||||
);
|
|
||||||
|
|
||||||
self.iota_connection.send_message(notification).await;
|
self.iota_connection.send_message(notification).await;
|
||||||
|
|
||||||
|
|
@ -83,7 +78,7 @@ impl RhoConnection {
|
||||||
|
|
||||||
OmegaConnection::client_changed(
|
OmegaConnection::client_changed(
|
||||||
self.get_iota_id().await,
|
self.get_iota_id().await,
|
||||||
connection.get_user_id().await.unwrap_or(Uuid::nil()),
|
connection.get_user_id().await,
|
||||||
UserStatus::online,
|
UserStatus::online,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
@ -94,12 +89,10 @@ impl RhoConnection {
|
||||||
{
|
{
|
||||||
let mut connections = self.client_connections.write().await;
|
let mut connections = self.client_connections.write().await;
|
||||||
|
|
||||||
let target_user_id = connection.get_user_id().await.unwrap();
|
let target_user_id = connection.get_user_id().await;
|
||||||
|
|
||||||
connections.retain(|con| {
|
connections.retain(|con| {
|
||||||
futures::executor::block_on(async {
|
futures::executor::block_on(async { con.get_user_id().await != target_user_id })
|
||||||
con.get_user_id().await.unwrap() != target_user_id
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
connections.push(Arc::clone(&connection));
|
connections.push(Arc::clone(&connection));
|
||||||
|
|
@ -108,7 +101,7 @@ impl RhoConnection {
|
||||||
// Notify OmegaConnection
|
// Notify OmegaConnection
|
||||||
OmegaConnection::client_changed(
|
OmegaConnection::client_changed(
|
||||||
self.get_iota_id().await,
|
self.get_iota_id().await,
|
||||||
connection.get_user_id().await.unwrap_or(Uuid::nil()),
|
connection.get_user_id().await,
|
||||||
UserStatus::user_offline,
|
UserStatus::user_offline,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
@ -131,15 +124,9 @@ impl RhoConnection {
|
||||||
|
|
||||||
/// Send message from Iota to specific client
|
/// Send message from Iota to specific client
|
||||||
pub async fn message_to_client(&self, cv: CommunicationValue) {
|
pub async fn message_to_client(&self, cv: CommunicationValue) {
|
||||||
if let Some(receiver_id) = Some(cv.get_receiver()) {
|
let connections = self.client_connections.read().await;
|
||||||
let connections = self.client_connections.read().await;
|
for connection in connections.iter() {
|
||||||
for connection in connections.iter() {
|
connection.send_message(&cv).await;
|
||||||
if let Some(conn_user_id) = connection.get_user_id().await {
|
|
||||||
if conn_user_id == receiver_id {
|
|
||||||
connection.send_message(&cv).await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -154,16 +141,15 @@ impl RhoConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set interested users for a specific client
|
/// Set interested users for a specific client
|
||||||
pub async fn set_interested(&self, user_id: Uuid, interested_ids: Vec<Uuid>) {
|
pub async fn set_interested(&self, user_id: i64, interested_ids: Vec<i64>) {
|
||||||
let connections = self.client_connections.read().await;
|
let connections = self.client_connections.read().await;
|
||||||
for connection in connections.iter() {
|
for connection in connections.iter() {
|
||||||
if let Some(conn_user_id) = connection.get_user_id().await {
|
let conn_user_id = connection.get_user_id().await;
|
||||||
if conn_user_id == user_id {
|
if conn_user_id == user_id {
|
||||||
connection
|
connection
|
||||||
.set_interested_users(interested_ids.clone())
|
.set_interested_users(interested_ids.clone())
|
||||||
.await;
|
.await;
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -182,16 +168,15 @@ impl RhoConnection {
|
||||||
let mut pings = HashMap::new();
|
let mut pings = HashMap::new();
|
||||||
|
|
||||||
for connection in connections.iter() {
|
for connection in connections.iter() {
|
||||||
if let Some(user_id) = connection.get_user_id().await {
|
let user_id = connection.get_user_id().await;
|
||||||
pings.insert(user_id.to_string(), connection.get_ping().await);
|
pings.insert(user_id.to_string(), connection.get_ping().await);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pings
|
pings
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if this RhoConnection contains a specific user ID
|
/// Check if this RhoConnection contains a specific user ID
|
||||||
pub fn contains_user(&self, user_id: &Uuid) -> bool {
|
pub fn contains_user(&self, user_id: &i64) -> bool {
|
||||||
self.user_ids.contains(user_id)
|
self.user_ids.contains(user_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,11 @@ use std::{
|
||||||
sync::{Arc, LazyLock},
|
sync::{Arc, LazyLock},
|
||||||
};
|
};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub static RHO_CONNECTIONS: LazyLock<Arc<RwLock<HashMap<Uuid, Arc<RhoConnection>>>>> =
|
pub static RHO_CONNECTIONS: LazyLock<Arc<RwLock<HashMap<i64, Arc<RhoConnection>>>>> =
|
||||||
LazyLock::new(|| Arc::new(RwLock::new(HashMap::new())));
|
LazyLock::new(|| Arc::new(RwLock::new(HashMap::new())));
|
||||||
|
|
||||||
pub async fn get_rho_con_for_user(user_id: Uuid) -> Option<Arc<RhoConnection>> {
|
pub async fn get_rho_con_for_user(user_id: i64) -> Option<Arc<RhoConnection>> {
|
||||||
let connections = RHO_CONNECTIONS.read().await;
|
let connections = RHO_CONNECTIONS.read().await;
|
||||||
line(
|
line(
|
||||||
PrintType::ClientIn,
|
PrintType::ClientIn,
|
||||||
|
|
@ -32,13 +31,13 @@ pub async fn get_rho_con_for_user(user_id: Uuid) -> Option<Arc<RhoConnection>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn contains_iota(iota_id: Uuid) -> bool {
|
pub async fn contains_iota(iota_id: i64) -> bool {
|
||||||
let connections = RHO_CONNECTIONS.read().await;
|
let connections = RHO_CONNECTIONS.read().await;
|
||||||
connections.contains_key(&iota_id)
|
connections.contains_key(&iota_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a RhoConnection by Iota ID
|
/// Remove a RhoConnection by Iota ID
|
||||||
pub async fn remove_rho(iota_id: Uuid) -> Option<Arc<RhoConnection>> {
|
pub async fn remove_rho(iota_id: i64) -> Option<Arc<RhoConnection>> {
|
||||||
let mut connections = RHO_CONNECTIONS.write().await;
|
let mut connections = RHO_CONNECTIONS.write().await;
|
||||||
connections.remove(&iota_id)
|
connections.remove(&iota_id)
|
||||||
}
|
}
|
||||||
|
|
@ -51,7 +50,7 @@ pub async fn add_rho(rho_connection: Arc<RhoConnection>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a RhoConnection by Iota ID directly
|
/// Get a RhoConnection by Iota ID directly
|
||||||
pub async fn get_rho_by_iota(iota_id: Uuid) -> Option<Arc<RhoConnection>> {
|
pub async fn get_rho_by_iota(iota_id: i64) -> Option<Arc<RhoConnection>> {
|
||||||
let connections = RHO_CONNECTIONS.read().await;
|
let connections = RHO_CONNECTIONS.read().await;
|
||||||
connections.get(&iota_id).map(Arc::clone)
|
connections.get(&iota_id).map(Arc::clone)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue