diff --git a/retroshare-gui/src/util/RsProtectedTimer.cpp b/retroshare-gui/src/util/RsProtectedTimer.cpp index b6c648773..24f017adb 100644 --- a/retroshare-gui/src/util/RsProtectedTimer.cpp +++ b/retroshare-gui/src/util/RsProtectedTimer.cpp @@ -24,17 +24,27 @@ #include "RsProtectedTimer.h" +#define TIMER_FAST_POLL 499 // unique time in RetroShare + //#define PROTECTED_TIMER_DEBUG RsProtectedTimer::RsProtectedTimer(QObject *parent) : QTimer(parent) { + mInterval = 0; } void RsProtectedTimer::timerEvent(QTimerEvent *e) { if(RsAutoUpdatePage::eventsLocked()) { + if (!mInterval && interval() > TIMER_FAST_POLL) { + /* Save interval */ + mInterval = interval(); + /* Set fast interval */ + setInterval(TIMER_FAST_POLL); // restart timer + } + #ifdef PROTECTED_TIMER_DEBUG if (isSingleShot()) { /* Singleshot timer will be stopped in QTimer::timerEvent */ @@ -56,4 +66,15 @@ void RsProtectedTimer::timerEvent(QTimerEvent *e) #endif QTimer::timerEvent(e) ; + + if (mInterval) { + if (interval() == TIMER_FAST_POLL) { + /* Still fast poll */ + if (!isSingleShot()) { + /* Restore interval */ + setInterval(mInterval); // restart timer + } + } + mInterval = 0; + } } diff --git a/retroshare-gui/src/util/RsProtectedTimer.h b/retroshare-gui/src/util/RsProtectedTimer.h index e6069aaf6..e97cd1d4d 100644 --- a/retroshare-gui/src/util/RsProtectedTimer.h +++ b/retroshare-gui/src/util/RsProtectedTimer.h @@ -31,6 +31,10 @@ public: protected: virtual void timerEvent(QTimerEvent *e); +private: // do not use, please use setInterval, setSingleShot and connect signal timeout static void singleShot(int /*msec*/, QObject */*receiver*/, const char */*member*/) {} + +private: + int mInterval; }; diff --git a/retroshare-gui/src/util/TokenQueue.cpp b/retroshare-gui/src/util/TokenQueue.cpp index afdf092fc..d2ecc722a 100644 --- a/retroshare-gui/src/util/TokenQueue.cpp +++ b/retroshare-gui/src/util/TokenQueue.cpp @@ -22,7 +22,7 @@ */ #include "util/TokenQueue.h" -#include "retroshare-gui/RsAutoUpdatePage.h" +#include "util/RsProtectedTimer.h" #include #include @@ -35,6 +35,9 @@ TokenQueue::TokenQueue(RsTokenService *service, TokenResponse *resp) : QObject(NULL), mService(service), mResponder(resp) { + mTrigger = new RsProtectedTimer(this); + mTrigger->setSingleShot(true); + connect(mTrigger, SIGNAL(timeout()), this, SLOT(pollRequests())); } bool TokenQueue::requestGroupInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptions &opts, std::list& ids, uint32_t usertype) @@ -73,8 +76,7 @@ bool TokenQueue::requestMsgRelatedInfo(uint32_t &token, uint32_t anstype, const return true; } -bool TokenQueue::requestMsgInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptions &opts, - const std::list &grpIds, uint32_t usertype) +bool TokenQueue::requestMsgInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptions &opts, const std::list &grpIds, uint32_t usertype) { uint32_t basictype = TOKENREQ_MSGINFO; mService->requestMsgInfo(token, anstype, opts, grpIds); @@ -83,7 +85,6 @@ bool TokenQueue::requestMsgInfo(uint32_t &token, uint32_t anstype, const RsTokRe return true; } - void TokenQueue::queueRequest(uint32_t token, uint32_t basictype, uint32_t anstype, uint32_t usertype) { std::cerr << "TokenQueue::queueRequest() Token: " << token << " Type: " << basictype; @@ -99,10 +100,8 @@ void TokenQueue::queueRequest(uint32_t token, uint32_t basictype, uint32_t ansty gettimeofday(&req.mRequestTs, NULL); req.mPollTs = req.mRequestTs; - mRequests.push_back(req); - if (mRequests.size() == 1) { /* start the timer */ @@ -113,15 +112,11 @@ void TokenQueue::queueRequest(uint32_t token, uint32_t basictype, uint32_t ansty void TokenQueue::doPoll(float dt) { /* single shot poll */ - //mTrigger->singlesshot(dt * 1000); - QTimer::singleShot((int) (dt * 1000.0), this, SLOT(pollRequests())); + mTrigger->start(dt * 1000); } void TokenQueue::pollRequests() { - if(RsAutoUpdatePage::eventsLocked()) - return ; - double pollPeriod = 1.0; // max poll period. if (mRequests.empty()) { @@ -130,11 +125,9 @@ void TokenQueue::pollRequests() TokenRequest req; - req = mRequests.front(); mRequests.pop_front(); - if (checkForRequest(req.mToken)) { /* clean it up and handle */ @@ -148,9 +141,7 @@ void TokenQueue::pollRequests() /* drop old requests too */ if (time(NULL) - req.mRequestTs.tv_sec < MAX_REQUEST_AGE) { - mRequests.push_back(req); - } else { @@ -176,8 +167,6 @@ bool TokenQueue::checkForRequest(uint32_t token) bool TokenQueue::activeRequestExist(const uint32_t& userType) const { - - std::list::const_iterator lit = mRequests.begin(); for(; lit != mRequests.end(); lit++) @@ -186,20 +175,15 @@ bool TokenQueue::activeRequestExist(const uint32_t& userType) const if(req.mUserType == userType) { - return true; } } - - return false; } void TokenQueue::activeRequestTokens(const uint32_t& userType, std::list& tokens) const { - - std::list::const_iterator lit = mRequests.begin(); for(; lit != mRequests.end(); lit++) @@ -209,8 +193,6 @@ void TokenQueue::activeRequestTokens(const uint32_t& userType, std::list::iterator it; - for(it = mRequests.begin(); it != mRequests.end(); it++) { if (it->mToken == token) @@ -252,13 +233,10 @@ bool TokenQueue::cancelRequest(const uint32_t token) std::cerr << "TokenQueue::cancelRequest() Cleared Request: " << token; std::cerr << std::endl; - - return true; } } - std::cerr << "TokenQueue::cancelRequest() Failed to Find Request: " << token; std::cerr << std::endl; diff --git a/retroshare-gui/src/util/TokenQueue.h b/retroshare-gui/src/util/TokenQueue.h index 16e212832..e1b8d8ecd 100644 --- a/retroshare-gui/src/util/TokenQueue.h +++ b/retroshare-gui/src/util/TokenQueue.h @@ -33,12 +33,11 @@ #include -#define COMPLETED_REQUEST 4 - -#define TOKENREQ_GROUPINFO 1 -#define TOKENREQ_MSGINFO 2 -#define TOKENREQ_MSGRELATEDINFO 3 +#define COMPLETED_REQUEST 4 +#define TOKENREQ_GROUPINFO 1 +#define TOKENREQ_MSGINFO 2 +#define TOKENREQ_MSGRELATEDINFO 3 class TokenQueue; @@ -114,7 +113,7 @@ private: std::list mRequests; RsTokenService *mService; - TokenResponse *mResponder; + TokenResponse *mResponder; QTimer *mTrigger; };