Browse Source

multiple layers for group chat

pull/18/head
Sun 4 years ago
parent
commit
a76f4edfa6
  1. 1
      Cargo.toml
  2. 18
      src/apps.rs
  3. 47
      src/apps/group_chat/layer.rs
  4. 12
      src/apps/group_chat/mod.rs
  5. 10
      src/apps/group_chat/rpc.rs
  6. 8
      src/layer.rs

1
Cargo.toml

@ -38,6 +38,7 @@ tdn = { git = "https://github.com/cypherlink/TDN", branch="main", default-featur @@ -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 }

18
src/apps.rs

@ -1,5 +1,6 @@ @@ -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<RpcState>) { @@ -22,13 +23,18 @@ pub(crate) fn app_rpc_inject(handler: &mut RpcHandler<RpcState>) {
group_chat::new_rpc_handler(handler);
}
pub(crate) fn _app_layer_handle(
_gid: GroupId,
_fgid: GroupId,
_addr: PeerAddr,
_data: Vec<u8>,
pub(crate) fn app_layer_handle(
fgid: GroupId,
mgid: GroupId,
msg: RecvType,
) -> Result<HandleResult> {
todo!()
match fgid {
group_chat::GROUP_ID => group_chat::layer_handle(mgid, msg),
_ => {
// todo!()
Ok(HandleResult::new())
}
}
}
pub(crate) fn _app_group_handle() -> Result<HandleResult> {

47
src/apps/group_chat/layer.rs

@ -0,0 +1,47 @@ @@ -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<HandleResult> {
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)
}

12
src/apps/group_chat/mod.rs

@ -1,4 +1,16 @@ @@ -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;

10
src/apps/group_chat/rpc.rs

@ -8,6 +8,7 @@ use tdn::types::{ @@ -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<RpcState>) {
@ -22,12 +23,11 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler<RpcState>) { @@ -22,12 +23,11 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler<RpcState>) {
.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)
},
);
}

8
src/layer.rs

@ -17,6 +17,7 @@ use crate::event::{InnerEvent, StatusEvent}; @@ -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 { @@ -75,14 +76,15 @@ impl Layer {
mgid: GroupId,
msg: RecvType,
) -> Result<HandleResult> {
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)

Loading…
Cancel
Save