func (h HealthChecker) Rcv(msg bh.Msg, ctx bh.RcvContext) error { db := msg.From() dict := ctx.Dict(driversDict) dict.ForEach(func(k string, v interface{}) { nd := v.(nodeDrivers) updated := false for i := range nd.Drivers { if nd.Drivers[i].BeeID == db { nd.Drivers[i].LastSeen = time.Now() // TODO(soheil): Maybe if outpings was more than MaxPings we // should emit a connected message. nd.Drivers[i].OutPings-- updated = true } } if !updated { return } if err := dict.Put(k, nd); err != nil { glog.Warningf("error in encoding drivers: %v", err) } }) return nil }
func (p *pinger) Rcv(msg bh.Msg, ctx bh.RcvContext) error { dict := ctx.Dict(PingPongDict) data := msg.Data() switch data := data.(type) { case ping: fmt.Printf("Rx Ping %d %v->%v\n", data.Seq, msg.From(), ctx.ID()) time.Sleep(300 * time.Millisecond) v, err := dict.Get("ping") var p ping if err == nil { p = v.(ping) } if data != p { return fmt.Errorf("Invalid ping: ping=%d, want=%d", data.Seq, p.Seq) } p.Seq += 1 dict.Put("ping", p) fmt.Printf("Ping stored to %v\n", p.Seq) if !msg.NoReply() { fmt.Printf("Tx Pong %d @ %v\n", data.pong().Seq, ctx.ID()) ctx.Emit(data.pong()) } case pong: fmt.Printf("Rx Pong %d %v->%v\n", data.Seq, msg.From(), ctx.ID()) time.Sleep(300 * time.Millisecond) dict := ctx.Dict(PingPongDict) v, err := dict.Get("pong") var p pong if err == nil { p = v.(pong) } if data != p { return fmt.Errorf("Invalid pong: pong=%d, want=%d", data.Seq, p.Seq) } p.Seq += 1 dict.Put("pong", p) fmt.Printf("Pong stored to %v\n", p.Seq) fmt.Printf("Tx Ping %d @ %v\n", data.ping().Seq, ctx.ID()) ctx.Emit(data.ping()) } return nil }