/*
   通过Router转发消息(对终端开发者不可见)
   Router -> MsgServer
       ROUTE_SEND_TOPIC_MSG_CMD
       arg0: Msg           //消息内容
       arg1: TopicName     //群组名
       arg2: ClientID      //发送方用户ID
       arg3: ClientType    //发送方终端类型,是client还是device
*/
func (self *ProtoProc) procRouteTopicMsg(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procRouteTopicMsg")
	var err error

	//send2Msg := cmd.GetArgs()[0]
	topicName := cmd.GetArgs()[1]
	//fromID := cmd.GetArgs()[2]
	//fromType := cmd.GetArgs()[3]

	// check whether the topic exist
	topicCacheData, err := self.msgServer.topicCache.Get(topicName)
	if topicCacheData == nil {
		log.Warningf("TOPIC %s not exist: %s", topicName, err.Error())
		return common.TOPIC_NOT_EXIST
	}

	cmd.ChangeCmdName(protocol.IND_SEND_TOPIC_MSG_CMD)

	// exactly in this server, just broadcasting
	log.Warningf("topic %s has %d member(s) in this server", topicName, topicCacheData.AliveMemberNumMap[self.msgServer.cfg.LocalIP])
	for _, mID := range topicCacheData.MemberList {
		if self.msgServer.sessions[mID.ID] != nil {
			self.msgServer.sessions[mID.ID].Send(libnet.Json(cmd))
			if err != nil {
				log.Fatalln(err.Error())
			}
		}
	}

	return nil
}
func (self *ProtoProc) procSendMsgP2P(cmd protocol.Cmd, session *libnet.Session) error {
	glog.Info("procSendMsgP2P")
	var err error
	send2ID := cmd.GetArgs()[0]
	send2Msg := cmd.GetArgs()[1]
	glog.Info(send2Msg)
	self.Monitor.readMutex.Lock()
	defer self.Monitor.readMutex.Unlock()
	store_session, err := common.GetSessionFromCID(self.Monitor.sessionStore, send2ID)
	if err != nil {
		glog.Warningf("no ID : %s", send2ID)

		return err
	}
	glog.Info(store_session.MsgServerAddr)

	cmd.ChangeCmdName(protocol.ROUTE_MESSAGE_P2P_CMD)

	err = self.Monitor.msgServerClientMap[store_session.MsgServerAddr].Send(libnet.Json(cmd))
	if err != nil {
		glog.Error("error:", err)
		return err
	}

	return nil
}
/*
   MsgServer -> Router
       REQ_SEND_P2P_MSG_CMD
       arg0: Sent2ID       //接收方用户ID
       arg1: Msg           //消息内容
       arg2: FromID        //发送方用户ID
       arg3: uuid          //MsgServer分配的消息uuid
*/
func (self *ProtoProc) procSendMsgP2P(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procSendMsgP2P")
	var err error
	send2ID := cmd.GetArgs()[0]
	send2Msg := cmd.GetArgs()[1]
	log.Info(send2Msg)

	self.Router.readMutex.Lock()
	defer self.Router.readMutex.Unlock()
	cacheSession, err := self.Router.sessionCache.Get(send2ID)
	if cacheSession == nil || cacheSession.Alive == false {
		storeSession, err := self.Router.mongoStore.GetSessionFromCid(send2ID)
		if err != nil {
			log.Warningf("ID %s not registered, msg dropped", send2ID)
			return err
		}
		log.Info(storeSession)
		log.Warningf("ID registered but offline: %s", send2ID)

		cmd.ChangeCmdName(protocol.ROUTE_SEND_P2P_MSG_CMD)
		//ms := self.Router.cfg.MsgServerList[0]
		ms := session.Conn().RemoteAddr().String()
		err = self.Router.msgServerClientMap[ms].Send(libnet.Json(cmd))
		if err != nil {
			log.Error("error:", err)
			return err
		}
	} else {
		log.Info(cacheSession.MsgServerAddr)

		cmd.ChangeCmdName(protocol.ROUTE_SEND_P2P_MSG_CMD)
		log.Info(cmd)
		err = self.Router.msgServerClientMap[cacheSession.MsgServerAddr].Send(libnet.Json(cmd))
		if err != nil {
			log.Error("error:", err)
			return err
		}
	}

	return nil
}
func (self *ProtoProc) procSendMsgP2P(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procSendMsgP2P")
	var err error
	send2ID := cmd.GetArgs()[0]
	send2Msg := cmd.GetArgs()[1]
	log.Info(send2Msg)
	self.Router.readMutex.Lock()
	defer self.Router.readMutex.Unlock()
	cacheSession, err := common.GetSessionFromCID(self.Router.sessionCache, send2ID)
	if err != nil {
		log.Warningf("no ID in cache : %s", send2ID)
		storeSession, err := self.Router.mongoStore.GetSessionFromCid(mongo_store.DATA_BASE_NAME,
			mongo_store.TOPIC_INFO_COLLECTION, send2ID)
		if err != nil {
			return err
		}
		log.Info(storeSession.MsgServerAddr)

		cmd.ChangeCmdName(protocol.ROUTE_MESSAGE_P2P_CMD)

		err = self.Router.msgServerClientMap[storeSession.MsgServerAddr].Send(libnet.Json(cmd))
		if err != nil {
			log.Error("error:", err)
			return err
		}
	} else {
		log.Info(cacheSession.MsgServerAddr)

		cmd.ChangeCmdName(protocol.ROUTE_MESSAGE_P2P_CMD)

		err = self.Router.msgServerClientMap[cacheSession.MsgServerAddr].Send(libnet.Json(cmd))
		if err != nil {
			log.Error("error:", err)
			return err
		}
	}

	return nil
}
/*
   Router -> MsgServer
       ROUTE_SEND_TOPIC_MSG_CMD
       arg0: ClientID      //发送方用户ID
       arg1: ClientType    //发送方终端类型,是client还是device
       arg2: Msg           //消息内容
       arg3: TopicName     //群组名, device无须提供
*/
func (self *ProtoProc) procSendMsgTopic(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procSendMsgTopic")
	var err error
	ms := session.Conn().RemoteAddr().String()

	//send2Msg := cmd.GetArgs()[0]
	topicName := cmd.GetArgs()[1]
	//fromID := cmd.GetArgs()[2]
	//fromType := cmd.GetArgs()[3]

	// check whether the topic exist
	topicCacheData, err := self.Router.topicCache.Get(topicName)
	if topicCacheData == nil {
		log.Warningf("TOPIC %s not exist: %s", topicName, err.Error())
		return err
	}

	cmd.ChangeCmdName(protocol.ROUTE_SEND_TOPIC_MSG_CMD)
	log.Info(protocol.ROUTE_SEND_TOPIC_MSG_CMD)
	log.Info(cmd)

	for ip, num := range topicCacheData.AliveMemberNumMap {
		if num > 0 {
			log.Warningf("topic %s has %d member(s) in ip %s", topicName, num, ip)
			if ip != ms {
				// not in this server, routing it
				err = self.Router.msgServerClientMap[ip].Send(libnet.Json(cmd))
				if err != nil {
					log.Error("error:", err)
					return err
				}
			}
		}
	}

	return nil
}