From 5a0fa66ac0cba978941f1f1b696049c2d0bed79e Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 8 Jun 2016 22:44:28 -0400 Subject: [PATCH] added proper talking between pqihandler and GUI for maxrates fixing --- libretroshare/src/pqi/p3peermgr.cc | 17 ++++- libretroshare/src/pqi/p3peermgr.h | 2 + libretroshare/src/pqi/pqihandler.cc | 66 ++++++++++---------- libretroshare/src/pqi/pqihandler.h | 32 +++++++++- libretroshare/src/retroshare/rsconfig.h | 2 +- libretroshare/src/retroshare/rspeers.h | 1 + libretroshare/src/rsserver/p3peers.cc | 5 ++ libretroshare/src/rsserver/p3peers.h | 1 + libretroshare/src/rsserver/p3serverconfig.cc | 7 +++ libretroshare/src/rsserver/p3serverconfig.h | 1 + 10 files changed, 97 insertions(+), 37 deletions(-) diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index f114d5ecb..48e588a8f 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -2075,6 +2075,20 @@ bool p3PeerMgrIMPL::saveList(bool &cleanup, std::list& saveData) return true; } +bool p3PeerMgrIMPL::getMaxRates(const RsPeerId& pid,uint32_t& maxUp,uint32_t& maxDn) +{ + RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + + /* check if it is a friend */ + std::map::iterator it = mFriendList.find(pid) ; + + if(mFriendList.end() == it) + return false ; + + maxUp = it->second.maxUpRate ; + maxDn = it->second.maxDnRate ; + return true ; +} bool p3PeerMgrIMPL::setMaxRates(const RsPeerId& pid,uint32_t maxUp,uint32_t maxDn) { RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ @@ -2171,7 +2185,8 @@ bool p3PeerMgrIMPL::loadList(std::list& load) setLocation(pitem->peerId, pitem->location); } - setMaxRates(pitem->peerId,pitem->maxUploadRate,pitem->maxDownloadRate) ; + #warning needs to talk to pqihandler from here + //setMaxRates(pitem->peerId,pitem->maxUploadRate,pitem->maxDownloadRate) ; if (pitem->netMode == RS_NET_MODE_HIDDEN) { diff --git a/libretroshare/src/pqi/p3peermgr.h b/libretroshare/src/pqi/p3peermgr.h index 7e3af89c1..383514826 100644 --- a/libretroshare/src/pqi/p3peermgr.h +++ b/libretroshare/src/pqi/p3peermgr.h @@ -210,6 +210,7 @@ virtual uint32_t getHiddenType(const RsPeerId &ssl_id) = 0; virtual int getFriendCount(bool ssl, bool online) = 0; virtual bool setMaxRates(const RsPeerId& pid,uint32_t maxR,uint32_t minR)=0; +virtual bool getMaxRates(const RsPeerId& pid,uint32_t& maxR,uint32_t& minR)=0; /************* DEPRECIATED FUNCTIONS (TO REMOVE) ********/ @@ -325,6 +326,7 @@ virtual int getFriendCount(bool ssl, bool online); virtual bool haveOnceConnected(); virtual bool setMaxRates(const RsPeerId& pid,uint32_t maxR,uint32_t minR); +virtual bool getMaxRates(const RsPeerId& pid,uint32_t& maxR,uint32_t& minR); /************************************************************************************************/ /* Extra IMPL Functions (used by p3LinkMgr, p3NetMgr + Setup) */ diff --git a/libretroshare/src/pqi/pqihandler.cc b/libretroshare/src/pqi/pqihandler.cc index e2c3006df..c6f3b29d9 100644 --- a/libretroshare/src/pqi/pqihandler.cc +++ b/libretroshare/src/pqi/pqihandler.cc @@ -27,6 +27,7 @@ #include "util/rsdebug.h" #include "util/rsstring.h" +#include "retroshare/rspeers.h" #include #include @@ -113,27 +114,30 @@ int pqihandler::tick() moreToTick = 1; } #endif - } + } -// static time_t last_print_time = 0 ; -// time_t now = time(NULL) ; -// if(now > last_print_time + 3) -// { -// std::map per_service_count ; -// std::vector per_priority_count ; -// -// ExtractOutQueueStatistics(per_service_count,per_priority_count) ; -// -// std::cerr << "PQIHandler outqueues: " << std::endl; -// -// for(std::map::const_iterator it=per_service_count.begin();it!=per_service_count.end();++it) -// std::cerr << " " << std::hex << it->first << std::dec << " " << it->second << std::endl; -// -// for(int i=0;i last_print_time + 5) + { + // every 5 secs, update the max rates for all modules + + RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/ + for(std::map::iterator it = mods.begin(); it != mods.end(); ++it) + { + // This is rather inelegant, but pqihandler has searchModules that are dynamically allocated, so the max rates + // need to be updated from inside. + uint32_t maxUp,maxDn ; + rsPeers->getPeerMaximumRates(it->first,maxUp,maxDn); + + std::cerr << "Updating searchModule " << it->first << " max rates to " << maxUp << " - " << maxDn << std::endl; + + it->second->mMaxDnRate = maxDn ; + it->second->mMaxUpRate = maxUp ; + } + + last_print_time = now ; + } UpdateRates(); return moreToTick; @@ -502,6 +506,7 @@ int pqihandler::UpdateRates() for(it = mods.begin(); it != mods.end(); ++it) { SearchModule *mod = (it -> second); + mod -> pqi -> setMaxRate(true, in_max_bw); mod -> pqi -> setMaxRate(false, out_max_bw); } @@ -511,18 +516,15 @@ int pqihandler::UpdateRates() for(it = mods.begin(); it != mods.end(); ++it) { SearchModule *mod = (it -> second); - if (mod -> pqi -> getMaxRate(false) < max_out_effective) { - mod -> pqi -> setMaxRate(false, max_out_effective); - } - 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); - } + if (mod -> pqi -> getMaxRate(false) < max_out_effective) mod -> pqi -> setMaxRate(false, max_out_effective); + 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); + + // Caps the allowed max speeds to the user defined values, leaving more space for other peers. + + if(mod->mMaxUpRate > 0 && mod->pqi->getMaxRate(false) > mod->mMaxUpRate) mod->pqi->setMaxRate(false, mod->mMaxUpRate) ; + if(mod->mMaxDnRate > 0 && mod->pqi->getMaxRate(true ) > mod->mMaxDnRate) mod->pqi->setMaxRate(true , mod->mMaxDnRate) ; } return 1; diff --git a/libretroshare/src/pqi/pqihandler.h b/libretroshare/src/pqi/pqihandler.h index 63af1f131..107a10930 100644 --- a/libretroshare/src/pqi/pqihandler.h +++ b/libretroshare/src/pqi/pqihandler.h @@ -39,8 +39,12 @@ class SearchModule { public: - RsPeerId peerid; - PQInterface *pqi; + SearchModule() { mMaxUpRate=0;mMaxDnRate=0;} + + RsPeerId peerid ; + PQInterface *pqi; + uint32_t mMaxUpRate ; // max allowed speeds in kB/s + uint32_t mMaxDnRate ; }; // Presents a P3 Face to the world! @@ -71,14 +75,18 @@ class pqihandler: public P3Interface, public pqiPublisher #endif // rate control. + void setMaxRate(const RsPeerId& pid,bool in, uint32_t val_kBs); void setMaxRate(bool in, float val); float getMaxRate(bool in); + + void setMaxRates(const RsPeerId& pid,bool in,float val) ; + float getMaxRates(const RsPeerId& pid,bool in) ; void getCurrentRates(float &in, float &out); // TESTING INTERFACE. int ExtractRates(std::map &ratemap, RsBwRates &totals); - int ExtractTrafficInfo(std::list &out_lst, std::list &in_lst); + int ExtractTrafficInfo(std::list &out_lst, std::list &in_lst); protected: /* check to be overloaded by those that can @@ -118,6 +126,24 @@ protected: float ticks_per_sec ; }; +inline void pqihandler::setMaxRate(const RsPeerId& pid,bool in,uint32_t val_kBs) +{ + RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/ + + std::map::iterator it = mods.find(pid); + + if(it == mods.end()) + { + std::cerr << "(EE) no search module for pid " << pid << ": cannot set max rate" << std::endl; + return ; + } + + if(in) + it->second->mMaxDnRate = val_kBs ; + else + it->second->mMaxUpRate = val_kBs ; +} + inline void pqihandler::setMaxRate(bool in, float val) { RsStackMutex stack(coreMtx); /**************** LOCKED MUTEX ****************/ diff --git a/libretroshare/src/retroshare/rsconfig.h b/libretroshare/src/retroshare/rsconfig.h index f967a01fc..a47eb7e8d 100644 --- a/libretroshare/src/retroshare/rsconfig.h +++ b/libretroshare/src/retroshare/rsconfig.h @@ -293,8 +293,8 @@ public: /* Data Rate Control - to be moved here */ virtual int SetMaxDataRates( int downKb, int upKb ) = 0; virtual int GetMaxDataRates( int &inKb, int &outKb ) = 0; + virtual int GetCurrentDataRates( float &inKb, float &outKb ) = 0; - }; #endif diff --git a/libretroshare/src/retroshare/rspeers.h b/libretroshare/src/retroshare/rspeers.h index d636c7a36..cbdaca063 100644 --- a/libretroshare/src/retroshare/rspeers.h +++ b/libretroshare/src/retroshare/rspeers.h @@ -433,6 +433,7 @@ public: virtual void setServicePermissionFlags(const RsPgpId& gpg_id,const ServicePermissionFlags& flags) = 0; virtual bool setPeerMaximumRates(const RsPeerId& pid,uint32_t maxUploadRate,uint32_t maxDownloadRate) =0; + virtual bool getPeerMaximumRates(const RsPeerId& pid,uint32_t& maxUploadRate,uint32_t& maxDownloadRate) =0; }; #endif diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index bec18e9d0..9a4bc34c6 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -241,6 +241,11 @@ bool p3Peers::isFriend(const RsPeerId &ssl_id) return mPeerMgr->isFriend(ssl_id); } +bool p3Peers::getPeerMaximumRates(const RsPeerId& pid,uint32_t& maxUploadRate,uint32_t& maxDownloadRate) +{ + return mPeerMgr->getMaxRates(pid,maxUploadRate,maxDownloadRate) ; +} + bool p3Peers::setPeerMaximumRates(const RsPeerId& pid,uint32_t maxUploadRate,uint32_t maxDownloadRate) { return mPeerMgr->setMaxRates(pid,maxUploadRate,maxDownloadRate) ; diff --git a/libretroshare/src/rsserver/p3peers.h b/libretroshare/src/rsserver/p3peers.h index cd5219728..357de88ae 100644 --- a/libretroshare/src/rsserver/p3peers.h +++ b/libretroshare/src/rsserver/p3peers.h @@ -140,6 +140,7 @@ public: virtual void setServicePermissionFlags(const RsPgpId& gpg_id,const ServicePermissionFlags& flags); virtual bool setPeerMaximumRates(const RsPeerId& pid,uint32_t maxUploadRate,uint32_t maxDownloadRate); + virtual bool getPeerMaximumRates(const RsPeerId& pid,uint32_t& maxUploadRate,uint32_t& maxDownloadRate); private: p3LinkMgr *mLinkMgr; diff --git a/libretroshare/src/rsserver/p3serverconfig.cc b/libretroshare/src/rsserver/p3serverconfig.cc index b34471de3..0220a388d 100644 --- a/libretroshare/src/rsserver/p3serverconfig.cc +++ b/libretroshare/src/rsserver/p3serverconfig.cc @@ -503,6 +503,13 @@ bool p3ServerConfig::switchToOperatingMode(uint32_t opMode) return true; } +int p3ServerConfig::SetMaxDataRates(const RsPeerId& pid, int downKb, int upKb ) /* in kbrates */ +{ + mPqiHandler->setMaxRate(pid,true,downKb) ; + mPqiHandler->setMaxRate(pid,false,upKb) ; + + return true ; +} /* handle data rates. * Mutex must be handled at the lower levels: TODO */ diff --git a/libretroshare/src/rsserver/p3serverconfig.h b/libretroshare/src/rsserver/p3serverconfig.h index d9ce3eaed..8ecacc917 100644 --- a/libretroshare/src/rsserver/p3serverconfig.h +++ b/libretroshare/src/rsserver/p3serverconfig.h @@ -95,6 +95,7 @@ virtual bool setOperatingMode(uint32_t opMode); virtual int SetMaxDataRates( int downKb, int upKb ); virtual int GetMaxDataRates( int &downKb, int &upKb ); virtual int GetCurrentDataRates( float &inKb, float &outKb ); +virtual int SetMaxDataRates(const RsPeerId& pid, int downKb, int upKb ); /********************* ABOVE is RsConfig Interface *******/