예제 #1
0
func (self *Router) handleMsgServerClient(msc *libnet.Session) {
	msc.Process(func(msg *libnet.InBuffer) error {
		log.Info("msg_server", msc.Conn().RemoteAddr().String(), " say: ", string(msg.Data))
		var c protocol.CmdInternal
		pp := NewProtoProc(self)
		err := json.Unmarshal(msg.Data, &c)
		if err != nil {
			log.Error("error:", err)
			return err
		}
		switch c.GetCmdName() {
		case protocol.REQ_SEND_P2P_MSG_CMD:
			err := pp.procSendMsgP2P(&c, msc)
			if err != nil {
				log.Warning(err.Error())
			}

		case protocol.IND_ACK_P2P_STATUS_CMD:
			err := pp.procAckP2pStatus(&c, msc)
			if err != nil {
				log.Warning(err.Error())
			}

		case protocol.REQ_SEND_TOPIC_MSG_CMD:
			err := pp.procSendMsgTopic(&c, msc)
			if err != nil {
				log.Warning(err.Error())
			}

		}
		return nil
	})
}
예제 #2
0
func (self *ProtoProc) procSendMessageTopic(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procSendMessageTopic")
	var err error
	topicName := cmd.GetArgs()[0]
	send2Msg := cmd.GetArgs()[1]
	log.Info(send2Msg)
	log.Info(topicName)

	if self.msgServer.topics[topicName] == nil {
		log.Warning(topicName + " is not exist")
	} else {
		resp := protocol.NewCmdSimple(protocol.RESP_MESSAGE_TOPIC_CMD)
		resp.AddArg(topicName)
		resp.AddArg(send2Msg)
		resp.AddArg(session.State.(*base.SessionState).ClientID)

		_, err = self.msgServer.topics[topicName].Channel.Broadcast(libnet.Json(resp))
		if err != nil {
			log.Error(err.Error())
			return err
		}
	}

	return err
}
예제 #3
0
func (self *ProtoProc) procSubscribeChannel(cmd protocol.Cmd, session *libnet.Session) {
	log.Info("procSubscribeChannel")
	channelName := cmd.GetArgs()[0]
	cUUID := cmd.GetArgs()[1]
	log.Info(channelName)
	if self.msgServer.channels[channelName] != nil {
		self.msgServer.channels[channelName].Channel.Join(session, nil)
		self.msgServer.channels[channelName].ClientIDlist = append(self.msgServer.channels[channelName].ClientIDlist, cUUID)
	} else {
		log.Warning(channelName + " is not exist")
	}

	log.Info(self.msgServer.channels)
}
예제 #4
0
func (self *Router) handleMsgServerClient(msc *libnet.Session) {
	msc.Process(func(msg *libnet.InBuffer) error {
		log.Info("msg_server", msc.Conn().RemoteAddr().String(), " say: ", string(msg.Data))
		var c protocol.CmdInternal
		pp := NewProtoProc(self)
		err := json.Unmarshal(msg.Data, &c)
		if err != nil {
			log.Error("error:", err)
			return err
		}
		switch c.GetCmdName() {
		case protocol.SEND_MESSAGE_P2P_CMD:
			err := pp.procSendMsgP2P(c, msc)
			if err != nil {
				log.Warning(err.Error())
			}
		case protocol.CREATE_TOPIC_CMD:
			err := pp.procCreateTopic(c, msc)
			if err != nil {
				log.Warning(err.Error())
			}
		case protocol.JOIN_TOPIC_CMD:
			err := pp.procJoinTopic(c, msc)
			if err != nil {
				log.Warning(err.Error())
			}
		case protocol.SEND_MESSAGE_TOPIC_CMD:
			err := pp.procSendMsgTopic(c, msc)
			if err != nil {
				log.Warning(err.Error())
			}

		}
		return nil
	})
}
예제 #5
0
func (self *ProtoProc) procJoinTopic(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procJoinTopic")
	var err error
	if len(cmd.GetArgs()) != 2 {
		return CMD_NOT_CORRECT
	}
	topicName := cmd.GetArgs()[0]
	clientID := cmd.GetArgs()[1]

	if self.msgServer.topics[topicName] == nil {
		log.Warning("no topic in this server :" + topicName)
		t, err := self.findTopicMsgAddr(topicName)
		if err != nil {
			log.Warningf("no topicName : %s", topicName)
			return err
		}

		resp := protocol.NewCmdSimple(protocol.LOCATE_TOPIC_MSG_ADDR_CMD)
		resp.AddArg(t.MsgServerAddr)
		resp.AddArg(topicName)

		err = session.Send(libnet.Json(resp))

		if err != nil {
			log.Error(err.Error())
			return err
		}

		return err
	} else {
		t, err := self.findTopicMsgAddr(topicName)
		if err != nil {
			log.Warningf("no topicName : %s", topicName)
			return err
		}
		resp := protocol.NewCmdSimple(protocol.LOCATE_TOPIC_MSG_ADDR_CMD)
		resp.AddArg(t.MsgServerAddr)
		resp.AddArg(topicName)

		err = session.Send(libnet.Json(resp))

		if err != nil {
			log.Error(err.Error())
			return err
		}
	}

	m := redis_store.NewMember(clientID)

	self.msgServer.topics[topicName].ClientIDList = append(self.msgServer.topics[topicName].ClientIDList,
		clientID)

	self.msgServer.topics[topicName].AddMember(m)

	self.msgServer.topics[topicName].Channel.Join(session, nil)

	args := make([]string, 0)
	args = append(args, topicName)
	CCmd := protocol.NewCmdInternal(protocol.CACHE_TOPIC_CMD, args, self.msgServer.topics[topicName].TSD)

	log.Info(CCmd)

	if self.msgServer.channels[protocol.SYSCTRL_TOPIC_STATUS] != nil {
		_, err = self.msgServer.channels[protocol.SYSCTRL_TOPIC_STATUS].Channel.Broadcast(libnet.Json(CCmd))
		if err != nil {
			log.Error(err.Error())
			return err
		}
	}

	// for store mongo

	//member := mongo_store.NewMember(clientID)

	//self.msgServer.topics[topicName].ClientIDList = append(self.msgServer.topics[topicName].ClientIDList,
	//clientID)

	//self.msgServer.topics[topicName].AddMember(member)

	//self.msgServer.topics[topicName].Channel.Join(session, nil)

	args = make([]string, 0)
	args = append(args, topicName)
	CCmd = protocol.NewCmdInternal(protocol.STORE_TOPIC_CMD, args, self.msgServer.topics[topicName].TSD)

	log.Info(CCmd)

	if self.msgServer.channels[protocol.STORE_TOPIC_INFO] != nil {
		_, err = self.msgServer.channels[protocol.STORE_TOPIC_INFO].Channel.Broadcast(libnet.Json(CCmd))
		if err != nil {
			log.Error(err.Error())
			return err
		}
	}

	return nil
}