Send an event if the connection is terminated for whatever reason.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 14 Apr 2013 10:14:04 +0000 (12:14 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 14 Apr 2013 10:14:04 +0000 (12:14 +0200)
src/main/java/net/pterodactylus/irc/Connection.java
src/main/java/net/pterodactylus/irc/event/ConnectionClosed.java [new file with mode: 0644]

index f5b29b4..10edc3c 100644 (file)
@@ -46,6 +46,7 @@ import net.pterodactylus.irc.event.ChannelNotJoined;
 import net.pterodactylus.irc.event.ChannelNotJoined.Reason;
 import net.pterodactylus.irc.event.ChannelTopic;
 import net.pterodactylus.irc.event.ClientQuit;
+import net.pterodactylus.irc.event.ConnectionClosed;
 import net.pterodactylus.irc.event.ConnectionEstablished;
 import net.pterodactylus.irc.event.ConnectionFailed;
 import net.pterodactylus.irc.event.DccAcceptReceived;
@@ -450,8 +451,10 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                }
                                oldConnectionStatus = connectionStatus;
                        }
+                       eventBus.post(new ConnectionClosed(this));
                } catch (IOException ioe1) {
                        logger.log(Level.WARNING, "I/O error", ioe1);
+                       eventBus.post(new ConnectionClosed(this, ioe1));
                } finally {
                        logger.info("Closing Connection.");
                        try {
diff --git a/src/main/java/net/pterodactylus/irc/event/ConnectionClosed.java b/src/main/java/net/pterodactylus/irc/event/ConnectionClosed.java
new file mode 100644 (file)
index 0000000..184f1f1
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * XdccDownloader - ConnectionClosed.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.irc.event;
+
+import net.pterodactylus.irc.Connection;
+
+import com.google.common.base.Optional;
+
+/**
+ * Notifies a listener that a connection to an IRC server was closed.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ConnectionClosed extends AbstractConnectionEvent {
+
+       /** The optional cause of the connection closing. */
+       private final Optional<Throwable> cause;
+
+       /**
+        * Creates a new connection established event.
+        *
+        * @param connection
+        *              The connection the event occured on
+        */
+       public ConnectionClosed(Connection connection) {
+               this(connection, null);
+       }
+
+       /**
+        * Creates a new connection established event.
+        *
+        * @param connection
+        *              The connection the event occured on
+        * @param cause
+        *              The cause of the connection closing
+        */
+       public ConnectionClosed(Connection connection, Throwable cause) {
+               super(connection);
+               this.cause = Optional.fromNullable(cause);
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the cause of the connection closing.
+        *
+        * @return The cause of the connection closing, or {@link Optional#absent()} if
+        *         the connection was closed properly
+        */
+       public Optional<Throwable> cause() {
+               return cause;
+       }
+
+}