From 203fd6bffc32c27bf5662be411bd6a9f1733a4a1 Mon Sep 17 00:00:00 2001 From: drbob Date: Mon, 19 Dec 2011 18:56:11 +0000 Subject: [PATCH] Added external interface to control Relay Parameters. * Modified UdpRelay handles the counting... now each Relay Class has a bandwidth limit, and you can drop down to a lower class - if your one is full. This ensures that the desired bandwidth is kept, and allows more friends to use the relays. * Added Interface in p3bitdht. TODO: Test this stuff! git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-dhtmods@4728 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/dht/p3bitdht.h | 7 ++ libretroshare/src/dht/p3bitdht_interface.cc | 47 ++++++++++ libretroshare/src/retroshare/rsdht.h | 34 +++++--- libretroshare/src/tcponudp/udprelay.cc | 97 +++++++++++++++------ libretroshare/src/tcponudp/udprelay.h | 27 +++--- 5 files changed, 159 insertions(+), 53 deletions(-) diff --git a/libretroshare/src/dht/p3bitdht.h b/libretroshare/src/dht/p3bitdht.h index 63385703e..5c0cfb3a7 100644 --- a/libretroshare/src/dht/p3bitdht.h +++ b/libretroshare/src/dht/p3bitdht.h @@ -164,6 +164,13 @@ virtual int getRelayProxies(std::list &relayProxies); virtual std::string getUdpAddressString(); + // Interface for controlling Relays & DHT Relay Mode +virtual uint32_t getRelayMode(); +virtual int setRelayMode(uint32_t mode); + +virtual int getRelayAllowance(int classIdx, uint32_t &count, uint32_t &bandwidth); +virtual int setRelayAllowance(int classIdx, uint32_t count, uint32_t bandwidth); + /*********************************************************************************************** ********** External RsDHT Interface (defined in libretroshare/src/retroshare/rsdht.h) ********* ************************************************************************************************/ diff --git a/libretroshare/src/dht/p3bitdht_interface.cc b/libretroshare/src/dht/p3bitdht_interface.cc index 07ea48781..7ed33fe54 100644 --- a/libretroshare/src/dht/p3bitdht_interface.cc +++ b/libretroshare/src/dht/p3bitdht_interface.cc @@ -197,6 +197,53 @@ std::string p3BitDht::getUdpAddressString() /*********************************************************************************************** ********** External RsDHT Interface (defined in libretroshare/src/retroshare/rsdht.h) ********* ************************************************************************************************/ +/*********************************************************************************************** + ********** External RsDHT Interface for Dht-Relay Control ************************************* +************************************************************************************************/ + +uint32_t p3BitDht::getRelayMode() +{ + + return 1; +} + +int p3BitDht::setRelayMode(uint32_t mode) +{ + + return 1; +} + +int p3BitDht::getRelayAllowance(int classIdx, uint32_t &count, uint32_t &bandwidth) +{ + std::cerr << "p3BitDht::getRelayAllowance(" << classIdx << "): "; + if ((classIdx >= 0) && (classIdx < RSDHT_RELAY_NUM_CLASS)) + { + count = mRelay->getRelayClassMax(classIdx); + bandwidth = mRelay->getRelayClassBandwidth(classIdx); + + std::cerr << " count: " << count << " bandwidth: " << bandwidth; + std::cerr << std::endl; + return 1; + } + std::cerr << " Invalid classIdx"; + std::cerr << std::endl; + + return 0; +} + +int p3BitDht::setRelayAllowance(int classIdx, uint32_t count, uint32_t bandwidth) +{ + std::cerr << "p3BitDht::getRelayAllowance(" << classIdx << ", "; + std::cerr << ", " << count << ", " << bandwidth << ")"; + std::cerr << std::endl; + + return mRelay->setRelayClassMax(classIdx, count, bandwidth); +} + + +/*********************************************************************************************** + ********** External RsDHT Interface for Dht-Relay Control ************************************* +************************************************************************************************/ void convertBdPeerToRsDhtPeer(RsDhtPeer &peer, const bdPeer &int_peer) diff --git a/libretroshare/src/retroshare/rsdht.h b/libretroshare/src/retroshare/rsdht.h index 16c29b5a3..a7b53213f 100644 --- a/libretroshare/src/retroshare/rsdht.h +++ b/libretroshare/src/retroshare/rsdht.h @@ -71,6 +71,13 @@ extern RsDht *rsDht; #define RSDHT_TOU_MODE_RELAY 3 +#define RSDHT_RELAY_NUM_CLASS 4 + +#define RSDHT_RELAY_CLASS_ALL 0 +#define RSDHT_RELAY_CLASS_GENERAL 1 +#define RSDHT_RELAY_CLASS_FOF 2 +#define RSDHT_RELAY_CLASS_FRIENDS 3 + class RsDhtPeer { @@ -96,26 +103,18 @@ class RsDhtNetPeer std::string mRsId; uint32_t mPeerType; - uint32_t mDhtState; - //connectLogic. - std::string mConnectState; - - // connect Status - uint32_t mPeerConnectState; - // connect mode - uint32_t mPeerConnectMode; + std::string mConnectState; // connectLogic. + uint32_t mPeerConnectState; // connect Status + uint32_t mPeerConnectMode; // connect mode bool mExclusiveProxyLock; std::string mPeerConnectProxyId; - // Req Status. - uint32_t mPeerReqState; - - // Peer Cb Mgs. - std::string mCbPeerMsg; + uint32_t mPeerReqState; // Req Status. + std::string mCbPeerMsg; // Peer Cb Mgs. }; @@ -168,6 +167,15 @@ virtual int getRelayProxies(std::list &relayProxies) = 0; virtual std::string getUdpAddressString() = 0; + + // Interface for controlling Relays & DHT Relay Mode +virtual uint32_t getRelayMode() = 0; +virtual int setRelayMode(uint32_t mode) = 0; + +virtual int getRelayAllowance(int classIdx, uint32_t &count, uint32_t &bandwidth) = 0; +virtual int setRelayAllowance(int classIdx, uint32_t count, uint32_t bandwidth) = 0; + + #if 0 virtual std::string getPeerStatusString(); virtual std::string getDhtStatusString(); diff --git a/libretroshare/src/tcponudp/udprelay.cc b/libretroshare/src/tcponudp/udprelay.cc index 6723ac525..553157c16 100644 --- a/libretroshare/src/tcponudp/udprelay.cc +++ b/libretroshare/src/tcponudp/udprelay.cc @@ -49,14 +49,17 @@ UdpRelayReceiver::UdpRelayReceiver(UdpPublisher *pub) { mClassLimit.resize(UDP_RELAY_NUM_CLASS); mClassCount.resize(UDP_RELAY_NUM_CLASS); + mClassBandwidth.resize(UDP_RELAY_NUM_CLASS); - setRelayTotal(UDP_RELAY_DEFAULT_COUNT_ALL); for(int i = 0; i < UDP_RELAY_NUM_CLASS; i++) { mClassCount[i] = 0; + mClassBandwidth[i] = 0; } + setRelayTotal(UDP_RELAY_DEFAULT_COUNT_ALL); + /* only allocate this space once */ mTmpSendPkt = malloc(MAX_RELAY_UDP_PACKET_SIZE); mTmpSendSize = MAX_RELAY_UDP_PACKET_SIZE; @@ -279,7 +282,7 @@ int UdpRelayReceiver::removeUdpRelay(UdpRelayAddrSet *addrSet) } -int UdpRelayReceiver::addUdpRelay(UdpRelayAddrSet *addrSet, int relayClass, uint32_t &bandwidth) +int UdpRelayReceiver::addUdpRelay(UdpRelayAddrSet *addrSet, int &relayClass, uint32_t &bandwidth) { RsStackMutex stack(relayMtx); /********** LOCK MUTEX *********/ @@ -288,22 +291,22 @@ int UdpRelayReceiver::addUdpRelay(UdpRelayAddrSet *addrSet, int relayClass, uint int ok = (rit == mRelays.end()); if (!ok) { -//#ifdef DEBUG_UDP_RELAY +#ifdef DEBUG_UDP_RELAY std::cerr << "UdpRelayReceiver::addUdpRelay() ERROR Peer already exists!" << std::endl; -//#endif +#endif return 0; } /* will install if there is space! */ - if (installRelayClass_relayLocked(relayClass)) + if (installRelayClass_relayLocked(relayClass, bandwidth)) { -//#ifdef DEBUG_UDP_RELAY +#ifdef DEBUG_UDP_RELAY std::cerr << "UdpRelayReceiver::addUdpRelay() adding Relay" << std::endl; -//#endif +#endif /* create UdpRelay */ - UdpRelayProxy udpRelay(addrSet, relayClass); + UdpRelayProxy udpRelay(addrSet, relayClass, bandwidth); UdpRelayAddrSet alt = addrSet->flippedSet(); - UdpRelayProxy altUdpRelay(&alt, relayClass); + UdpRelayProxy altUdpRelay(&alt, relayClass, bandwidth); /* must install two (A, B) & (B, A) */ mRelays[*addrSet] = udpRelay; @@ -315,9 +318,9 @@ int UdpRelayReceiver::addUdpRelay(UdpRelayAddrSet *addrSet, int relayClass, uint return 1; } -//#ifdef DEBUG_UDP_RELAY +#ifdef DEBUG_UDP_RELAY std::cerr << "UdpRelayReceiver::addUdpRelay() WARNING Too many Relays!" << std::endl; -//#endif +#endif return 0; } @@ -359,8 +362,11 @@ int UdpRelayReceiver::removeUdpRelay_relayLocked(UdpRelayAddrSet *addrSet) return 1; } - /* Need some stats, to work out how many relays we are supporting */ -int UdpRelayReceiver::installRelayClass_relayLocked(int classIdx) + /* Need some stats, to work out how many relays we are supporting + * modified the code to allow degrading of class .... + * so if you have too many friends, they will fill a FOF spot + */ +int UdpRelayReceiver::installRelayClass_relayLocked(int &classIdx, uint32_t &bandwidth) { /* check for total number of Relays */ if (mClassCount[UDP_RELAY_CLASS_ALL] >= mClassLimit[UDP_RELAY_CLASS_ALL]) @@ -379,12 +385,25 @@ int UdpRelayReceiver::installRelayClass_relayLocked(int classIdx) } /* now check the specifics of the class */ - if (mClassCount[classIdx] >= mClassLimit[classIdx]) + while(mClassCount[classIdx] >= mClassLimit[classIdx]) { std::cerr << "UdpRelayReceiver::installRelayClass() WARNING Relay Class Limit Exceeded"; std::cerr << std::endl; + std::cerr << "UdpRelayReceiver::installRelayClass() ClassIdx: " << classIdx; + std::cerr << std::endl; + std::cerr << "UdpRelayReceiver::installRelayClass() ClassLimit: " << mClassLimit[classIdx]; + std::cerr << std::endl; + std::cerr << "UdpRelayReceiver::installRelayClass() Degrading Class =>: " << classIdx; + std::cerr << std::endl; - return 0; + classIdx--; + if (classIdx == 0) + { + std::cerr << "UdpRelayReceiver::installRelayClass() No Spaces Left"; + std::cerr << std::endl; + + return 0; + } } std::cerr << "UdpRelayReceiver::installRelayClass() Relay Class Ok, Count incremented"; @@ -393,6 +412,7 @@ int UdpRelayReceiver::installRelayClass_relayLocked(int classIdx) /* if we get here we can add one */ mClassCount[UDP_RELAY_CLASS_ALL]++; mClassCount[classIdx]++; + bandwidth = mClassBandwidth[classIdx]; return 1; } @@ -448,7 +468,7 @@ int UdpRelayReceiver::setRelayTotal(int count) } -int UdpRelayReceiver::setRelayClassMax(int classIdx, int count) +int UdpRelayReceiver::setRelayClassMax(int classIdx, int count, int bandwidth) { RsStackMutex stack(relayMtx); /********** LOCK MUTEX *********/ @@ -461,6 +481,7 @@ int UdpRelayReceiver::setRelayClassMax(int classIdx, int count) } mClassLimit[classIdx] = count; + mClassBandwidth[classIdx] = bandwidth; return 1; } @@ -480,6 +501,21 @@ int UdpRelayReceiver::getRelayClassMax(int classIdx) return mClassLimit[classIdx]; } +int UdpRelayReceiver::getRelayClassBandwidth(int classIdx) +{ + RsStackMutex stack(relayMtx); /********** LOCK MUTEX *********/ + + /* check the idx */ + if ((classIdx < 0) || (classIdx >= UDP_RELAY_NUM_CLASS)) + { + std::cerr << "UdpRelayReceiver::getRelayMaximum() ERROR class Idx invalid"; + std::cerr << std::endl; + return 0; + } + + return mClassBandwidth[classIdx]; +} + int UdpRelayReceiver::getRelayCount(int classIdx) { RsStackMutex stack(relayMtx); /********** LOCK MUTEX *********/ @@ -840,7 +876,7 @@ UdpRelayProxy::UdpRelayProxy() mBandwidthLimit = 0; } -UdpRelayProxy::UdpRelayProxy(UdpRelayAddrSet *addrSet, int relayClass) +UdpRelayProxy::UdpRelayProxy(UdpRelayAddrSet *addrSet, int relayClass, uint32_t bandwidth) { mAddrs = *addrSet; mRelayClass = relayClass; @@ -852,18 +888,23 @@ UdpRelayProxy::UdpRelayProxy(UdpRelayAddrSet *addrSet, int relayClass) mStartTS = time(NULL); - switch(relayClass) + mBandwidthLimit = bandwidth; + /* fallback */ + if (mBandwidthLimit == 0) { - default: - case UDP_RELAY_CLASS_GENERAL: - mBandwidthLimit = RELAY_MAX_BANDWIDTH; - break; - case UDP_RELAY_CLASS_FOF: - mBandwidthLimit = RELAY_MAX_BANDWIDTH; - break; - case UDP_RELAY_CLASS_FRIENDS: - mBandwidthLimit = RELAY_MAX_BANDWIDTH; - break; + switch(relayClass) + { + default: + case UDP_RELAY_CLASS_GENERAL: + mBandwidthLimit = RELAY_MAX_BANDWIDTH; + break; + case UDP_RELAY_CLASS_FOF: + mBandwidthLimit = RELAY_MAX_BANDWIDTH; + break; + case UDP_RELAY_CLASS_FRIENDS: + mBandwidthLimit = RELAY_MAX_BANDWIDTH; + break; + } } } diff --git a/libretroshare/src/tcponudp/udprelay.h b/libretroshare/src/tcponudp/udprelay.h index 320f7626e..2387e7d81 100644 --- a/libretroshare/src/tcponudp/udprelay.h +++ b/libretroshare/src/tcponudp/udprelay.h @@ -27,6 +27,7 @@ */ #include "tcponudp/udppeer.h" +#include #include class UdpRelayAddrSet; @@ -49,7 +50,7 @@ class UdpRelayProxy { public: UdpRelayProxy(); - UdpRelayProxy(UdpRelayAddrSet *addrSet, int relayClass); + UdpRelayProxy(UdpRelayAddrSet *addrSet, int relayClass, uint32_t bandwidth); UdpRelayAddrSet mAddrs; double mBandwidth; @@ -94,18 +95,19 @@ std::ostream &operator<<(std::ostream &out, const UdpRelayEnd &ure); #define UDP_RELAY_DEFAULT_COUNT_ALL 10 #define UDP_RELAY_FRAC_GENERAL (0.2) #define UDP_RELAY_FRAC_FOF (0.5) -#define UDP_RELAY_FRAC_FRIENDS (0.8) +#define UDP_RELAY_FRAC_FRIENDS (0.3) -#define UDP_RELAY_NUM_CLASS 4 +/**** DEFINED IN EXTERNAL HEADER FILE ***/ +#define UDP_RELAY_NUM_CLASS RSDHT_RELAY_NUM_CLASS -#define UDP_RELAY_CLASS_ALL 0 -#define UDP_RELAY_CLASS_GENERAL 1 -#define UDP_RELAY_CLASS_FOF 2 -#define UDP_RELAY_CLASS_FRIENDS 3 +#define UDP_RELAY_CLASS_ALL RSDHT_RELAY_CLASS_ALL +#define UDP_RELAY_CLASS_GENERAL RSDHT_RELAY_CLASS_GENERAL +#define UDP_RELAY_CLASS_FOF RSDHT_RELAY_CLASS_FOF +#define UDP_RELAY_CLASS_FRIENDS RSDHT_RELAY_CLASS_FRIENDS // Just for some testing fun! //#define UDP_RELAY_LIFETIME_GENERAL 180 // 3 minutes -//#define UDP_RELAY_LIFETIME_FOF 360 // 6 minutes. +//#define UDP_RELAY_LIFETIME_FOF 360 // 6 minutes. //#define UDP_RELAY_LIFETIME_FRIENDS 720 // 12 minutes. #define UDP_RELAY_LIFETIME_GENERAL 1800 // 30 minutes @@ -131,15 +133,16 @@ int removeUdpPeer(UdpPeer *peer); * the end-points drop the connections */ - int addUdpRelay(UdpRelayAddrSet *addrSet, int relayClass, uint32_t &bandwidth); + int addUdpRelay(UdpRelayAddrSet *addrSet, int &relayClass, uint32_t &bandwidth); int removeUdpRelay(UdpRelayAddrSet *addrs); /* Need some stats, to work out how many relays we are supporting */ int checkRelays(); int setRelayTotal(int count); /* sets all the Relay Counts (frac based on total) */ - int setRelayClassMax(int classIdx, int count); /* set a specific class maximum */ + int setRelayClassMax(int classIdx, int count, int bandwidth); /* set a specific class maximum */ int getRelayClassMax(int classIdx); + int getRelayClassBandwidth(int classIdx); int getRelayCount(int classIdx); /* how many relays (of this type) do we have */ int RelayStatus(std::ostream &out); @@ -158,7 +161,7 @@ int status(std::ostream &out); private: int removeUdpRelay_relayLocked(UdpRelayAddrSet *addrs); - int installRelayClass_relayLocked(int classIdx); + int installRelayClass_relayLocked(int &classIdx, uint32_t &bandwidth); int removeRelayClass_relayLocked(int classIdx); /* Unfortunately, Due the reentrant nature of this classes activities... @@ -176,7 +179,7 @@ int status(std::ostream &out); RsMutex relayMtx; /* for all class data (below) */ - std::vector mClassLimit, mClassCount; + std::vector mClassLimit, mClassCount, mClassBandwidth; std::map mStreams; /* indexed by */ std::map mRelays; /* indexed by */