2012-06-08 09:55:00 -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-09-14 20:26:33 -04:00
|
|
|
#include "util/TokenQueueVEG.h"
|
2012-06-08 09:55:00 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
/******
|
|
|
|
* #define ID_DEBUG 1
|
|
|
|
*****/
|
|
|
|
|
|
|
|
/** Constructor */
|
2012-09-14 20:26:33 -04:00
|
|
|
TokenQueueVEG::TokenQueueVEG(RsTokenServiceVEG *service, TokenResponseVEG *resp)
|
2012-06-08 09:55:00 -04:00
|
|
|
:QWidget(NULL), mService(service), mResponder(resp)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
bool TokenQueueVEG::requestGroupInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptionsVEG &opts, std::list<std::string> ids, uint32_t usertype)
|
2012-06-08 09:55:00 -04:00
|
|
|
{
|
2012-06-13 20:36:25 -04:00
|
|
|
uint32_t basictype = TOKENREQ_GROUPINFO;
|
|
|
|
mService->requestGroupInfo(token, anstype, opts, ids);
|
|
|
|
queueRequest(token, basictype, anstype, usertype);
|
2012-06-08 09:55:00 -04:00
|
|
|
|
2012-06-13 20:36:25 -04:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-08 09:55:00 -04:00
|
|
|
|
2012-06-08 18:06:23 -04:00
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
bool TokenQueueVEG::requestMsgInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptionsVEG &opts, std::list<std::string> ids, uint32_t usertype)
|
2012-06-13 20:36:25 -04:00
|
|
|
{
|
|
|
|
uint32_t basictype = TOKENREQ_MSGINFO;
|
|
|
|
mService->requestMsgInfo(token, anstype, opts, ids);
|
|
|
|
queueRequest(token, basictype, anstype, usertype);
|
2012-06-08 09:55:00 -04:00
|
|
|
|
2012-06-13 20:36:25 -04:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-08 09:55:00 -04:00
|
|
|
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
bool TokenQueueVEG::requestMsgRelatedInfo(uint32_t &token, uint32_t anstype, const RsTokReqOptionsVEG &opts, std::list<std::string> ids, uint32_t usertype)
|
2012-06-13 20:36:25 -04:00
|
|
|
{
|
|
|
|
uint32_t basictype = TOKENREQ_MSGRELATEDINFO;
|
|
|
|
mService->requestMsgRelatedInfo(token, anstype, opts, ids);
|
|
|
|
queueRequest(token, basictype, anstype, usertype);
|
2012-06-08 09:55:00 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
void TokenQueueVEG::queueRequest(uint32_t token, uint32_t basictype, uint32_t anstype, uint32_t usertype)
|
2012-06-08 09:55:00 -04:00
|
|
|
{
|
2012-09-14 20:26:33 -04:00
|
|
|
std::cerr << "TokenQueueVEG::queueRequest() Token: " << token << " Type: " << basictype;
|
2012-06-13 20:36:25 -04:00
|
|
|
std::cerr << " AnsType: " << anstype << " UserType: " << usertype;
|
2012-06-08 09:55:00 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
TokenRequestVEG req;
|
2012-06-08 09:55:00 -04:00
|
|
|
req.mToken = token;
|
|
|
|
req.mType = basictype;
|
2012-06-13 20:36:25 -04:00
|
|
|
req.mAnsType = anstype;
|
2012-06-08 09:55:00 -04:00
|
|
|
req.mUserType = usertype;
|
|
|
|
|
|
|
|
gettimeofday(&req.mRequestTs, NULL);
|
|
|
|
req.mPollTs = req.mRequestTs;
|
|
|
|
|
|
|
|
mRequests.push_back(req);
|
|
|
|
|
|
|
|
if (mRequests.size() == 1)
|
|
|
|
{
|
|
|
|
/* start the timer */
|
|
|
|
doPoll(0.1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
void TokenQueueVEG::doPoll(float dt)
|
2012-06-08 09:55:00 -04:00
|
|
|
{
|
|
|
|
/* single shot poll */
|
|
|
|
//mTrigger->singlesshot(dt * 1000);
|
|
|
|
QTimer::singleShot((int) (dt * 1000.0), this, SLOT(pollRequests()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
void TokenQueueVEG::pollRequests()
|
2012-06-08 09:55:00 -04:00
|
|
|
{
|
2012-09-14 20:26:33 -04:00
|
|
|
std::list<TokenRequestVEG>::iterator it;
|
2012-06-08 09:55:00 -04:00
|
|
|
|
|
|
|
double pollPeriod = 1.0; // max poll period.
|
2012-07-31 08:15:58 -04:00
|
|
|
for(it = mRequests.begin(); it != mRequests.end();)
|
2012-06-08 09:55:00 -04:00
|
|
|
{
|
|
|
|
if (checkForRequest(it->mToken))
|
|
|
|
{
|
|
|
|
/* clean it up and handle */
|
|
|
|
loadRequest(*it);
|
|
|
|
it = mRequests.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* calculate desired poll period */
|
|
|
|
|
|
|
|
/* if less then current poll period, adjust */
|
|
|
|
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mRequests.size() > 0)
|
|
|
|
{
|
|
|
|
doPoll(pollPeriod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
bool TokenQueueVEG::checkForRequest(uint32_t token)
|
2012-06-08 09:55:00 -04:00
|
|
|
{
|
|
|
|
/* check token */
|
|
|
|
return (COMPLETED_REQUEST == mService->requestStatus(token));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
void TokenQueueVEG::loadRequest(const TokenRequestVEG &req)
|
2012-06-08 09:55:00 -04:00
|
|
|
{
|
2012-09-14 20:26:33 -04:00
|
|
|
std::cerr << "TokenQueueVEG::loadRequest(): ";
|
2012-06-13 20:36:25 -04:00
|
|
|
std::cerr << "Token: " << req.mToken << " Type: " << req.mType;
|
|
|
|
std::cerr << " AnsType: " << req.mAnsType << " UserType: " << req.mUserType;
|
2012-06-08 09:55:00 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
mResponder->loadRequest(this, req);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
bool TokenQueueVEG::cancelRequest(const uint32_t token)
|
2012-06-13 20:36:25 -04:00
|
|
|
{
|
|
|
|
/* cancel at lower level first */
|
|
|
|
mService->cancelRequest(token);
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
std::list<TokenRequestVEG>::iterator it;
|
2012-06-13 20:36:25 -04:00
|
|
|
|
|
|
|
for(it = mRequests.begin(); it != mRequests.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->mToken == token)
|
|
|
|
{
|
|
|
|
mRequests.erase(it);
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
std::cerr << "TokenQueueVEG::cancelRequest() Cleared Request: " << token;
|
2012-06-13 20:36:25 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 20:26:33 -04:00
|
|
|
std::cerr << "TokenQueueVEG::cancelRequest() Failed to Find Request: " << token;
|
2012-06-13 20:36:25 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-08 09:55:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|