import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
import javax.net.SocketFactory;
+import net.pterodactylus.irc.connection.ConnectionEstablishHandler;
import net.pterodactylus.irc.event.ChannelJoined;
import net.pterodactylus.irc.event.ChannelLeft;
import net.pterodactylus.irc.event.ChannelMessageReceived;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
import com.google.common.io.Closeables;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
private ConnectionHandler connectionHandler;
/** Whether the connection has already been established. */
- private boolean established;
+ private final AtomicBoolean established = new AtomicBoolean();
/**
* Creates a new connection.
* false} otherwise
*/
public boolean established() {
- return established;
+ return established.get();
}
/**
return;
}
+ eventBus.register(this);
/* now read replies and react. */
try {
/* some status variables. */
Map<String, String> nickPrefixes = Maps.newHashMap();
Set<Character> channelTypes = Sets.newHashSet();
+ ConnectionEstablishHandler connectionEstablishHandler = new ConnectionEstablishHandler(eventBus, this);
+
while (connected) {
Reply reply = connectionHandler.readReply();
eventBus.post(new ReplyReceived(this, reply));
} else if (command.equalsIgnoreCase("PING")) {
connectionHandler.sendCommand("PONG", getOptional(parameters, 0), getOptional(parameters, 1));
- /* replies 001-004 don’t hold information but they have to be sent on a successful connection. */
- } else if (command.equals("001")) {
- connectionStatus |= 0x01;
- } else if (command.equals("002")) {
- connectionStatus |= 0x02;
- } else if (command.equals("003")) {
- connectionStatus |= 0x04;
- } else if (command.equals("004")) {
- connectionStatus |= 0x08;
+ } else if (connectionEstablishHandler.willHandle(reply)) {
+ connectionEstablishHandler.handleReply(reply);
/* 005 originally was a bounce message, now used to transmit useful information about the server. */
} else if (command.equals("005")) {
} else {
eventBus.post(new UnknownReplyReceived(this, reply));
}
-
- if ((connectionStatus == 0x0f) && (connectionStatus != oldConnectionStatus)) {
- /* connection succeeded! */
- established = true;
- eventBus.post(new ConnectionEstablished(this));
- }
- oldConnectionStatus = connectionStatus;
}
eventBus.post(new ConnectionClosed(this));
} catch (IOException ioe1) {
logger.error("Runtime error", re1);
eventBus.post(new ConnectionClosed(this, re1));
} finally {
- established = false;
+ established.set(false);
+ eventBus.unregister(this);
logger.info("Closing Connection.");
try {
Closeables.close(connectionHandler, true);
}
+ @Subscribe
+ public void connectionEstablished(ConnectionEstablished connectionEstablished) {
+ if (connectionEstablished.connection() == this) {
+ established.set(true);
+ }
+ }
+
//
// PRIVATE METHODS
//
--- /dev/null
+package net.pterodactylus.irc.connection;
+
+import static java.util.Arrays.asList;
+
+import net.pterodactylus.irc.Connection;
+import net.pterodactylus.irc.Reply;
+import net.pterodactylus.irc.event.ConnectionEstablished;
+
+import com.google.common.eventbus.EventBus;
+
+/**
+ * Handles the necessary commands that establish a connection.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ConnectionEstablishHandler implements Handler {
+
+ private static final int FIRST_COMMAND_RECEIVED = 0x01;
+ private static final int SECOND_COMMAND_RECEIVED = 0x02;
+ private static final int THIRD_COMMAND_RECEIVED = 0x04;
+ private static final int FOURTH_COMMAND_RECEIVED = 0x08;
+ private static final int ALL_COMMANDS_RECEIVED = 0x0f;
+
+ private final EventBus eventBus;
+ private final Connection connection;
+ private int connectionStatus;
+ private int oldConnectionStatus;
+
+ public ConnectionEstablishHandler(EventBus eventBus,
+ Connection connection) {
+ this.eventBus = eventBus;
+ this.connection = connection;
+ }
+
+ @Override
+ public boolean willHandle(Reply reply) {
+ return asList("001", "002", "003", "004").contains(reply.command());
+ }
+
+ @Override
+ public void handleReply(Reply reply) {
+ String command = reply.command();
+ /* replies 001-004 don’t hold information but they
+ * have to be sent on a successful connection. */
+ if (command.equals("001")) {
+ connectionStatus |= FIRST_COMMAND_RECEIVED;
+ } else if (command.equals("002")) {
+ connectionStatus |= SECOND_COMMAND_RECEIVED;
+ } else if (command.equals("003")) {
+ connectionStatus |= THIRD_COMMAND_RECEIVED;
+ } else if (command.equals("004")) {
+ connectionStatus |= FOURTH_COMMAND_RECEIVED;
+ }
+
+ if (hasConnectionStatusChanged()
+ && isFinalConnectionStatusReached()) {
+ eventBus.post(new ConnectionEstablished(connection));
+ }
+ oldConnectionStatus = connectionStatus;
+ }
+
+ private boolean hasConnectionStatusChanged() {
+ return connectionStatus != oldConnectionStatus;
+ }
+
+ private boolean isFinalConnectionStatusReached() {
+ return connectionStatus == ALL_COMMANDS_RECEIVED;
+ }
+
+}