Skip to content

Commit

Permalink
Added lookup_connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Schwerdel committed Nov 6, 2015
1 parent 76453f9 commit ee9a11f
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,7 @@ impl<M: Message, N: NodeId, I: InitMessage> Node<M, N, I> {
/// initialization messages have been exchanged.
///
/// For more details see [`connect(...)`](#method.connect).
pub fn connect_request<A: ToSocketAddrs>(&self,
addr: A)
-> Result<ConnectionRequest<M, N, I>, Error<N>> {
pub fn connect_request<A: ToSocketAddrs>(&self, addr: A) -> Result<ConnectionRequest<M, N, I>, Error<N>> {
if *self.closed.read().expect("Lock poisoned") {
return Err(Error::AlreadyClosed);
}
Expand Down Expand Up @@ -443,6 +441,22 @@ impl<M: Message, N: NodeId, I: InitMessage> Node<M, N, I> {
Ok(id)
}

/// Returns the node id for an address if connected
///
/// This method retrns the node id for an address if a connection to this address exists.
pub fn lookup_connection<A: ToSocketAddrs>(&self, addr: A) -> Option<N> {
let lock = self.connections.read().expect("Lock poisoned");
for (id, con) in &lock as &HashMap<N, Connection<M, N, I>> {
let sock_addr = con.get_address();
for a in addr.to_socket_addrs().unwrap() {
if a == sock_addr {
return Some(id.clone());
}
}
}
None
}

fn shutdown_socket(&self, socket: &TcpListener) -> Result<(), Error<N>> {
// TODO: Remove this workaround once a proper API is available
let socket = unsafe { mem::transmute::<&TcpListener, &TcpStream>(socket) };
Expand Down Expand Up @@ -645,6 +659,10 @@ impl<M: Message, N: NodeId, I: InitMessage> Connection<M, N, I> {
&self.node_id
}

fn get_address(&self) -> SocketAddr {
self.socket.lock().expect("Lock poisoned").peer_addr().unwrap()
}

fn stats(&self) -> ConnectionStats {
let reader_stats = self.reader_stats.read().expect("Lock poisoned");
let writer_stats = self.writer_stats.read().expect("Lock poisoned");
Expand Down

0 comments on commit ee9a11f

Please sign in to comment.