diff --git a/cmd/root.go b/cmd/root.go index 6dc53246ef0270e0fa0a498e565a33f00bd14794..79c914a57f885692ff0a1952efc5673098564a3a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -198,6 +198,7 @@ var rootCmd = &cobra.Command{ go func(i int) { defer wg.Done() fmt.Printf("Sending to %s: %s\n", recipientID, msgBody) + sendLoop: var roundIDs []id.Round var roundTimeout time.Duration if unsafe { @@ -213,21 +214,57 @@ var rootCmd = &cobra.Command{ jww.FATAL.Panicf("%+v", err) } - // Construct the callback function which prints out the rounds' results - f := func(allRoundsSucceeded, timedOut bool, - rounds map[id.Round]api.RoundResult) { - printRoundResults(allRoundsSucceeded, timedOut, rounds, roundIDs, msg) - } - - // Have the client report back the round results - err = errors.New("derp") - for j := 0; j < 5 && err != nil; j++ { + retryChan := make(chan struct{}) + done := make(chan struct{}, 1) + if viper.GetBool("verify-sends") { // Verify message sends were successful + // Construct the callback function which + // verifies successful message send or retries + f := func(allRoundsSucceeded, timedOut bool, + rounds map[id.Round]api.RoundResult) { + if !allRoundsSucceeded { + retryChan <- struct{}{} + } else { + done <- struct{}{} + } + } + + // Monitor rounds for results err = client.GetRoundResults(roundIDs, roundTimeout, f) + if err != nil { + jww.DEBUG.Printf("Could not verify messages were sent successfully, resending messages...") + goto sendLoop + } + + select { + case <-retryChan: + // On a retry, go to the top of the loop + jww.DEBUG.Printf("Could not verify messages were sent successfully, resending messages...") + goto sendLoop + case <-done: + break + } + + } else { // Does not verify successful sends + // Construct the callback function which prints out the rounds' results + f := func(allRoundsSucceeded, timedOut bool, + rounds map[id.Round]api.RoundResult) { + printRoundResults(allRoundsSucceeded, timedOut, rounds, roundIDs, msg) + } + + // Have the client report back the round results + err = errors.New("derp") + for j := 0; j < 5 && err != nil; j++ { + err = client.GetRoundResults(roundIDs, roundTimeout, f) + } + + if err != nil { + jww.FATAL.Panicf("Message sending for send %d failed: %+v", i, err) + } } - if err != nil { - jww.FATAL.Panicf("Message sending for send %d failed: %+v", i, err) - } + close(done) + close(retryChan) + }(i) } }()