From 186f85c44eddcf2bcb1db61b3c0ba07948635252 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Sun, 31 Jan 2016 00:38:03 +0100 Subject: [PATCH] Refactorings, small improvements/fixes --- .../trade/offer/OpenOfferManager.java | 8 +-- .../java/io/bitsquare/app/BitsquareApp.java | 2 +- .../disputes/trader/TraderDisputeView.java | 2 +- .../io/bitsquare/p2p/network/Connection.java | 64 +++++++++---------- .../bitsquare/p2p/network/IllegalRequest.java | 2 +- 5 files changed, 36 insertions(+), 42 deletions(-) diff --git a/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java b/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java index 01cd1f0168..ddd89520ef 100644 --- a/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java +++ b/core/src/main/java/io/bitsquare/trade/offer/OpenOfferManager.java @@ -177,14 +177,12 @@ public class OpenOfferManager { if (!shutDownRequested) { log.debug("shutDown"); shutDownRequested = true; + int numOffers = openOffers.size(); // we remove own offers from offerbook when we go offline - for (OpenOffer openOffer : openOffers) { - offerBookService.removeOfferAtShutDown(openOffer.getOffer()); - } + openOffers.forEach(openOffer -> offerBookService.removeOfferAtShutDown(openOffer.getOffer())); - // delay a bit before we signal that we are done to give time for network if (completeHandler != null) - UserThread.runAfter(() -> completeHandler.run(), 500, TimeUnit.MILLISECONDS); + UserThread.runAfter(completeHandler::run, numOffers * 200 + 300, TimeUnit.MILLISECONDS); } } diff --git a/gui/src/main/java/io/bitsquare/app/BitsquareApp.java b/gui/src/main/java/io/bitsquare/app/BitsquareApp.java index ca051ddea6..a935b6c023 100644 --- a/gui/src/main/java/io/bitsquare/app/BitsquareApp.java +++ b/gui/src/main/java/io/bitsquare/app/BitsquareApp.java @@ -75,7 +75,7 @@ import static io.bitsquare.app.BitsquareEnvironment.APP_NAME_KEY; public class BitsquareApp extends Application { private static final Logger log = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(BitsquareApp.class); - public static final boolean DEV_MODE = false; + public static final boolean DEV_MODE = true; public static final boolean IS_RELEASE_VERSION = !DEV_MODE && true; private static Environment env; diff --git a/gui/src/main/java/io/bitsquare/gui/main/disputes/trader/TraderDisputeView.java b/gui/src/main/java/io/bitsquare/gui/main/disputes/trader/TraderDisputeView.java index 8a0d69eaeb..208771781a 100644 --- a/gui/src/main/java/io/bitsquare/gui/main/disputes/trader/TraderDisputeView.java +++ b/gui/src/main/java/io/bitsquare/gui/main/disputes/trader/TraderDisputeView.java @@ -239,7 +239,7 @@ public class TraderDisputeView extends ActivatableView { tempAttachments.add(new DisputeDirectMessage.Attachment(result.getName(), filesAsBytes)); inputTextArea.setText(inputTextArea.getText() + "\n[Attachment " + result.getName() + "]"); } else { - new Popup().error("The max. allowed file size is " + maxSizeInKB + " kB.").show(); + new Popup().warning("The max. allowed file size is " + maxSizeInKB + " kB.").show(); } } catch (java.io.IOException e) { e.printStackTrace(); diff --git a/network/src/main/java/io/bitsquare/p2p/network/Connection.java b/network/src/main/java/io/bitsquare/p2p/network/Connection.java index 9e01fb7093..25ce057f98 100644 --- a/network/src/main/java/io/bitsquare/p2p/network/Connection.java +++ b/network/src/main/java/io/bitsquare/p2p/network/Connection.java @@ -23,10 +23,7 @@ import java.io.*; import java.net.Socket; import java.net.SocketException; import java.net.SocketTimeoutException; -import java.util.Date; -import java.util.List; -import java.util.Optional; -import java.util.UUID; +import java.util.*; import java.util.concurrent.*; import static com.google.common.base.Preconditions.checkArgument; @@ -39,8 +36,8 @@ import static com.google.common.base.Preconditions.checkNotNull; public class Connection implements MessageListener { private static final Logger log = LoggerFactory.getLogger(Connection.class); private static final int MAX_MSG_SIZE = 100 * 1024; // 100 kb of compressed data - private static final int MAX_MSG_PER_SEC = 10; // With MAX_MSG_SIZE of 100kb results in bandwidth of 10 mbit/sec - private static final int MAX_MSG_PER_10SEC = 50; // With MAX_MSG_SIZE of 100kb results in bandwidth of 5 mbit/sec for 10 sec + private static final int MSG_THROTTLE_PER_SEC = 10; // With MAX_MSG_SIZE of 100kb results in bandwidth of 10 mbit/sec + private static final int MSG_THROTTLE_PER_10SEC = 50; // With MAX_MSG_SIZE of 100kb results in bandwidth of 5 mbit/sec for 10 sec //timeout on blocking Socket operations like ServerSocket.accept() or SocketInputStream.read() private static final int SOCKET_TIMEOUT = 30 * 60 * 1000; // 30 min. @@ -86,7 +83,7 @@ public class Connection implements MessageListener { private final boolean useCompression = false; private PeerType peerType; private final ObjectProperty nodeAddressProperty = new SimpleObjectProperty<>(); - + private List messageTimeStamps = new ArrayList<>(); /////////////////////////////////////////////////////////////////////////////////////////// // Constructor @@ -204,6 +201,26 @@ public class Connection implements MessageListener { sharedModel.reportIllegalRequest(illegalRequest); } + public boolean violatesThrottleLimit() { + long now = System.currentTimeMillis(); + boolean violated = false; + if (messageTimeStamps.size() >= MSG_THROTTLE_PER_SEC) { + // check if we got more than 10 msg per sec. + long compareTo = messageTimeStamps.get(messageTimeStamps.size() - MSG_THROTTLE_PER_SEC); + violated = now - compareTo < 1000; + } + + if (messageTimeStamps.size() >= MSG_THROTTLE_PER_10SEC) { + // check if we got more than 50 msg per 10 sec. + long compareTo = messageTimeStamps.get(messageTimeStamps.size() - MSG_THROTTLE_PER_10SEC); + violated = violated || now - compareTo < 10000; + // we limit to max 50 entries + messageTimeStamps.remove(0); + } + + messageTimeStamps.add(now); + return violated; + } /////////////////////////////////////////////////////////////////////////////////////////// // MessageListener implementation @@ -428,7 +445,6 @@ public class Connection implements MessageListener { private Date lastActivityDate; private volatile boolean stopped; private ConnectionListener.Reason shutDownReason; - private List messageTimeStamps = new CopyOnWriteArrayList<>(); public SharedModel(Connection connection, Socket socket) { Log.traceCall(); @@ -518,27 +534,6 @@ public class Connection implements MessageListener { this.stopped = true; } - private boolean tooManyMessages() { - long now = System.currentTimeMillis(); - boolean exceeds = false; - if (messageTimeStamps.size() >= MAX_MSG_PER_SEC) { - // check if we got more than 10 msg per sec. - long compareTo = messageTimeStamps.get(messageTimeStamps.size() - MAX_MSG_PER_SEC); - exceeds = now - compareTo < 1000; - } - - if (messageTimeStamps.size() >= MAX_MSG_PER_10SEC) { - // check if we got more than 50 msg per 10 sec. - long compareTo = messageTimeStamps.get(messageTimeStamps.size() - MAX_MSG_PER_10SEC); - exceeds = exceeds || now - compareTo < 10000; - // we limit to max 50 entries - messageTimeStamps.remove(0); - } - - messageTimeStamps.add(now); - return exceeds; - } - public synchronized ConnectionListener.Reason getShutDownReason() { return shutDownReason; } @@ -635,6 +630,12 @@ public class Connection implements MessageListener { sharedModel.reportIllegalRequest(IllegalRequest.MaxSizeExceeded); return; } + + if (sharedModel.connection.violatesThrottleLimit()) { + sharedModel.reportIllegalRequest(IllegalRequest.ViolatedThrottleLimit); + return; + } + if (!(serializable instanceof Message)) { sharedModel.reportIllegalRequest(IllegalRequest.InvalidDataType); return; @@ -646,11 +647,6 @@ public class Connection implements MessageListener { return; } - if (sharedModel.tooManyMessages()) { - sharedModel.reportIllegalRequest(IllegalRequest.TooManyMessages); - return; - } - Connection connection = sharedModel.connection; sharedModel.updateLastActivityDate(); if (message instanceof CloseConnectionMessage) { diff --git a/network/src/main/java/io/bitsquare/p2p/network/IllegalRequest.java b/network/src/main/java/io/bitsquare/p2p/network/IllegalRequest.java index cf98878d8e..1c25338a68 100644 --- a/network/src/main/java/io/bitsquare/p2p/network/IllegalRequest.java +++ b/network/src/main/java/io/bitsquare/p2p/network/IllegalRequest.java @@ -4,7 +4,7 @@ public enum IllegalRequest { MaxSizeExceeded(1), InvalidDataType(0), WrongNetworkId(0), - TooManyMessages(1); + ViolatedThrottleLimit(1); public final int maxTolerance;