Minor test tweaks and code cleanup

This commit is contained in:
arkpar 2016-02-16 23:33:32 +01:00
parent fbe06d3f2f
commit 7e0dfb41d0
2 changed files with 5 additions and 22 deletions

View File

@ -544,6 +544,7 @@ mod tests {
for _ in 0..1200 { for _ in 0..1200 {
discovery.add_node(NodeEntry { id: NodeId::random(), endpoint: ep.clone() }); discovery.add_node(NodeEntry { id: NodeId::random(), endpoint: ep.clone() });
} }
assert!(Discovery::nearest_node_entries(&NodeId::new(), &discovery.node_buckets).len() <= 16);
let removed = discovery.check_expired(true).len(); let removed = discovery.check_expired(true).len();
assert!(removed > 0); assert!(removed > 0);
} }

View File

@ -534,42 +534,24 @@ impl<Message> Host<Message> where Message: Send + Sync + Clone {
} }
fn handshake_writable(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) { fn handshake_writable(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
let mut create_session = false;
let mut kill = false;
let handshake = { self.handshakes.read().unwrap().get(token).cloned() }; let handshake = { self.handshakes.read().unwrap().get(token).cloned() };
if let Some(handshake) = handshake { if let Some(handshake) = handshake {
let mut h = handshake.lock().unwrap(); let mut h = handshake.lock().unwrap();
if let Err(e) = h.writable(io, &self.info.read().unwrap()) { if let Err(e) = h.writable(io, &self.info.read().unwrap()) {
debug!(target: "net", "Handshake write error: {}:{:?}", token, e); debug!(target: "net", "Handshake write error: {}:{:?}", token, e);
kill = true;
} }
if h.done() {
create_session = true;
}
}
if kill {
self.kill_connection(token, io, true); //TODO: mark connection as dead an check in kill_connection
return;
} else if create_session {
self.start_session(token, io);
io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
} }
} }
fn session_writable(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) { fn session_writable(&self, token: StreamToken, io: &IoContext<NetworkIoMessage<Message>>) {
let mut kill = false;
let session = { self.sessions.read().unwrap().get(token).cloned() }; let session = { self.sessions.read().unwrap().get(token).cloned() };
if let Some(session) = session { if let Some(session) = session {
let mut s = session.lock().unwrap(); let mut s = session.lock().unwrap();
if let Err(e) = s.writable(io, &self.info.read().unwrap()) { if let Err(e) = s.writable(io, &self.info.read().unwrap()) {
debug!(target: "net", "Session write error: {}:{:?}", token, e); debug!(target: "net", "Session write error: {}:{:?}", token, e);
kill = true;
} }
io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e)); io.update_registration(token).unwrap_or_else(|e| debug!(target: "net", "Session registration error: {:?}", e));
} }
if kill {
self.kill_connection(token, io, true); //TODO: mark connection as dead an check in kill_connection
}
} }
fn connection_closed(&self, token: TimerToken, io: &IoContext<NetworkIoMessage<Message>>) { fn connection_closed(&self, token: TimerToken, io: &IoContext<NetworkIoMessage<Message>>) {
@ -793,7 +775,7 @@ impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Messa
if let Some(node_changes) = self.discovery.as_ref().unwrap().lock().unwrap().readable() { if let Some(node_changes) = self.discovery.as_ref().unwrap().lock().unwrap().readable() {
self.update_nodes(io, node_changes); self.update_nodes(io, node_changes);
} }
io.update_registration(DISCOVERY).expect("Error updating disicovery registration"); io.update_registration(DISCOVERY).expect("Error updating discovery registration");
}, },
TCP_ACCEPT => self.accept(io), TCP_ACCEPT => self.accept(io),
_ => panic!("Received unknown readable token"), _ => panic!("Received unknown readable token"),
@ -806,7 +788,7 @@ impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Messa
FIRST_HANDSHAKE ... LAST_HANDSHAKE => self.handshake_writable(stream, io), FIRST_HANDSHAKE ... LAST_HANDSHAKE => self.handshake_writable(stream, io),
DISCOVERY => { DISCOVERY => {
self.discovery.as_ref().unwrap().lock().unwrap().writable(); self.discovery.as_ref().unwrap().lock().unwrap().writable();
io.update_registration(DISCOVERY).expect("Error updating disicovery registration"); io.update_registration(DISCOVERY).expect("Error updating discovery registration");
} }
_ => panic!("Received unknown writable token"), _ => panic!("Received unknown writable token"),
} }
@ -819,13 +801,13 @@ impl<Message> IoHandler<NetworkIoMessage<Message>> for Host<Message> where Messa
FIRST_HANDSHAKE ... LAST_HANDSHAKE => self.connection_timeout(token, io), FIRST_HANDSHAKE ... LAST_HANDSHAKE => self.connection_timeout(token, io),
DISCOVERY_REFRESH => { DISCOVERY_REFRESH => {
self.discovery.as_ref().unwrap().lock().unwrap().refresh(); self.discovery.as_ref().unwrap().lock().unwrap().refresh();
io.update_registration(DISCOVERY).expect("Error updating disicovery registration"); io.update_registration(DISCOVERY).expect("Error updating discovery registration");
}, },
DISCOVERY_ROUND => { DISCOVERY_ROUND => {
if let Some(node_changes) = self.discovery.as_ref().unwrap().lock().unwrap().round() { if let Some(node_changes) = self.discovery.as_ref().unwrap().lock().unwrap().round() {
self.update_nodes(io, node_changes); self.update_nodes(io, node_changes);
} }
io.update_registration(DISCOVERY).expect("Error updating disicovery registration"); io.update_registration(DISCOVERY).expect("Error updating discovery registration");
}, },
_ => match self.timers.read().unwrap().get(&token).cloned() { _ => match self.timers.read().unwrap().get(&token).cloned() {
Some(timer) => match self.handlers.read().unwrap().get(timer.protocol).cloned() { Some(timer) => match self.handlers.read().unwrap().get(timer.protocol).cloned() {