Esempio n. 1
0
func (p Poller) Rcv(msg bh.Msg, ctx bh.RcvContext) error {
	dict := ctx.Dict(driversDict)
	dict.ForEach(func(k string, v interface{}) {
		node := nom.UID(k)
		query := nom.FlowStatsQuery{
			Node: node,
		}
		sendToMaster(query, node, ctx)

		nd := v.(nodeDrivers)
		for i := range nd.Drivers {
			// TODO(soheil): remove the hardcoded value.
			if nd.Drivers[i].OutPings > MaxPings {
				ctx.SendToBee(nom.NodeDisconnected{
					Node:   nom.Node{ID: nom.NodeID(node)},
					Driver: nd.Drivers[i].Driver,
				}, ctx.ID())
				continue
			}

			ctx.SendToBee(nom.Ping{}, nd.Drivers[i].BeeID)
			nd.Drivers[i].OutPings++
		}

		if err := dict.Put(k, nd); err != nil {
			glog.Warningf("error in encoding drivers: %v", err)
		}
	})
	return nil
}
Esempio n. 2
0
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
}