func main() { flag.Parse() var err error if Conf, err = ParseBootstrap(BootstrapPath); err != nil { log.Fatalln(err.Error()) } AgentName += strconv.Itoa(os.Getpid()) AgentName = "test.json" ToRouter = servicebase.NewZmqRouter(AgentName) ToAgentConfigure = servicebase.NewZmqRouter(AgentName) ToPostAuction = servicebase.NewZmqRouter(AgentName) ZkService = servicebase.NewServiceBase([]string{"0.0.0.0:2181"}, "rtb-test", "mtl") go ZkService.AutoRecovery() ZkService.Connect() // agent config ZkService.NewWatcher("rtbAgentConfiguration", "agents").OnNew(func(name string, uri string) { log.Println(">>", name, uri) if err := ToAgentConfigure.NewConnection(name, uri, zmq.DEALER); err != nil { log.Println(err.Error()) } else { log.Println("connected to", name) /* enabled := GetEnableConfigures() if enabled != nil { for _, config := range *enabled { ToAgentConfigure.SendMessage(name, "CONFIG", config.Name, config.Raw) } } */ } }).Watch() // post auction ZkService.NewWatcher("rtbPostAuctionService", "logger").OnNew(func(name string, uri string) { log.Println(">>", name, uri) if err := ToPostAuction.NewConnection(name, uri, zmq.SUB); err != nil { log.Println(err.Error()) } else { log.Println("connected to", name) } }).Watch() // router ZkService.NewWatcher("rtbRequestRouter", "agents").OnNew(func(name string, uri string) { log.Println(">>", name, uri) if err := ToRouter.NewConnection(name, uri, zmq.DEALER); err != nil { log.Println(err.Error()) } else { log.Println("connected to", name) } }).Watch() ToRouter.OnRecvMessage = func(name string, sock *zmq.Socket) { msg, err := sock.RecvMessage(0) if err != nil { log.Println("recv error:", err.Error()) return } log.Println("Msg:", msg) if len(msg) == 0 { return } switch msg[0] { case "AUCTION": OnAuction(name, msg) case "NEEDCONFIG": enabled := GetEnableConfigures() if enabled != nil { for _, config := range *enabled { ToAgentConfigure.SendMessageToAll(name, "CONFIG", config.Name, config.Raw) } } case "GOTCONFIG": fmt.Println("router confirmed configure request") case "PING0": if len(msg) < 2 { return } received := msg[1] now := fmt.Sprintf("%.5f", float64(time.Now().UnixNano())/1e9) sock.SendMessage("PONG0", received, now, msg) case "PING1": if len(msg) < 2 { return } // payload := msg[2:] case "BYEBYE": fmt.Println("rejected by router, need reconnect") default: fmt.Println(msg) } } go ToRouter.MessageLoop() ToPostAuction.OnRecvMessage = func(name string, sock *zmq.Socket) { msg, err := sock.RecvMessage(0) if err != nil { return } if len(msg) == 0 { return } OnEvent(name, msg) } go ToPostAuction.MessageLoop() go UpdateAgentConfigure() Loop() }
func main() { flag.Parse() var err error if Conf, err = ParseBootstrap(BootstrapPath); err != nil { log.Fatalln(err.Error()) } if f, err := os.Open(JsConfigure); err != nil { log.Fatalln(err.Error()) } else { defer f.Close() if c, err := ioutil.ReadAll(f); err != nil { log.Fatalln(err.Error) } else { JsConfig = string(c) } } AgentName += strconv.Itoa(os.Getpid()) ToRouter = servicebase.NewZmqRouter(AgentName) ToAgentConfigure = servicebase.NewZmqRouter(AgentName) ToPostAuction = servicebase.NewZmqRouter(AgentName) ZkService = servicebase.NewServiceBase([]string{Conf.ZookeeprUri}, Conf.Installation, Conf.Location) go ZkService.AutoRecovery() ZkService.Connect() // agent config ZkService.NewWatcher("rtbAgentConfiguration", "agents").OnNew(func(name string, uri string) { log.Println(">>", name, uri) if err := ToAgentConfigure.NewConnection(name, uri, zmq.DEALER); err != nil { log.Println(err.Error()) } else { log.Println("connected to", name) ToAgentConfigure.SendMessage(name, "CONFIG", AgentName, JsConfig) } }).Watch() // post auction ZkService.NewWatcher("rtbPostAuctionService", "logger").OnNew(func(name string, uri string) { log.Println(">>", name, uri) if err := ToPostAuction.NewConnection(name, uri, zmq.SUB); err != nil { log.Println(err.Error()) } else { log.Println("connected to", name) } }).Watch() // router ZkService.NewWatcher("rtbRequestRouter", "agents").OnNew(func(name string, uri string) { log.Println(">>", name, uri) if err := ToRouter.NewConnection(name, uri, zmq.DEALER); err != nil { log.Println(err.Error()) } else { log.Println("connected to", name) // ToRouter.SendMessage(name, AgentName, "CONFIG", "test_agent") ToRouter.SendMessage(name, "CONFIG", AgentName) } }).Watch() ToRouter.OnRecvMessage = func(name string, sock *zmq.Socket) { msg, err := sock.RecvMessage(0) if err != nil { log.Println("recv error:", err.Error()) return } log.Println(msg) if len(msg) == 0 { return } switch msg[0] { case "AUCTION": OnAuction(name, msg) case "NEEDCONFIG": case "GOTCONFIG": fmt.Println("router confirmed configure request") case "PING0": if len(msg) < 2 { return } received := msg[1] now := fmt.Sprintf("%.5f", float64(time.Now().UnixNano())/1e9) sock.SendMessage("PONG0", received, now, msg) case "PING1": if len(msg) < 2 { return } // payload := msg[2:] case "BYEBYE": fmt.Println("rejected by router, need reconnect") default: fmt.Println(msg) } } go ToRouter.MessageLoop() ToPostAuction.OnRecvMessage = func(name string, sock *zmq.Socket) { msg, err := sock.RecvMessage(0) if err != nil { return } if len(msg) == 0 { return } OnEvent(name, msg) } go ToPostAuction.MessageLoop() Loop() }