Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change multi to filter #312

Merged
merged 2 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,3 @@ jobs:
name: Bench
script:
- make bench_p2p
- stage: Test
name: Fuzz
script:
- rustup install nightly
- cargo +nightly install cargo-fuzz
- make fuzz
35 changes: 19 additions & 16 deletions tentacle/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,20 +545,20 @@ where
}
}
// Send data to the specified protocol for the specified sessions.
TargetSession::Multi(ids) => {
for id in ids {
TargetSession::Filter(filter) => self
.sessions
.iter_mut()
.filter(|(id, _)| filter(id))
.for_each(|(id, control)| {
debug!(
"send message to session [{}], proto [{}], data len: {}",
id,
proto_id,
data.len()
);
if let Some(control) = self.sessions.get_mut(&id) {
control.push_message(proto_id, priority, data.clone());
control.try_send(cx);
}
}
}
control.push_message(proto_id, priority, data.clone());
control.try_send(cx);
}),
// Broadcast data for a specified protocol.
TargetSession::All => {
debug!(
Expand Down Expand Up @@ -775,11 +775,11 @@ where
session.open_proto_stream(&meta.name());
}
}
TargetProtocol::Multi(proto_ids) => proto_ids.into_iter().for_each(|id| {
if let Some(meta) = self.protocol_configs.get(&id) {
session.open_proto_stream(&meta.name());
}
}),
TargetProtocol::Filter(filter) => self
.protocol_configs
.iter()
.filter(|(id, _)| filter(id))
.for_each(|(_, meta)| session.open_proto_stream(&meta.name())),
}
}

Expand Down Expand Up @@ -1149,9 +1149,12 @@ where
}
}
TargetProtocol::Single(id) => self.protocol_open(cx, session_id, id),
TargetProtocol::Multi(ids) => ids
.into_iter()
.for_each(|id| self.protocol_open(cx, session_id, id)),
TargetProtocol::Filter(filter) => {
let ids = self.protocol_configs.keys().copied().collect::<Vec<_>>();
ids.into_iter()
.filter(filter)
.for_each(|id| self.protocol_open(cx, session_id, id))
}
},
ServiceTask::ProtocolClose {
session_id,
Expand Down
10 changes: 4 additions & 6 deletions tentacle/src/service/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ impl Default for SessionConfig {
}

/// When dial, specify which protocol want to open
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum TargetProtocol {
/// Try open all protocol
All,
/// Try open one protocol
Single(ProtocolId),
/// Try open some protocol
Multi(Vec<ProtocolId>),
/// Try open some protocol, if return true, open it
Filter(Box<dyn Fn(&ProtocolId) -> bool + Send>),
}

impl From<ProtocolId> for TargetProtocol {
Expand All @@ -96,14 +95,13 @@ impl From<usize> for TargetProtocol {
}

/// When sending a message, select the specified session
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum TargetSession {
/// Try broadcast
All,
/// Try send to only one
Single(SessionId),
/// Try send to some session
Multi(Vec<SessionId>),
/// Try send to some session, if return true, send to it
Filter(Box<dyn Fn(&SessionId) -> bool + Send>),
}

impl From<SessionId> for TargetSession {
Expand Down
16 changes: 4 additions & 12 deletions tentacle/src/service/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,9 @@ impl fmt::Debug for ServiceTask {
use self::ServiceTask::*;

match self {
ProtocolMessage {
target,
proto_id,
data,
} => write!(
f,
"id: {:?}, proto_id: {}, message: {:?}",
target, proto_id, data
),
ProtocolMessage { proto_id, data, .. } => {
write!(f, "proto_id: {}, message: {:?}", proto_id, data)
}
SetProtocolNotify {
proto_id, token, ..
} => write!(f, "set protocol({}) notify({})", proto_id, token),
Expand Down Expand Up @@ -235,9 +229,7 @@ impl fmt::Debug for ServiceTask {
Disconnect { session_id } => write!(f, "Disconnect session [{}]", session_id),
Dial { address, .. } => write!(f, "Dial address: {}", address),
Listen { address } => write!(f, "Listen address: {}", address),
ProtocolOpen { session_id, target } => {
write!(f, "Open session [{}] proto [{:?}]", session_id, target)
}
ProtocolOpen { session_id, .. } => write!(f, "Open session [{}] proto", session_id),
ProtocolClose {
session_id,
proto_id,
Expand Down