From de80f2aa3e8505467c8abcc5054a06cf663043b2 Mon Sep 17 00:00:00 2001 From: drbob Date: Sun, 25 Nov 2012 20:25:34 +0000 Subject: [PATCH] * Moved DataRate interface from rsiface (old) -> rsConfig. * Internally moved storage of data rate from pqipersongroup -> p3serverconfig * made DataRate + Operating Mode work together. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5889 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/pqi/pqipersongrp.cc | 53 +--------- libretroshare/src/pqi/pqipersongrp.h | 5 - libretroshare/src/retroshare/rsconfig.h | 5 +- libretroshare/src/retroshare/rsiface.h | 2 - libretroshare/src/retroshare/rstypes.h | 5 - libretroshare/src/rsserver/p3face-config.cc | 48 --------- libretroshare/src/rsserver/p3face.h | 2 - libretroshare/src/rsserver/p3serverconfig.cc | 103 +++++++++++++++++-- libretroshare/src/rsserver/p3serverconfig.h | 13 ++- libretroshare/src/rsserver/rsinit.cc | 7 +- 10 files changed, 113 insertions(+), 130 deletions(-) diff --git a/libretroshare/src/pqi/pqipersongrp.cc b/libretroshare/src/pqi/pqipersongrp.cc index 70188cb1f..5c5fd846f 100644 --- a/libretroshare/src/pqi/pqipersongrp.cc +++ b/libretroshare/src/pqi/pqipersongrp.cc @@ -108,7 +108,7 @@ int pqipersongrp::tickServiceSend() // init pqipersongrp::pqipersongrp(SecurityPolicy *glob, unsigned long flags) - :pqihandler(glob), pqil(NULL), config(NULL), initFlags(flags) + :pqihandler(glob), pqil(NULL), initFlags(flags) { } @@ -222,57 +222,6 @@ bool pqipersongrp::resetListener(struct sockaddr_in &local) return 1; } -/* NOT bothering to protect Config with a mutex.... it is not going to change - * and has its own internal mutexs. - */ -int pqipersongrp::setConfig(p3GeneralConfig *cfg) -{ - config = cfg; - return 1; -} - -static const std::string pqih_ftr("PQIH_FTR"); - -int pqipersongrp::save_config() -{ - char line[512]; - sprintf(line, "%f %f", getMaxRate(true), getMaxRate(false)); - if (config) - { - config -> setSetting(pqih_ftr, std::string(line)); - } - return 1; -} - -int pqipersongrp::load_config() -{ - std::string line; - if (config) - { - line = config -> getSetting(pqih_ftr); - } - - float mri, mro; - - if (2 == sscanf(line.c_str(), "%f %f", &mri, &mro)) - { - setMaxRate(true, mri); - setMaxRate(false, mro); - } - else - { - pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone, - "pqipersongrp::load_config() Loading Default Rates!"); - - - setMaxRate(true, DEFAULT_DOWNLOAD_KB_RATE); - setMaxRate(false, DEFAULT_UPLOAD_KB_RATE); - } - - return 1; -} - - void pqipersongrp::statusChange(const std::list &plist) { diff --git a/libretroshare/src/pqi/pqipersongrp.h b/libretroshare/src/pqi/pqipersongrp.h index c857b71b7..fd8118925 100644 --- a/libretroshare/src/pqi/pqipersongrp.h +++ b/libretroshare/src/pqi/pqipersongrp.h @@ -57,10 +57,6 @@ class pqipersongrp: public pqihandler, public pqiMonitor, public p3ServiceServer virtual bool resetListener(struct sockaddr_in &local); // overloaded from pqiNetListener int init_listener(); -int setConfig(p3GeneralConfig *cfg); -int save_config(); -int load_config(); - /*************** pqiMonitor callback ***********************/ virtual void statusChange(const std::list &plist); @@ -119,7 +115,6 @@ virtual int checkOutgoingRsItem(RsItem *item, int global) int tickServiceSend(); pqilistener *pqil; - p3GeneralConfig *config; unsigned long initFlags; }; diff --git a/libretroshare/src/retroshare/rsconfig.h b/libretroshare/src/retroshare/rsconfig.h index 95fdd45db..0bc6a9fc8 100644 --- a/libretroshare/src/retroshare/rsconfig.h +++ b/libretroshare/src/retroshare/rsconfig.h @@ -278,8 +278,9 @@ virtual uint32_t getOperatingMode() = 0; virtual bool setOperatingMode(uint32_t opMode) = 0; /* Data Rate Control - to be moved here */ -virtual int SetDataRates( int downKb, int upKb ) = 0; -virtual int GetDataRates( float &inKb, float &outKb ) = 0; +virtual int SetMaxDataRates( int downKb, int upKb ) = 0; +virtual int GetMaxDataRates( int &inKb, int &outKb ) = 0; +virtual int GetCurrentDataRates( float &inKb, float &outKb ) = 0; }; diff --git a/libretroshare/src/retroshare/rsiface.h b/libretroshare/src/retroshare/rsiface.h index 64ceebf17..c0b30cb43 100644 --- a/libretroshare/src/retroshare/rsiface.h +++ b/libretroshare/src/retroshare/rsiface.h @@ -157,8 +157,6 @@ class RsControl /* The Main Interface Class - for controlling the server */ /****************************************/ /* Config */ - virtual int ConfigSetDataRates( int totalDownload, int totalUpload ) = 0; - virtual int ConfigGetDataRates( float &inKb, float &outKb) = 0; virtual int ConfigSetBootPrompt( bool on ) = 0; virtual void ConfigFinalSave( ) = 0; virtual void rsGlobalShutDown( ) = 0; diff --git a/libretroshare/src/retroshare/rstypes.h b/libretroshare/src/retroshare/rstypes.h index b3a40dc3f..035c4a0e7 100644 --- a/libretroshare/src/retroshare/rstypes.h +++ b/libretroshare/src/retroshare/rstypes.h @@ -112,7 +112,6 @@ class RsConfig { localPort = extPort = 0 ; firewalled = forwardPort = false ; - maxDownloadDataRate = maxUploadDataRate = maxIndivDataRate = 0 ; promptAtBoot = 0 ; DHTActive = uPnPActive = netLocalOk = netUpnpOk = netDhtOk = netStunOk = netExtraAddressOk = false ; uPnPState = DHTPeers = 0 ; @@ -129,10 +128,6 @@ class RsConfig bool firewalled; bool forwardPort; - int maxDownloadDataRate; /* kb */ - int maxUploadDataRate; /* kb */ - int maxIndivDataRate; /* kb */ - int promptAtBoot; /* popup the password prompt */ /* older data types */ diff --git a/libretroshare/src/rsserver/p3face-config.cc b/libretroshare/src/rsserver/p3face-config.cc index 9624a43e4..ed01ea5a3 100644 --- a/libretroshare/src/rsserver/p3face-config.cc +++ b/libretroshare/src/rsserver/p3face-config.cc @@ -45,49 +45,6 @@ const int p3facemsgzone = 11453; /* RsIface Config */ /* Config */ -int RsServer::ConfigSetDataRates( int totalDownload, int totalUpload ) /* in kbrates */ -{ - /* fill the rsiface class */ - RsIface &iface = getIface(); - - /* lock Mutexes */ - lockRsCore(); /* LOCK */ - iface.lockData(); /* LOCK */ - - pqih -> setMaxRate(true, totalDownload); - pqih -> setMaxRate(false, totalUpload); - - pqih -> save_config(); - - /* unlock Mutexes */ - iface.unlockData(); /* UNLOCK */ - unlockRsCore(); /* UNLOCK */ - - /* does its own locking */ - UpdateAllConfig(); - return 1; -} - - -int RsServer::ConfigGetDataRates( float &inKb, float &outKb ) /* in kbrates */ -{ - /* fill the rsiface class */ - RsIface &iface = getIface(); - - /* lock Mutexes */ - lockRsCore(); /* LOCK */ - iface.lockData(); /* LOCK */ - - pqih -> getCurrentRates(inKb, outKb); - - /* unlock Mutexes */ - iface.unlockData(); /* UNLOCK */ - unlockRsCore(); /* UNLOCK */ - - return 1; -} - - int RsServer::ConfigSetBootPrompt( bool /*on*/ ) { @@ -120,11 +77,6 @@ int RsServer::UpdateAllConfig() config.extAddr = rs_inet_ntoa(pstate.serveraddr.sin_addr); config.extPort = ntohs(pstate.serveraddr.sin_port); - - /* data rates */ - config.maxDownloadDataRate = (int) pqih -> getMaxRate(true); /* kb */ - config.maxUploadDataRate = (int) pqih -> getMaxRate(false); /* kb */ - config.promptAtBoot = true; /* popup the password prompt */ /* update network configuration */ diff --git a/libretroshare/src/rsserver/p3face.h b/libretroshare/src/rsserver/p3face.h index d7e3a4e90..9d938f397 100644 --- a/libretroshare/src/rsserver/p3face.h +++ b/libretroshare/src/rsserver/p3face.h @@ -128,8 +128,6 @@ class RsServer: public RsControl, public RsThread public: /* Config */ - virtual int ConfigGetDataRates(float &inKb, float &outKb); - virtual int ConfigSetDataRates( int totalDownload, int totalUpload ); virtual int ConfigSetBootPrompt( bool on ); virtual void ConfigFinalSave( ); diff --git a/libretroshare/src/rsserver/p3serverconfig.cc b/libretroshare/src/rsserver/p3serverconfig.cc index 031a95b5d..31daa9000 100644 --- a/libretroshare/src/rsserver/p3serverconfig.cc +++ b/libretroshare/src/rsserver/p3serverconfig.cc @@ -28,17 +28,27 @@ RsServerConfig *rsConfig = NULL; +static const std::string pqih_ftr("PQIH_FTR"); -p3ServerConfig::p3ServerConfig(p3PeerMgr *peerMgr, p3LinkMgr *linkMgr, p3NetMgr *netMgr, p3GeneralConfig *genCfg) +#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) :configMtx("p3ServerConfig") { mPeerMgr = peerMgr; mLinkMgr = linkMgr; mNetMgr = netMgr; + mPqiHandler = pqih; mGeneralConfig = genCfg; mUserLevel = RSCONFIG_USER_LEVEL_NEW; /* START LEVEL */ + mRateDownload = DEFAULT_DOWNLOAD_KB_RATE; + mRateUpload = DEFAULT_UPLOAD_KB_RATE; rsConfig = this; } @@ -50,6 +60,33 @@ p3ServerConfig::~p3ServerConfig() } +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); +} + + #define RS_CONFIG_ADVANCED_STRING "AdvMode" bool p3ServerConfig::findConfigurationOption(uint32_t key, std::string &keystr) @@ -305,6 +342,7 @@ uint32_t p3ServerConfig::getOperatingMode() return mode; } + bool p3ServerConfig::setOperatingMode(uint32_t opMode) { std::string modestr = "FULL"; @@ -329,8 +367,18 @@ bool p3ServerConfig::setOperatingMode(uint32_t opMode) return switchToOperatingMode(opMode); } + bool p3ServerConfig::switchToOperatingMode(uint32_t opMode) { + float dl_rate = 0; + float ul_rate = 0; + + { + RsStackMutex stack(configMtx); /******* LOCKED MUTEX *****/ + dl_rate = mRateDownload; + ul_rate = mRateUpload; + } + switch (opMode) { default: @@ -338,6 +386,8 @@ bool p3ServerConfig::switchToOperatingMode(uint32_t opMode) /* switch on all transfers */ /* 100% bandwidth */ /* switch on popups, enable hashing */ + //setMaxRate(true, mri); // In / Download + //setMaxRate(false, mro); // Out / Upload. break; case RS_OPMODE_NOTURTLE: /* switch on all transfers - except turtle, enable hashing */ @@ -349,13 +399,36 @@ bool p3ServerConfig::switchToOperatingMode(uint32_t opMode) /* switch on all transfers */ /* reduce bandwidth to 25% */ /* switch off popups, enable hashing */ + + dl_rate *= 0.25; + ul_rate *= 0.25; 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 */ + + + 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; + } + break; } + + if (mPqiHandler) + { + mPqiHandler -> setMaxRate(true, dl_rate); + mPqiHandler -> setMaxRate(false, ul_rate); + } + return true; } @@ -363,20 +436,36 @@ bool p3ServerConfig::switchToOperatingMode(uint32_t opMode) /* handle data rates. * Mutex must be handled at the lower levels: TODO */ -int p3ServerConfig::SetDataRates( int downKb, int upKb ) /* in kbrates */ +int p3ServerConfig::SetMaxDataRates( int downKb, int upKb ) /* in kbrates */ { - //pqih -> setMaxRate(true, totalDownload); - //pqih -> setMaxRate(false, totalUpload); + char line[512]; - //pqih -> save_config(); + { + RsStackMutex stack(configMtx); /******* LOCKED MUTEX *****/ + mRateDownload = downKb; + mRateUpload = upKb; + sprintf(line, "%f %f", mRateDownload, mRateUpload); + } + mGeneralConfig->setSetting(pqih_ftr, std::string(line)); + + load_config(); // load and setup everything. return 1; } -int p3ServerConfig::GetDataRates( float &inKb, float &outKb ) /* in kbrates */ +int p3ServerConfig::GetMaxDataRates( int &inKb, int &outKb ) /* in kbrates */ { - //pqih -> getCurrentRates(inKb, outKb); + RsStackMutex stack(configMtx); /******* LOCKED MUTEX *****/ + + inKb = mRateDownload; + outKb = mRateUpload; return 1; } +int p3ServerConfig::GetCurrentDataRates( float &inKb, float &outKb ) +{ + mPqiHandler->getCurrentRates(inKb, outKb); + return 1; +} + diff --git a/libretroshare/src/rsserver/p3serverconfig.h b/libretroshare/src/rsserver/p3serverconfig.h index 7f4cfaf3e..b892337aa 100644 --- a/libretroshare/src/rsserver/p3serverconfig.h +++ b/libretroshare/src/rsserver/p3serverconfig.h @@ -32,6 +32,7 @@ #include "pqi/p3linkmgr.h" #include "pqi/p3netmgr.h" #include "pqi/p3cfgmgr.h" +#include "pqi/pqihandler.h" #define RS_CONFIG_ADVANCED_STRING "AdvMode" @@ -42,9 +43,11 @@ class p3ServerConfig: public RsServerConfig { public: - p3ServerConfig(p3PeerMgr *peerMgr, p3LinkMgr *linkMgr, p3NetMgr *netMgr, p3GeneralConfig *genCfg); + p3ServerConfig(p3PeerMgr *peerMgr, p3LinkMgr *linkMgr, p3NetMgr *netMgr, pqihandler *pqih, p3GeneralConfig *genCfg); virtual ~p3ServerConfig(); + void load_config(); + /* From RsIface::RsConfig */ virtual int getConfigNetStatus(RsConfigNetStatus &status); @@ -88,8 +91,9 @@ virtual bool setConfigurationOption(uint32_t key, const std::string &opt); virtual uint32_t getOperatingMode(); virtual bool setOperatingMode(uint32_t opMode); -virtual int SetDataRates( int downKb, int upKb ); -virtual int GetDataRates( float &inKb, float &outKb ); +virtual int SetMaxDataRates( int downKb, int upKb ); +virtual int GetMaxDataRates( int &downKb, int &upKb ); +virtual int GetCurrentDataRates( float &inKb, float &outKb ); /********************* ABOVE is RsConfig Interface *******/ @@ -102,10 +106,13 @@ bool findConfigurationOption(uint32_t key, std::string &keystr); p3PeerMgr *mPeerMgr; p3LinkMgr *mLinkMgr; p3NetMgr *mNetMgr; + pqihandler *mPqiHandler; p3GeneralConfig *mGeneralConfig; RsMutex configMtx; uint32_t mUserLevel; // store last one... will later be a config Item too. + float mRateDownload; + float mRateUpload; }; diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 7dabbe7bf..0c9398df6 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -2407,9 +2407,8 @@ int RsServer::StartupRetroShare() /**************************************************************************/ /* trigger generalConfig loading for classes that require it */ /**************************************************************************/ - pqih->setConfig(mGeneralConfig); - - pqih->load_config(); + p3ServerConfig *serverConfig = new p3ServerConfig(mPeerMgr, mLinkMgr, mNetMgr, pqih, mGeneralConfig); + serverConfig->load_config(); /**************************************************************************/ /* Force Any Configuration before Startup (After Load) */ @@ -2543,7 +2542,7 @@ int RsServer::StartupRetroShare() rsDisc = new p3Discovery(ad); rsBandwidthControl = mBwCtrl; - rsConfig = new p3ServerConfig(mPeerMgr, mLinkMgr, mNetMgr, mGeneralConfig); + rsConfig = serverConfig; rsMsgs = new p3Msgs(msgSrv, chatSrv); rsForums = mForums;