node: Move state transition code to session

This commit is contained in:
Alexis Sellier 2023-02-16 13:18:25 +01:00
parent 011923f1df
commit 71561a8e12
No known key found for this signature in database
2 changed files with 15 additions and 10 deletions

View File

@ -603,16 +603,8 @@ where
} }
if let Some(session) = self.sessions.get_mut(&remote) { if let Some(session) = self.sessions.get_mut(&remote) {
if let session::State::Connected { protocol, .. } = &mut session.state { // Transition session back to gossip protocol.
if let session::Protocol::Fetch { .. } = protocol { session.to_gossip();
*protocol = session::Protocol::default();
} else {
panic!(
"Unexpected session state for {}: expected 'fetch' protocol, got 'gossip'",
session.id
);
}
}
// Drain any messages in the session's outbox, which might have accumulated during the // Drain any messages in the session's outbox, which might have accumulated during the
// fetch, and send them to the peer. // fetch, and send them to the peer.
self.reactor.drain(session); self.reactor.drain(session);

View File

@ -250,6 +250,19 @@ impl Session {
*protocol = Protocol::Fetch { rid }; *protocol = Protocol::Fetch { rid };
} }
pub fn to_gossip(&mut self) {
if let State::Connected { protocol, .. } = &mut self.state {
if let Protocol::Fetch { .. } = protocol {
*protocol = Protocol::default();
} else {
panic!(
"Unexpected session state for {}: expected 'fetch' protocol, got 'gossip'",
self.id
);
}
}
}
pub fn to_connecting(&mut self) { pub fn to_connecting(&mut self) {
assert!( assert!(
self.is_disconnected(), self.is_disconnected(),