Expose whether the connection is established.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 14 Apr 2013 10:15:02 +0000 (12:15 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 14 Apr 2013 10:15:02 +0000 (12:15 +0200)
src/main/java/net/pterodactylus/irc/Connection.java

index 10edc3c..88059ba 100644 (file)
@@ -115,6 +115,9 @@ public class Connection extends AbstractExecutionThreadService implements Servic
        /** The connection handler. */
        private ConnectionHandler connectionHandler;
 
+       /** Whether the connection has already been established. */
+       private boolean established;
+
        /**
         * Creates a new connection.
         *
@@ -139,6 +142,16 @@ public class Connection extends AbstractExecutionThreadService implements Servic
        //
 
        /**
+        * Returns whether this connection has already been established.
+        *
+        * @return {@code true} as long as this connection is established, {@code
+        *         false} otherwise
+        */
+       public boolean established() {
+               return established;
+       }
+
+       /**
         * Returns the nickname that is currently in use by this connection. The
         * nickname is only available once the connection has been {@link #start()}ed.
         *
@@ -447,6 +460,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
 
                                if ((connectionStatus == 0x0f) && (connectionStatus != oldConnectionStatus)) {
                                        /* connection succeeded! */
+                                       established = true;
                                        eventBus.post(new ConnectionEstablished(this));
                                }
                                oldConnectionStatus = connectionStatus;
@@ -456,6 +470,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                        logger.log(Level.WARNING, "I/O error", ioe1);
                        eventBus.post(new ConnectionClosed(this, ioe1));
                } finally {
+                       established = false;
                        logger.info("Closing Connection.");
                        try {
                                Closeables.close(connectionHandler, true);