func (mp *master_peer) Handle(id uint16, msgbody []byte) error { switch id { case share.M_ADD_SERVER: var as share.AddApp if err := share.DecodeMsg(msgbody, &as); err != nil { return err } AddApp(as.Type, as.Id, as.Name, as.Host, as.Port, as.ClientHost, as.ClientPort, as.Ready, as.EnableGlobalData) case share.M_REMOVE_SERVER: var rs share.RemoveApp if err := share.DecodeMsg(msgbody, &rs); err != nil { return err } RemoveAppById(rs.Id) case share.M_SERVER_LIST: var sl share.AppInfo if err := share.DecodeMsg(msgbody, &sl); err != nil { return err } for _, a := range sl.Apps { AddApp(a.Type, a.Id, a.Name, a.Host, a.Port, a.ClientHost, a.ClientPort, a.Ready, a.EnableGlobalData) } case share.M_HEARTBEAT: data, err := util.CreateMsg(nil, []byte{}, share.M_HEARTBEAT) if err != nil { log.LogFatalf(err) } core.noder.Send(data) case share.M_READY: var ready share.AppReady if err := share.DecodeMsg(msgbody, &ready); err != nil { return err } app := GetAppById(ready.Id) if app != nil { app.SetReady(true) } else { log.LogFatalf("app not found") } case share.M_MUSTAPPREADY: { log.LogMessage("must app ready") core.MustReady() } case share.M_SHUTDOWN: core.Closing = true close(core.exitChannel) return nil } core.Emitter.Push(MASERTINMSG, map[string]interface{}{"msg": MasterMsg{id, msgbody}}, false) return nil }
func (tcp *tcp_server) Handle(clientconn net.Conn) { buff := make([]byte, 2048) id, msgbody, err := util.ReadPkg(clientconn, buff) if err != nil { log.LogError(err) clientconn.Close() return } switch id { case share.M_REGISTER_APP: var reg share.RegisterApp if err := share.DecodeMsg(msgbody, ®); err != nil { clientconn.Close() log.LogFatalf(err) } app := &app{typ: reg.Type, id: reg.Id, name: reg.Name, conn: clientconn, host: reg.Host, port: reg.Port, clienthost: reg.ClientHost, clientport: reg.ClientPort, enableglobaldata: reg.EnableGlobalData} log.LogMessage(app.id, ":", app.conn.RemoteAddr().String()) app.SendList() AddApp(app) app.Loop() case share.M_REGISTER_AGENT: var reg share.RegisterAgent if err := share.DecodeMsg(msgbody, ®); err != nil { clientconn.Close() log.LogFatalf(err) } agent := &AgentNode{id: reg.AgentId, nobalance: reg.NoBalance} agent.Handle(clientconn) context.agentlist.AddAgent(agent) log.LogMessage("agent add:", reg.AgentId) default: log.LogError("first message must reg app") return } }
func (app *AreaMgr) OnEvent(e string, args map[string]interface{}) { switch e { case server.MASERTINMSG: msg := args["msg"].(server.MasterMsg) if msg.Id == share.M_CREATEAPP_BAK { var cab share.CreateAppBak if err := share.DecodeMsg(msg.Body, &cab); err != nil { log.LogError(err) return } App.Area.createAppBak(cab) } } }
func (a *Agent) Handle(id uint16, msgbody []byte) error { switch id { case share.M_SHUTDOWN: context.Stop() case share.M_CREATEAPP: create := share.CreateApp{} if err := share.DecodeMsg(msgbody, &create); err != nil { log.LogError(err) } go a.CreateApp(create) } return nil }
func (app *app) Handle(id uint16, body []byte) error { switch id { case share.M_HEARTBEAT: app.lastcheck = time.Now() log.LogFine(app.id, " recv app heartbeat") case share.M_READY: app.ready = true Ready(app) case share.M_CREATEAPP: create := share.CreateApp{} if err := share.DecodeMsg(body, &create); err != nil { log.LogError(err) } go app.CreateApp(create) } return nil }
func (a *AgentNode) HandleMsg(id uint16, msg []byte) error { switch id { case share.M_SHUTDOWN: a.Close() case share.M_FORWARD_MSG: forward := &share.SendAppMsg{} err := share.DecodeMsg(msg, forward) if err != nil { log.LogError(err) return err } err = context.SendToApp(forward.Id, forward.Data) if err != nil { log.LogError(err) return err } } return nil }