2012-08-03 02:39:50 -04:00
|
|
|
/*
|
|
|
|
* Token Queue.
|
|
|
|
*
|
|
|
|
* Copyright 2012-2012 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2.1 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
#include "util/TokenQueue.h"
|
2012-08-03 02:39:50 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
/******
|
|
|
|
* #define ID_DEBUG 1
|
|
|
|
*****/
|
|
|
|
|
|
|
|
/** Constructor */
|
2012-10-21 15:45:35 -04:00
|
|
|
TokenQueue::TokenQueue(RsTokenService *service, TokenResponse *resp)
|
2012-11-25 18:44:51 -05:00
|
|
|
: QWidget(NULL), mService(service), mResponder(resp)
|
2012-08-03 02:39:50 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
bool TokenQueue::requestGroupInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptions &opts, std::list<RsGxsGroupId>& ids, uint32_t usertype)
|
2012-08-03 02:39:50 -04:00
|
|
|
{
|
|
|
|
uint32_t basictype = TOKENREQ_GROUPINFO;
|
|
|
|
mService->requestGroupInfo(token, anstype, opts, ids);
|
|
|
|
queueRequest(token, basictype, anstype, usertype);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
bool TokenQueue::requestGroupInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptions &opts, uint32_t usertype)
|
2012-09-13 18:58:42 -04:00
|
|
|
{
|
2012-11-25 18:44:51 -05:00
|
|
|
uint32_t basictype = TOKENREQ_GROUPINFO;
|
|
|
|
mService->requestGroupInfo(token, anstype, opts);
|
|
|
|
queueRequest(token, basictype, anstype, usertype);
|
2012-09-13 18:58:42 -04:00
|
|
|
|
2012-11-25 18:44:51 -05:00
|
|
|
return true;
|
2012-09-13 18:58:42 -04:00
|
|
|
}
|
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
bool TokenQueue::requestMsgInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptions &opts, const GxsMsgReq& ids, uint32_t usertype)
|
2012-08-03 02:39:50 -04:00
|
|
|
{
|
|
|
|
uint32_t basictype = TOKENREQ_MSGINFO;
|
|
|
|
mService->requestMsgInfo(token, anstype, opts, ids);
|
|
|
|
queueRequest(token, basictype, anstype, usertype);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-13 17:36:06 -05:00
|
|
|
bool TokenQueue::requestMsgRelatedInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptions &opts, const std::vector<RsGxsGrpMsgIdPair> &msgId, uint32_t usertype)
|
2012-09-17 18:08:23 -04:00
|
|
|
{
|
2012-11-25 18:44:51 -05:00
|
|
|
uint32_t basictype = TOKENREQ_MSGINFO;
|
|
|
|
mService->requestMsgRelatedInfo(token, anstype, opts, msgId);
|
|
|
|
queueRequest(token, basictype, anstype, usertype);
|
2012-09-17 18:08:23 -04:00
|
|
|
|
2012-11-25 18:44:51 -05:00
|
|
|
return true;
|
2012-09-17 18:08:23 -04:00
|
|
|
}
|
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
bool TokenQueue::requestMsgInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptions &opts,
|
2012-11-25 18:44:51 -05:00
|
|
|
const std::list<RsGxsGroupId> &grpIds, uint32_t usertype)
|
2012-09-13 18:58:42 -04:00
|
|
|
{
|
2012-11-25 18:44:51 -05:00
|
|
|
uint32_t basictype = TOKENREQ_MSGINFO;
|
|
|
|
mService->requestMsgInfo(token, anstype, opts, grpIds);
|
|
|
|
queueRequest(token, basictype, anstype, usertype);
|
2012-09-13 18:58:42 -04:00
|
|
|
|
2012-11-25 18:44:51 -05:00
|
|
|
return true;
|
2012-09-13 18:58:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
void TokenQueue::queueRequest(uint32_t token, uint32_t basictype, uint32_t anstype, uint32_t usertype)
|
2012-08-03 02:39:50 -04:00
|
|
|
{
|
2012-11-25 18:44:51 -05:00
|
|
|
std::cerr << "TokenQueue::queueRequest() Token: " << token << " Type: " << basictype;
|
2012-08-03 02:39:50 -04:00
|
|
|
std::cerr << " AnsType: " << anstype << " UserType: " << usertype;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2012-11-25 18:44:51 -05:00
|
|
|
TokenRequest req;
|
2012-08-03 02:39:50 -04:00
|
|
|
req.mToken = token;
|
|
|
|
req.mType = basictype;
|
|
|
|
req.mAnsType = anstype;
|
|
|
|
req.mUserType = usertype;
|
|
|
|
|
|
|
|
gettimeofday(&req.mRequestTs, NULL);
|
|
|
|
req.mPollTs = req.mRequestTs;
|
|
|
|
|
2012-12-05 17:45:44 -05:00
|
|
|
mTokenMtx.lock();
|
2012-11-25 18:44:51 -05:00
|
|
|
mRequests.push_back(req);
|
2012-12-05 17:45:44 -05:00
|
|
|
mTokenMtx.unlock();
|
2012-08-03 02:39:50 -04:00
|
|
|
|
|
|
|
if (mRequests.size() == 1)
|
|
|
|
{
|
|
|
|
/* start the timer */
|
|
|
|
doPoll(0.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
void TokenQueue::doPoll(float dt)
|
2012-08-03 02:39:50 -04:00
|
|
|
{
|
|
|
|
/* single shot poll */
|
|
|
|
//mTrigger->singlesshot(dt * 1000);
|
|
|
|
QTimer::singleShot((int) (dt * 1000.0), this, SLOT(pollRequests()));
|
|
|
|
}
|
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
void TokenQueue::pollRequests()
|
2012-08-03 02:39:50 -04:00
|
|
|
{
|
|
|
|
double pollPeriod = 1.0; // max poll period.
|
|
|
|
|
2012-11-25 18:44:51 -05:00
|
|
|
if (mRequests.empty()) {
|
2012-11-12 15:47:55 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-25 18:44:51 -05:00
|
|
|
TokenRequest req;
|
2012-12-05 17:45:44 -05:00
|
|
|
|
|
|
|
mTokenMtx.lock();
|
2012-11-25 18:44:51 -05:00
|
|
|
req = mRequests.front();
|
|
|
|
mRequests.pop_front();
|
2012-12-05 17:45:44 -05:00
|
|
|
mTokenMtx.unlock();
|
2012-09-30 10:21:17 -04:00
|
|
|
|
2012-11-25 18:44:51 -05:00
|
|
|
if (checkForRequest(req.mToken))
|
|
|
|
{
|
|
|
|
/* clean it up and handle */
|
|
|
|
loadRequest(req);
|
|
|
|
}
|
2012-11-12 15:47:55 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
#define MAX_REQUEST_AGE 30
|
|
|
|
|
|
|
|
/* drop old requests too */
|
|
|
|
if (time(NULL) - req.mRequestTs.tv_sec < MAX_REQUEST_AGE)
|
|
|
|
{
|
2012-12-05 17:45:44 -05:00
|
|
|
mTokenMtx.lock();
|
2012-11-25 18:44:51 -05:00
|
|
|
mRequests.push_back(req);
|
2012-12-05 17:45:44 -05:00
|
|
|
mTokenMtx.unlock();
|
2012-11-12 15:47:55 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-25 18:44:51 -05:00
|
|
|
std::cerr << "TokenQueue::loadRequest(): ";
|
2012-11-12 15:47:55 -05:00
|
|
|
std::cerr << "Dropping old Token: " << req.mToken << " Type: " << req.mType;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 02:39:50 -04:00
|
|
|
|
|
|
|
if (mRequests.size() > 0)
|
|
|
|
{
|
|
|
|
doPoll(pollPeriod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
bool TokenQueue::checkForRequest(uint32_t token)
|
2012-08-03 02:39:50 -04:00
|
|
|
{
|
|
|
|
/* check token */
|
2012-11-25 18:44:51 -05:00
|
|
|
uint32_t status = mService->requestStatus(token);
|
|
|
|
return ( (RsTokenService::GXS_REQUEST_V2_STATUS_FAILED == status) ||
|
|
|
|
(RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE == status) );
|
2012-08-03 02:39:50 -04:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:45:44 -05:00
|
|
|
bool TokenQueue::activeRequestExist(const uint32_t& userType)
|
|
|
|
{
|
|
|
|
mTokenMtx.lock();
|
|
|
|
|
|
|
|
std::list<TokenRequest>::const_iterator lit = mRequests.begin();
|
|
|
|
|
|
|
|
for(; lit != mRequests.end(); lit++)
|
|
|
|
{
|
|
|
|
const TokenRequest& req = *lit;
|
|
|
|
|
|
|
|
if(req.mUserType == userType)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTokenMtx.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TokenQueue::activeRequestTokens(const uint32_t& userType, std::list<uint32_t>& tokens)
|
|
|
|
{
|
|
|
|
mTokenMtx.lock();
|
|
|
|
|
|
|
|
std::list<TokenRequest>::const_iterator lit = mRequests.begin();
|
|
|
|
|
|
|
|
for(; lit != mRequests.end(); lit++)
|
|
|
|
{
|
|
|
|
const TokenRequest& req = *lit;
|
|
|
|
|
|
|
|
if(req.mUserType == userType)
|
|
|
|
tokens.push_back(req.mToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
mTokenMtx.unlock();
|
|
|
|
}
|
2012-08-03 02:39:50 -04:00
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
void TokenQueue::loadRequest(const TokenRequest &req)
|
2012-08-03 02:39:50 -04:00
|
|
|
{
|
2012-11-25 18:44:51 -05:00
|
|
|
std::cerr << "TokenQueue::loadRequest(): ";
|
2012-08-03 02:39:50 -04:00
|
|
|
std::cerr << "Token: " << req.mToken << " Type: " << req.mType;
|
|
|
|
std::cerr << " AnsType: " << req.mAnsType << " UserType: " << req.mUserType;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
mResponder->loadRequest(this, req);
|
|
|
|
}
|
|
|
|
|
2012-10-21 15:45:35 -04:00
|
|
|
bool TokenQueue::cancelRequest(const uint32_t token)
|
2012-08-03 02:39:50 -04:00
|
|
|
{
|
|
|
|
/* cancel at lower level first */
|
|
|
|
mService->cancelRequest(token);
|
|
|
|
|
2012-11-25 18:44:51 -05:00
|
|
|
std::list<TokenRequest>::iterator it;
|
2012-08-03 02:39:50 -04:00
|
|
|
|
2012-12-05 17:45:44 -05:00
|
|
|
mTokenMtx.lock();
|
2012-08-03 02:39:50 -04:00
|
|
|
for(it = mRequests.begin(); it != mRequests.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->mToken == token)
|
|
|
|
{
|
|
|
|
mRequests.erase(it);
|
|
|
|
|
2012-11-25 18:44:51 -05:00
|
|
|
std::cerr << "TokenQueue::cancelRequest() Cleared Request: " << token;
|
2012-08-03 02:39:50 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-12-05 17:45:44 -05:00
|
|
|
mTokenMtx.unlock();
|
2012-08-03 02:39:50 -04:00
|
|
|
|
2012-11-25 18:44:51 -05:00
|
|
|
std::cerr << "TokenQueue::cancelRequest() Failed to Find Request: " << token;
|
2012-08-03 02:39:50 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|