with_context on LightSync

This commit is contained in:
Robert Habermeier 2017-02-02 17:02:46 +01:00
parent fd44dcda15
commit 3b9ac7648c
3 changed files with 14 additions and 4 deletions

View File

@ -674,6 +674,16 @@ impl LightSync {
subprotocol_name: params.subprotocol_name,
})
}
/// Execute a closure with a protocol context.
pub fn with_context<F, T>(&self, f: F) -> Option<T>
where F: FnOnce(&::light::net::BasicContext) -> T
{
self.network.with_context_eval(
self.subprotocol_name,
move |ctx| self.proto.with_context(ctx, f),
)
}
}
impl ManageNetwork for LightSync {

View File

@ -983,14 +983,14 @@ impl Host {
self.nodes.write().update(node_changes, &*self.reserved_nodes.read());
}
pub fn with_context<F>(&self, protocol: ProtocolId, io: &IoContext<NetworkIoMessage>, action: F) where F: Fn(&NetworkContext) {
pub fn with_context<F>(&self, protocol: ProtocolId, io: &IoContext<NetworkIoMessage>, action: F) where F: FnOnce(&NetworkContext) {
let reserved = { self.reserved_nodes.read() };
let context = NetworkContext::new(io, protocol, None, self.sessions.clone(), &reserved);
action(&context);
}
pub fn with_context_eval<F, T>(&self, protocol: ProtocolId, io: &IoContext<NetworkIoMessage>, action: F) -> T where F: Fn(&NetworkContext) -> T {
pub fn with_context_eval<F, T>(&self, protocol: ProtocolId, io: &IoContext<NetworkIoMessage>, action: F) -> T where F: FnOnce(&NetworkContext) -> T {
let reserved = { self.reserved_nodes.read() };
let context = NetworkContext::new(io, protocol, None, self.sessions.clone(), &reserved);

View File

@ -177,7 +177,7 @@ impl NetworkService {
}
/// Executes action in the network context
pub fn with_context<F>(&self, protocol: ProtocolId, action: F) where F: Fn(&NetworkContext) {
pub fn with_context<F>(&self, protocol: ProtocolId, action: F) where F: FnOnce(&NetworkContext) {
let io = IoContext::new(self.io_service.channel(), 0);
let host = self.host.read();
if let Some(ref host) = host.as_ref() {
@ -186,7 +186,7 @@ impl NetworkService {
}
/// Evaluates function in the network context
pub fn with_context_eval<F, T>(&self, protocol: ProtocolId, action: F) -> Option<T> where F: Fn(&NetworkContext) -> T {
pub fn with_context_eval<F, T>(&self, protocol: ProtocolId, action: F) -> Option<T> where F: FnOnce(&NetworkContext) -> T {
let io = IoContext::new(self.io_service.channel(), 0);
let host = self.host.read();
host.as_ref().map(|ref host| host.with_context_eval(protocol, &io, action))