Пример #1
0
// Get offline message interface
func (r *MessageRPC) Get(m *myrpc.MessageGetArgs, rw *myrpc.MessageGetResp) error {
	if m == nil || m.Key == "" || m.MsgId < 0 {
		return myrpc.ErrParam
	}
	msgs, err := UseStorage.Get(m.Key, m.MsgId)
	if err != nil {
		glog.Errorf("UseStorage.Get(\"%s\", %d) error(%v)", m.Key, m.MsgId, err)
		return err
	}
	rw.Msgs = msgs
	rw.PubMsgs = nil
	return nil
}
Пример #2
0
// Get offline message interface
func (r *MessageRPC) Get(m *myrpc.MessageGetArgs, rw *myrpc.MessageGetResp) error {
	Log.Info("request message (mid:%d,pmid:%d,key:%s)", m.MsgID, m.PubMsgID, m.Key)
	// Get all of offline messages which larger than MsgID that corresponding to m.Key
	msgs, err := UseStorage.Get(m.Key, m.MsgID)
	if err != nil {
		Log.Error("UseStorage.Get(\"%s\", \"%d\") error(%v)", m.Key, m.MsgID, err)
		rw.Ret = myrpc.InternalErr
		return nil
	}

	// Get public offline messages which larger than PubMsgID
	pMsgs, err := UseStorage.Get(Conf.PKey, m.PubMsgID)
	if err != nil {
		Log.Error("UseStorage.Get(\"%s\", \"%d\") error(%v)", Conf.PKey, m.PubMsgID, err)
		rw.Ret = myrpc.InternalErr
		return nil
	}

	numMsg := len(msgs)
	numPMsg := len(pMsgs)
	if numMsg == 0 && numPMsg == 0 {
		rw.Ret = myrpc.OK
		Log.Info("response message nil, request key(\"%s\") mid(\"%d\") pmid(\"%d\")", m.Key, m.MsgID, m.PubMsgID)
		return nil
	}

	var (
		data     []string
		pData    []string
		delMsgs  []string
		delPMsgs []string
		msg      = &Message{}
		tNow     = time.Now().UnixNano()
	)

	// Checkout expired offline messages
	for i := 0; i < numMsg; i++ {
		if err := json.Unmarshal([]byte(msgs[i]), &msg); err != nil {
			Log.Error("internal message:\"%s\" error(%v)", msgs[i], err)
			rw.Ret = myrpc.InternalErr
			return nil
		}
		if tNow > msg.Expire {
			delMsgs = append(delMsgs, msgs[i])
			continue
		}
		data = append(data, msgs[i])
	}
	for i := 0; i < numPMsg; i++ {
		if err := json.Unmarshal([]byte(pMsgs[i]), &msg); err != nil {
			Log.Error("internal message:\"%s\" error(%v)", pMsgs[i], err)
			rw.Ret = myrpc.InternalErr
			return nil
		}
		if tNow > msg.Expire {
			delPMsgs = append(delPMsgs, pMsgs[i])
			continue
		}
		pData = append(pData, pMsgs[i])
	}

	// Send to delete message process
	if len(delMsgs) != 0 {
		Log.Info("delete expire private messages:\"%s\"", msgs)
		DelChan <- &DelMessageInfo{Key: m.Key, Msgs: delMsgs}
	}
	if len(delPMsgs) != 0 {
		Log.Info("delete expire public messages:\"%s\"", pMsgs)
		DelChan <- &DelMessageInfo{Key: Conf.PKey, Msgs: delPMsgs}
	}

	rw.Ret = myrpc.OK
	rw.Msgs = data
	rw.PubMsgs = pData
	Log.Info("response private_message(%s) public_message(%s)", data, pData)

	return nil
}