func (self *ProtoProc) procCreateTopic(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procCreateTopic")
	var err error

	if len(cmd.GetArgs()) != 1 {
		return CMD_NOT_CORRECT
	}
	topicName := cmd.GetArgs()[0]

	topicCacheData := redis_store.NewTopicCacheData(topicName, session.State.(*base.SessionState).ClientID,
		self.msgServer.cfg.LocalIP)

	t := protocol.NewTopic(topicName, self.msgServer.cfg.LocalIP, session.State.(*base.SessionState).ClientID, session)
	t.ClientIDList = append(t.ClientIDList, session.State.(*base.SessionState).ClientID)
	t.TSD = topicCacheData
	self.msgServer.topics[topicName] = t
	self.msgServer.topics[topicName].Channel = libnet.NewChannel(self.msgServer.server.Protocol())

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

	log.Info(topicCacheData)
	args := make([]string, 0)
	args = append(args, topicName)
	CCmd := protocol.NewCmdInternal(protocol.CACHE_TOPIC_CMD, args, topicCacheData)
	m := redis_store.NewMember(session.State.(*base.SessionState).ClientID)
	CCmd.AnyData.(*redis_store.TopicCacheData).MemberList = append(CCmd.AnyData.(*redis_store.TopicCacheData).MemberList, m)

	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
		}
	}

	// store topic
	topicStoreData := mongo_store.NewTopicStoreData(topicName, session.State.(*base.SessionState).ClientID,
		self.msgServer.cfg.LocalIP)

	args = make([]string, 0)
	args = append(args, topicName)
	CCmd = protocol.NewCmdInternal(protocol.STORE_TOPIC_CMD, args, topicStoreData)
	member := mongo_store.NewMember(session.State.(*base.SessionState).ClientID)
	CCmd.AnyData.(*mongo_store.TopicStoreData).MemberList = append(CCmd.AnyData.(*mongo_store.TopicStoreData).MemberList, member)

	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
}
func (self *ProtoProc) procClientID(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procClientID")
	var err error
	ID := cmd.GetArgs()[0]
	// for cache data
	sessionCacheData := redis_store.NewSessionCacheData(cmd.GetArgs()[0], session.Conn().RemoteAddr().String(),
		self.msgServer.cfg.LocalIP, strconv.FormatUint(session.Id(), 10))

	log.Info(sessionCacheData)
	args := make([]string, 0)
	args = append(args, cmd.GetArgs()[0])
	CCmd := protocol.NewCmdInternal(protocol.CACHE_SESSION_CMD, args, sessionCacheData)

	log.Info(CCmd)

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

	// for store data
	sessionStoreData := mongo_store.SessionStoreData{ID, session.Conn().RemoteAddr().String(),
		self.msgServer.cfg.LocalIP, true}

	log.Info(sessionStoreData)
	args = make([]string, 0)
	args = append(args, cmd.GetArgs()[0])
	CCmd = protocol.NewCmdInternal(protocol.STORE_SESSION_CMD, args, sessionStoreData)

	log.Info(CCmd)

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

	self.msgServer.sessions[cmd.GetArgs()[0]] = session
	self.msgServer.sessions[cmd.GetArgs()[0]].State = base.NewSessionState(true, cmd.GetArgs()[0])

	err = self.procOfflineMsg(session, ID)
	if err != nil {
		log.Error(err.Error())
		return err
	}
	return nil
}
Example #3
0
func (self *ProtoProc) procJoinTopic(cmd protocol.Cmd, session *libnet.Session) error {
	glog.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 {
		glog.Warning("no topic in this server :" + topicName)
		t, err := self.findTopicMsgAddr(topicName)
		if err != nil {
			glog.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 {
			glog.Error(err.Error())
			return err
		}

		return err
	} else {
		t, err := self.findTopicMsgAddr(topicName)
		if err != nil {
			glog.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 {
			glog.Error(err.Error())
			return err
		}
	}

	m := storage.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.STORE_TOPIC_CMD, args, self.msgServer.topics[topicName].TSD)

	glog.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 {
			glog.Error(err.Error())
			return err
		}
	}

	return nil
}