Skip to content

Commit

Permalink
update use new chamomile's message type
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhuachuang committed Sep 1, 2020
1 parent 88a5505 commit 010fac7
Show file tree
Hide file tree
Showing 9 changed files with 515 additions and 324 deletions.
740 changes: 437 additions & 303 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ serde_json = "1"
bincode = "1"
lazy_static = "1"
toml = "0.5"
sha3 = "0.8"
sha3 = "0.9"
rand = "0.7"
dirs = "2"
log = "0.4"
httparse = "1"
mime = "0.3"
async-tungstenite = "0.4"
chamomile = "0.1.1"
# chamomile = { path = "../chamomile" }

[dev-dependencies]
tdn_permission = { git = "https://github.com/cypherlink/TDN-Permission.git" }
#tdn_permission = { path = "../TDN-Permission" } # Dev
# chamomile = "0.1.1"
chamomile = { path = "../chamomile" }
34 changes: 34 additions & 0 deletions examples/app_permissionless.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use async_std::task;
use tdn::prelude::*;
use tdn_permission::PermissionlessGroup;

fn main() {
task::block_on(async {
let mut group = PermissionlessGroup::default();
let (peer_addr, send, out_recv) = start().await.unwrap();
println!("Example: peer id: {}", peer_addr.short_show());

while let Ok(message) = out_recv.recv().await {
match message {
ReceiveMessage::Group(msg) => match msg {
GroupReceiveMessage::PeerJoin(peer, addr, data) => {
group.join(peer, addr, data, send.clone()).await;
}
GroupReceiveMessage::PeerJoinResult(..) => {
//
}
GroupReceiveMessage::PeerLeave(peer) => {
group.leave(&peer);
}
GroupReceiveMessage::Event(peer, _data) => {
println!("receive group event from {}", peer.short_show());
}
GroupReceiveMessage::Stream(..) => {
//
}
},
_ => {}
}
}
});
}
20 changes: 20 additions & 0 deletions examples/app_with_config_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use async_std::task;
use std::path::PathBuf;
use tdn::prelude::*;

fn main() {
task::block_on(async {
// use crate root directory's config.toml
let dir_path = PathBuf::from(".");
let config = Config::load_with_path(dir_path).await;

let (peer_addr, _send, out_recv) = start_with_config(config).await.unwrap();
println!("Example: peer id: {}", peer_addr.short_show());

while let Ok(message) = out_recv.recv().await {
match message {
_ => {}
}
}
});
}
2 changes: 0 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use async_std::task;
use tdn::prelude::*;
use tdn_permission::PermissionlessGroup;

struct State(u32);

fn main() {
task::block_on(async {
let mut group = PermissionlessGroup::default();
let (peer_addr, send, out_recv) = start().await.unwrap();
println!("Example: peer id: {}", peer_addr.short_show());

Expand Down
13 changes: 11 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::storage::read_string_absolute_file;
pub struct Config {
pub db_path: Option<PathBuf>,
pub group_id: GroupId,
pub permission: bool,

pub p2p_addr: SocketAddr,
pub p2p_join_data: Vec<u8>,
Expand Down Expand Up @@ -47,6 +48,7 @@ impl Config {
db_path,
group_id: _, // DEBUG Not used ?

permission,
p2p_addr,
p2p_join_data,
p2p_transport,
Expand Down Expand Up @@ -81,6 +83,7 @@ impl Config {
black_list: p2p_black_list,
white_peer_list: p2p_white_peer_list,
black_peer_list: p2p_black_peer_list,
permission: permission,
};

let layer_config = LayerConfig {
Expand Down Expand Up @@ -108,6 +111,7 @@ impl Config {
Config {
db_path: None,
group_id: GroupId::default(),
permission: false, //default is permissionless
p2p_addr: p2p_addr,
p2p_join_data: vec![],
p2p_transport: P2P_TRANSPORT.to_owned(),
Expand Down Expand Up @@ -151,13 +155,16 @@ impl Config {
}

pub async fn load_with_path(path: PathBuf) -> Self {
let string = load_file_string(path).await;
let string = load_file_string(path.clone()).await;
match string {
Ok(string) => {
let raw_config: RawConfig = toml::from_str(&string).unwrap();
raw_config.parse()
}
Err(_) => Config::default(),
Err(_err) => {
warn!("File {:?} not found, use the config default.", path);
Config::default()
}
}
}

Expand Down Expand Up @@ -192,6 +199,7 @@ pub struct RawConfig {
pub db_path: Option<PathBuf>,
pub group_id: Option<String>,
pub group_symbol: Option<String>,
pub permission: Option<bool>,

pub p2p_addr: Option<SocketAddr>,
pub p2p_join_data: Option<String>,
Expand Down Expand Up @@ -248,6 +256,7 @@ impl RawConfig {
.map(|s| GroupId::from_symbol(s))
.unwrap_or(GroupId::default()),
),
permission: self.permission.unwrap_or(false),
p2p_addr: self.p2p_addr.unwrap_or(P2P_ADDR.parse().unwrap()),
p2p_join_data: self
.p2p_join_data
Expand Down
6 changes: 3 additions & 3 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use crate::primitive::{Broadcast, GroupId, PeerAddr, RpcParam, StreamType};
pub enum GroupSendMessage {
/// when need add a peer, send to TDN from outside.
/// params is `peer_id`, `socket_addr` and peer `join_info`.
PeerJoin(PeerAddr, SocketAddr, Vec<u8>),
PeerConnect(PeerAddr, Option<SocketAddr>, Vec<u8>),
/// when outside want to close a connectioned peer. use it force close.
/// params is `peer_id`.
PeerLeave(PeerAddr),
PeerDisconnect(PeerAddr),
/// when peer request for join, outside decide connect or not.
/// params is `peer_id`, `is_connect`, `is_force_close`, `result info`.
/// if `is_connect` is true, it will add to white directly list.
Expand All @@ -30,7 +30,7 @@ pub enum GroupSendMessage {
/// when need send a data to a peer, only need know the peer_id,
/// the TDN will help you send data to there.
/// params is `peer_id` and `data_bytes`.
Event(PeerAddr, Vec<u8>),
Data(PeerAddr, Vec<u8>),
/// when need broadcast a data to all network, TDN support some
/// common algorithm, use it, donnot worry.
/// params is `broadcast_type` and `data_bytes`
Expand Down
10 changes: 5 additions & 5 deletions src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ async fn run_listen<M: GroupMessage>(
msg = self_recv.recv().fuse() => match msg {
Ok(msg) => {
match msg {
GroupSendMessage::PeerJoin(peer_addr, addr, data) => {
p2p_send.send(SendMessage::PeerJoin(peer_addr, addr, data)).await;
GroupSendMessage::PeerConnect(peer_addr, addr, data) => {
p2p_send.send(SendMessage::PeerConnect(peer_addr, addr, data)).await;
},
GroupSendMessage::PeerLeave(peer_addr) => {
p2p_send.send(SendMessage::PeerLeave(peer_addr)).await;
GroupSendMessage::PeerDisconnect(peer_addr) => {
p2p_send.send(SendMessage::PeerDisconnect(peer_addr)).await;
},
GroupSendMessage::PeerJoinResult(peer_addr, is_ok, is_force, result) => {
p2p_send.send(SendMessage::PeerJoinResult(
Expand All @@ -94,7 +94,7 @@ async fn run_listen<M: GroupMessage>(
GroupSendMessage::DisConnect(addr) => {
p2p_send.send(SendMessage::DisConnect(addr)).await;
},
GroupSendMessage::Event(peer_addr, data) => {
GroupSendMessage::Data(peer_addr, data) => {
debug!("DEBUG: Outside Event Length: {}", data.len());
p2p_send.send(SendMessage::Data(peer_addr, data)).await;
},
Expand Down
4 changes: 2 additions & 2 deletions src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ impl GroupId {
pub fn from_symbol(s: impl ToString) -> GroupId {
let s = s.to_string();
let mut sha = Sha3_256::new();
sha.input(&s);
sha.update(&s);
let mut peer_bytes = [0u8; 32];
peer_bytes.copy_from_slice(&sha.result()[..]);
peer_bytes.copy_from_slice(&sha.finalize()[..]);
GroupId(peer_bytes)
}

Expand Down

0 comments on commit 010fac7

Please sign in to comment.