2007-11-14 22:18:48 -05:00
|
|
|
/*
|
|
|
|
* "$Id: pqihandler.cc,v 1.12 2007-03-31 09:41:32 rmf24 Exp $"
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2006 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 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pqi/pqihandler.h"
|
|
|
|
|
2008-07-10 12:29:18 -04:00
|
|
|
#include "util/rsdebug.h"
|
2012-04-13 20:30:23 -04:00
|
|
|
#include "util/rsstring.h"
|
2009-06-08 13:09:00 -04:00
|
|
|
#include <stdlib.h>
|
2007-11-14 22:18:48 -05:00
|
|
|
const int pqihandlerzone = 34283;
|
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
static const int PQI_HANDLER_NB_PRIORITY_LEVELS = 10 ;
|
|
|
|
static const float PQI_HANDLER_NB_PRIORITY_RATIO = 2 ;
|
|
|
|
|
2008-04-03 08:51:28 -04:00
|
|
|
/****
|
|
|
|
#define DEBUG_TICK 1
|
|
|
|
#define RSITEM_DEBUG 1
|
|
|
|
****/
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-06-23 08:10:41 -04:00
|
|
|
pqihandler::pqihandler(SecurityPolicy *Global) : coreMtx("pqihandler")
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// The global security....
|
|
|
|
// if something is disabled here...
|
|
|
|
// cannot be enabled by module.
|
|
|
|
globsec = Global;
|
|
|
|
|
2012-04-13 20:30:23 -04:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "New pqihandler()\nSecurity Policy: " + secpolicy_print(globsec));
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// setup minimal total+individual rates.
|
|
|
|
rateIndiv_out = 0.01;
|
|
|
|
rateIndiv_in = 0.01;
|
|
|
|
rateMax_out = 0.01;
|
|
|
|
rateMax_in = 0.01;
|
2011-09-04 16:01:30 -04:00
|
|
|
last_m = time(NULL) ;
|
|
|
|
nb_ticks = 0 ;
|
|
|
|
ticks_per_sec = 5 ; // initial guess
|
2007-11-14 22:18:48 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pqihandler::tick()
|
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
int moreToTick = 0;
|
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2008-11-22 08:15:07 -05:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
// tick all interfaces...
|
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
|
|
|
for(it = mods.begin(); it != mods.end(); it++)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
if (0 < ((it -> second) -> pqi) -> tick())
|
|
|
|
{
|
2008-04-03 08:51:28 -04:00
|
|
|
#ifdef DEBUG_TICK
|
2011-09-04 16:01:30 -04:00
|
|
|
std::cerr << "pqihandler::tick() moreToTick from mod()" << std::endl;
|
2008-04-03 08:51:28 -04:00
|
|
|
#endif
|
2011-09-04 16:01:30 -04:00
|
|
|
moreToTick = 1;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2011-09-04 16:01:30 -04:00
|
|
|
// get the items, and queue them correctly
|
|
|
|
if (0 < locked_GetItems())
|
|
|
|
{
|
2008-04-03 08:51:28 -04:00
|
|
|
#ifdef DEBUG_TICK
|
2011-09-04 16:01:30 -04:00
|
|
|
std::cerr << "pqihandler::tick() moreToTick from GetItems()" << std::endl;
|
2008-04-03 08:51:28 -04:00
|
|
|
#endif
|
2011-09-04 16:01:30 -04:00
|
|
|
moreToTick = 1;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2011-09-04 16:01:30 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
UpdateRates();
|
|
|
|
return moreToTick;
|
|
|
|
}
|
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
|
|
|
|
bool pqihandler::queueOutRsItem(RsItem *item)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2012-06-23 08:10:41 -04:00
|
|
|
|
|
|
|
uint32_t size ;
|
|
|
|
locked_HandleRsItem(item, 0, size);
|
2011-09-04 16:01:30 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_QOS
|
|
|
|
if(item->priority_level() == QOS_PRIORITY_UNKNOWN)
|
|
|
|
std::cerr << "Caught an unprioritized item !" << std::endl;
|
|
|
|
|
|
|
|
print() ;
|
|
|
|
#endif
|
|
|
|
return true ;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
int pqihandler::status()
|
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
{ // for output
|
2012-04-13 20:30:23 -04:00
|
|
|
std::string out = "pqihandler::status() Active Modules:\n";
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-04-13 20:30:23 -04:00
|
|
|
// display all interfaces...
|
|
|
|
for(it = mods.begin(); it != mods.end(); it++)
|
|
|
|
{
|
|
|
|
rs_sprintf_append(out, "\tModule [%s] Pointer <%p>", it -> first.c_str(), (void *) ((it -> second) -> pqi));
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-04-13 20:30:23 -04:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
} // end of output.
|
|
|
|
|
|
|
|
|
|
|
|
// status all interfaces...
|
|
|
|
for(it = mods.begin(); it != mods.end(); it++)
|
|
|
|
{
|
|
|
|
((it -> second) -> pqi) -> status();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
bool pqihandler::AddSearchModule(SearchModule *mod)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2010-01-14 17:50:27 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2007-12-11 20:29:14 -05:00
|
|
|
// if peerid used -> error.
|
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
|
|
|
if (mod->peerid != mod->pqi->PeerId())
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
// ERROR!
|
2012-04-13 20:30:23 -04:00
|
|
|
pqioutput(PQL_ALERT, pqihandlerzone, "ERROR peerid != PeerId!");
|
2007-12-11 20:29:14 -05:00
|
|
|
return false;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
|
|
|
|
if (mod->peerid == "")
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
// ERROR!
|
2012-04-13 20:30:23 -04:00
|
|
|
pqioutput(PQL_ALERT, pqihandlerzone, "ERROR peerid == NULL");
|
2007-12-11 20:29:14 -05:00
|
|
|
return false;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
if (mods.find(mod->peerid) != mods.end())
|
|
|
|
{
|
|
|
|
// ERROR!
|
2012-04-13 20:30:23 -04:00
|
|
|
pqioutput(PQL_ALERT, pqihandlerzone, "ERROR PeerId Module already exists!");
|
2007-12-11 20:29:14 -05:00
|
|
|
return false;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// check security.
|
|
|
|
if (mod -> sp == NULL)
|
|
|
|
{
|
|
|
|
// create policy.
|
|
|
|
mod -> sp = secpolicy_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
// limit to what global security allows.
|
|
|
|
secpolicy_limit(globsec, mod -> sp);
|
|
|
|
|
|
|
|
// store.
|
2007-12-11 20:29:14 -05:00
|
|
|
mods[mod->peerid] = mod;
|
|
|
|
return true;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
bool pqihandler::RemoveSearchModule(SearchModule *mod)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2007-12-11 20:29:14 -05:00
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
for(it = mods.begin(); it != mods.end(); it++)
|
|
|
|
{
|
|
|
|
if (mod == it -> second)
|
|
|
|
{
|
|
|
|
mods.erase(it);
|
2007-12-11 20:29:14 -05:00
|
|
|
return true;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
return false;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// dummy output check
|
2012-11-25 11:36:55 -05:00
|
|
|
int pqihandler::locked_checkOutgoingRsItem(RsItem * /*item*/, int /*global*/)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-07-18 20:06:09 -04:00
|
|
|
//pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "pqihandler::checkOutgoingPQItem() NULL fn");
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// generalised output
|
2011-09-04 16:01:30 -04:00
|
|
|
int pqihandler::locked_HandleRsItem(RsItem *item, int allowglobal,uint32_t& computed_size)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
computed_size = 0 ;
|
2007-12-11 20:29:14 -05:00
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
|
2007-12-11 20:29:14 -05:00
|
|
|
"pqihandler::HandleRsItem()");
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
/* simplified to no global! */
|
|
|
|
if (allowglobal)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
/* error */
|
2012-04-13 20:30:23 -04:00
|
|
|
std::string out = "pqihandler::HandleSearchItem() Cannot send out Global RsItem";
|
|
|
|
pqioutput(PQL_ALERT, pqihandlerzone, out);
|
2009-12-13 16:59:26 -05:00
|
|
|
#ifdef DEBUG_TICK
|
2012-04-13 20:30:23 -04:00
|
|
|
std::cerr << out << std::endl;
|
2009-12-13 16:59:26 -05:00
|
|
|
#endif
|
2007-12-11 20:29:14 -05:00
|
|
|
delete item;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
if (!locked_checkOutgoingRsItem(item, allowglobal))
|
2007-12-11 20:29:14 -05:00
|
|
|
{
|
2012-04-13 20:30:23 -04:00
|
|
|
std::string out = "pqihandler::HandleRsItem() checkOutgoingPQItem Failed on item: \n";
|
2009-12-13 16:59:26 -05:00
|
|
|
#ifdef DEBUG_TICK
|
2012-04-13 20:30:23 -04:00
|
|
|
std::cerr << out;
|
2009-12-13 16:59:26 -05:00
|
|
|
#endif
|
2012-04-13 20:30:23 -04:00
|
|
|
item -> print_string(out);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-04-13 20:30:23 -04:00
|
|
|
pqioutput(PQL_ALERT, pqihandlerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
delete item;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
|
|
|
|
"pqihandler::HandleRsItem() Sending to One Channel");
|
2009-12-13 16:59:26 -05:00
|
|
|
#ifdef DEBUG_TICK
|
|
|
|
std::cerr << "pqihandler::HandleRsItem() Sending to One Channel" << std::endl;
|
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
// find module.
|
|
|
|
if ((it = mods.find(item->PeerId())) == mods.end())
|
|
|
|
{
|
2012-04-13 20:30:23 -04:00
|
|
|
std::string out = "pqihandler::HandleRsItem() Invalid chan!";
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out);
|
2009-12-13 16:59:26 -05:00
|
|
|
#ifdef DEBUG_TICK
|
2012-04-13 20:30:23 -04:00
|
|
|
std::cerr << out << std::endl;
|
2009-12-13 16:59:26 -05:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
delete item;
|
|
|
|
return -1;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
// check security... is output allowed.
|
|
|
|
if(0 < secpolicy_check((it -> second) -> sp, 0, PQI_OUTGOING))
|
|
|
|
{
|
2012-04-13 20:30:23 -04:00
|
|
|
std::string out = "pqihandler::HandleRsItem() sending to chan: " + it -> first;
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out);
|
2009-12-13 16:59:26 -05:00
|
|
|
#ifdef DEBUG_TICK
|
2012-04-13 20:30:23 -04:00
|
|
|
std::cerr << out << std::endl;
|
2009-12-13 16:59:26 -05:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
// if yes send on item.
|
2011-09-04 16:01:30 -04:00
|
|
|
((it -> second) -> pqi) -> SendItem(item,computed_size);
|
2007-12-11 20:29:14 -05:00
|
|
|
return 1;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-13 20:30:23 -04:00
|
|
|
std::string out = "pqihandler::HandleRsItem() Sec not approved";
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out);
|
2009-12-13 16:59:26 -05:00
|
|
|
#ifdef DEBUG_TICK
|
2012-04-13 20:30:23 -04:00
|
|
|
std::cerr << out << std::endl;
|
2009-12-13 16:59:26 -05:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
delete item;
|
2007-12-11 20:29:14 -05:00
|
|
|
return -1;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// if successfully sent to at least one.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
int pqihandler::SearchSpecific(RsCacheRequest *ns)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
return queueOutRsItem(ns) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
int pqihandler::SendSearchResult(RsCacheItem *ns)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
return queueOutRsItem(ns) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
int pqihandler::SendFileRequest(RsFileRequest *ns)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
return queueOutRsItem(ns) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
int pqihandler::SendFileData(RsFileData *ns)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
return queueOutRsItem(ns) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2010-07-25 15:04:31 -04:00
|
|
|
int pqihandler::SendFileChunkMapRequest(RsFileChunkMapRequest *ns)
|
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
return queueOutRsItem(ns) ;
|
2010-07-25 15:04:31 -04:00
|
|
|
}
|
|
|
|
int pqihandler::SendFileChunkMap(RsFileChunkMap *ns)
|
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
return queueOutRsItem(ns) ;
|
2010-07-25 15:04:31 -04:00
|
|
|
}
|
2010-07-29 17:07:07 -04:00
|
|
|
int pqihandler::SendFileCRC32MapRequest(RsFileCRC32MapRequest *ns)
|
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
return queueOutRsItem(ns) ;
|
2010-07-29 17:07:07 -04:00
|
|
|
}
|
|
|
|
int pqihandler::SendFileCRC32Map(RsFileCRC32Map *ns)
|
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
return queueOutRsItem(ns) ;
|
2010-07-29 17:07:07 -04:00
|
|
|
}
|
2012-03-15 15:55:43 -04:00
|
|
|
int pqihandler::SendFileSingleChunkCrcRequest(RsFileSingleChunkCrcRequest *ns)
|
|
|
|
{
|
|
|
|
return queueOutRsItem(ns) ;
|
|
|
|
}
|
|
|
|
int pqihandler::SendFileSingleChunkCrc(RsFileSingleChunkCrc *ns)
|
|
|
|
{
|
|
|
|
return queueOutRsItem(ns) ;
|
|
|
|
}
|
2011-09-04 16:01:30 -04:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
int pqihandler::SendRsRawItem(RsRawItem *ns)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "pqihandler::SendRsRawItem()");
|
|
|
|
|
2012-06-23 08:10:41 -04:00
|
|
|
// directly send item to streamers
|
2011-09-04 16:01:30 -04:00
|
|
|
|
|
|
|
return queueOutRsItem(ns) ;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// inputs. This is a very basic
|
|
|
|
// system that is completely biased and slow...
|
|
|
|
// someone please fix.
|
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
int pqihandler::locked_GetItems()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
RsItem *item;
|
2007-11-14 22:18:48 -05:00
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
// loop through modules....
|
|
|
|
for(it = mods.begin(); it != mods.end(); it++)
|
|
|
|
{
|
|
|
|
SearchModule *mod = (it -> second);
|
|
|
|
|
|
|
|
// check security... is output allowed.
|
|
|
|
if(0 < secpolicy_check((it -> second) -> sp,
|
2008-02-07 11:18:34 -05:00
|
|
|
0, PQI_INCOMING)) // PQI_ITEM_TYPE_ITEM, PQI_INCOMING))
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
// if yes... attempt to read.
|
|
|
|
while((item = (mod -> pqi) -> GetItem()) != NULL)
|
|
|
|
{
|
2008-04-03 08:51:28 -04:00
|
|
|
#ifdef RSITEM_DEBUG
|
2012-04-13 20:30:23 -04:00
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "pqihandler::GetItems() Incoming Item from: %p\n", mod -> pqi);
|
|
|
|
item -> print_string(out);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
pqioutput(PQL_DEBUG_BASIC,
|
2012-04-13 20:30:23 -04:00
|
|
|
pqihandlerzone, out);
|
2008-04-03 08:51:28 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
if (item->PeerId() != (mod->pqi)->PeerId())
|
|
|
|
{
|
|
|
|
/* ERROR */
|
|
|
|
pqioutput(PQL_ALERT,
|
|
|
|
pqihandlerzone, "ERROR PeerIds dont match!");
|
|
|
|
item->PeerId(mod->pqi->PeerId());
|
|
|
|
}
|
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
locked_SortnStoreItem(item);
|
2007-11-14 22:18:48 -05:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// not allowed to recieve from here....
|
|
|
|
while((item = (mod -> pqi) -> GetItem()) != NULL)
|
|
|
|
{
|
2012-04-13 20:30:23 -04:00
|
|
|
std::string out;
|
|
|
|
rs_sprintf(out, "pqihandler::GetItems() Incoming Item from: %p\n", mod -> pqi);
|
|
|
|
item -> print_string(out);
|
|
|
|
out += "\nItem Not Allowed (Sec Pol). deleting!";
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-04-13 20:30:23 -04:00
|
|
|
pqioutput(PQL_DEBUG_BASIC,
|
|
|
|
pqihandlerzone, out);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
void pqihandler::locked_SortnStoreItem(RsItem *item)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
/* get class type / subtype out of the item */
|
|
|
|
uint8_t vers = item -> PacketVersion();
|
|
|
|
uint8_t cls = item -> PacketClass();
|
|
|
|
uint8_t type = item -> PacketType();
|
|
|
|
uint8_t subtype = item -> PacketSubType();
|
|
|
|
|
|
|
|
/* whole Version reserved for SERVICES/CACHES */
|
|
|
|
if (vers == RS_PKT_VERSION_SERVICE)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
|
|
|
|
"SortnStore -> Service");
|
|
|
|
in_service.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
return;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
if (vers != RS_PKT_VERSION1)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
|
2007-12-11 20:29:14 -05:00
|
|
|
"SortnStore -> Invalid VERSION! Deleting!");
|
|
|
|
delete item;
|
|
|
|
item = NULL;
|
|
|
|
return;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
|
|
|
|
switch(cls)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
case RS_PKT_CLASS_BASE:
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case RS_PKT_TYPE_CACHE:
|
|
|
|
switch(subtype)
|
|
|
|
{
|
|
|
|
case RS_PKT_SUBTYPE_CACHE_REQUEST:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
|
|
|
|
"SortnStore -> Cache Request");
|
|
|
|
in_search.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RS_PKT_SUBTYPE_CACHE_ITEM:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
|
|
|
|
"SortnStore -> Cache Result");
|
|
|
|
in_result.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break; /* no match! */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RS_PKT_TYPE_FILE:
|
|
|
|
switch(subtype)
|
2010-07-25 15:04:31 -04:00
|
|
|
{
|
|
|
|
case RS_PKT_SUBTYPE_FI_REQUEST:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
|
|
|
|
"SortnStore -> File Request");
|
|
|
|
in_request.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RS_PKT_SUBTYPE_FI_DATA:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "SortnStore -> File Data");
|
|
|
|
in_data.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RS_PKT_SUBTYPE_FI_CHUNK_MAP_REQUEST:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "SortnStore -> File ChunkMap Request");
|
|
|
|
in_chunkmap_request.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RS_PKT_SUBTYPE_FI_CHUNK_MAP:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "SortnStore -> File ChunkMap");
|
|
|
|
in_chunkmap.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
break;
|
|
|
|
|
2010-07-29 17:07:07 -04:00
|
|
|
case RS_PKT_SUBTYPE_FI_CRC32_MAP_REQUEST:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "SortnStore -> File Crc32Map Request");
|
|
|
|
in_crc32map_request.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RS_PKT_SUBTYPE_FI_CRC32_MAP:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "SortnStore -> File CRC32Map");
|
|
|
|
in_crc32map.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
break;
|
|
|
|
|
2012-03-15 15:55:43 -04:00
|
|
|
case RS_PKT_SUBTYPE_FI_CHUNK_CRC_REQUEST:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "SortnStore -> File Crc Request");
|
|
|
|
in_singlechunkcrc_request.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RS_PKT_SUBTYPE_FI_CHUNK_CRC:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, "SortnStore -> File CRC32Map");
|
|
|
|
in_singlechunkcrc.push_back(item);
|
|
|
|
item = NULL;
|
|
|
|
break;
|
2010-07-29 17:07:07 -04:00
|
|
|
|
2010-07-25 15:04:31 -04:00
|
|
|
default:
|
|
|
|
break; /* no match! */
|
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break; /* no match! */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
|
|
|
|
"SortnStore -> Unknown");
|
|
|
|
break;
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
|
|
|
|
if (item)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
pqioutput(PQL_DEBUG_BASIC, pqihandlerzone,
|
|
|
|
"SortnStore -> Deleting Unsorted Item");
|
|
|
|
delete item;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// much like the input stuff.
|
2007-12-11 20:29:14 -05:00
|
|
|
RsCacheItem *pqihandler::GetSearchResult()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
if (in_result.size() != 0)
|
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
RsCacheItem *fi = dynamic_cast<RsCacheItem *>(in_result.front());
|
|
|
|
if (!fi) { delete in_result.front(); }
|
2007-11-14 22:18:48 -05:00
|
|
|
in_result.pop_front();
|
|
|
|
return fi;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
RsCacheRequest *pqihandler::RequestedSearch()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
if (in_search.size() != 0)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
RsCacheRequest *fi = dynamic_cast<RsCacheRequest *>(in_search.front());
|
|
|
|
if (!fi) { delete in_search.front(); }
|
|
|
|
in_search.pop_front();
|
2007-11-14 22:18:48 -05:00
|
|
|
return fi;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
RsFileRequest *pqihandler::GetFileRequest()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
if (in_request.size() != 0)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
RsFileRequest *fi = dynamic_cast<RsFileRequest *>(in_request.front());
|
|
|
|
if (!fi) { delete in_request.front(); }
|
|
|
|
in_request.pop_front();
|
|
|
|
return fi;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
RsFileData *pqihandler::GetFileData()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
if (in_data.size() != 0)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
RsFileData *fi = dynamic_cast<RsFileData *>(in_data.front());
|
|
|
|
if (!fi) { delete in_data.front(); }
|
|
|
|
in_data.pop_front();
|
|
|
|
return fi;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-07-25 15:04:31 -04:00
|
|
|
RsFileChunkMapRequest *pqihandler::GetFileChunkMapRequest()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
|
|
|
if (in_chunkmap_request.size() != 0)
|
|
|
|
{
|
|
|
|
RsFileChunkMapRequest *fi = dynamic_cast<RsFileChunkMapRequest *>(in_chunkmap_request.front());
|
|
|
|
if (!fi) { delete in_chunkmap_request.front(); }
|
|
|
|
in_chunkmap_request.pop_front();
|
|
|
|
return fi;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
RsFileChunkMap *pqihandler::GetFileChunkMap()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
|
|
|
if (in_chunkmap.size() != 0)
|
|
|
|
{
|
|
|
|
RsFileChunkMap *fi = dynamic_cast<RsFileChunkMap *>(in_chunkmap.front());
|
|
|
|
if (!fi) { delete in_chunkmap.front(); }
|
|
|
|
in_chunkmap.pop_front();
|
|
|
|
return fi;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-07-29 17:07:07 -04:00
|
|
|
RsFileCRC32MapRequest *pqihandler::GetFileCRC32MapRequest()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
|
|
|
if (in_crc32map_request.size() != 0)
|
|
|
|
{
|
|
|
|
RsFileCRC32MapRequest *fi = dynamic_cast<RsFileCRC32MapRequest *>(in_crc32map_request.front());
|
|
|
|
if (!fi) { delete in_crc32map_request.front(); }
|
|
|
|
in_crc32map_request.pop_front();
|
|
|
|
return fi;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
RsFileCRC32Map *pqihandler::GetFileCRC32Map()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2010-07-25 15:04:31 -04:00
|
|
|
|
2010-07-29 17:07:07 -04:00
|
|
|
if (in_crc32map.size() != 0)
|
|
|
|
{
|
|
|
|
RsFileCRC32Map *fi = dynamic_cast<RsFileCRC32Map *>(in_crc32map.front());
|
|
|
|
if (!fi) { delete in_crc32map.front(); }
|
|
|
|
in_crc32map.pop_front();
|
|
|
|
return fi;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-15 15:55:43 -04:00
|
|
|
RsFileSingleChunkCrcRequest *pqihandler::GetFileSingleChunkCrcRequest()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2010-07-25 15:04:31 -04:00
|
|
|
|
2012-03-15 15:55:43 -04:00
|
|
|
if (in_singlechunkcrc_request.size() != 0)
|
|
|
|
{
|
|
|
|
RsFileSingleChunkCrcRequest *fi = dynamic_cast<RsFileSingleChunkCrcRequest *>(in_singlechunkcrc_request.front());
|
|
|
|
if (!fi) { delete in_singlechunkcrc_request.front(); }
|
|
|
|
in_singlechunkcrc_request.pop_front();
|
|
|
|
return fi;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
RsFileSingleChunkCrc *pqihandler::GetFileSingleChunkCrc()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2012-03-15 15:55:43 -04:00
|
|
|
if (in_singlechunkcrc.size() != 0)
|
|
|
|
{
|
|
|
|
RsFileSingleChunkCrc *fi = dynamic_cast<RsFileSingleChunkCrc *>(in_singlechunkcrc.front());
|
|
|
|
if (!fi) { delete in_singlechunkcrc.front(); }
|
|
|
|
in_singlechunkcrc.pop_front();
|
|
|
|
return fi;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
RsRawItem *pqihandler::GetRsRawItem()
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
if (in_service.size() != 0)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
RsRawItem *fi = dynamic_cast<RsRawItem *>(in_service.front());
|
|
|
|
if (!fi) { delete in_service.front(); }
|
|
|
|
in_service.pop_front();
|
|
|
|
return fi;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const float MIN_RATE = 0.01; // 10 B/s
|
|
|
|
|
2012-06-21 19:23:46 -04:00
|
|
|
// NEW extern fn to extract rates.
|
|
|
|
int pqihandler::ExtractRates(std::map<std::string, RsBwRates> &ratemap, RsBwRates &total)
|
|
|
|
{
|
|
|
|
total.mMaxRateIn = getMaxRate(true);
|
|
|
|
total.mMaxRateOut = getMaxRate(false);
|
|
|
|
total.mRateIn = 0;
|
|
|
|
total.mRateOut = 0;
|
2012-06-21 21:35:32 -04:00
|
|
|
total.mQueueIn = 0;
|
|
|
|
total.mQueueOut = 0;
|
2012-06-21 19:23:46 -04:00
|
|
|
|
|
|
|
/* Lock once rates have been retrieved */
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
|
|
|
for(it = mods.begin(); it != mods.end(); it++)
|
|
|
|
{
|
|
|
|
SearchModule *mod = (it -> second);
|
|
|
|
|
|
|
|
RsBwRates peerRates;
|
|
|
|
mod -> pqi -> getRates(peerRates);
|
|
|
|
|
|
|
|
total.mRateIn += peerRates.mRateIn;
|
|
|
|
total.mRateOut += peerRates.mRateOut;
|
2012-06-21 21:35:32 -04:00
|
|
|
total.mQueueIn += peerRates.mQueueIn;
|
|
|
|
total.mQueueOut += peerRates.mQueueOut;
|
2012-06-21 19:23:46 -04:00
|
|
|
|
|
|
|
ratemap[it->first] = peerRates;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
// internal fn to send updates
|
|
|
|
int pqihandler::UpdateRates()
|
|
|
|
{
|
2007-12-11 20:29:14 -05:00
|
|
|
std::map<std::string, SearchModule *>::iterator it;
|
2007-11-14 22:18:48 -05:00
|
|
|
int num_sm = mods.size();
|
|
|
|
|
|
|
|
float avail_in = getMaxRate(true);
|
|
|
|
float avail_out = getMaxRate(false);
|
|
|
|
|
|
|
|
float used_bw_in = 0;
|
|
|
|
float used_bw_out = 0;
|
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
/* Lock once rates have been retrieved */
|
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
2009-06-08 13:09:00 -04:00
|
|
|
int effectiveUploadsSm = 0;
|
|
|
|
int effectiveDownloadsSm = 0;
|
|
|
|
// loop through modules to get the used bandwith and the number of modules that are affectively transfering
|
|
|
|
//std::cerr << " Looping through modules" << std::endl;
|
2007-11-14 22:18:48 -05:00
|
|
|
for(it = mods.begin(); it != mods.end(); it++)
|
|
|
|
{
|
|
|
|
SearchModule *mod = (it -> second);
|
|
|
|
float crate_in = mod -> pqi -> getRate(true);
|
2009-06-08 13:09:00 -04:00
|
|
|
if (crate_in > 0.01 * avail_in || crate_in > 0.1)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2009-06-08 13:09:00 -04:00
|
|
|
effectiveDownloadsSm ++;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2009-06-08 13:09:00 -04:00
|
|
|
float crate_out = mod -> pqi -> getRate(false);
|
|
|
|
if (crate_out > 0.01 * avail_out || crate_out > 0.1)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2009-06-08 13:09:00 -04:00
|
|
|
effectiveUploadsSm ++;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2009-06-08 13:09:00 -04:00
|
|
|
|
|
|
|
used_bw_in += crate_in;
|
|
|
|
used_bw_out += crate_out;
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2011-09-04 16:01:30 -04:00
|
|
|
#ifdef DEBUG_QOS
|
2009-06-08 13:09:00 -04:00
|
|
|
// std::cerr << "Totals (In) Used B/W " << used_bw_in;
|
|
|
|
// std::cerr << " Available B/W " << avail_in;
|
|
|
|
// std::cerr << " Effective transfers " << effectiveDownloadsSm << std::endl;
|
|
|
|
// std::cerr << "Totals (Out) Used B/W " << used_bw_out;
|
|
|
|
// std::cerr << " Available B/W " << avail_out;
|
|
|
|
// std::cerr << " Effective transfers " << effectiveUploadsSm << std::endl;
|
2011-09-04 16:01:30 -04:00
|
|
|
#endif
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-06-08 13:09:00 -04:00
|
|
|
locked_StoreCurrentRates(used_bw_in, used_bw_out);
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-06-08 13:09:00 -04:00
|
|
|
//computing average rates for effective transfers
|
|
|
|
float max_in_effective = avail_in / num_sm;
|
|
|
|
if (effectiveDownloadsSm != 0) {
|
|
|
|
max_in_effective = avail_in / effectiveDownloadsSm;
|
|
|
|
}
|
|
|
|
float max_out_effective = avail_out / num_sm;
|
|
|
|
if (effectiveUploadsSm != 0) {
|
|
|
|
max_out_effective = avail_out / effectiveUploadsSm;
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-06-08 13:09:00 -04:00
|
|
|
//modify the outgoing rates if bandwith is not used well
|
|
|
|
float rate_out_modifier = 0;
|
|
|
|
if (used_bw_out / avail_out < 0.95) {
|
|
|
|
rate_out_modifier = 0.001 * avail_out;
|
|
|
|
} else if (used_bw_out / avail_out > 1.05) {
|
|
|
|
rate_out_modifier = - 0.001 * avail_out;
|
|
|
|
}
|
|
|
|
if (rate_out_modifier != 0) {
|
|
|
|
for(it = mods.begin(); it != mods.end(); it++)
|
|
|
|
{
|
|
|
|
SearchModule *mod = (it -> second);
|
|
|
|
mod -> pqi -> setMaxRate(false, mod -> pqi -> getMaxRate(false) + rate_out_modifier);
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
|
2009-06-08 13:09:00 -04:00
|
|
|
//modify the incoming rates if bandwith is not used well
|
|
|
|
float rate_in_modifier = 0;
|
|
|
|
if (used_bw_in / avail_in < 0.95) {
|
|
|
|
rate_in_modifier = 0.001 * avail_in;
|
|
|
|
} else if (used_bw_in / avail_in > 1.05) {
|
|
|
|
rate_in_modifier = - 0.001 * avail_in;
|
|
|
|
}
|
|
|
|
if (rate_in_modifier != 0) {
|
|
|
|
for(it = mods.begin(); it != mods.end(); it++)
|
|
|
|
{
|
|
|
|
SearchModule *mod = (it -> second);
|
|
|
|
mod -> pqi -> setMaxRate(true, mod -> pqi -> getMaxRate(true) + rate_in_modifier);
|
|
|
|
}
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2009-06-08 13:09:00 -04:00
|
|
|
//cap the rates
|
|
|
|
for(it = mods.begin(); it != mods.end(); it++)
|
2007-11-14 22:18:48 -05:00
|
|
|
{
|
2009-06-08 13:09:00 -04:00
|
|
|
SearchModule *mod = (it -> second);
|
|
|
|
if (mod -> pqi -> getMaxRate(false) < max_out_effective) {
|
|
|
|
mod -> pqi -> setMaxRate(false, max_out_effective);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
2009-06-08 13:09:00 -04:00
|
|
|
if (mod -> pqi -> getMaxRate(false) > avail_out) {
|
|
|
|
mod -> pqi -> setMaxRate(false, avail_out);
|
|
|
|
}
|
|
|
|
if (mod -> pqi -> getMaxRate(true) < max_in_effective) {
|
|
|
|
mod -> pqi -> setMaxRate(true, max_in_effective);
|
|
|
|
}
|
|
|
|
if (mod -> pqi -> getMaxRate(true) > avail_in) {
|
|
|
|
mod -> pqi -> setMaxRate(true, avail_in);
|
2007-11-14 22:18:48 -05:00
|
|
|
}
|
|
|
|
}
|
2008-06-09 20:47:24 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-06-09 20:47:24 -04:00
|
|
|
void pqihandler::getCurrentRates(float &in, float &out)
|
|
|
|
{
|
2008-11-22 08:15:07 -05:00
|
|
|
RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/
|
|
|
|
|
2008-06-09 20:47:24 -04:00
|
|
|
in = rateTotal_in;
|
|
|
|
out = rateTotal_out;
|
|
|
|
}
|
|
|
|
|
2008-11-22 08:15:07 -05:00
|
|
|
void pqihandler::locked_StoreCurrentRates(float in, float out)
|
2008-06-09 20:47:24 -04:00
|
|
|
{
|
|
|
|
rateTotal_in = in;
|
|
|
|
rateTotal_out = out;
|
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
|