2011-07-11 14:48:56 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/rsserver: p3serverconfig.h
|
|
|
|
*
|
|
|
|
* RetroShare C++ Interface.
|
|
|
|
*
|
|
|
|
* Copyright 2011-2011 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 "rsserver/p3serverconfig.h"
|
2012-06-21 19:23:46 -04:00
|
|
|
#include "services/p3bwctrl.h"
|
2011-07-11 14:48:56 -04:00
|
|
|
|
|
|
|
RsServerConfig *rsConfig = NULL;
|
|
|
|
|
2012-11-25 15:25:34 -05:00
|
|
|
static const std::string pqih_ftr("PQIH_FTR");
|
2011-07-11 14:48:56 -04:00
|
|
|
|
2012-11-25 15:25:34 -05:00
|
|
|
#define DEFAULT_DOWNLOAD_KB_RATE (200.0)
|
|
|
|
#define DEFAULT_UPLOAD_KB_RATE (50.0)
|
|
|
|
|
|
|
|
#define MIN_MINIMAL_RATE (5.0)
|
|
|
|
|
|
|
|
|
|
|
|
p3ServerConfig::p3ServerConfig(p3PeerMgr *peerMgr, p3LinkMgr *linkMgr, p3NetMgr *netMgr, pqihandler *pqih, p3GeneralConfig *genCfg)
|
2011-07-11 14:48:56 -04:00
|
|
|
:configMtx("p3ServerConfig")
|
|
|
|
{
|
|
|
|
mPeerMgr = peerMgr;
|
|
|
|
mLinkMgr = linkMgr;
|
|
|
|
mNetMgr = netMgr;
|
2012-11-25 15:25:34 -05:00
|
|
|
mPqiHandler = pqih;
|
2011-07-11 14:48:56 -04:00
|
|
|
|
2011-07-30 12:57:40 -04:00
|
|
|
mGeneralConfig = genCfg;
|
|
|
|
|
2011-07-11 14:48:56 -04:00
|
|
|
mUserLevel = RSCONFIG_USER_LEVEL_NEW; /* START LEVEL */
|
2012-11-25 15:25:34 -05:00
|
|
|
mRateDownload = DEFAULT_DOWNLOAD_KB_RATE;
|
|
|
|
mRateUpload = DEFAULT_UPLOAD_KB_RATE;
|
2011-07-11 14:48:56 -04:00
|
|
|
|
|
|
|
rsConfig = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p3ServerConfig::~p3ServerConfig()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-30 12:57:40 -04:00
|
|
|
|
2012-11-25 15:25:34 -05:00
|
|
|
void p3ServerConfig::load_config()
|
|
|
|
{
|
|
|
|
/* get the real bandwidth setting from GeneralConfig */
|
|
|
|
std::string rates = mGeneralConfig -> getSetting(pqih_ftr);
|
|
|
|
|
|
|
|
float mri, mro;
|
|
|
|
if (2 == sscanf(rates.c_str(), "%f %f", &mri, &mro))
|
|
|
|
{
|
|
|
|
RsStackMutex stack(configMtx); /******* LOCKED MUTEX *****/
|
|
|
|
|
|
|
|
mRateDownload = mri;
|
|
|
|
mRateUpload = mro;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RsStackMutex stack(configMtx); /******* LOCKED MUTEX *****/
|
|
|
|
|
|
|
|
mRateDownload = DEFAULT_DOWNLOAD_KB_RATE;
|
|
|
|
mRateUpload = DEFAULT_UPLOAD_KB_RATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable operating mode */
|
|
|
|
uint32_t opMode = getOperatingMode();
|
|
|
|
switchToOperatingMode(opMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-30 12:57:40 -04:00
|
|
|
#define RS_CONFIG_ADVANCED_STRING "AdvMode"
|
|
|
|
|
|
|
|
bool p3ServerConfig::findConfigurationOption(uint32_t key, std::string &keystr)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
switch(key)
|
|
|
|
{
|
|
|
|
case RS_CONFIG_ADVANCED:
|
|
|
|
keystr = RS_CONFIG_ADVANCED_STRING;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3ServerConfig::getConfigurationOption(uint32_t key, std::string &opt)
|
|
|
|
{
|
|
|
|
std::string strkey;
|
|
|
|
if (!findConfigurationOption(key, strkey))
|
|
|
|
{
|
|
|
|
std::cerr << "p3ServerConfig::getConfigurationOption() OPTION NOT VALID: " << key;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
opt = mGeneralConfig->getSetting(strkey);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3ServerConfig::setConfigurationOption(uint32_t key, const std::string &opt)
|
|
|
|
{
|
|
|
|
std::string strkey;
|
|
|
|
if (!findConfigurationOption(key, strkey))
|
|
|
|
{
|
|
|
|
std::cerr << "p3ServerConfig::setConfigurationOption() OPTION NOT VALID: " << key;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mGeneralConfig->setSetting(strkey, opt);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-11 14:48:56 -04:00
|
|
|
/* From RsIface::RsConfig */
|
|
|
|
|
2011-08-12 09:42:30 -04:00
|
|
|
int p3ServerConfig::getConfigNetStatus(RsConfigNetStatus &/*status*/)
|
2011-07-11 14:48:56 -04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-12 09:42:30 -04:00
|
|
|
int p3ServerConfig::getConfigStartup(RsConfigStartup &/*params*/)
|
2011-07-11 14:48:56 -04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-21 19:23:46 -04:00
|
|
|
#if 0
|
2011-08-12 09:42:30 -04:00
|
|
|
int p3ServerConfig::getConfigDataRates(RsConfigDataRates &/*params*/)
|
2011-07-11 14:48:56 -04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-21 19:23:46 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/***** for RsConfig -> p3BandwidthControl ****/
|
|
|
|
|
|
|
|
int p3ServerConfig::getTotalBandwidthRates(RsConfigDataRates &rates)
|
|
|
|
{
|
|
|
|
if (rsBandwidthControl)
|
|
|
|
{
|
|
|
|
return rsBandwidthControl->getTotalBandwidthRates(rates);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int p3ServerConfig::getAllBandwidthRates(std::map<std::string, RsConfigDataRates> &ratemap)
|
|
|
|
{
|
|
|
|
if (rsBandwidthControl)
|
|
|
|
{
|
|
|
|
return rsBandwidthControl->getAllBandwidthRates(ratemap);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-11 14:48:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* From RsInit */
|
|
|
|
|
|
|
|
std::string p3ServerConfig::RsConfigDirectory()
|
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string p3ServerConfig::RsConfigKeysDirectory()
|
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string p3ServerConfig::RsProfileConfigDirectory()
|
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3ServerConfig::getStartMinimised()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string p3ServerConfig::getRetroShareLink()
|
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3ServerConfig::getAutoLogin()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-12 09:42:30 -04:00
|
|
|
void p3ServerConfig::setAutoLogin(bool /*autoLogin*/)
|
2011-07-11 14:48:56 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3ServerConfig::RsClearAutoLogin()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string p3ServerConfig::getRetroshareDataDirectory()
|
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* New Stuff */
|
|
|
|
|
|
|
|
uint32_t p3ServerConfig::getUserLevel()
|
|
|
|
{
|
|
|
|
uint32_t userLevel = RSCONFIG_USER_LEVEL_NEW;
|
|
|
|
{
|
|
|
|
RsStackMutex stack(configMtx); /******* LOCKED MUTEX *****/
|
2011-08-12 09:42:30 -04:00
|
|
|
userLevel = mUserLevel;
|
2011-07-11 14:48:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(userLevel)
|
|
|
|
{
|
|
|
|
case RSCONFIG_USER_LEVEL_OVERRIDE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
#define MIN_BASIC_FRIENDS 2
|
|
|
|
|
|
|
|
// FALL THROUGH EVERYTHING.
|
|
|
|
default:
|
|
|
|
case RSCONFIG_USER_LEVEL_NEW:
|
|
|
|
{
|
|
|
|
|
2012-02-24 20:48:56 -05:00
|
|
|
if (mPeerMgr->getFriendCount(true, false) > MIN_BASIC_FRIENDS)
|
2011-07-11 14:48:56 -04:00
|
|
|
{
|
2012-02-24 20:48:56 -05:00
|
|
|
userLevel = RSCONFIG_USER_LEVEL_BASIC;
|
2011-07-11 14:48:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case RSCONFIG_USER_LEVEL_BASIC:
|
|
|
|
{
|
|
|
|
/* check that we have some lastConnect > 0 */
|
|
|
|
if (mPeerMgr->haveOnceConnected())
|
|
|
|
{
|
2012-02-24 20:48:56 -05:00
|
|
|
userLevel = RSCONFIG_USER_LEVEL_CASUAL;
|
2011-07-11 14:48:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case RSCONFIG_USER_LEVEL_CASUAL:
|
2012-02-24 20:48:56 -05:00
|
|
|
case RSCONFIG_USER_LEVEL_POWER:
|
2011-07-11 14:48:56 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
/* check that the firewall is open */
|
|
|
|
|
|
|
|
uint32_t netMode = mNetMgr->getNetworkMode();
|
|
|
|
uint32_t firewallMode = mNetMgr->getNatHoleMode();
|
|
|
|
|
|
|
|
if ((RSNET_NETWORK_EXTERNALIP == netMode) ||
|
|
|
|
((RSNET_NETWORK_BEHINDNAT == netMode) &&
|
|
|
|
((RSNET_NATHOLE_UPNP == firewallMode) ||
|
|
|
|
(RSNET_NATHOLE_NATPMP == firewallMode) ||
|
|
|
|
(RSNET_NATHOLE_FORWARDED == firewallMode))))
|
|
|
|
{
|
2012-02-24 20:48:56 -05:00
|
|
|
userLevel = RSCONFIG_USER_LEVEL_POWER;
|
2011-07-11 14:48:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break; /* for all */
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(configMtx); /******* LOCKED MUTEX *****/
|
|
|
|
mUserLevel = userLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
return userLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t p3ServerConfig::getNetState()
|
|
|
|
{
|
|
|
|
return mNetMgr->getNetStateMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t p3ServerConfig::getNetworkMode()
|
|
|
|
{
|
|
|
|
return mNetMgr->getNetworkMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t p3ServerConfig::getNatTypeMode()
|
|
|
|
{
|
|
|
|
return mNetMgr->getNatTypeMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t p3ServerConfig::getNatHoleMode()
|
|
|
|
{
|
|
|
|
return mNetMgr->getNatHoleMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t p3ServerConfig::getConnectModes()
|
|
|
|
{
|
|
|
|
return mNetMgr->getConnectModes();
|
|
|
|
}
|
|
|
|
|
2012-11-25 09:59:42 -05:00
|
|
|
/* Operating Mode */
|
|
|
|
#define RS_CONFIG_OPERATING_STRING "OperatingMode"
|
|
|
|
|
|
|
|
uint32_t p3ServerConfig::getOperatingMode()
|
|
|
|
{
|
|
|
|
std::string modestr = mGeneralConfig->getSetting(RS_CONFIG_OPERATING_STRING);
|
|
|
|
uint32_t mode = RS_OPMODE_FULL;
|
|
|
|
|
|
|
|
if (modestr == "FULL")
|
|
|
|
{
|
|
|
|
mode = RS_OPMODE_FULL;
|
|
|
|
}
|
|
|
|
else if (modestr == "NOTURTLE")
|
|
|
|
{
|
|
|
|
mode = RS_OPMODE_NOTURTLE;
|
|
|
|
}
|
|
|
|
else if (modestr == "GAMING")
|
|
|
|
{
|
|
|
|
mode = RS_OPMODE_GAMING;
|
|
|
|
}
|
|
|
|
else if (modestr == "MINIMAL_TRANSFER")
|
|
|
|
{
|
|
|
|
mode = RS_OPMODE_MINIMAL;
|
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2012-11-25 15:25:34 -05:00
|
|
|
|
2012-11-25 09:59:42 -05:00
|
|
|
bool p3ServerConfig::setOperatingMode(uint32_t opMode)
|
|
|
|
{
|
|
|
|
std::string modestr = "FULL";
|
|
|
|
switch(opMode)
|
|
|
|
{
|
|
|
|
case RS_OPMODE_FULL:
|
|
|
|
modestr = "FULL";
|
|
|
|
break;
|
|
|
|
case RS_OPMODE_NOTURTLE:
|
|
|
|
modestr = "NOTURTLE";
|
|
|
|
|
|
|
|
break;
|
|
|
|
case RS_OPMODE_GAMING:
|
|
|
|
modestr = "GAMING";
|
|
|
|
|
|
|
|
break;
|
|
|
|
case RS_OPMODE_MINIMAL:
|
|
|
|
modestr = "MINIMAL_TRANSFER";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mGeneralConfig->setSetting(RS_CONFIG_OPERATING_STRING, modestr);
|
|
|
|
return switchToOperatingMode(opMode);
|
|
|
|
}
|
|
|
|
|
2012-11-25 15:25:34 -05:00
|
|
|
|
2012-11-25 09:59:42 -05:00
|
|
|
bool p3ServerConfig::switchToOperatingMode(uint32_t opMode)
|
|
|
|
{
|
2012-11-25 15:25:34 -05:00
|
|
|
float dl_rate = 0;
|
|
|
|
float ul_rate = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(configMtx); /******* LOCKED MUTEX *****/
|
|
|
|
dl_rate = mRateDownload;
|
|
|
|
ul_rate = mRateUpload;
|
|
|
|
}
|
|
|
|
|
2012-11-25 09:59:42 -05:00
|
|
|
switch (opMode)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case RS_OPMODE_FULL:
|
|
|
|
/* switch on all transfers */
|
|
|
|
/* 100% bandwidth */
|
|
|
|
/* switch on popups, enable hashing */
|
2012-11-25 15:25:34 -05:00
|
|
|
//setMaxRate(true, mri); // In / Download
|
|
|
|
//setMaxRate(false, mro); // Out / Upload.
|
2012-11-25 09:59:42 -05:00
|
|
|
break;
|
|
|
|
case RS_OPMODE_NOTURTLE:
|
|
|
|
/* switch on all transfers - except turtle, enable hashing */
|
|
|
|
/* 100% bandwidth */
|
|
|
|
/* switch on popups, enable hashing */
|
|
|
|
|
|
|
|
break;
|
|
|
|
case RS_OPMODE_GAMING:
|
|
|
|
/* switch on all transfers */
|
|
|
|
/* reduce bandwidth to 25% */
|
|
|
|
/* switch off popups, enable hashing */
|
2012-11-25 15:25:34 -05:00
|
|
|
|
|
|
|
dl_rate *= 0.25;
|
|
|
|
ul_rate *= 0.25;
|
2012-11-25 09:59:42 -05:00
|
|
|
break;
|
|
|
|
case RS_OPMODE_MINIMAL:
|
|
|
|
/* switch off all transfers */
|
|
|
|
/* reduce bandwidth to 10%, but make sure there is enough for VoIP */
|
|
|
|
/* switch on popups, enable hashing */
|
2012-11-25 15:25:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
dl_rate *= 0.10;
|
|
|
|
ul_rate *= 0.10;
|
|
|
|
if (dl_rate < MIN_MINIMAL_RATE)
|
|
|
|
{
|
|
|
|
dl_rate = MIN_MINIMAL_RATE;
|
|
|
|
}
|
|
|
|
if (ul_rate < MIN_MINIMAL_RATE)
|
|
|
|
{
|
|
|
|
ul_rate = MIN_MINIMAL_RATE;
|
|
|
|
}
|
|
|
|
|
2012-11-25 09:59:42 -05:00
|
|
|
break;
|
|
|
|
}
|
2012-11-25 15:25:34 -05:00
|
|
|
|
|
|
|
if (mPqiHandler)
|
|
|
|
{
|
|
|
|
mPqiHandler -> setMaxRate(true, dl_rate);
|
|
|
|
mPqiHandler -> setMaxRate(false, ul_rate);
|
|
|
|
}
|
|
|
|
|
2012-11-25 09:59:42 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* handle data rates.
|
|
|
|
* Mutex must be handled at the lower levels: TODO */
|
|
|
|
|
2012-11-25 15:25:34 -05:00
|
|
|
int p3ServerConfig::SetMaxDataRates( int downKb, int upKb ) /* in kbrates */
|
2012-11-25 09:59:42 -05:00
|
|
|
{
|
2012-11-25 15:25:34 -05:00
|
|
|
char line[512];
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(configMtx); /******* LOCKED MUTEX *****/
|
|
|
|
mRateDownload = downKb;
|
|
|
|
mRateUpload = upKb;
|
|
|
|
sprintf(line, "%f %f", mRateDownload, mRateUpload);
|
|
|
|
}
|
|
|
|
mGeneralConfig->setSetting(pqih_ftr, std::string(line));
|
2012-11-25 09:59:42 -05:00
|
|
|
|
2012-11-25 15:25:34 -05:00
|
|
|
load_config(); // load and setup everything.
|
2012-11-25 09:59:42 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-25 15:25:34 -05:00
|
|
|
int p3ServerConfig::GetMaxDataRates( int &inKb, int &outKb ) /* in kbrates */
|
2012-11-25 09:59:42 -05:00
|
|
|
{
|
2012-11-25 15:25:34 -05:00
|
|
|
RsStackMutex stack(configMtx); /******* LOCKED MUTEX *****/
|
|
|
|
|
|
|
|
inKb = mRateDownload;
|
|
|
|
outKb = mRateUpload;
|
2012-11-25 09:59:42 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-25 15:25:34 -05:00
|
|
|
int p3ServerConfig::GetCurrentDataRates( float &inKb, float &outKb )
|
|
|
|
{
|
|
|
|
mPqiHandler->getCurrentRates(inKb, outKb);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-07-11 14:48:56 -04:00
|
|
|
|