changed peer's uuid from struct to outer
This commit is contained in:
parent
7b7ae4b5f5
commit
ecf99847eb
@ -9,3 +9,10 @@ pub const SDLAN_FLAGS_OPTIONS: u16 = 0x0080;
|
||||
|
||||
pub const IPV4_SIZE: u8 = 4;
|
||||
pub const IPV6_SIZE: u8 = 16;
|
||||
|
||||
// add to LIST
|
||||
pub enum SDLanSNOPER {
|
||||
SDLanSNAdd,
|
||||
SDLanSNAddSkip,
|
||||
SDLanSNAdded,
|
||||
}
|
||||
|
||||
56
src/peer.rs
56
src/peer.rs
@ -1,4 +1,5 @@
|
||||
#![allow(unused)]
|
||||
use super::utils::Result;
|
||||
use dashmap::DashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::prelude::FromRow;
|
||||
@ -8,12 +9,13 @@ use std::sync::Mutex;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Peer {
|
||||
pub id: String,
|
||||
// pub id: Mutex<String>,
|
||||
// 对端阶段的udp信息,包括ipv4地址和子网掩码位数
|
||||
pub dev_addr: IpSubnet,
|
||||
// 对端对外开放的ip和端口信息
|
||||
pub sock: Mutex<SdlanSock>,
|
||||
pub pub_key: Mutex<Vec<u8>>,
|
||||
|
||||
pub timeout: isize,
|
||||
|
||||
// 最近一次遇见
|
||||
@ -22,15 +24,17 @@ pub struct Peer {
|
||||
pub last_p2p: AtomicU64,
|
||||
// 最近一次发送query
|
||||
pub last_send_query: AtomicU64,
|
||||
// 最近一次合法时间
|
||||
pub last_valid_timestamp: AtomicU64,
|
||||
|
||||
// 该节点锁属的网络
|
||||
pub network_id: Mutex<String>,
|
||||
}
|
||||
|
||||
impl Peer {
|
||||
pub fn new(id: &str) -> Self {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
id: id.to_string(),
|
||||
// id: Mutex::new(id.to_string()),
|
||||
dev_addr: IpSubnet::new(0, 0),
|
||||
sock: Mutex::new(SdlanSock {
|
||||
family: 0,
|
||||
@ -45,6 +49,7 @@ impl Peer {
|
||||
last_seen: AtomicU64::new(0),
|
||||
last_p2p: AtomicU64::new(0),
|
||||
last_send_query: AtomicU64::new(0),
|
||||
last_valid_timestamp: AtomicU64::new(0),
|
||||
network_id: Mutex::new(String::new()),
|
||||
}
|
||||
}
|
||||
@ -132,39 +137,48 @@ pub struct SdlanSock {
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::utils::SDLanError;
|
||||
pub struct PeerMap {
|
||||
pub peers: DashMap<String, Arc<Peer>>,
|
||||
// ip to peer's uuid, the ip is the logical ip
|
||||
// of tun
|
||||
pub peer_ip_map: DashMap<u32, String>,
|
||||
/// peer's read only sdlansock
|
||||
/// if a peer's sock changes, just remove
|
||||
/// and re-insert it.
|
||||
pub peer_sock: DashMap<String, SdlanSock>,
|
||||
// pub peer_ip_map: DashMap<u32, String>,
|
||||
|
||||
// peer's read only sdlansock
|
||||
// if a peer's sock changes, just remove
|
||||
// and re-insert it.
|
||||
// pub peer_sock: DashMap<String, SdlanSock>,
|
||||
}
|
||||
|
||||
impl PeerMap {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
peers: DashMap::new(),
|
||||
peer_ip_map: DashMap::new(),
|
||||
peer_sock: DashMap::new(),
|
||||
// peer_ip_map: DashMap::new(),
|
||||
// peer_sock: DashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_peer(&self, data: Arc<Peer>) {
|
||||
let ip = data.dev_addr.net_addr.load(Ordering::Relaxed);
|
||||
if ip != 0 {
|
||||
let id = data.id.clone();
|
||||
self.peer_ip_map.insert(ip, id);
|
||||
}
|
||||
self.peers.insert(data.id.clone(), data);
|
||||
pub fn insert_peer(&self, id: &str, data: Arc<Peer>) {
|
||||
self.peers.insert(id.to_string(), data);
|
||||
}
|
||||
|
||||
pub fn peer_match<F: Fn(&Arc<Peer>) -> bool>(&self, id: &str, f: F) -> Option<Arc<Peer>> {
|
||||
pub fn peer_id_change(&self, oldid: &str, newid: &str) {
|
||||
if let Some((_, v)) = self.peers.remove(oldid) {
|
||||
// *v.id.lock().unwrap() = newid.to_string();
|
||||
self.peers.insert(newid.to_string(), v);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn peer_match<F: Fn(&Arc<Peer>) -> bool>(
|
||||
&self,
|
||||
id: &str,
|
||||
f: F,
|
||||
) -> Option<(String, Arc<Peer>)> {
|
||||
if let Some(v) = self.peers.get(id) {
|
||||
if f(&v) {
|
||||
return Some(v.clone());
|
||||
return Some((v.key().to_owned(), v.clone()));
|
||||
}
|
||||
}
|
||||
None
|
||||
@ -202,14 +216,14 @@ mod test {
|
||||
fn test_peer_map() {
|
||||
let id = gen_uuid();
|
||||
let pm = PeerMap::new();
|
||||
let mut p = Peer::new(&id);
|
||||
let mut p = Peer::new();
|
||||
{
|
||||
let mut sl = p.sock.lock().unwrap();
|
||||
sl.family = 0;
|
||||
sl.v4 = [1; 4];
|
||||
sl.port = 20;
|
||||
}
|
||||
pm.insert_peer(Arc::new(p));
|
||||
pm.insert_peer(&id, Arc::new(p));
|
||||
if let Some(p2) = pm.find_peer_by_id(&id) {
|
||||
let pl = p2.sock.lock().unwrap();
|
||||
assert_eq!(pl.family, 0);
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
use dashmap::DashMap;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc::UnboundedReceiver;
|
||||
|
||||
pub struct MyDashMap<T>(DashMap<String, Arc<T>>);
|
||||
|
||||
@ -27,3 +29,33 @@ impl<T> MyDashMap<T> {
|
||||
self.0.clear()
|
||||
}
|
||||
}
|
||||
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
pub fn get_current_timestamp() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs()
|
||||
}
|
||||
|
||||
use super::{gen_uuid, Result};
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{Read, Write};
|
||||
|
||||
pub fn create_or_load_uuid() -> Result<String> {
|
||||
let filepath = Path::new("./.id");
|
||||
if filepath.exists() {
|
||||
let mut result = String::new();
|
||||
File::open(filepath)?.read_to_string(&mut result)?;
|
||||
let result = result.trim().to_string();
|
||||
return Ok(result);
|
||||
} else {
|
||||
let uuid = gen_uuid();
|
||||
OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.open(filepath)?
|
||||
.write_all(uuid.as_bytes())?;
|
||||
return Ok(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user