From 3a563250f73914c4fd140466d67b1a9d98068b75 Mon Sep 17 00:00:00 2001
From: Adam Ierymenko <adam.ierymenko@gmail.com>
Date: Tue, 10 Sep 2013 14:13:04 -0400
Subject: Finish stripping minBalance from BandwidthAccount

---
 node/BandwidthAccount.hpp | 21 +++++++--------------
 node/Network.cpp          |  2 +-
 node/Network.hpp          | 22 ++++++++--------------
 3 files changed, 16 insertions(+), 29 deletions(-)

(limited to 'node')

diff --git a/node/BandwidthAccount.hpp b/node/BandwidthAccount.hpp
index 42e68bfe..1f337ca4 100644
--- a/node/BandwidthAccount.hpp
+++ b/node/BandwidthAccount.hpp
@@ -31,9 +31,6 @@
 #include <stdint.h>
 #include <math.h>
 
-#include <algorithm>
-#include <utility>
-
 #include "Constants.hpp"
 #include "Utils.hpp"
 
@@ -66,30 +63,27 @@ public:
 	 * Create and initialize
 	 *
 	 * @param preload Initial balance to place in account
-	 * @param minb Minimum allowed balance (or maximum debt) (<= 0)
 	 * @param maxb Maximum allowed balance (> 0)
 	 * @param acc Rate of accrual in bytes per second
 	 */
-	BandwidthAccount(int32_t preload,int32_t minb,int32_t maxb,int32_t acc)
+	BandwidthAccount(int32_t preload,int32_t maxb,int32_t acc)
 		throw()
 	{
-		init(preload,minb,maxb,acc);
+		init(preload,maxb,acc);
 	}
 
 	/**
 	 * Initialize or re-initialize account
 	 *
 	 * @param preload Initial balance to place in account
-	 * @param minb Minimum allowed balance (or maximum debt) (<= 0)
 	 * @param maxb Maximum allowed balance (> 0)
 	 * @param acc Rate of accrual in bytes per second
 	 */
-	inline void init(int32_t preload,int32_t minb,int32_t maxb,int32_t acc)
+	inline void init(int32_t preload,int32_t maxb,int32_t acc)
 		throw()
 	{
 		_lastTime = Utils::nowf();
 		_balance = preload;
-		_minBalance = minb;
 		_maxBalance = maxb;
 		_accrual = acc;
 	}
@@ -98,23 +92,22 @@ public:
 	 * Update balance by accruing and then deducting
 	 *
 	 * @param deduct Amount to deduct, or 0.0 to just update
-	 * @return New balance with deduction applied, and whether or not deduction fit
+	 * @return New balance after deduction -- if negative, it didn't fit
 	 */
-	inline std::pair<int32_t,bool> update(int32_t deduct)
+	inline int32_t update(int32_t deduct)
 		throw()
 	{
 		double lt = _lastTime;
 		double now = Utils::nowf();
 		_lastTime = now;
 		int32_t newbal = (int32_t)round((double)_balance + ((double)_accrual * (now - lt))) - deduct;
-		bool fits = (newbal > 0);
-		return std::pair<int32_t,bool>((_balance = std::max(_minBalance,std::min(_maxBalance,newbal))),fits);
+		_balance = std::max((int32_t)0,std::min(_maxBalance,newbal));
+		return newbal;
 	}
 
 private:
 	double _lastTime;
 	int32_t _balance;
-	int32_t _minBalance;
 	int32_t _maxBalance;
 	int32_t _accrual;
 };
diff --git a/node/Network.cpp b/node/Network.cpp
index b16daaf3..3fd0e428 100644
--- a/node/Network.cpp
+++ b/node/Network.cpp
@@ -91,7 +91,7 @@ bool Network::Certificate::qualifyMembership(const Network::Certificate &mc) con
 }
 
 // A low default global rate, fast enough for something like ARP
-const Network::MulticastRates::Rate Network::MulticastRates::GLOBAL_DEFAULT_RATE(128,-32,128,64);
+const Network::MulticastRates::Rate Network::MulticastRates::GLOBAL_DEFAULT_RATE(128,128,64);
 
 const char *Network::statusString(const Status s)
 	throw()
diff --git a/node/Network.hpp b/node/Network.hpp
index 76718a77..0cf50e1f 100644
--- a/node/Network.hpp
+++ b/node/Network.hpp
@@ -166,10 +166,9 @@ public:
 	 * Preload and rates of accrual for multicast group bandwidth limits
 	 *
 	 * Key is multicast group in lower case hex format: MAC (without :s) /
-	 * ADI (hex). Value is a comma-delimited list of: preload, min, max,
-	 * rate of accrual for bandwidth accounts. A key called '*' indicates
-	 * the default for unlisted groups. Values are in hexadecimal and may
-	 * be prefixed with '-' to indicate a negative value.
+	 * ADI (hex). Value is preload, maximum balance, and rate of accrual in
+	 * hex. These are signed hex numbers, so a negative value can be prefixed
+	 * with '-'.
 	 */
 	class MulticastRates : private Dictionary
 	{
@@ -180,15 +179,13 @@ public:
 		struct Rate
 		{
 			Rate() {}
-			Rate(int32_t pl,int32_t minb,int32_t maxb,int32_t acc)
+			Rate(int32_t pl,int32_t maxb,int32_t acc)
 			{
 				preload = pl;
-				minBalance = minb;
 				maxBalance = maxb;
 				accrual = acc;
 			}
 			int32_t preload;
-			int32_t minBalance;
 			int32_t maxBalance;
 			int32_t accrual;
 		};
@@ -234,7 +231,7 @@ public:
 		{
 			char tmp[16384];
 			Utils::scopy(tmp,sizeof(tmp),s.c_str());
-			Rate r(0,0,0,0);
+			Rate r(0,0,0);
 			char *saveptr = (char *)0;
 			unsigned int fn = 0;
 			for(char *f=Utils::stok(tmp,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
@@ -243,12 +240,9 @@ public:
 						r.preload = (int32_t)Utils::hexStrToLong(f);
 						break;
 					case 1:
-						r.minBalance = (int32_t)Utils::hexStrToLong(f);
-						break;
-					case 2:
 						r.maxBalance = (int32_t)Utils::hexStrToLong(f);
 						break;
-					case 3:
+					case 2:
 						r.accrual = (int32_t)Utils::hexStrToLong(f);
 						break;
 				}
@@ -577,9 +571,9 @@ public:
 		std::map< std::pair<Address,MulticastGroup>,BandwidthAccount >::iterator bal(_multicastRateAccounts.find(k));
 		if (bal == _multicastRateAccounts.end()) {
 			MulticastRates::Rate r(_mcRates.get(mg));
-			bal = _multicastRateAccounts.insert(std::make_pair(k,BandwidthAccount(r.preload,r.minBalance,r.maxBalance,r.accrual))).first;
+			bal = _multicastRateAccounts.insert(std::pair< std::pair<Address,MulticastGroup>,BandwidthAccount >(k,BandwidthAccount(r.preload,r.maxBalance,r.accrual))).first;
 		}
-		return bal->second.update((int32_t)bytes).second;
+		return (bal->second.update((int32_t)bytes) >= 0);
 	}
 
 private:
-- 
cgit v1.2.3