diff --git a/cmd/root.go b/cmd/root.go index eb39a493335bcca837cd421b123078b29ee87fdd..dc91e38df92e4d225faac5983ed63d447e2f13be 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -157,17 +157,28 @@ var rootCmd = &cobra.Command{ jww.FATAL.Panicf("%+v", err) } + time.Sleep(10 * time.Second) + + // Wait until connected or crash on timeout + connected := make(chan bool, 1) + client.GetHealth().AddChannel(connected) + waitTimeout := time.Duration(viper.GetUint("waitTimeout")) + timeoutTick := time.NewTicker(waitTimeout * time.Second) + isConnected := false + for !isConnected { + select { + case isConnected = <-connected: + jww.INFO.Printf("health status: %b\n", + isConnected) + break + case <-timeoutTick.C: + jww.FATAL.Panic("timeout on connection") + } + } + // Send Messages msgBody := viper.GetString("message") - recipientIDBytes, err := hex.DecodeString( - viper.GetString("destid")) - if err != nil { - jww.FATAL.Panicf("%+v", err) - } - recipientID, err := id.Unmarshal(recipientIDBytes) - if err != nil { - jww.FATAL.Panicf("%+v", err) - } + recipientID := getUIDFromString(viper.GetString("destid")) msg := client.NewCMIXMessage(recipientID, []byte(msgBody)) params := params.GetDefaultCMIX() @@ -175,19 +186,25 @@ var rootCmd = &cobra.Command{ sendCnt := int(viper.GetUint("sendCount")) sendDelay := time.Duration(viper.GetUint("sendDelay")) for i := 0; i < sendCnt; i++ { - client.SendCMIX(msg, params) + fmt.Printf("Sending to %s: %s\n", recipientID, msgBody) + roundID, err := client.SendCMIX(msg, params) + if err != nil { + jww.FATAL.Panicf("%+v", err) + } + jww.INFO.Printf("RoundID: %d\n", roundID) time.Sleep(sendDelay * time.Millisecond) } // Wait until message timeout or we receive enough then exit // TODO: Actually check for how many messages we've received receiveCnt := viper.GetUint("receiveCount") - waitTimeout := time.Duration(viper.GetUint("waitTimeout")) - timeoutTick := time.NewTicker(waitTimeout * time.Second) - for { + timeoutTick = time.NewTicker(waitTimeout * time.Second) + done := false + for !done { select { case <-timeoutTick.C: fmt.Println("Timed out!") + done = true break } } @@ -195,6 +212,19 @@ var rootCmd = &cobra.Command{ }, } +func getUIDFromString(idStr string) *id.ID { + idBytes, err := hex.DecodeString(fmt.Sprintf("%0*d%s", + 66-len(idStr), 0, idStr)) + if err != nil { + jww.FATAL.Panicf("%+v", err) + } + ID, err := id.Unmarshal(idBytes) + if err != nil { + jww.FATAL.Panicf("%+v", err) + } + return ID +} + func initLog(verbose bool, logPath string) { if logPath != "-" && logPath != "" { // Disable stdout output diff --git a/network/health/tracker.go b/network/health/tracker.go index 3a297ba9bb26a68f8e570d4762b840cac491684e..4961bf61534511668aaff490f53925f99e532b4a 100644 --- a/network/health/tracker.go +++ b/network/health/tracker.go @@ -54,13 +54,22 @@ func newTracker(timeout time.Duration) *Tracker { // Add a channel to the list of Tracker channels // such that each channel can be notified of network changes func (t *Tracker) AddChannel(c chan bool) { + t.mux.Lock() t.channels = append(t.channels, c) + t.mux.Unlock() + select { + case c <- t.IsHealthy(): + default: + } } // Add a function to the list of Tracker function // such that each function can be run after network changes func (t *Tracker) AddFunc(f func(isHealthy bool)) { + t.mux.Lock() t.funcs = append(t.funcs, f) + t.mux.Unlock() + go f(t.IsHealthy()) } func (t *Tracker) IsHealthy() bool { @@ -115,6 +124,7 @@ func (t *Tracker) start(quitCh <-chan struct{}) { // Handle thread kill break case heartbeat = <-t.heartbeat: + jww.INFO.Printf("heartbeat: %+v", heartbeat) if healthy(heartbeat) { timerChan = time.NewTimer(t.timeout).C t.setHealth(true) diff --git a/network/message/sendCmix.go b/network/message/sendCmix.go index 6d1cfd85bf599af221cac19eb7a22a306b053380..c2dd6151c9e7409d46953c2b45aac6a118932d4f 100644 --- a/network/message/sendCmix.go +++ b/network/message/sendCmix.go @@ -39,7 +39,7 @@ func (m *Manager) SendCMIX(msg format.Message, param params.CMIX) (id.Round, err //build the topology idList, err := id.NewIDListFromBytes(bestRound.Topology) - if err == nil { + if err != nil { jww.ERROR.Printf("Failed to use topology for round %v: %s", bestRound.ID, err) continue }