-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// | ||
// Copyright (c) 202 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use async_std::net::{SocketAddr, TcpListener, TcpStream}; | ||
use async_std::prelude::*; | ||
use async_std::task; | ||
use async_std::task::JoinHandle; | ||
use async_trait::async_trait; | ||
use std::collections::HashMap; | ||
use std::convert::TryInto; | ||
use std::fmt; | ||
use std::net::{IpAddr, Shutdown}; | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use std::sync::{Arc, RwLock}; | ||
use std::time::Duration; | ||
use zenoh_core::{zread, zwrite}; | ||
use zenoh_protocol::core::{EndPoint, Locator}; | ||
use zenoh_result::{bail, zerror, Error as ZError, ZResult}; | ||
use zenoh_sync::Signal; | ||
|
||
pub struct UnicastListener { | ||
endpoint: EndPoint, | ||
active: Arc<AtomicBool>, | ||
signal: Signal, | ||
handle: JoinHandle<ZResult<()>>, | ||
} | ||
|
||
impl UnicastListener { | ||
fn new( | ||
endpoint: EndPoint, | ||
active: Arc<AtomicBool>, | ||
signal: Signal, | ||
handle: JoinHandle<ZResult<()>>, | ||
) -> UnicastListener { | ||
UnicastListener { | ||
endpoint, | ||
active, | ||
signal, | ||
handle, | ||
} | ||
} | ||
} | ||
|
||
pub struct UnicastListeners { | ||
listeners: Arc<RwLock<HashMap<SocketAddr, UnicastListener>>>, | ||
} | ||
|
||
impl UnicastListeners { | ||
fn new() -> UnicastListeners { | ||
UnicastListeners { | ||
listeners: Arc::new(RwLock::new(HashMap::new())), | ||
} | ||
} | ||
|
||
async fn add_listener( | ||
&self, | ||
endpoint: EndPoint, | ||
addr: SocketAddr, | ||
active: Arc<AtomicBool>, | ||
signal: Signal, | ||
handle: JoinHandle<ZResult<()>>, | ||
) -> ZResult<()> { | ||
let mut listeners = zwrite!(self.listeners); | ||
let c_listeners = self.listeners.clone(); | ||
let c_addr = addr; | ||
let wraphandle = task::spawn(async move { | ||
// Wait for the accept loop to terminate | ||
let res = handle.await; | ||
zwrite!(c_listeners).remove(&c_addr); | ||
res | ||
}); | ||
|
||
let listener = UnicastListener::new(endpoint, active, signal, wraphandle); | ||
// Update the list of active listeners on the manager | ||
listeners.insert(addr, listener); | ||
Ok(()) | ||
} | ||
|
||
async fn del_listener(&self, addr: SocketAddr) -> ZResult<()> { | ||
// Stop the listener | ||
let listener = zwrite!(self.listeners).remove(&addr).ok_or_else(|| { | ||
zerror!( | ||
"Can not delete the listener because it has not been found: {}", | ||
addr | ||
) | ||
})?; | ||
|
||
// Send the stop signal | ||
listener.active.store(false, Ordering::Release); | ||
listener.signal.trigger(); | ||
listener.handle.await | ||
} | ||
|
||
fn get_endpoints(&self) -> Vec<EndPoint> { | ||
zread!(self.listeners) | ||
.values() | ||
.map(|l| l.endpoint.clone()) | ||
.collect() | ||
} | ||
|
||
fn get_locators(&self) -> Vec<Locator> { | ||
let mut locators = vec![]; | ||
|
||
let guard = zread!(self.listeners); | ||
for (key, value) in guard.iter() { | ||
let (kip, kpt) = (key.ip(), key.port()); | ||
|
||
// Either ipv4/0.0.0.0 or ipv6/[::] | ||
if kip.is_unspecified() { | ||
let mut addrs = match kip { | ||
IpAddr::V4(_) => zenoh_util::net::get_ipv4_ipaddrs(), | ||
IpAddr::V6(_) => zenoh_util::net::get_ipv6_ipaddrs(), | ||
}; | ||
let iter = addrs.drain(..).map(|x| { | ||
Locator::new( | ||
value.endpoint.protocol(), | ||
SocketAddr::new(x, kpt).to_string(), | ||
value.endpoint.metadata(), | ||
) | ||
.unwrap() | ||
}); | ||
locators.extend(iter); | ||
} else { | ||
locators.push(value.endpoint.to_locator()); | ||
} | ||
} | ||
|
||
locators | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters