|
|
#include "curlRequestsManager.h"
#define LOG( X ) m_pLog->Log( "CURL_RQM", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "CURL_RQM", this, _LOG_ERROR, X )
// Constructor implementation
curlRequestsManager::curlRequestsManager(){ m_pLog = basicLogCollection::getInstance(); curl_global_init(CURL_GLOBAL_ALL); m_pCURLM = curl_multi_init(); init();};
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; #ifdef DEBUG
LOG("Starting Finished Requests Reaper");#endif
m_bThreadStatus = TH_WORKING; 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( mMaxFD == -1 ){ SLEEP( 10 ); RetVal = 0; } 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(); index++){ 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; }; }; 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
m_mPoolProtector.lock(); CURLMcode RetCode = curl_multi_add_handle( m_pCURLM, RC->getEH()); if( RetCode == CURLM_OK ) { m_vRequestPool.push_back( RC ); m_mPoolProtector.unlock(); return true; } else { LOG_ERROR(curl_multi_strerror(RetCode)); }; m_mPoolProtector.unlock(); }; return false;};
bool curlRequestsManager::get(CSTR URL, CSTR contentType ){ curlRequest *tRC; tRC = new curlRequest( URL, contentType ); if (tRC != NULL){ return accomplish(tRC); }; return false;};
bool curlRequestsManager::post(CSTR URL, CSTR postData, CSTR contentType ){ curlRequest *tRC; tRC = new curlRequest( URL, contentType ); if (tRC != NULL){ tRC->setPOST( postData, strlen(postData) ); return accomplish(tRC); }; return false;};
#undef LOG
#undef LOG_ERROR
|