boolean CRhodesApp::sendLog() { String strDevicePin = rho::sync::CClientRegister::getInstance() ? rho::sync::CClientRegister::getInstance()->getDevicePin() : ""; String strClientID = rho::sync::CSyncThread::getSyncEngine().readClientID(); String strLogUrl = RHOCONF().getPath("logserver"); if ( strLogUrl.length() == 0 ) strLogUrl = RHOCONF().getPath("syncserver"); String strQuery = strLogUrl + "client_log?" + "client_id=" + strClientID + "&device_pin=" + strDevicePin + "&log_name=" + RHOCONF().getString("logname"); net::CMultipartItem oItem; oItem.m_strFilePath = LOGCONF().getLogFilePath(); oItem.m_strContentType = "application/octet-stream"; boolean bOldSaveToFile = LOGCONF().isLogToFile(); LOGCONF().setLogToFile(false); NetRequest oNetRequest; oNetRequest.setSslVerifyPeer(false); NetResponse resp = getNetRequest(&oNetRequest).pushMultipartData( strQuery, oItem, &(rho::sync::CSyncThread::getSyncEngine()), null ); LOGCONF().setLogToFile(bOldSaveToFile); if ( !resp.isOK() ) { LOG(ERROR) + "send_log failed : network error - " + resp.getRespCode() + "; Body - " + resp.getCharData(); return false; } return true; }
void rho_do_send_log(rho::apiGenerator::CMethodResult& oResult) { String strDevicePin, strClientID; net::IRhoSession* pSession = 0; if ( sync::RhoconnectClientManager::haveRhoconnectClientImpl() ) { strDevicePin = rho::sync::RhoconnectClientManager::clientRegisterGetDevicePin(); strClientID = rho::sync::RhoconnectClientManager::syncEnineReadClientID(); pSession = rho::sync::RhoconnectClientManager::getRhoSession(); } String strQuery; String strLogUrl = RHOCONF().getString("Log.destinationURI"); if ( strLogUrl.length() == 0 ) { strLogUrl = RHOCONF().getPath("logserver"); if ( strLogUrl.length() == 0 ) strLogUrl = RHOCONF().getPath("syncserver"); strQuery = strLogUrl + "client_log?" + "client_id=" + strClientID + "&device_pin=" + strDevicePin + "&log_name=" + RHOCONF().getString("logname"); }else { String strSign = "?"; if ( strrchr(strLogUrl.c_str(), '?') ) strSign = "&"; strQuery = strLogUrl + strSign + "client_id=" + strClientID + "&device_pin=" + strDevicePin; } net::CMultipartItem oItem; oItem.m_strFilePath = LOGCONF().getLogFilePath(); oItem.m_strContentType = "application/octet-stream"; boolean bOldSaveToFile = LOGCONF().isLogToFile(); LOGCONF().setLogToFile(false); NetRequest oNetRequest; oNetRequest.setSslVerifyPeer(false); NetResponse resp = getNetRequest(&oNetRequest).pushMultipartData( strQuery, oItem, pSession, null ); LOGCONF().setLogToFile(bOldSaveToFile); boolean isOK = true; if ( !resp.isOK() ) { LOG(ERROR) + "send_log failed : network error - " + resp.getRespCode() + "; Body - " + resp.getCharData(); isOK = false; } Hashtable<String, String> hashRes; hashRes["status"] = isOK ? "ok" : "error"; oResult.set(hashRes); RHODESAPPBASE().setSendingLog(false); }
void run(common::CRhoThread &) { String strDevicePin = rho::sync::CClientRegister::getInstance() ? rho::sync::CClientRegister::getInstance()->getDevicePin() : ""; String strClientID = rho::sync::CSyncThread::getSyncEngine().readClientID(); String strLogUrl = RHOCONF().getPath("logserver"); if ( strLogUrl.length() == 0 ) strLogUrl = RHOCONF().getPath("syncserver"); String strQuery = strLogUrl + "client_log?" + "client_id=" + strClientID + "&device_pin=" + strDevicePin + "&log_name=" + RHOCONF().getString("logname"); net::CMultipartItem oItem; oItem.m_strFilePath = LOGCONF().getLogFilePath(); oItem.m_strContentType = "application/octet-stream"; boolean bOldSaveToFile = LOGCONF().isLogToFile(); LOGCONF().setLogToFile(false); NetRequest oNetRequest; oNetRequest.setSslVerifyPeer(false); NetResponse resp = getNetRequest(&oNetRequest).pushMultipartData( strQuery, oItem, &(rho::sync::CSyncThread::getSyncEngine()), null ); LOGCONF().setLogToFile(bOldSaveToFile); boolean isOK = true; if ( !resp.isOK() ) { LOG(ERROR) + "send_log failed : network error - " + resp.getRespCode() + "; Body - " + resp.getCharData(); isOK = false; } if (m_strCallback.length() > 0) { const char* body = isOK ? "rho_callback=1&status=ok" : "rho_callback=1&status=error"; rho_net_request_with_data(RHODESAPP().canonicalizeRhoUrl(m_strCallback).c_str(), body); } RHODESAPP().setSendingLog(false); }
//Add a get task to queue void NetClient::postMessage(const char* buffer, size_t n, const NetResponseCallback& callback) { if (false == lazyInitThreadSemphore()) { return; } ++asyncRequestCount; NetRequest* request = new NetRequest(); request->setRequestData(buffer, n); request->setCallback(callback); // request->retain(); this->requestqMtx.lock(); this->requestq.push_back(request); this->requestqMtx.unlock(); // Notify thread start to work this->sleepCondition.notify_one(); }
// Worker thread void NetClient::networkThread(void) { NetRequest *request = NULL; while (true) { if (needQuit) { break; } // step 1: send http request if the requestQueue isn't empty request = NULL; this->requestqMtx.lock(); //Get request task from queue if (!requestq.empty()) { request = dynamic_cast<NetRequest*>(*this->requestq.begin()); this->requestq.pop_front(); } this->requestqMtx.unlock(); if (NULL == request) { // Wait for http request tasks from main thread std::unique_lock<std::mutex> lk(this->sleepMutex); this->sleepCondition.wait(lk); continue; } // Create a HttpResponse object, the default setting is http access failed NetResponse *response = new NetResponse(request); // // // request's refcount = 2 here, it's retained by HttpRespose constructor // request->release(); // ok, refcount = 1 now, only HttpResponse hold it. int32_t responseCode = -1; this->client->set_response_buffer(response); this->client->start_request(request->getRequestData(), request->getRequestDataSize()); // retValue = this->processPostTask(request, write_data_callback, response, &responseCode); response->setResponseCode(this->client->get_error_code()); response->setSucceed(this->client->get_error_code() == 0); request->release(); // add response packet into queue this->responseqMtx.lock(); this->responseq.push_back(response); this->responseqMtx.unlock(); // resume dispatcher selector CCDirector::sharedDirector()->getScheduler()->resumeTarget(this); } asyncRequestCount -= this->requestq.size(); this->threadStarted = false; threadRunningCond.notify_one(); CCLOG("NetClient: thread exiting..."); }