-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
protocols/dcutr/example: Wait for relay to accept reservation request #2642
Changes from 2 commits
665913f
2abd894
1128711
75697c2
6b86cff
dc28fb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,25 +197,41 @@ fn main() -> Result<(), Box<dyn Error>> { | |
} | ||
} | ||
|
||
// Wait till connected to relay to learn external address. | ||
// Wait till connected to relay to learn external address. In case we are in listening mode, | ||
// wait for the relay to accept our reservation request. | ||
block_on(async { | ||
let mut learned_observed_addr = false; | ||
let mut relay_accepted_reservation = false; | ||
|
||
loop { | ||
match swarm.next().await.unwrap() { | ||
SwarmEvent::NewListenAddr { .. } => {} | ||
SwarmEvent::Dialing { .. } => {} | ||
SwarmEvent::ConnectionEstablished { .. } => {} | ||
SwarmEvent::Behaviour(Event::Ping(_)) => {} | ||
SwarmEvent::Behaviour(Event::Relay(client::Event::ReservationReqAccepted { | ||
.. | ||
})) => { | ||
info!("Relay accepted our reservation request."); | ||
relay_accepted_reservation = true | ||
} | ||
SwarmEvent::Behaviour(Event::Relay(_)) => {} | ||
SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Sent { .. })) => {} | ||
SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Received { | ||
info: IdentifyInfo { observed_addr, .. }, | ||
.. | ||
})) => { | ||
info!("Observed address: {:?}", observed_addr); | ||
break; | ||
info!("Relay observes us under the address: {:?}", observed_addr); | ||
learned_observed_addr = true; | ||
} | ||
event => panic!("{:?}", event), | ||
} | ||
|
||
if learned_observed_addr | ||
&& (matches!(opts.mode, Mode::Dial) || relay_accepted_reservation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this be easier to understand if we matched explicitly on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of 75697c2? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much better, thank you! |
||
{ | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also catch the case of
client::Event::ReservationReqFailed
and return an error in that case.Shouldn't happen in practice but I'd rather have an explicit error than a non-terminating program.
Alternatively, I think there is no valid case in which any other relay event should occur anyway/ So we could remove the match block here and let it fall to the blank panic below.