From a76f4edfa60c944ccd7812ffd543ae022439a0c2 Mon Sep 17 00:00:00 2001 From: Sun Date: Thu, 6 May 2021 11:29:50 +0800 Subject: [PATCH] multiple layers for group chat --- Cargo.toml | 1 + src/apps.rs | 18 +++++++++----- src/apps/group_chat/layer.rs | 47 ++++++++++++++++++++++++++++++++++++ src/apps/group_chat/mod.rs | 12 +++++++++ src/apps/group_chat/rpc.rs | 10 ++++---- src/layer.rs | 8 +++--- 6 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 src/apps/group_chat/layer.rs diff --git a/Cargo.toml b/Cargo.toml index f9462f4..0f4e261 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ tdn = { git = "https://github.com/cypherlink/TDN", branch="main", default-featur tdn-did = { git = "https://github.com/cypherlink/tdn-did", branch="main" } tdn-storage = { git = "https://github.com/cypherlink/tdn-storage", branch="main" } group-chat-types = { git = "https://github.com/cympletech/group-chat", branch="main" } +# group-chat-types = { path = "../group-chat/types" } [target.'cfg(target_os="android")'.dependencies] jni = { version = "0.19", default-features = false } \ No newline at end of file diff --git a/src/apps.rs b/src/apps.rs index fe7bbed..3e51305 100644 --- a/src/apps.rs +++ b/src/apps.rs @@ -1,5 +1,6 @@ use tdn::types::{ group::GroupId, + message::RecvType, primitive::{HandleResult, PeerAddr, Result}, rpc::RpcHandler, }; @@ -22,13 +23,18 @@ pub(crate) fn app_rpc_inject(handler: &mut RpcHandler) { group_chat::new_rpc_handler(handler); } -pub(crate) fn _app_layer_handle( - _gid: GroupId, - _fgid: GroupId, - _addr: PeerAddr, - _data: Vec, +pub(crate) fn app_layer_handle( + fgid: GroupId, + mgid: GroupId, + msg: RecvType, ) -> Result { - todo!() + match fgid { + group_chat::GROUP_ID => group_chat::layer_handle(mgid, msg), + _ => { + // todo!() + Ok(HandleResult::new()) + } + } } pub(crate) fn _app_group_handle() -> Result { diff --git a/src/apps/group_chat/layer.rs b/src/apps/group_chat/layer.rs new file mode 100644 index 0000000..19c0f28 --- /dev/null +++ b/src/apps/group_chat/layer.rs @@ -0,0 +1,47 @@ +use tdn::types::{ + group::GroupId, + message::{RecvType, SendType}, + primitive::{new_io_error, HandleResult, Result}, +}; + +use group_chat_types::{Event, GroupConnect, GroupEvent, GroupInfo, GroupResult, GroupType}; + +pub(crate) fn handle(mgid: GroupId, msg: RecvType) -> Result { + let mut results = HandleResult::new(); + + match msg { + RecvType::Connect(addr, data) => { + // None. + } + RecvType::Leave(addr) => { + // + } + RecvType::Result(addr, is_ok, data) => { + let res: GroupResult = postcard::from_bytes(&data) + .map_err(|_e| new_io_error("Deseralize result failure"))?; + match res { + GroupResult::Check(is_ok, supported) => { + println!("check: {}, supported: {:?}", is_ok, supported); + } + _ => { + // + } + } + } + RecvType::ResultConnect(addr, data) => { + let _res: GroupResult = postcard::from_bytes(&data) + .map_err(|_e| new_io_error("Deseralize result failure"))?; + } + RecvType::Event(addr, bytes) => { + // + } + RecvType::Stream(_uid, _stream, _bytes) => { + // TODO stream + } + RecvType::Delivery(t, tid, is_ok) => { + // + } + } + + Ok(results) +} diff --git a/src/apps/group_chat/mod.rs b/src/apps/group_chat/mod.rs index bcd2463..0bf7ec7 100644 --- a/src/apps/group_chat/mod.rs +++ b/src/apps/group_chat/mod.rs @@ -1,4 +1,16 @@ mod models; +pub use group_chat_types::GROUP_CHAT_ID as GROUP_ID; +use tdn::types::{group::GroupId, message::SendType, primitive::HandleResult}; + +/// Group chat server to ESSE. +#[inline] +pub(super) fn add_layer(results: &mut HandleResult, gid: GroupId, msg: SendType) { + results.layers.push((gid, GROUP_ID, msg)); +} + pub(crate) mod rpc; pub(crate) use rpc::new_rpc_handler; + +mod layer; +pub(crate) use layer::handle as layer_handle; diff --git a/src/apps/group_chat/rpc.rs b/src/apps/group_chat/rpc.rs index 1d2a204..edc1708 100644 --- a/src/apps/group_chat/rpc.rs +++ b/src/apps/group_chat/rpc.rs @@ -8,6 +8,7 @@ use tdn::types::{ }; //use crate::group::GroupEvent; +use super::add_layer; use crate::rpc::RpcState; pub(crate) fn new_rpc_handler(handler: &mut RpcHandler) { @@ -22,12 +23,11 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler) { .map_err(|_e| new_io_error("PeerAddr invalid!"))?; println!("addr: {}", addr.to_hex()); + let mut results = HandleResult::new(); let data = postcard::to_allocvec(&GroupConnect::Check).unwrap_or(vec![]); - Ok(HandleResult::layer( - gid, - gid, - SendType::Connect(0, addr, None, None, data), - )) + let s = SendType::Connect(0, addr, None, None, data); + add_layer(&mut results, gid, s); + Ok(results) }, ); } diff --git a/src/layer.rs b/src/layer.rs index 31ff2f8..5609eda 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -17,6 +17,7 @@ use crate::event::{InnerEvent, StatusEvent}; use crate::group::Group; use crate::migrate::consensus::{FRIEND_TABLE_PATH, MESSAGE_TABLE_PATH, REQUEST_TABLE_PATH}; +use crate::apps::app_layer_handle; use crate::apps::chat::rpc; use crate::storage::{ read_avatar, read_file, read_record, session_db, write_avatar_sync, write_file, write_image, @@ -75,14 +76,15 @@ impl Layer { mgid: GroupId, msg: RecvType, ) -> Result { - let mut results = HandleResult::new(); - // 1. check to account is online. if not online, nothing. if !self.runnings.contains_key(&mgid) { - return Ok(results); + return Err(new_io_error("running account not found.")); } + // TODO app_layer_handle(fgid, mgid, msg) + // 2. handle receive message by type. + let mut results = HandleResult::new(); match msg { RecvType::Connect(addr, data) => { let request: LayerRequest = postcard::from_bytes(&data)