Exemple #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");
    }
}
 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");
     }
 }