func (self *Manager) subscribeChannels() error { glog.Info("subscribeChannels") var msgServerClientList []*link.Session for _, ms := range self.cfg.MsgServerList { msgServerClient, err := self.connectMsgServer(ms) if err != nil { glog.Error(err.Error()) return err } cmd := protocol.NewCmdSimple() cmd.CmdName = protocol.SUBSCRIBE_CHANNEL_CMD cmd.Args = append(cmd.Args, protocol.SYSCTRL_CLIENT_STATUS) cmd.Args = append(cmd.Args, self.cfg.UUID) err = msgServerClient.Send(link.JSON{ cmd, }) if err != nil { glog.Error(err.Error()) return err } cmd = protocol.NewCmdSimple() cmd.CmdName = protocol.SUBSCRIBE_CHANNEL_CMD cmd.Args = append(cmd.Args, protocol.SYSCTRL_TOPIC_STATUS) cmd.Args = append(cmd.Args, self.cfg.UUID) err = msgServerClient.Send(link.JSON{ cmd, }) if err != nil { glog.Error(err.Error()) return err } msgServerClientList = append(msgServerClientList, msgServerClient) } for _, msc := range msgServerClientList { go self.handleMsgServerClient(msc) } return nil }
func (self *Router) subscribeChannels() error { glog.Info("subscribeChannels") for _, ms := range self.cfg.MsgServerList { msgServerClient, err := self.connectMsgServer(ms) if err != nil { glog.Error(err.Error()) return err } cmd := protocol.NewCmdSimple() cmd.CmdName = protocol.SUBSCRIBE_CHANNEL_CMD cmd.Args = append(cmd.Args, protocol.SYSCTRL_SEND) cmd.Args = append(cmd.Args, self.cfg.UUID) err = msgServerClient.Send(link.JSON{ cmd, }) if err != nil { glog.Error(err.Error()) return err } cmd = protocol.NewCmdSimple() cmd.CmdName = protocol.SUBSCRIBE_CHANNEL_CMD cmd.Args = append(cmd.Args, protocol.SYSCTRL_TOPIC_SYNC) cmd.Args = append(cmd.Args, self.cfg.UUID) err = msgServerClient.Send(link.JSON{ cmd, }) if err != nil { glog.Error(err.Error()) return err } self.msgServerClientMap[ms] = msgServerClient } for _, msc := range self.msgServerClientMap { go self.handleMsgServerClient(msc) } return nil }
func (self *HeartBeat) Beat() { timer := time.NewTicker(self.timeout * time.Second) ttl := time.After(self.expire * time.Second) for { select { case <-timer.C: go func() { cmd := protocol.NewCmdSimple() cmd.CmdName = protocol.SEND_PING_CMD cmd.Args = append(cmd.Args, protocol.PING) err := self.session.Send(link.JSON{ cmd, }) if err != nil { glog.Error(err.Error()) } }() case <-ttl: break } } }
func main() { flag.Parse() cfg, err := LoadConfig(*InputConfFile) if err != nil { glog.Error(err.Error()) return } p := link.PacketN(2, link.BigEndianBO, link.LittleEndianBF) gatewayClient, err := link.Dial("tcp", cfg.GatewayServer, p) if err != nil { panic(err) } fmt.Println("input id :") var input string if _, err := fmt.Scanf("%s\n", &input); err != nil { glog.Error(err.Error()) } inMsg, err := gatewayClient.Read() if err != nil { glog.Error(err.Error()) } glog.Info(string(inMsg.Get())) gatewayClient.Close(nil) msgServerClient, err := link.Dial("tcp", string(inMsg.Get()), p) if err != nil { panic(err) } glog.Info("test.. send id...") cmd := protocol.NewCmdSimple() cmd.CmdName = protocol.SEND_CLIENT_ID_CMD cmd.Args = append(cmd.Args, input) err = msgServerClient.Send(link.JSON{ cmd, }) if err != nil { glog.Error(err.Error()) } go heartBeat(cfg, msgServerClient) glog.Info("test.. send create topic...") cmd = protocol.NewCmdSimple() cmd.CmdName = protocol.CREATE_TOPIC_CMD fmt.Println("input topic name :") if _, err = fmt.Scanf("%s\n", &input); err != nil { glog.Error(err.Error()) } cmd.Args = append(cmd.Args, input) err = msgServerClient.Send(link.JSON{ cmd, }) if err != nil { glog.Error(err.Error()) } glog.Info("test.. send join topic...") cmd = protocol.NewCmdSimple() cmd.CmdName = protocol.JOIN_TOPIC_CMD fmt.Println("input topic name :") if _, err = fmt.Scanf("%s\n", &input); err != nil { glog.Error(err.Error()) } cmd.Args = append(cmd.Args, input) err = msgServerClient.Send(link.JSON{ cmd, }) if err != nil { glog.Error(err.Error()) } glog.Info("test.. send send topic msg...") cmd = protocol.NewCmdSimple() cmd.CmdName = protocol.SEND_MESSAGE_TOPIC_CMD fmt.Println("input topic name :") if _, err = fmt.Scanf("%s\n", &input); err != nil { glog.Error(err.Error()) } cmd.Args = append(cmd.Args, input) fmt.Println("input topic msg :") if _, err = fmt.Scanf("%s\n", &input); err != nil { glog.Error(err.Error()) } cmd.Args = append(cmd.Args, input) err = msgServerClient.Send(link.JSON{ cmd, }) if err != nil { glog.Error(err.Error()) } defer msgServerClient.Close(nil) msgServerClient.ReadLoop(func(msg link.InBuffer) { glog.Info(string(msg.Get())) }) glog.Flush() }