本文整理汇总了Golang中github.com/kandoo/beehive.RcvContext.Emit方法的典型用法代码示例。如果您正苦于以下问题:Golang RcvContext.Emit方法的具体用法?Golang RcvContext.Emit怎么用?Golang RcvContext.Emit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/kandoo/beehive.RcvContext
的用法示例。
在下文中一共展示了RcvContext.Emit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Rcv
func (h *newLinkHandler) Rcv(msg bh.Msg, ctx bh.RcvContext) error {
l := nom.Link(msg.Data().(NewLink))
n, _ := nom.ParsePortUID(l.From)
d := ctx.Dict(nodeDict)
k := string(n)
v, err := d.Get(k)
if err != nil {
return nil
}
np := v.(nodePortsAndLinks)
if oldl, ok := np.linkFrom(l.From); ok {
if oldl.UID() == l.UID() {
return nil
}
np.removeLink(oldl)
ctx.Emit(nom.LinkDeleted(oldl))
}
glog.V(2).Infof("Link detected %v", l)
ctx.Emit(nom.LinkAdded(l))
np.L = append(np.L, l)
return d.Put(k, np)
}
示例2: Rcv
func (h *arpPktInHandler) Rcv(msg bh.Msg, ctx bh.RcvContext) error {
pin := msg.Data().(nom.PacketIn)
p := gopacket.NewPacket([]byte(pin.Packet), layers.LayerTypeEthernet, gopacket.Default)
etherlayer := p.Layer(layers.LayerTypeEthernet)
if etherlayer == nil {
return nil
}
e, _ := etherlayer.(*layers.Ethernet)
if e.EthernetType != layers.EthernetTypeARP {
return nil
}
host, _, err := decodeARP([]byte(pin.Packet))
host.Node = pin.Node
if err != nil {
glog.Errorf("ARP decoding error: %v", err)
return err
}
glog.V(2).Infof("Host detected: %v", host)
ctx.Emit(nom.HostConnected(host))
return nil
}
示例3: Rcv
func (h portStatusHandler) Rcv(msg bh.Msg, ctx bh.RcvContext) error {
// FIXME(soheil): This implementation is very naive and cannot tolerate
// faults. We need to first check if the driver is the mater, and then apply
// the change. Otherwise, we need to enque this message for that driver and
// make sure we apply the log to the port status
data := msg.Data().(nom.PortStatusChanged)
dict := ctx.Dict(driversDict)
k := string(data.Port.Node)
v, err := dict.Get(k)
if err != nil {
return fmt.Errorf("NOMController: node %v not found", data.Port.Node)
}
n := v.(nodeDrivers)
if !n.isMaster(data.Driver) {
glog.Warningf("NOMController: %v ignored, %v is not master, master is %v",
data.Port, data.Driver, n.master())
return nil
}
if p, ok := n.Ports.GetPort(data.Port.UID()); ok {
if p == data.Port {
return fmt.Errorf("NOMController: duplicate port status change for %v",
data.Port)
}
n.Ports.DelPort(p)
}
n.Ports.AddPort(data.Port)
ctx.Emit(nom.PortUpdated(data.Port))
return nil
}
示例4: Rcv
func (d *Driver) Rcv(m beehive.Msg, ctx beehive.RcvContext) error {
if m.NoReply() {
return nil
}
q, ok := m.Data().(StatQuery)
if !ok {
return nil
}
s, ok := d.switches[q.Switch]
if !ok {
return fmt.Errorf("No switch stored in the driver: %+v", s)
}
for i, f := range s.Flows {
f.Bytes += uint64(rand.Intn(maxSpike))
s.Flows[i] = f
glog.V(2).Infof("Emitting stat result for %+v", f)
ctx.Emit(StatResult{q, f.Flow, f.Bytes})
}
d.switches[q.Switch] = s
return nil
}
示例5: handleAck
func (h *ConnHandler) handleAck(req ReqID, ctx beehive.RcvContext,
r *bufio.Reader) error {
q, err := r.ReadString(' ')
if err != nil {
return err
}
q = q[:len(q)-1]
s, err := r.ReadString('\n')
if err != nil {
return err
}
s = dropNewLine(s)
tid, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return err
}
ctx.Emit(Ack{
ID: req,
Queue: Queue(q),
TaskID: TaskID(tid),
})
return nil
}
示例6: Rcv
func (c *Collector) Rcv(m beehive.Msg, ctx beehive.RcvContext) error {
res := m.Data().(StatResult)
glog.V(2).Infof("Stat results: %+v", res)
matrix := ctx.Dict(matrixDict)
key := res.Switch.Key()
v, err := matrix.Get(key)
if err != nil {
return fmt.Errorf("No such switch in matrix: %+v", res)
}
c.poller.query <- StatQuery{res.Switch}
sw := v.(SwitchStats)
stat, ok := sw[res.Flow]
sw[res.Flow] = res.Bytes
glog.V(2).Infof("Previous stats: %+v, Now: %+v", stat, res.Bytes)
if !ok || res.Bytes-stat > c.delta {
glog.Infof("Found an elephent flow: %+v, %+v, %+v", res, stat,
ctx.Hive().ID())
ctx.Emit(MatrixUpdate(res))
}
matrix.Put(key, sw)
return nil
}
示例7: addFlowEntriesForPath
func addFlowEntriesForPath(sub bh.AppCellKey, path nom.Path,
flows []nom.FlowEntry, ctx bh.RcvContext) {
fs := make([]flowAndStatus, 0, len(flows))
path.ID = strconv.FormatUint(reservePathID(ctx), 16)
for i := range flows {
flows[i].ID = path.ID
fs = append(fs, flowAndStatus{Flow: flows[i]})
}
pf := pathAndFlows{
Subscriber: sub,
Path: path,
Flows: fs,
Timestamp: time.Now(),
}
d := ctx.Dict(dictPath)
if err := d.Put(path.ID, pf); err != nil {
glog.Fatalf("error in storing path entry: %v", err)
}
ack := centralizedAppCellKey(ctx.App())
for _, f := range flows {
addf := nom.AddFlowEntry{
Flow: f,
Subscriber: ack,
}
ctx.Emit(addf)
}
}
示例8: Rcv
func (p Poller) Rcv(msg bh.Msg, ctx bh.RcvContext) error {
ctx.Dict("Switches").ForEach(func(k string, v interface{}) {
fmt.Printf("poller: polling switch %v\n", k)
ctx.Emit(nom.FlowStatsQuery{
Node: nom.UID(k),
})
})
return nil
}
示例9: Rcv
func (h *UpdateHandler) Rcv(m beehive.Msg, ctx beehive.RcvContext) error {
if m.NoReply() {
return nil
}
u := m.Data().(MatrixUpdate)
glog.Infof("Received matrix update: %+v", u)
ctx.Emit(FlowMod{Switch: u.Switch})
return nil
}
示例10: Rcv
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
}
示例11: Start
// Start is called once the detached handler starts.
func (h *HelloConn) Start(ctx beehive.RcvContext) {
defer h.conn.Close()
r := bufio.NewReader(h.conn)
for {
name, _, err := r.ReadLine()
if err != nil {
return
}
ctx.Emit(HelloDetached{Name: string(name)})
}
}
示例12: sendLLDPPacket
func sendLLDPPacket(n nom.Node, p nom.Port, ctx bh.RcvContext) {
pkt := nom.PacketOut{
Node: n.UID(),
Packet: nom.Packet(encodeLLDP(n, p)),
BufferID: 0xFFFFFFFF,
Actions: []nom.Action{
nom.ActionForward{
Ports: []nom.UID{p.UID()},
},
},
}
ctx.Emit(pkt)
}
示例13: handleDeQ
func (h *ConnHandler) handleDeQ(req ReqID, ctx beehive.RcvContext,
r *bufio.Reader) error {
q, err := r.ReadString('\n')
if err != nil {
return err
}
q = dropNewLine(q)
ctx.Emit(Deque{
ID: req,
Queue: Queue(q),
})
return nil
}
示例14: Rcv
func (h addFlowHandler) Rcv(msg bh.Msg, ctx bh.RcvContext) error {
add := msg.Data().(nom.AddFlowEntry)
var nf nodeFlows
if v, err := ctx.Dict(flowsDict).Get(string(add.Flow.Node)); err == nil {
nf = v.(nodeFlows)
}
added := nom.FlowEntryAdded{Flow: add.Flow}
if nf.maybeAddFlow(add) {
ctx.Emit(added)
sendToMaster(add, add.Flow.Node, ctx)
}
if !add.Subscriber.IsNil() {
ctx.SendToCell(added, add.Subscriber.App, add.Subscriber.Cell())
}
return ctx.Dict(flowsDict).Put(string(add.Flow.Node), nf)
}
示例15: Rcv
func (h nodeConnectedHandler) Rcv(msg bh.Msg, ctx bh.RcvContext) error {
nc := msg.Data().(nom.NodeConnected)
ddict := ctx.Dict(driversDict)
k := string(nc.Node.ID)
n := nodeDrivers{
Node: nc.Node,
}
if v, err := ddict.Get(k); err == nil {
n = v.(nodeDrivers)
}
if _, ok := n.driver(nc.Driver); ok {
return fmt.Errorf("driver %v reconnects to %v", nc.Driver, n.Node)
}
gdict := ctx.Dict(genDict)
gen := uint64(0)
if v, err := gdict.Get("gen"); err == nil {
gen = v.(uint64)
}
gen++
db := nc.Driver.BeeID
if len(n.Drivers) == 0 {
nc.Driver.Role = nom.DriverRoleMaster
ctx.Emit(nom.NodeJoined(nc.Node))
glog.V(2).Infof("%v connected to master controller", nc.Node)
} else {
nc.Driver.Role = nom.DriverRoleSlave
glog.V(2).Infof("%v connected to slave controller", nc.Node)
}
n.Drivers = append(n.Drivers, driverInfo{
Driver: nc.Driver,
LastSeen: time.Now(),
})
ctx.SendToBee(nom.ChangeDriverRole{
Node: nc.Node.UID(),
Role: nc.Driver.Role,
Generation: gen,
}, db)
gdict.Put("gen", gen)
return ddict.Put(k, n)
}