You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
261 lines
6.4 KiB
261 lines
6.4 KiB
#include "curlRequestsManager.h"
|
|
|
|
#define LOG(X) m_pLog->Log("CURL_RQM", this, X)
|
|
#define LOG_ERROR(X) \
|
|
{ \
|
|
char _tBfrErrMSG2w34[1024]; \
|
|
sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
|
|
m_pLog->Log("CURL_RQM", this, _LOG_ERROR, _tBfrErrMSG2w34); \
|
|
}
|
|
|
|
// Constructor implementation
|
|
curlRequestsManager::curlRequestsManager() :
|
|
m_mPoolProtector()
|
|
{
|
|
m_pLog = basicLogCollection::getInstance();
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
m_pCURLM = curl_multi_init();
|
|
if (m_pCURLM)
|
|
{
|
|
init();
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Curl Muti Handle Cannot be created!!");
|
|
};
|
|
}
|
|
|
|
curlRequestsManager::~curlRequestsManager()
|
|
{
|
|
waitThreadEnd(true);
|
|
curl_multi_cleanup(m_pCURLM);
|
|
}
|
|
|
|
curlRequestsManager * curlRequestsManager::_instance = 0;
|
|
|
|
/**
|
|
* Returns the single instance of the object.
|
|
*/
|
|
curlRequestsManager * curlRequestsManager::getInstance()
|
|
{
|
|
// check if the instance has been created yet
|
|
if (0 == _instance)
|
|
{
|
|
// if not, then create it
|
|
_instance = new curlRequestsManager();
|
|
}
|
|
// return the single instance
|
|
return _instance;
|
|
}
|
|
|
|
void curlRequestsManager::m_fThreadProc(void)
|
|
{
|
|
CURLMsg * mMsg = NULL; /* for picking up messages with the transfer status */
|
|
int nMsgsLeft = 0; /* how many messages are left */
|
|
struct timeval mTimeOut;
|
|
int RetVal; /* select() return code */
|
|
CURLMcode mRetCode; /* curl_multi_fdset() return code */
|
|
fd_set mReadFD;
|
|
fd_set mWriteFD;
|
|
fd_set mExcepFD;
|
|
int mMaxFD = -1;
|
|
long mCURLTimeOut;
|
|
int mStillRunning;
|
|
bool bProceedThread = false;
|
|
#ifdef DEBUG
|
|
LOG("Starting Finished Requests Reaper");
|
|
#endif
|
|
pthread_setname_np( pthread_self(), "cURL Manager" );
|
|
// BugCurl Multi cannot starty if MR = empty ....
|
|
m_bThreadStatus = TH_WORKING;
|
|
while ((m_bThreadStatus < TH_ENDED) && ! bProceedThread)
|
|
{
|
|
SLEEP(10);
|
|
m_mPoolProtector.lock();
|
|
if (m_vRequestPool.size())
|
|
{
|
|
bProceedThread = true;
|
|
}
|
|
m_mPoolProtector.unlock();
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("First Req Enqueued");
|
|
#endif
|
|
while (m_bThreadStatus < TH_ENDED)
|
|
{
|
|
/* we start some action by calling perform right away */
|
|
curl_multi_perform(m_pCURLM, &mStillRunning);
|
|
do
|
|
{
|
|
curl_multi_timeout(m_pCURLM, &mCURLTimeOut);
|
|
if (mCURLTimeOut < 0)
|
|
{
|
|
mCURLTimeOut = 1000;
|
|
}
|
|
|
|
mTimeOut.tv_sec = mCURLTimeOut / 1000;
|
|
mTimeOut.tv_usec = (mCURLTimeOut % 1000) * 1000;
|
|
|
|
FD_ZERO(&mReadFD);
|
|
FD_ZERO(&mWriteFD);
|
|
FD_ZERO(&mExcepFD);
|
|
/* get file descriptors from the transfers */
|
|
mRetCode = curl_multi_fdset(m_pCURLM, &mReadFD, &mWriteFD, &mExcepFD, &mMaxFD);
|
|
if (mRetCode != CURLM_OK)
|
|
{
|
|
char tT[256];
|
|
sprintf(tT, "curl_multi_fdset() failed, code %d!", mRetCode);
|
|
LOG_ERROR(tT);
|
|
break;
|
|
};
|
|
if (mMaxFD == -1)
|
|
{
|
|
SLEEP(10);
|
|
RetVal = 0;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
RetVal = select(mMaxFD + 1, &mReadFD, &mWriteFD, &mExcepFD, &mTimeOut);
|
|
};
|
|
switch (RetVal)
|
|
{
|
|
case -1:
|
|
/* select error */
|
|
break;
|
|
case 0:
|
|
default:
|
|
/* timeout or readable/writable sockets */
|
|
curl_multi_perform(m_pCURLM, &mStillRunning);
|
|
break;
|
|
};
|
|
/* See how the transfers went */
|
|
while ((mMsg = curl_multi_info_read(m_pCURLM, &nMsgsLeft)) && (m_bThreadStatus < TH_ENDED))
|
|
{
|
|
if (mMsg->msg == CURLMSG_DONE)
|
|
{
|
|
CURLcode rC = mMsg->data.result;
|
|
#ifdef DEBUG_EXTRA
|
|
char tB[256];
|
|
sprintf(tB, "Request Done with Code: %d", rC);
|
|
LOG(tB);
|
|
#endif
|
|
/* Find out which handle this message is about */
|
|
m_mPoolProtector.lock();
|
|
for (RequestPool::iterator index = m_vRequestPool.begin(); index < m_vRequestPool.end();)
|
|
{
|
|
if ((*index)->getEH() == mMsg->easy_handle)
|
|
{
|
|
mRetCode = curl_multi_remove_handle(m_pCURLM, (*index)->getEH());
|
|
if (rC == CURLE_OK)
|
|
{
|
|
(*index)->issue();
|
|
};
|
|
// CURLE_OPERATION_TIMEDOUT
|
|
delete(*index);
|
|
m_vRequestPool.erase(index);
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
index++;
|
|
};
|
|
};
|
|
m_mPoolProtector.unlock();
|
|
};
|
|
};
|
|
}
|
|
while (mStillRunning && (m_bThreadStatus < TH_ENDED)); /* if there are still transfers, loop! */
|
|
|
|
/* call curl_multi_perform or curl_multi_socket_action first, then loop
|
|
through and check if there are any transfers that have completed */
|
|
/* See how the transfers went */
|
|
/*
|
|
while( ( mMsg = curl_multi_info_read( m_pCURLM, &nMsgsLeft ) ) && ( m_bThreadStatus < TH_ENDED ) ){
|
|
char tB[129];
|
|
printf( tB, "[%d] Msgs Left", nMsgsLeft);
|
|
LOG( tB );
|
|
if( mMsg->msg == CURLMSG_DONE ){
|
|
// Find out which handle this message is about
|
|
LOG("REQ -> Done!");
|
|
m_mPoolProtector.lock();
|
|
for (RequestPool::iterator index = m_vRequestPool.begin(); index < m_vRequestPool.end(); index++){
|
|
if ((*index)->getEH() == mMsg->easy_handle){
|
|
mRetCode = curl_multi_remove_handle( m_pCURLM, (*index)->getEH());
|
|
(*index)->issue();
|
|
delete( (*index) );
|
|
m_vRequestPool.erase( index );
|
|
break;
|
|
};
|
|
};
|
|
m_mPoolProtector.unlock();
|
|
};
|
|
};
|
|
*/
|
|
SLEEP(10);
|
|
};
|
|
#ifdef DEBUG
|
|
LOG("Ending Finished Requests Reaper");
|
|
#endif
|
|
}
|
|
|
|
bool curlRequestsManager::accomplish(curlRequest * RC)
|
|
{
|
|
if (RC)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("New Request");
|
|
#endif
|
|
CURLMcode RetCode = curl_multi_add_handle(m_pCURLM, RC->getEH());
|
|
if (RetCode == CURLM_OK)
|
|
{
|
|
m_mPoolProtector.lock();
|
|
m_vRequestPool.push_back(RC);
|
|
m_mPoolProtector.unlock();
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Added OK");
|
|
#endif
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR(curl_multi_strerror(RetCode));
|
|
};
|
|
// The manager owns the request: on success it is deleted when completed,
|
|
// on failure it must be deleted here (no caller uses it afterwards).
|
|
delete (RC);
|
|
};
|
|
return false;
|
|
}
|
|
|
|
bool curlRequestsManager::get(CSTR URL, CSTR contentType)
|
|
{
|
|
curlRequest * tRC;
|
|
tRC = new curlRequest(URL, contentType);
|
|
if (tRC != NULL)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Adding GET RQ");
|
|
#endif
|
|
return accomplish(tRC);
|
|
};
|
|
return false;
|
|
}
|
|
|
|
bool curlRequestsManager::post(CSTR URL, CSTR postData, CSTR contentType)
|
|
{
|
|
curlRequest * tRC;
|
|
tRC = new curlRequest(URL, contentType);
|
|
if (tRC != NULL)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Adding POST RQ");
|
|
#endif
|
|
tRC->setPOST(postData, strlen(postData));
|
|
return accomplish(tRC);
|
|
};
|
|
return false;
|
|
}
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|