Skip to content

Commit e11b91c

Browse files
authored
Merge pull request #10 from sine-fdn/connection-error-msg-fixes
Connection error msg fixes
2 parents fbcc064 + a60169f commit e11b91c

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/main.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,20 @@ async fn main() -> Result<(), Box<dyn Error>> {
219219
let mut result = None;
220220

221221
loop {
222+
if let Phase::ConfirmingParticipants = phase {
223+
if swarm.behaviour().gossipsub.all_peers().count() == 0 {
224+
if result.is_none() {
225+
eprintln!("Not everyone agreed to participate, exiting without running the benchmark.");
226+
}
227+
std::process::exit(1);
228+
}
229+
}
222230
if let Phase::SendingShares = phase {
223231
if swarm.behaviour().gossipsub.all_peers().count() == 0 {
224-
std::process::exit(0);
232+
if result.is_none() {
233+
eprintln!("The benchmark was cancelled by one of the participants, exiting.");
234+
}
235+
std::process::exit(1);
225236
}
226237
if sent_shares.is_empty() {
227238
for public_key in participants.keys() {
@@ -451,21 +462,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
451462
(_, Event::Upnp(ev)) => info!("{ev:?}"),
452463
(Phase::WaitingForParticipants, Event::ConnectionClosed(peer_id)) => {
453464
if result.is_none() {
454-
let Some(disconnected) =
465+
let Some((_, (disconnected, _))) =
455466
participants.iter().find(|(_, (_, id))| *id == peer_id)
456467
else {
457-
println!("Connection error, please try again.");
458-
std::process::exit(1);
468+
continue;
459469
};
460470

461-
let disconnected = disconnected.1 .0.clone();
462-
463471
println!("\nParticipant {disconnected} disconnected");
464472

465473
if swarm.connected_peers().count() == 0 && is_leader {
466474
participants.retain(|_, (_, id)| *id != peer_id);
467475
} else if is_leader {
468-
let msg = Msg::Quit(peer_id, disconnected).serialize()?;
476+
let msg = Msg::Quit(peer_id, disconnected.clone()).serialize()?;
469477
swarm
470478
.behaviour_mut()
471479
.gossipsub
@@ -539,7 +547,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
539547
);
540548
continue;
541549
}
542-
Msg::Quit(..) | Msg::Share { .. } => {},
550+
Msg::Quit(..) | Msg::Share { .. } => {}
543551
Msg::Sum(public_key, sum) => {
544552
if is_leader {
545553
sums.insert(public_key, sum);
@@ -551,6 +559,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
551559
}
552560
},
553561
(Phase::SendingShares, Event::ConnectionClosed(peer_id)) => {
562+
if result.is_some() {
563+
continue;
564+
}
554565
if is_leader {
555566
let Some((_, (disconnected, _))) =
556567
participants.iter().find(|(_, (_, id))| *id == peer_id)
@@ -559,11 +570,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
559570
std::process::exit(1);
560571
};
561572

562-
println!(
563-
"Aborting benchmark: participant {disconnected} left the while waiting for shares"
564-
);
573+
println!("Participant {disconnected} left, aborting the benchmark.");
565574
} else {
566-
println!("Aborting benchmark: a participant left while waiting for shares");
575+
println!("A participant left, aborting the benchmark.");
567576
}
568577
std::process::exit(1);
569578
}

0 commit comments

Comments
 (0)