From 9d3def37afb5389d19f2f2115eab0c0a8999b673 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 12 Feb 2018 14:37:06 +0200 Subject: [PATCH] Connection fail log warning should be less common. --- .../system/info/connection/ConnectionLog.java | 13 +++++++- .../info/connection/WebExceptionLogger.java | 32 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionLog.java b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionLog.java index b99856c84..1064d5ff0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionLog.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionLog.java @@ -56,7 +56,7 @@ public class ConnectionLog { return log; } - public static class Entry { + public static class Entry implements Comparable { private final int responseCode; private final long timeSent; @@ -73,6 +73,17 @@ public class ConnectionLog { public long getTimeSent() { return timeSent; } + + /** + * Most recent first. + * + * @param o + * @return + */ + @Override + public int compareTo(Entry o) { + return -Long.compare(this.timeSent, o.timeSent); + } } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/WebExceptionLogger.java b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/WebExceptionLogger.java index d61bca6a2..229833265 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/WebExceptionLogger.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/WebExceptionLogger.java @@ -7,6 +7,11 @@ package com.djrapitops.plan.system.info.connection; import com.djrapitops.plan.api.exceptions.connection.*; import com.djrapitops.plugin.api.utility.log.Log; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + /** * Class that decides what to do with WebExceptions. * @@ -17,7 +22,11 @@ public class WebExceptionLogger { public static void logIfOccurs(Class c, ExceptionLoggingAction action) { try { action.performAction(); - } catch (ConnectionFailException | UnsupportedTransferDatabaseException | UnauthorizedServerException + } catch (ConnectionFailException e) { + if (shouldLog(e)) { + Log.warn(e.getMessage()); + } + } catch (UnsupportedTransferDatabaseException | UnauthorizedServerException | NotFoundException | NoServersException e) { Log.warn(e.getMessage()); } catch (WebException e) { @@ -25,6 +34,27 @@ public class WebExceptionLogger { } } + private static boolean shouldLog(ConnectionFailException e) { + String address = null; + if (e.getMessage().contains("to address")) { + String[] split = e.getMessage().split("to address: "); + if (split.length == 2) { + String[] split2 = split[1].split("
"); + if (split2.length == 2) { + address = split2[0]; + } + } + } + Map> logEntries = ConnectionLog.getLogEntries(); + Map entries = logEntries.get("Out: " + address); + if (entries != null) { + List connections = new ArrayList<>(entries.values()); + Collections.sort(connections); + return connections.isEmpty() || connections.get(0).getResponseCode() != -1; + } + return true; + } + public interface ExceptionLoggingAction { void performAction() throws WebException;