Example #1
0
func (self *ProtoProc) procSendMessageP2P(cmd protocol.Cmd, session *libnet.Session) error {
	log.Info("procSendMessageP2P")
	var err error
	var sessionCacheData *redis_store.SessionCacheData
	var sessionStoreData *mongo_store.SessionStoreData
	var uuid string
	var send2ID string
	var send2Msg string

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

	resp := protocol.NewCmdSimple(protocol.RSP_SEND_P2P_MSG_CMD)

	if len(cmd.GetArgs()) != 2 {
		log.Warningf("syntax error: (id,msg) needed")
		err = common.SYNTAX_ERROR
		goto errout
	}

	send2ID = cmd.GetArgs()[0]
	send2Msg = cmd.GetArgs()[1]

	sessionCacheData, err = self.msgServer.sessionCache.Get(send2ID)
	if sessionCacheData == nil {
		sessionStoreData, err = self.msgServer.mongoStore.GetSessionFromCid(send2ID)
		if sessionStoreData == nil {
			log.Warningf("send2ID %s not found", send2ID)
			err = common.NOTFOUNT
			goto errout
		}
	}

	uuid = common.NewV4().String()
	log.Info("uuid : ", uuid)

	self.procP2PAckStatus(fromID, uuid, protocol.P2P_ACK_FALSE)

	if sessionCacheData == nil || sessionCacheData.Alive == false {
		//offline
		log.Info("procSendMessageP2P: " + send2ID + " | is offline")

		omrd, err := self.msgServer.offlineMsgCache.Get(send2ID)
		log.Info(omrd)
		if err != nil {
			log.Error(err.Error())
		}
		if omrd == nil {
			omrd = redis_store.NewOfflineMsgCacheData(send2ID)
		}
		omrd.AddMsg(redis_store.NewOfflineMsgData(send2Msg, fromID, uuid))

		err = self.msgServer.offlineMsgCache.Set(omrd)

		if err != nil {
			log.Error(err.Error())
			goto errout
		}
	} else if sessionCacheData.MsgServerAddr == self.msgServer.cfg.LocalIP {
		log.Info("procSendMessageP2P: in the same server")
		req := protocol.NewCmdSimple(protocol.IND_SEND_P2P_MSG_CMD)
		req.AddArg(send2Msg)
		req.AddArg(fromID)
		// add uuid
		req.AddArg(uuid)

		if self.msgServer.sessions[send2ID] != nil {
			self.msgServer.sessions[send2ID].Send(libnet.Json(req))
			if err != nil {
				log.Error(err.Error())
				goto errout
			}
			self.procP2PAckStatus(fromID, uuid, protocol.P2P_ACK_SENT)
		}
	} else {
		log.Info("procSendMessageP2P: not in the same server")
		if self.msgServer.channels[protocol.SYSCTRL_SEND] != nil {
			cmd.AddArg(fromID)
			//add uuid
			cmd.AddArg(uuid)
			_, err = self.msgServer.channels[protocol.SYSCTRL_SEND].Channel.Broadcast(libnet.Json(cmd))
			if err != nil {
				log.Error(err.Error())
				goto errout
			}
			//self.procP2PAckStatus(fromID, uuid, protocol.P2P_ACK_SENT)
		}
	}
errout:
	if err != nil {
		resp.AddArg(err.Error())
	} else {
		resp.AddArg(protocol.RSP_SUCCESS)
		resp.AddArg(uuid)
	}
	err = session.Send(libnet.Json(resp))
	if err != nil {
		log.Error(err.Error())
	}

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

		return err
	}

	uuid := common.NewV4().String()

	log.Info("uuid : ", uuid)

	uuidTmpMap := make(map[string]bool)
	uuidTmpMap[uuid] = false

	self.msgServer.p2pAckStatus[fromID] = uuidTmpMap

	if self.msgServer.sessions[send2ID] == nil {
		//offline
		log.Info(send2ID + " | is offline")
		exist, err := self.msgServer.offlineMsgCache.IsKeyExist(send2ID)
		if exist.(int64) == 0 {
			tmp := redis_store.NewOfflineMsgCacheData(send2ID)
			tmp.AddMsg(redis_store.NewOfflineMsgData(send2Msg, fromID, uuid))

			self.msgServer.offlineMsgCache.Set(tmp)
			if err != nil {
				log.Error(err.Error())
				return err
			}
		} else {
			omrd, err := common.GetOfflineMsgFromOwnerName(self.msgServer.offlineMsgCache, send2ID)
			if err != nil {
				log.Error(err.Error())
				return err
			}
			omrd.AddMsg(redis_store.NewOfflineMsgData(send2Msg, fromID, uuid))
			self.msgServer.offlineMsgCache.Set(omrd)
			if err != nil {
				log.Error(err.Error())
				return err
			}
		}
	}

	if store_session.MsgServerAddr == self.msgServer.cfg.LocalIP {
		log.Info("in the same server")
		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.Error(err.Error())
			}
		}
	} else {
		if self.msgServer.channels[protocol.SYSCTRL_SEND] != nil {
			//add uuid
			cmd.AddArg(uuid)
			_, err = self.msgServer.channels[protocol.SYSCTRL_SEND].Channel.Broadcast(libnet.Json(cmd))
			if err != nil {
				log.Error(err.Error())
				return err
			}
		}
	}

	return nil
}