Pārlūkot izejas kodu

Tidying of Server.

Split out error description getting into its own method. Other
minor changes.
pull/606/head
Chris Smith 8 gadus atpakaļ
vecāks
revīzija
daad29f32b
1 mainītis faili ar 38 papildinājumiem un 33 dzēšanām
  1. 38
    33
      src/com/dmdirc/Server.java

+ 38
- 33
src/com/dmdirc/Server.java Parādīt failu

@@ -620,11 +620,9 @@ public class Server extends FrameContainer implements Connection {
620 620
 
621 621
     @Override
622 622
     public void sendMessage(final String target, final String message) {
623
-        parser.ifPresent(p -> {
624
-            if (!message.isEmpty()) {
625
-                p.sendMessage(target, message);
626
-            }
627
-        });
623
+        if (!message.isEmpty()) {
624
+            parser.ifPresent(p -> p.sendMessage(target, message));
625
+        }
628 626
     }
629 627
 
630 628
     @Override
@@ -656,8 +654,7 @@ public class Server extends FrameContainer implements Connection {
656 654
             return parser.map(p -> p.getNetworkName().isEmpty()
657 655
                             ? getNetworkFromServerName(p.getServerName()) : p.getNetworkName())
658 656
                     .orElseThrow(() -> new IllegalStateException(
659
-                            "getNetwork called when " + "parser is null (state: " + getState() +
660
-                                    ')'));
657
+                            "getNetwork called when parser is null (state: " + getState() + ')'));
661 658
         } finally {
662 659
             parserLock.readLock().unlock();
663 660
         }
@@ -882,8 +879,7 @@ public class Server extends FrameContainer implements Connection {
882 879
             inviteManager.removeInvites();
883 880
             updateAwayState(Optional.empty());
884 881
 
885
-            if (getConfigManager().getOptionBool(DOMAIN_GENERAL,
886
-                    "reconnectondisconnect")
882
+            if (getConfigManager().getOptionBool(DOMAIN_GENERAL, "reconnectondisconnect")
887 883
                     && myState.getState() == ServerState.TRANSIENTLY_DISCONNECTED) {
888 884
                 doDelayedReconnect();
889 885
             }
@@ -923,30 +919,8 @@ public class Server extends FrameContainer implements Connection {
923 919
 
924 920
             updateIcon();
925 921
 
926
-            final String description;
927
-
928
-            if (errorInfo.getException() == null) {
929
-                description = errorInfo.getData();
930
-            } else {
931
-                final Exception exception = errorInfo.getException();
932
-
933
-                if (exception instanceof UnknownHostException) {
934
-                    description = "Unknown host (unable to resolve)";
935
-                } else if (exception instanceof NoRouteToHostException) {
936
-                    description = "No route to host";
937
-                } else if (exception instanceof SocketTimeoutException) {
938
-                    description = "Connection attempt timed out";
939
-                } else if (exception instanceof SocketException
940
-                        || exception instanceof SSLException) {
941
-                    description = exception.getMessage();
942
-                } else {
943
-                    LOG.info(APP_ERROR, "Unknown socket error: {}",
944
-                            exception.getClass().getCanonicalName(), exception);
945
-                    description = "Unknown error: " + exception.getMessage();
946
-                }
947
-            }
948
-
949
-            getEventBus().publish(new ServerConnectErrorEvent(this, description));
922
+            getEventBus().publish(new ServerConnectErrorEvent(this, getErrorDescription
923
+                    (errorInfo)));
950 924
 
951 925
             if (getConfigManager().getOptionBool(DOMAIN_GENERAL, "reconnectonconnectfailure")) {
952 926
                 doDelayedReconnect();
@@ -954,6 +928,37 @@ public class Server extends FrameContainer implements Connection {
954 928
         }
955 929
     }
956 930
 
931
+    /**
932
+     * Gets a user-readable description of the specified error.
933
+     *
934
+     * @param errorInfo The parser error to get a description for.
935
+     * @return A user-readable error description.
936
+     */
937
+    private static String getErrorDescription(final ParserError errorInfo) {
938
+        final String description;
939
+        if (errorInfo.getException() == null) {
940
+            description = errorInfo.getData();
941
+        } else {
942
+            final Exception exception = errorInfo.getException();
943
+
944
+            if (exception instanceof UnknownHostException) {
945
+                description = "Unknown host (unable to resolve)";
946
+            } else if (exception instanceof NoRouteToHostException) {
947
+                description = "No route to host";
948
+            } else if (exception instanceof SocketTimeoutException) {
949
+                description = "Connection attempt timed out";
950
+            } else if (exception instanceof SocketException
951
+                    || exception instanceof SSLException) {
952
+                description = exception.getMessage();
953
+            } else {
954
+                LOG.info(APP_ERROR, "Unknown socket error: {}",
955
+                        exception.getClass().getCanonicalName(), exception);
956
+                description = "Unknown error: " + exception.getMessage();
957
+            }
958
+        }
959
+        return description;
960
+    }
961
+
957 962
     /**
958 963
      * Called after the parser receives the 005 headers from the server.
959 964
      */

Notiek ielāde…
Atcelt
Saglabāt