|
|
@ -12,7 +12,6 @@ use tokio::sync::RwLock; |
|
|
|
|
|
|
|
|
|
|
|
use crate::account::User; |
|
|
|
use crate::account::User; |
|
|
|
use crate::group::GroupEvent; |
|
|
|
use crate::group::GroupEvent; |
|
|
|
//use crate::apps::group::{group_conn, GROUP_ID};
|
|
|
|
|
|
|
|
use crate::own::Own; |
|
|
|
use crate::own::Own; |
|
|
|
use crate::session::{Session, SessionType}; |
|
|
|
use crate::session::{Session, SessionType}; |
|
|
|
|
|
|
|
|
|
|
@ -121,96 +120,6 @@ impl Layer { |
|
|
|
false |
|
|
|
false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// pub fn remove_running(&mut self, gid: &GroupId) -> HashMap<PeerId, GroupId> {
|
|
|
|
|
|
|
|
// // check close the stable connection.
|
|
|
|
|
|
|
|
// let mut addrs: HashMap<PeerId, GroupId> = HashMap::new();
|
|
|
|
|
|
|
|
// if let Some(running) = self.runnings.remove(gid) {
|
|
|
|
|
|
|
|
// for (addr, fgid) in running.remove_onlines() {
|
|
|
|
|
|
|
|
// addrs.insert(addr, fgid);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// let mut need_keep = vec![];
|
|
|
|
|
|
|
|
// for (_, running) in &self.runnings {
|
|
|
|
|
|
|
|
// for addr in addrs.keys() {
|
|
|
|
|
|
|
|
// if running.check_addr_online(addr) {
|
|
|
|
|
|
|
|
// need_keep.push(*addr);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// for i in need_keep {
|
|
|
|
|
|
|
|
// addrs.remove(&i);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// addrs
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pub fn remove_all_running(&mut self) -> HashMap<PeerId, GroupId> {
|
|
|
|
|
|
|
|
// let mut addrs: HashMap<PeerId, GroupId> = HashMap::new();
|
|
|
|
|
|
|
|
// for (_, running) in self.runnings.drain() {
|
|
|
|
|
|
|
|
// for (addr, fgid) in running.remove_onlines() {
|
|
|
|
|
|
|
|
// addrs.insert(addr, fgid);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// addrs
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pub fn get_running_remote_id(&self, mgid: &GroupId, fgid: &GroupId) -> Result<(i64, i64)> {
|
|
|
|
|
|
|
|
// debug!("onlines: {:?}, find: {:?}", self.runnings.keys(), mgid);
|
|
|
|
|
|
|
|
// self.running(mgid)?.get_online_id(fgid)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pub fn remove_online(&mut self, gid: &GroupId, fgid: &GroupId) -> Option<PeerId> {
|
|
|
|
|
|
|
|
// self.running_mut(gid).ok()?.remove_online(fgid)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pub async fn all_layer_conns(&self) -> Result<HashMap<GroupId, Vec<(GroupId, SendType)>>> {
|
|
|
|
|
|
|
|
// let mut conns = HashMap::new();
|
|
|
|
|
|
|
|
// let own_lock = self.group.read().await;
|
|
|
|
|
|
|
|
// for mgid in self.runnings.keys() {
|
|
|
|
|
|
|
|
// let mut vecs = vec![];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// let db = own_lock.session_db(&mgid)?;
|
|
|
|
|
|
|
|
// let sessions = Session::list(&db)?;
|
|
|
|
|
|
|
|
// drop(db);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for s in sessions {
|
|
|
|
|
|
|
|
// match s.s_type {
|
|
|
|
|
|
|
|
// SessionType::Chat => {
|
|
|
|
|
|
|
|
// let proof = own_lock.prove_addr(mgid, &s.addr)?;
|
|
|
|
|
|
|
|
// vecs.push((s.gid, chat_conn(proof, Peer::peer(s.addr))));
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// SessionType::Group => {
|
|
|
|
|
|
|
|
// let proof = own_lock.prove_addr(mgid, &s.addr)?;
|
|
|
|
|
|
|
|
// vecs.push((GROUP_ID, group_conn(proof, Peer::peer(s.addr), s.gid)));
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// _ => {}
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// conns.insert(*mgid, vecs);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ok(conns)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pub fn is_addr_online(&self, faddr: &PeerId) -> bool {
|
|
|
|
|
|
|
|
// for (_, running) in &self.runnings {
|
|
|
|
|
|
|
|
// if running.check_addr_online(faddr) {
|
|
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pub fn is_online(&self, gid: &GroupId, fgid: &GroupId) -> bool {
|
|
|
|
|
|
|
|
// if let Some(running) = self.runnings.get(gid) {
|
|
|
|
|
|
|
|
// running.is_online(fgid)
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// false
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pub fn broadcast(&self, user: User, results: &mut HandleResult) {
|
|
|
|
// pub fn broadcast(&self, user: User, results: &mut HandleResult) {
|
|
|
|
// let info = GroupEvent::InfoRes(user);
|
|
|
|
// let info = GroupEvent::InfoRes(user);
|
|
|
|
// let data = bincode::serialize(&info).unwrap_or(vec![]);
|
|
|
|
// let data = bincode::serialize(&info).unwrap_or(vec![]);
|
|
|
@ -219,50 +128,13 @@ impl Layer { |
|
|
|
// }
|
|
|
|
// }
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// pub(crate) struct OnlineSession {
|
|
|
|
|
|
|
|
// pub pid: PeerId,
|
|
|
|
|
|
|
|
// /// session database id.
|
|
|
|
|
|
|
|
// pub id: i64,
|
|
|
|
|
|
|
|
// /// session ref's service(friend/group) database id.
|
|
|
|
|
|
|
|
// pub fid: i64,
|
|
|
|
|
|
|
|
// pub suspend_me: bool,
|
|
|
|
|
|
|
|
// pub suspend_remote: bool,
|
|
|
|
|
|
|
|
// pub remain: u16, // keep-alive remain minutes
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// impl OnlineSession {
|
|
|
|
|
|
|
|
// fn new(online: Online, db_id: i64, db_fid: i64) -> Self {
|
|
|
|
|
|
|
|
// Self {
|
|
|
|
|
|
|
|
// online,
|
|
|
|
|
|
|
|
// db_id,
|
|
|
|
|
|
|
|
// db_fid,
|
|
|
|
|
|
|
|
// suspend_me: false,
|
|
|
|
|
|
|
|
// suspend_remote: false,
|
|
|
|
|
|
|
|
// remain: 0,
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fn close_suspend(&mut self) -> bool {
|
|
|
|
|
|
|
|
// if self.suspend_me && self.suspend_remote {
|
|
|
|
|
|
|
|
// if self.remain == 0 {
|
|
|
|
|
|
|
|
// true
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// self.remain -= 1;
|
|
|
|
|
|
|
|
// false
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// false
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// online connected layer session.
|
|
|
|
/// online connected layer session.
|
|
|
|
pub(crate) struct LayerSession { |
|
|
|
pub(crate) struct LayerSession { |
|
|
|
pub height: i64, |
|
|
|
pub height: i64, |
|
|
|
/// session network addr.
|
|
|
|
/// session network addr.
|
|
|
|
pub addrs: Vec<PeerId>, |
|
|
|
pub addrs: Vec<PeerId>, |
|
|
|
/// session database id.
|
|
|
|
/// session database id.
|
|
|
|
pub s_id: i64, |
|
|
|
pub sid: i64, |
|
|
|
/// layer service database id.
|
|
|
|
/// layer service database id.
|
|
|
|
pub db_id: i64, |
|
|
|
pub db_id: i64, |
|
|
|
/// if session is suspend by me.
|
|
|
|
/// if session is suspend by me.
|
|
|
@ -274,9 +146,9 @@ pub(crate) struct LayerSession { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl LayerSession { |
|
|
|
impl LayerSession { |
|
|
|
fn new(addr: PeerId, s_id: i64, db_id: i64, height: i64) -> Self { |
|
|
|
fn new(addr: PeerId, sid: i64, db_id: i64, height: i64) -> Self { |
|
|
|
Self { |
|
|
|
Self { |
|
|
|
s_id, |
|
|
|
sid, |
|
|
|
db_id, |
|
|
|
db_id, |
|
|
|
height, |
|
|
|
height, |
|
|
|
addrs: vec![addr], |
|
|
|
addrs: vec![addr], |
|
|
@ -287,7 +159,7 @@ impl LayerSession { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn info(&self) -> (i64, i64, i64, PeerId) { |
|
|
|
pub fn info(&self) -> (i64, i64, i64, PeerId) { |
|
|
|
(self.height, self.s_id, self.db_id, self.addrs[0]) |
|
|
|
(self.height, self.sid, self.db_id, self.addrs[0]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn increased(&mut self) -> i64 { |
|
|
|
pub fn increased(&mut self) -> i64 { |
|
|
@ -325,121 +197,16 @@ impl LayerSession { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// pub fn get_online_id(&self, gid: &GroupId) -> Result<(i64, i64)> {
|
|
|
|
pub fn clear(&mut self) -> bool { |
|
|
|
// debug!("onlines: {:?}, find: {:?}", self.sessions.keys(), gid);
|
|
|
|
if self.suspend_me && self.suspend_remote { |
|
|
|
// self.sessions
|
|
|
|
if self.remain == 0 { |
|
|
|
// .get(gid)
|
|
|
|
true |
|
|
|
// .map(|online| (online.db_id, online.db_fid))
|
|
|
|
} else { |
|
|
|
// .ok_or(anyhow!("remote not online"))
|
|
|
|
self.remain -= 1; |
|
|
|
// }
|
|
|
|
false |
|
|
|
|
|
|
|
} |
|
|
|
// /// get online peer's addr.
|
|
|
|
} else { |
|
|
|
// pub fn online(&self, gid: &GroupId) -> Result<PeerId> {
|
|
|
|
false |
|
|
|
// self.sessions
|
|
|
|
} |
|
|
|
// .get(gid)
|
|
|
|
} |
|
|
|
// .map(|online| *online.online.addr())
|
|
|
|
|
|
|
|
// .ok_or(anyhow!("remote not online"))
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pub fn online_direct(&self, gid: &GroupId) -> Result<PeerId> {
|
|
|
|
|
|
|
|
// if let Some(online) = self.sessions.get(gid) {
|
|
|
|
|
|
|
|
// match online.online {
|
|
|
|
|
|
|
|
// Online::Direct(addr) => return Ok(addr),
|
|
|
|
|
|
|
|
// _ => {}
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Err(anyhow!("no direct online"))
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// get all online peer.
|
|
|
|
|
|
|
|
// pub fn onlines(&self) -> Vec<(&GroupId, &PeerId)> {
|
|
|
|
|
|
|
|
// self.sessions
|
|
|
|
|
|
|
|
// .iter()
|
|
|
|
|
|
|
|
// .map(|(fgid, online)| (fgid, online.online.addr()))
|
|
|
|
|
|
|
|
// .collect()
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// check add online.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// check offline, and return is direct.
|
|
|
|
|
|
|
|
// pub fn check_offline(&mut self, gid: &GroupId, addr: &PeerId) -> bool {
|
|
|
|
|
|
|
|
// if let Some(online) = self.sessions.remove(gid) {
|
|
|
|
|
|
|
|
// if online.online.addr() != addr {
|
|
|
|
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// match online.online {
|
|
|
|
|
|
|
|
// Online::Direct(..) => {
|
|
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// _ => {}
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// false
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// pub fn remove_online(&mut self, gid: &GroupId) -> Option<PeerId> {
|
|
|
|
|
|
|
|
// self.sessions
|
|
|
|
|
|
|
|
// .remove(gid)
|
|
|
|
|
|
|
|
// .map(|online| *online.online.addr())
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// remove all onlines peer.
|
|
|
|
|
|
|
|
// pub fn remove_onlines(self) -> Vec<(PeerId, GroupId)> {
|
|
|
|
|
|
|
|
// let mut peers = vec![];
|
|
|
|
|
|
|
|
// for (fgid, online) in self.sessions {
|
|
|
|
|
|
|
|
// match online.online {
|
|
|
|
|
|
|
|
// Online::Direct(addr) => peers.push((addr, fgid)),
|
|
|
|
|
|
|
|
// _ => {}
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// peers
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// check if addr is online.
|
|
|
|
|
|
|
|
// pub fn check_addr_online(&self, addr: &PeerId) -> bool {
|
|
|
|
|
|
|
|
// for (_, online) in &self.sessions {
|
|
|
|
|
|
|
|
// if online.online.addr() == addr {
|
|
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// false
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// peer leave, remove online peer.
|
|
|
|
|
|
|
|
// pub fn peer_leave(&mut self, addr: &PeerId) -> Vec<i64> {
|
|
|
|
|
|
|
|
// let mut peers = vec![];
|
|
|
|
|
|
|
|
// let mut deletes = vec![];
|
|
|
|
|
|
|
|
// for (fgid, online) in &self.sessions {
|
|
|
|
|
|
|
|
// if online.online.addr() == addr {
|
|
|
|
|
|
|
|
// peers.push(online.db_id);
|
|
|
|
|
|
|
|
// deletes.push(*fgid);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// for i in &deletes {
|
|
|
|
|
|
|
|
// self.sessions.remove(&i);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// peers
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// list all onlines groups.
|
|
|
|
|
|
|
|
// pub fn close_suspend(&mut self, self_addr: &PeerId) -> Vec<(GroupId, PeerId, i64)> {
|
|
|
|
|
|
|
|
// let mut needed = vec![];
|
|
|
|
|
|
|
|
// for (fgid, online) in &mut self.sessions {
|
|
|
|
|
|
|
|
// // when online is self. skip.
|
|
|
|
|
|
|
|
// if online.online == Online::Direct(*self_addr) {
|
|
|
|
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if online.close_suspend() {
|
|
|
|
|
|
|
|
// needed.push((*fgid, *online.online.addr(), online.db_id));
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for (gid, _, _) in needed.iter() {
|
|
|
|
|
|
|
|
// self.sessions.remove(gid);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// needed
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|