From f32d3aaef591a56aa12d661ee16d507c0e4f5352 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sat, 6 Jan 2018 16:15:15 +0000
Subject: [PATCH] Console: Show and set burst bandwidth on /config (ticket
 #2123) Better error handling Comment out some unused things, misc. cleanups

---
 .../router/web/helpers/ConfigNetHandler.java  | 56 ++++++++++++-------
 .../router/web/helpers/ConfigNetHelper.java   | 26 ++++++---
 apps/routerconsole/jsp/config.jsp             | 18 +++---
 .../transport/FIFOBandwidthRefiller.java      |  4 ++
 4 files changed, 68 insertions(+), 36 deletions(-)

diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNetHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNetHandler.java
index bcac4198fb..be2ed8a0ff 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNetHandler.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNetHandler.java
@@ -43,11 +43,11 @@ public class ConfigNetHandler extends FormHandler {
     private boolean _upnp;
     private boolean _laptop;
     private String _inboundRate;
-    private String _inboundBurstRate;
-    private String _inboundBurst;
+    //private String _inboundBurstRate;
+    //private String _inboundBurst;
     private String _outboundRate;
-    private String _outboundBurstRate;
-    private String _outboundBurst;
+    //private String _outboundBurstRate;
+    //private String _outboundBurst;
     private String _sharePct;
     private boolean _ratesOnly;
     private boolean _udpDisabled;
@@ -114,21 +114,29 @@ public class ConfigNetHandler extends FormHandler {
     public void setInboundrate(String rate) { 
         _inboundRate = (rate != null ? rate.trim() : null); 
     }
+
+/*
     public void setInboundburstrate(String rate) { 
         _inboundBurstRate = (rate != null ? rate.trim() : null); 
     }
     public void setInboundburstfactor(String factor) { 
         _inboundBurst = (factor != null ? factor.trim() : null); 
     }
+****/
+
     public void setOutboundrate(String rate) { 
         _outboundRate = (rate != null ? rate.trim() : null); 
     }
+
+/*
     public void setOutboundburstrate(String rate) { 
         _outboundBurstRate = (rate != null ? rate.trim() : null); 
     }
     public void setOutboundburstfactor(String factor) { 
         _outboundBurst = (factor != null ? factor.trim() : null); 
     }
+****/
+
     public void setSharePercentage(String pct) {
         _sharePct = (pct != null ? pct.trim() : null);
     }
@@ -487,28 +495,38 @@ public class ConfigNetHandler extends FormHandler {
             }
         }
 
-        // Since burst is now hidden in the gui, set burst to +10% for 20 seconds
+        // Since burst is now hidden in the gui, set burst to +10% for 20 seconds (prior to 0.9.33)
+        // As of 0.9.33, we set strict bandwidth limits. Specified rate is the burst rate,
+        // and we set the standard rate to 50KB or 10% lower (whichever is less).
         if ( (_inboundRate != null) && (_inboundRate.length() > 0) &&
-            !_inboundRate.equals(_context.getProperty(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH, "" + FIFOBandwidthRefiller.DEFAULT_INBOUND_BANDWIDTH))) {
-            changes.put(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH, _inboundRate);
+            !_inboundRate.equals(_context.getProperty(FIFOBandwidthRefiller.PROP_INBOUND_BURST_BANDWIDTH,
+                                                      Integer.toString(FIFOBandwidthRefiller.DEFAULT_INBOUND_BURST_BANDWIDTH)))) {
             try {
-                int rate = Integer.parseInt(_inboundRate) * (100 + DEF_BURST_PCT) / 100;
+                int rate = Integer.parseInt(_inboundRate);
                 int kb = DEF_BURST_TIME * rate;
-                changes.put(FIFOBandwidthRefiller.PROP_INBOUND_BURST_BANDWIDTH, "" + rate);
-                changes.put(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH_PEAK, "" + kb);
-            } catch (NumberFormatException nfe) {}
-            bwUpdated = true;
+                changes.put(FIFOBandwidthRefiller.PROP_INBOUND_BURST_BANDWIDTH, Integer.toString(rate));
+                changes.put(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH_PEAK, Integer.toString(kb));
+                rate -= Math.min(rate * DEF_BURST_PCT / 100, 50);
+                changes.put(FIFOBandwidthRefiller.PROP_INBOUND_BANDWIDTH, Integer.toString(rate));
+	        bwUpdated = true;
+            } catch (NumberFormatException nfe) {
+                addFormError(_t("Invalid bandwidth"));
+            }
         }
         if ( (_outboundRate != null) && (_outboundRate.length() > 0) &&
-            !_outboundRate.equals(_context.getProperty(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH, "" + FIFOBandwidthRefiller.DEFAULT_OUTBOUND_BANDWIDTH))) {
-            changes.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH, _outboundRate);
+            !_outboundRate.equals(_context.getProperty(FIFOBandwidthRefiller.PROP_OUTBOUND_BURST_BANDWIDTH,
+                                                       Integer.toString(FIFOBandwidthRefiller.DEFAULT_OUTBOUND_BURST_BANDWIDTH)))) {
             try {
-                int rate = Integer.parseInt(_outboundRate) * (100 + DEF_BURST_PCT) / 100;
+                int rate = Integer.parseInt(_outboundRate);
                 int kb = DEF_BURST_TIME * rate;
-                changes.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BURST_BANDWIDTH, "" + rate);
-                changes.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH_PEAK, "" + kb);
-            } catch (NumberFormatException nfe) {}
-            bwUpdated = true;
+                changes.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BURST_BANDWIDTH, Integer.toString(rate));
+                changes.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH_PEAK, Integer.toString(kb));
+                rate -= Math.min(rate * DEF_BURST_PCT / 100, 50);
+                changes.put(FIFOBandwidthRefiller.PROP_OUTBOUND_BANDWIDTH, Integer.toString(rate));
+	        bwUpdated = true;
+            } catch (NumberFormatException nfe) {
+                addFormError(_t("Invalid bandwidth"));
+            }
         }
 
         if (bwUpdated) {
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNetHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNetHelper.java
index f1593d488e..1503114235 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNetHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNetHelper.java
@@ -235,17 +235,21 @@ public class ConfigNetHelper extends HelperBase {
     }
 
     public String getInboundRate() {
-        return "" + _context.bandwidthLimiter().getInboundKBytesPerSecond();
+        return Integer.toString(_context.bandwidthLimiter().getInboundKBytesPerSecond());
     }
+
     public String getOutboundRate() {
-        return "" + _context.bandwidthLimiter().getOutboundKBytesPerSecond();
+        return Integer.toString(_context.bandwidthLimiter().getOutboundKBytesPerSecond());
     }
-    public String getInboundRateBits() {
-        return kbytesToBits(_context.bandwidthLimiter().getInboundKBytesPerSecond());
+
+    public String getInboundBurstRateBits() {
+        return kbytesToBits(_context.bandwidthLimiter().getInboundBurstKBytesPerSecond());
     }
-    public String getOutboundRateBits() {
-        return kbytesToBits(_context.bandwidthLimiter().getOutboundKBytesPerSecond());
+
+    public String getOutboundBurstRateBits() {
+        return kbytesToBits(_context.bandwidthLimiter().getOutboundBurstKBytesPerSecond());
     }
+
     public String getShareRateBits() {
         return kbytesToBits(getShareBandwidth());
     }
@@ -253,12 +257,16 @@ public class ConfigNetHelper extends HelperBase {
         return DataHelper.formatSize(kbytes * (8 * 1024L)) + ' ' + _t("bits per second") +
                ' ' + _t("or {0} bytes per month maximum", DataHelper.formatSize(kbytes * (1024L * 60 * 60 * 24 * 31)));
     }
+
     public String getInboundBurstRate() {
-        return "" + _context.bandwidthLimiter().getInboundBurstKBytesPerSecond();
+        return Integer.toString(_context.bandwidthLimiter().getInboundBurstKBytesPerSecond());
     }
+
     public String getOutboundBurstRate() {
-        return "" + _context.bandwidthLimiter().getOutboundBurstKBytesPerSecond();
+        return Integer.toString(_context.bandwidthLimiter().getOutboundBurstKBytesPerSecond());
     }
+
+/*
     public String getInboundBurstFactorBox() {
         int numSeconds = 1;
         int rateKBps = _context.bandwidthLimiter().getInboundBurstKBytesPerSecond();
@@ -301,10 +309,10 @@ public class ConfigNetHelper extends HelperBase {
         return buf.toString();
     }
     
-    /** removed */
     public String getEnableLoadTesting() {
         return "";
     }
+****/
     
     public String getSharePercentageBox() {
         int pct = (int) (100 * _context.router().getSharePercentage());
diff --git a/apps/routerconsole/jsp/config.jsp b/apps/routerconsole/jsp/config.jsp
index aba7eb4717..4e0149e0c6 100644
--- a/apps/routerconsole/jsp/config.jsp
+++ b/apps/routerconsole/jsp/config.jsp
@@ -30,21 +30,23 @@
  <tr><td class="infohelp" colspan="2">
  <b><%=intl._t("I2P will work best if you configure your rates to match the speed of your internet connection.")%></b>
  </td></tr>
-   <tr><td><input style="text-align: right; width: 5em;" name="inboundrate" type="text" size="5" maxlength="5" value="<jsp:getProperty name="nethelper" property="inboundRate" />" >
+<%-- display burst, set standard, handler will fix up --%>
+   <tr><td><input style="text-align: right; width: 5em;" name="inboundrate" type="text" size="5" maxlength="5" value="<jsp:getProperty name="nethelper" property="inboundBurstRate" />" >
           <%=intl._t("KBps In")%>
-        </td><td>(<jsp:getProperty name="nethelper" property="inboundRateBits" />)</td>
-<% /********
+        </td><td>(<jsp:getProperty name="nethelper" property="inboundBurstRateBits" />)</td>
+<%--
 <!-- let's keep this simple...
  bursting up to
     <input name="inboundburstrate" type="text" size="5" value="<jsp:getProperty name="nethelper" property="inboundBurstRate" />" /> KBps for
     <jsp:getProperty name="nethelper" property="inboundBurstFactorBox" /><br>
 -->
-*********/ %>
+--%>
     </tr><tr>
-        <td><input style="text-align: right; width: 5em;" name="outboundrate" type="text" size="5" maxlength="5" value="<jsp:getProperty name="nethelper" property="outboundRate" />" >
+<%-- display burst, set standard, handler will fix up --%>
+        <td><input style="text-align: right; width: 5em;" name="outboundrate" type="text" size="5" maxlength="5" value="<jsp:getProperty name="nethelper" property="outboundBurstRate" />" >
          <%=intl._t("KBps Out")%>
-        </td><td>(<jsp:getProperty name="nethelper" property="outboundRateBits" />)</td>
-<% /********
+        </td><td>(<jsp:getProperty name="nethelper" property="outboundBurstRateBits" />)</td>
+<%--
 <!-- let's keep this simple...
  bursting up to
     <input name="outboundburstrate" type="text" size="2" value="<jsp:getProperty name="nethelper" property="outboundBurstRate" />" /> KBps for
@@ -52,7 +54,7 @@
  <i>KBps = kilobytes per second = 1024 bytes per second = 8192 bits per second.<br>
     A negative rate sets the default.</i><br>
 -->
-*********/ %>
+--%>
     </tr><tr>
         <td><jsp:getProperty name="nethelper" property="sharePercentageBox" /> <%=intl._t("Share")%></td>
         <td>(<jsp:getProperty name="nethelper" property="shareRateBits" />)
diff --git a/router/java/src/net/i2p/router/transport/FIFOBandwidthRefiller.java b/router/java/src/net/i2p/router/transport/FIFOBandwidthRefiller.java
index a936043b38..5ab2045605 100644
--- a/router/java/src/net/i2p/router/transport/FIFOBandwidthRefiller.java
+++ b/router/java/src/net/i2p/router/transport/FIFOBandwidthRefiller.java
@@ -233,6 +233,8 @@ public class FIFOBandwidthRefiller implements Runnable {
         
         if (_inboundBurstKBytesPerSecond <= 0)
             _inboundBurstKBytesPerSecond = DEFAULT_INBOUND_BURST_BANDWIDTH;
+        if (_inboundBurstKBytesPerSecond < _inboundKBytesPerSecond)
+            _inboundBurstKBytesPerSecond = _inboundKBytesPerSecond;
         _limiter.setInboundBurstKBps(_inboundBurstKBytesPerSecond);
     }
     
@@ -250,6 +252,8 @@ public class FIFOBandwidthRefiller implements Runnable {
         
         if (_outboundBurstKBytesPerSecond <= 0)
             _outboundBurstKBytesPerSecond = DEFAULT_OUTBOUND_BURST_BANDWIDTH;
+        if (_outboundBurstKBytesPerSecond < _outboundKBytesPerSecond)
+            _outboundBurstKBytesPerSecond = _outboundKBytesPerSecond;
         _limiter.setOutboundBurstKBps(_outboundBurstKBytesPerSecond);
     }
     
-- 
GitLab