Esempio n. 1
0
/*
   通过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
}
Esempio n. 2
0
/*
   Router -> MsgServer
       ROUTE_SEND_P2P_MSG_CMD
       arg0: Sent2ID       //接收方用户ID
       arg1: Msg           //消息内容
       arg2: FromID        //发送方用户ID
       arg3: uuid          //MsgServer分配的消息uuid

   发送给消息接受者的消息
   MsgServer -> device/client
       REQ_SEND_P2P_MSG_CMD
       arg0: Msg           //消息内容
       arg1: FromID        //发送方用户ID
       arg2: uuid          //MsgServer分配的消息uuid,可选,如果提供了则须IND_ACK_P2P_STATUS_CMD(ClientID, uuid)
*/
func (self *ProtoProc) procRouteMessageP2P(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procRouteMessageP2P")
	var err error

	send2ID := cmd.GetArgs()[0]
	send2Msg := cmd.GetArgs()[1]
	fromID := cmd.GetArgs()[2]
	uuid := cmd.GetArgs()[3]

	if self.msgServer.sessions[send2ID] != nil {
		resp := protocol.NewCmdSimple(protocol.IND_SEND_P2P_MSG_CMD)
		resp.AddArg(send2Msg)
		resp.AddArg(fromID)
		// add uuid
		resp.AddArg(uuid)

		err = self.msgServer.sessions[send2ID].Send(libnet.Json(resp))
		if err != nil {
			log.Fatalln(err.Error())
		} else {
			self.procP2PAckStatus(fromID, uuid, protocol.P2P_ACK_SENT)
		}
	}

	return nil
}
Esempio n. 3
0
func (self *ProtoProc) procRouteMessageP2P(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procRouteMessageP2P")
	var err error
	send2ID := cmd.GetArgs()[0]
	send2Msg := cmd.GetArgs()[1]
	fromID := cmd.GetArgs()[2]
	uuid := cmd.GetArgs()[3]
	_, err = common.GetSessionFromCID(self.msgServer.sessionCache, send2ID)
	if err != nil {
		log.Warningf("no ID : %s", send2ID)

		return err
	}

	resp := protocol.NewCmdSimple(protocol.RESP_MESSAGE_P2P_CMD)
	resp.AddArg(send2Msg)
	resp.AddArg(fromID)
	// add uuid
	resp.AddArg(uuid)

	if self.msgServer.sessions[send2ID] != nil {
		self.msgServer.sessions[send2ID].Send(libnet.Json(resp))
		if err != nil {
			log.Fatalln(err.Error())
		}
	}

	return nil
}
Esempio n. 4
0
/*
   device/client -> MsgServer -> Router
       REQ_SEND_TOPIC_MSG_CMD
       arg0: Msg           //消息内容
       arg1: TopicName     //群组名, device无须提供

   返回给消息发送者的消息
   MsgServer -> device/client
       RSP_SEND_TOPIC_MSG_CMD
       arg0: SUCCESS/FAILED

   通过Router转发消息(对终端开发者不可见)
   Router -> MsgServer
       ROUTE_SEND_TOPIC_MSG_CMD
       arg0: Msg           //消息内容
       arg1: TopicName     //群组名
       arg2: ClientID      //发送方用户ID
       arg3: ClientType    //发送方终端类型,是client还是device

   发送给消息接受者的消息
   MsgServer -> device/client
       REQ_SEND_TOPIC_MSG_CMD
       arg0: Msg           //消息内容
       arg1: TopicName     //群组名
       arg2: ClientID      //发送方用户ID
       arg3: ClientType    //发送方终端类型,是client还是device
*/
func (self *ProtoProc) procSendTopicMsg(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procSendTopicMsg")
	var err error
	var topicName string
	var topicCacheData *redis_store.TopicCacheData
	var sessionCacheData *redis_store.SessionCacheData
	//var sessionStoreData *mongo_store.SessionStoreData

	msg := cmd.GetArgs()[0]
	resp := protocol.NewCmdSimple(protocol.RSP_SEND_TOPIC_MSG_CMD)

	ClientID := session.State.(*base.SessionState).ClientID
	ClientType := session.State.(*base.SessionState).ClientType

	if ClientType == protocol.DEV_TYPE_CLIENT {
		if len(cmd.GetArgs()) != 2 {
			err = common.SYNTAX_ERROR
			goto ErrOut
		}
	} else if len(cmd.GetArgs()) != 1 {
		err = common.SYNTAX_ERROR
		goto ErrOut
	}

	// get session cache
	sessionCacheData, err = self.msgServer.sessionCache.Get(ClientID)
	if sessionCacheData == nil {
		log.Errorf("ID %s cache missing", ClientID)
		err = common.NOT_ONLINE
		goto ErrOut
	} else if ClientType == protocol.DEV_TYPE_WATCH {
		topicName = sessionCacheData.GetTopics()[0]
	} else {
		topicName = cmd.GetArgs()[1]
	}

	// check whether the topic exist
	topicCacheData, err = self.msgServer.topicCache.Get(topicName)
	if topicCacheData == nil {
		log.Warningf("TOPIC %s not exist", topicName)
		err = common.TOPIC_NOT_EXIST
	} else {
		topic_msg_resp := protocol.NewCmdSimple(cmd.GetCmdName())
		topic_msg_resp.AddArg(msg)
		topic_msg_resp.AddArg(topicName)
		topic_msg_resp.AddArg(ClientID)
		topic_msg_resp.AddArg(ClientType)

		if topicCacheData.AliveMemberNumMap[self.msgServer.cfg.LocalIP] > 1 {
			// exactly in this server, just broadcasting
			topic_msg_resp.ChangeCmdName(protocol.IND_SEND_TOPIC_MSG_CMD)
			log.Warningf("topic %s has %d member(s) in this server", topicName, topicCacheData.AliveMemberNumMap[self.msgServer.cfg.LocalIP])
			for _, mID := range topicCacheData.MemberList {
				if mID.ID != ClientID && self.msgServer.sessions[mID.ID] != nil {
					self.msgServer.sessions[mID.ID].Send(libnet.Json(topic_msg_resp))
					if err != nil {
						log.Fatalln(err.Error())
					}
				}
			}
		}
		if self.msgServer.channels[protocol.SYSCTRL_SEND] != nil {
			//topic_msg_resp.ChangeCmdName(protocol.ROUTE_SEND_TOPIC_MSG_CMD)
			topic_msg_resp.ChangeCmdName(protocol.REQ_SEND_TOPIC_MSG_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 != self.msgServer.cfg.LocalIP {
						// not in this server, routing it
						_, err = self.msgServer.channels[protocol.SYSCTRL_SEND].Channel.Broadcast(libnet.Json(topic_msg_resp))
						if err != nil {
							log.Error(err.Error())
						}
						break
					}
				}
			}
		}
	}

ErrOut:
	if err != nil {
		resp.AddArg(err.Error())
	} else {
		resp.AddArg(protocol.RSP_SUCCESS)
	}
	err = session.Send(libnet.Json(resp))
	if err != nil {
		log.Error(err.Error())
	}

	return err
}