Example #1
0
/*
 * 初始化函数,从cache里面加载上次同步的时间信息等
 */
void CSyncCenter::init()
{
    // Load total update time
    CacheManager* pCacheManager = CacheManager::getInstance();
    // increase message count
    CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
    if (pCacheConn)
    {
        string strTotalUpdate = pCacheConn->get("total_user_updated");

        string strLastUpdateGroup = pCacheConn->get("last_update_group");
        pCacheManager->RelCacheConn(pCacheConn);
	if(strTotalUpdate != "")
        {
            m_nLastUpdate = string2int(strTotalUpdate);
        }
        else
        {
            updateTotalUpdate(time(NULL));
        }
        if(strLastUpdateGroup.empty())
        {
            m_nLastUpdateGroup = string2int(strLastUpdateGroup);
        }
        else
        {
            updateLastUpdateGroup(time(NULL));
        }
    }
    else
    {
        log("no cache connection to get total_user_updated");
    }
}
Example #2
0
void CMessageModel::getUnReadCntAll(uint32_t nUserId, uint32_t &nTotalCnt)
{
    CacheManager* pCacheManager = CacheManager::getInstance();
    CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
    if (pCacheConn)
    {
        map<string, string> mapUnread;
        string strKey = "unread_" + int2string(nUserId);
        bool bRet = pCacheConn->hgetAll(strKey, mapUnread);
        pCacheManager->RelCacheConn(pCacheConn);
        
        if(bRet)
        {
            for (auto it = mapUnread.begin(); it != mapUnread.end(); it++) {
                nTotalCnt += atoi(it->second.c_str());
            }
        }
        else
        {
            log("hgetall %s failed!", strKey.c_str());
        }
    }
    else
    {
        log("no cache connection for unread");
    }
}
Example #3
0
void CMessageModel::incMsgCount(uint32_t nFromId, uint32_t nToId)
{
	CacheManager* pCacheManager = CacheManager::getInstance();
	// increase message count
	CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
	if (pCacheConn) {
		pCacheConn->hincrBy("unread_" + int2string(nToId), int2string(nFromId), 1);
		pCacheManager->RelCacheConn(pCacheConn);
	} else {
		log("no cache connection to increase unread count: %d->%d", nFromId, nToId);
	}
}
Example #4
0
uint32_t CMessageModel::getMsgId(uint32_t nRelateId)
{
    uint32_t nMsgId = 0;
    CacheManager* pCacheManager = CacheManager::getInstance();
    CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
    if(pCacheConn)
    {
        string strKey = "msg_id_" + int2string(nRelateId);
        nMsgId = pCacheConn->incrBy(strKey, 1);
        pCacheManager->RelCacheConn(pCacheConn);
    }
    return nMsgId;
}
Example #5
0
void CMessageModel::getUnreadMsgCount(uint32_t nUserId, uint32_t &nTotalCnt,
                                      list<IM::BaseDefine::UnreadInfo>& lsUnreadCount)
{
    CacheManager* pCacheManager = CacheManager::getInstance();
    CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
    if (pCacheConn)
    {
        map<string, string> mapUnread;
        string strKey = "unread_" + int2string(nUserId);
        bool bRet = pCacheConn->hgetAll(strKey, mapUnread);
        pCacheManager->RelCacheConn(pCacheConn);
        if(bRet)
        {
            IM::BaseDefine::UnreadInfo cUnreadInfo;
            for (auto it = mapUnread.begin(); it != mapUnread.end(); it++)
            {
                cUnreadInfo.set_session_id(atoi(it->first.c_str()));
                cUnreadInfo.set_unread_cnt(atoi(it->second.c_str()));
                cUnreadInfo.set_session_type(IM::BaseDefine::SESSION_TYPE_SINGLE);
                uint32_t nMsgId = 0;
                string strMsgData;
                IM::BaseDefine::MsgType nMsgType;
                getLastMsg(cUnreadInfo.session_id(), nUserId, nMsgId, strMsgData, nMsgType);
                if(IM::BaseDefine::MsgType_IsValid(nMsgType))
                {
                    cUnreadInfo.set_latest_msg_id(nMsgId);
                    cUnreadInfo.set_latest_msg_data(strMsgData);
                    cUnreadInfo.set_latest_msg_type(nMsgType);
                    cUnreadInfo.set_latest_msg_from_user_id(cUnreadInfo.session_id());
                    lsUnreadCount.push_back(cUnreadInfo);
                    nTotalCnt += cUnreadInfo.unread_cnt();
                }
                else
                {
                    log("invalid msgType. userId=%u, peerId=%u, msgType=%u", nUserId, cUnreadInfo.session_id(), nMsgType);
                }
            }
        }
        else
        {
            log("hgetall %s failed!", strKey.c_str());
        }
    }
    else
    {
        log("no cache connection for unread");
    }
}
Example #6
0
void CSyncCenter::updateTotalUpdate(uint32_t nUpdated)
{
    CacheManager* pCacheManager = CacheManager::getInstance();
    CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
    if (pCacheConn) {
        last_update_lock_.lock();
        m_nLastUpdate = nUpdated;
        last_update_lock_.unlock();
        string strUpdated = int2string(nUpdated);
        pCacheConn->set("total_user_update", strUpdated);
        pCacheManager->RelCacheConn(pCacheConn);
    }
    else
    {
        log("no cache connection to get total_user_updated");
    }
}
Example #7
0
bool CMessageModel::resetMsgId(uint32_t nRelateId)
{
    bool bRet = false;
    uint32_t nMsgId = 0;
    CacheManager* pCacheManager = CacheManager::getInstance();
    CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
    if(pCacheConn)
    {
        string strKey = "msg_id_" + int2string(nRelateId);
        string strValue = "0";
        string strReply = pCacheConn->set(strKey, strValue);
        if(strReply == strValue)
        {
            bRet = true;
        }
        pCacheManager->RelCacheConn(pCacheConn);
    }
    return bRet;
}
Example #8
0
bool CGroupModel::insertNewMember(uint32_t nGroupId, set<uint32_t>& setUsers)
{
    bool bRet = false;
    uint32_t nUserCnt = (uint32_t)setUsers.size();
    if(nGroupId != INVALID_VALUE &&  nUserCnt > 0)
    {
        CDBManager* pDBManager = CDBManager::getInstance();
        CDBConn* pDBConn = pDBManager->GetDBConn("dffxIMDB_slave");
        if (pDBConn)
        {
            uint32_t nCreated = (uint32_t)time(NULL);
            // 获取 已经存在群里的用户
            string strClause;
            bool bFirst = true;
            for (auto it=setUsers.begin(); it!=setUsers.end(); ++it)
            {
                if(bFirst)
                {
                    bFirst = false;
                    strClause = int2string(*it);
                }
                else
                {
                    strClause += ("," + int2string(*it));
                }
            }
            string strSql = "select userId from IMGroupMember where groupId=" + int2string(nGroupId) + " and userId in (" + strClause + ")";
            CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str());
            set<uint32_t> setHasUser;
            if(pResultSet)
            {
                while (pResultSet->Next()) {
                    setHasUser.insert(pResultSet->GetInt("userId"));
                }
                pResultSet->Clear();
            }
            else
            {
                log("no result for sql:%s", strSql.c_str());
            }
            pDBManager->RelDBConn(pDBConn);
            
            pDBConn = pDBManager->GetDBConn("dffxIMDB_master");
            if (pDBConn)
            {
                CacheManager* pCacheManager = CacheManager::getInstance();
                CacheConn* pCacheConn = pCacheManager->GetCacheConn("group_member");
                if (pCacheConn)
                {
                    // 设置已经存在群中人的状态
                    if (!setHasUser.empty())
                    {
                        strClause.clear();
                        bFirst = true;
                        for (auto it=setHasUser.begin(); it!=setHasUser.end(); ++it) {
                            if(bFirst)
                            {
                                bFirst = false;
                                strClause = int2string(*it);
                            }
                            else
                            {
                                strClause += ("," + int2string(*it));
                            }
                        }
                        
                        strSql = "update IMGroupMember set status=0, updated="+int2string(nCreated)+" where groupId=" + int2string(nGroupId) + " and userId in (" + strClause + ")";
                        pDBConn->ExecuteUpdate(strSql.c_str());
                    }
                    strSql = "insert into IMGroupMember(`groupId`, `userId`, `status`, `created`, `updated`) values\
                    (?,?,?,?,?)";
                    
                    //插入新成员
                    auto it = setUsers.begin();
                    uint32_t nStatus = 0;
                    uint32_t nIncMemberCnt = 0;
                    for (;it != setUsers.end();)
                    {
                        uint32_t nUserId = *it;
                        if(setHasUser.find(nUserId) == setHasUser.end())
                        {
                            CPrepareStatement* pStmt = new CPrepareStatement();
                            if (pStmt->Init(pDBConn->GetMysql(), strSql))
                            {
                                uint32_t index = 0;
                                pStmt->SetParam(index++, nGroupId);
                                pStmt->SetParam(index++, nUserId);
                                pStmt->SetParam(index++, nStatus);
                                pStmt->SetParam(index++, nCreated);
                                pStmt->SetParam(index++, nCreated);
                                pStmt->ExecuteUpdate();
                                ++nIncMemberCnt;
                                delete pStmt;
                            }
                            else
                            {
                                setUsers.erase(it++);
                                delete pStmt;
                                continue;
                            }
                        }
                        ++it;
                    }
                    if(nIncMemberCnt != 0)
                    {
                        strSql = "update IMGroup set userCnt=userCnt+" + int2string(nIncMemberCnt) + " where id="+int2string(nGroupId);
                        pDBConn->ExecuteUpdate(strSql.c_str());
                    }
                    
                    //更新一份到redis中
                    string strKey = "group_member_"+int2string(nGroupId);
                    for(auto it = setUsers.begin(); it!=setUsers.end(); ++it)
                    {
                        pCacheConn->hset(strKey, int2string(*it), int2string(nCreated));
                    }
                    pCacheManager->RelCacheConn(pCacheConn);
                    bRet = true;
                }
                else
                {
                    log("no cache connection");
                }
                pDBManager->RelDBConn(pDBConn);
            }
            else
            {
Example #9
0
 void setDevicesToken(CImPdu* pPdu, uint32_t conn_uuid)
 {
     IM::Login::IMDeviceTokenReq msg;
     IM::Login::IMDeviceTokenRsp msgResp;
     if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength()))
     {
         uint32_t nUserId = msg.user_id();
         string strToken = msg.device_token();
         CImPdu* pPduResp = new CImPdu;
         CacheManager* pCacheManager = CacheManager::getInstance();
         CacheConn* pCacheConn = pCacheManager->GetCacheConn("token");
         if (pCacheConn)
         {
             IM::BaseDefine::ClientType nClientType = msg.client_type();
             string strValue;
             if(IM::BaseDefine::CLIENT_TYPE_IOS == nClientType)
             {
                 strValue = "ios:"+strToken;
             }
             else if(IM::BaseDefine::CLIENT_TYPE_ANDROID == nClientType)
             {
                 strValue = "android:"+strToken;
             }
             else
             {
                 strValue = strToken;
             }
             
             string strOldValue = pCacheConn->get("device_"+int2string(nUserId));
             
             if(!strOldValue.empty())
             {
                 size_t nPos = strOldValue.find(":");
                 if(nPos!=string::npos)
                 {
                     string strOldToken = strOldValue.substr(nPos + 1);
                     string strReply = pCacheConn->get("device_"+strOldToken);
                     if (!strReply.empty()) {
                         string strNewValue("");
                         pCacheConn->set("device_"+strOldToken, strNewValue);
                     }
                 }
             }
             
             pCacheConn->set("device_"+int2string(nUserId), strValue);
             string strNewValue = int2string(nUserId);
             pCacheConn->set("device_"+strToken, strNewValue);
         
             log("setDeviceToken. userId=%u, deviceToken=%s", nUserId, strToken.c_str());
             pCacheManager->RelCacheConn(pCacheConn);
         }
         else
         {
             log("no cache connection for token");
         }
         
         log("setDeviceToken. userId=%u, deviceToken=%s", nUserId, strToken.c_str());
         msgResp.set_attach_data(msg.attach_data());
         msgResp.set_user_id(nUserId);
         pPduResp->SetPBMsg(&msgResp);
         pPduResp->SetSeqNum(pPdu->GetSeqNum());
         pPduResp->SetServiceId(IM::BaseDefine::SID_LOGIN);
         pPduResp->SetCommandId(IM::BaseDefine::CID_LOGIN_RES_DEVICETOKEN);
         CProxyConn::AddResponsePdu(conn_uuid, pPduResp);
     }
     else
     {
         log("parse pb failed");
     }
 }
Example #10
0
 void getDevicesToken(CImPdu* pPdu, uint32_t conn_uuid)
 {
     IM::Server::IMGetDeviceTokenReq msg;
     IM::Server::IMGetDeviceTokenRsp msgResp;
     if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength()))
     {
         CacheManager* pCacheManager = CacheManager::getInstance();
         CacheConn* pCacheConn = pCacheManager->GetCacheConn("token");
         CImPdu* pPduResp = new CImPdu;
         uint32_t nCnt = msg.user_id_size();
         if (pCacheConn)
         {
             vector<string> vecTokens;
             for (uint32_t i=0; i<nCnt; ++i) {
                 string strKey = "device_"+int2string(msg.user_id(i));
                 vecTokens.push_back(strKey);
             }
             map<string, string> mapTokens;
             bool bRet = pCacheConn->mget(vecTokens, mapTokens);
             pCacheManager->RelCacheConn(pCacheConn);
             
             if(bRet)
             {
                 for (auto it=mapTokens.begin(); it!=mapTokens.end(); ++it) {
                     string strKey = it->first;
                     size_t nPos = strKey.find("device_");
                     if( nPos != string::npos)
                     {
                         string strUserId = strKey.substr(nPos + strlen("device_"));
                         uint32_t nUserId = string2int(strUserId);
                         string strValue = it->second;
                         nPos = strValue.find(":");
                         if(nPos!=string::npos)
                         {
                             string strType = strValue.substr(0, nPos);
                             string strToken = strValue.substr(nPos + 1);
                             IM::BaseDefine::ClientType nClientType = IM::BaseDefine::ClientType(0);
                             if(strType == "ios")
                             {
                                 nClientType = IM::BaseDefine::CLIENT_TYPE_IOS;
                             }
                             else if(strType == "android")
                             {
                                 nClientType = IM::BaseDefine::CLIENT_TYPE_ANDROID;
                             }
                             if(IM::BaseDefine::ClientType_IsValid(nClientType))
                             {
                                 IM::BaseDefine::UserTokenInfo* pToken = msgResp.add_user_token_info();
                                 pToken->set_user_id(nUserId);
                                 pToken->set_token(strToken);
                                 pToken->set_user_type(nClientType);
                                 uint32_t nTotalCnt = 0;
                                 CMessageModel::getInstance()->getUnReadCntAll(nUserId, nTotalCnt);
                                 CGroupMessageModel::getInstance()->getUnReadCntAll(nUserId, nTotalCnt);
                                 pToken->set_push_count(nTotalCnt);
                                 pToken->set_push_type(1);
                             }
                             else
                             {
                                 log("invalid clientType.clientType=%u", nClientType);
                             }
                         }
                         else
                         {
                             log("invalid value. value=%s", strValue.c_str());
                         }
                         
                     }
                     else
                     {
                         log("invalid key.key=%s", strKey.c_str());
                     }
                 }
             }
             else
             {
                 log("mget failed!");
             }
         }
         else
         {
             log("no cache connection for token");
         }
         
         log("req devices token.reqCnt=%u, resCnt=%u", nCnt, msgResp.user_token_info_size());
         
         msgResp.set_attach_data(msg.attach_data());
         pPduResp->SetPBMsg(&msgResp);
         pPduResp->SetSeqNum(pPdu->GetSeqNum());
         pPduResp->SetServiceId(IM::BaseDefine::SID_OTHER);
         pPduResp->SetCommandId(IM::BaseDefine::CID_OTHER_GET_DEVICE_TOKEN_RSP);
         CProxyConn::AddResponsePdu(conn_uuid, pPduResp);
     }
     else
     {
         log("parse pb failed");
     }
 }