From e4793dbb593cf8babe654ed9311eae466d4e6291 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 16 Oct 2014 19:44:46 +0200 Subject: [PATCH] Process handlers generically. --- src/main/java/net/pterodactylus/irc/Connection.java | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/pterodactylus/irc/Connection.java b/src/main/java/net/pterodactylus/irc/Connection.java index 6354c90..86de535 100644 --- a/src/main/java/net/pterodactylus/irc/Connection.java +++ b/src/main/java/net/pterodactylus/irc/Connection.java @@ -18,6 +18,7 @@ package net.pterodactylus.irc; import static com.google.common.base.Preconditions.checkState; +import static java.util.Arrays.asList; import static java.util.concurrent.TimeUnit.SECONDS; import java.io.BufferedReader; @@ -42,6 +43,7 @@ import javax.net.SocketFactory; import net.pterodactylus.irc.connection.ChannelNotJoinedHandler; import net.pterodactylus.irc.connection.ConnectionEstablishHandler; +import net.pterodactylus.irc.connection.Handler; import net.pterodactylus.irc.event.ChannelJoined; import net.pterodactylus.irc.event.ChannelLeft; import net.pterodactylus.irc.event.ChannelMessageReceived; @@ -382,8 +384,10 @@ public class Connection extends AbstractExecutionThreadService implements Servic Map nickPrefixes = Maps.newHashMap(); Set channelTypes = Sets.newHashSet(); - ConnectionEstablishHandler connectionEstablishHandler = new ConnectionEstablishHandler(eventBus, this); - ChannelNotJoinedHandler channelNotJoinedHandler = new ChannelNotJoinedHandler(eventBus, this); + List handlers = asList( + new ConnectionEstablishHandler(eventBus, this), + new ChannelNotJoinedHandler(eventBus, this) + ); while (connected) { Reply reply = connectionHandler.readReply(); @@ -392,6 +396,13 @@ public class Connection extends AbstractExecutionThreadService implements Servic String command = reply.command(); List parameters = reply.parameters(); + for (Handler handler : handlers) { + if (handler.willHandle(reply)) { + handler.handleReply(reply); + break; + } + } + /* most common events. */ if (command.equalsIgnoreCase("PRIVMSG")) { String recipient = parameters.get(0); @@ -455,16 +466,10 @@ public class Connection extends AbstractExecutionThreadService implements Servic } else if (command.equalsIgnoreCase("QUIT")) { eventBus.post(new ClientQuit(this, reply.source().get(), parameters.get(0))); - } else if (channelNotJoinedHandler.willHandle(reply)) { - channelNotJoinedHandler.handleReply(reply); - /* basic connection housekeeping. */ } else if (command.equalsIgnoreCase("PING")) { connectionHandler.sendCommand("PONG", getOptional(parameters, 0), getOptional(parameters, 1)); - } 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")) { for (String parameter : parameters) { -- 2.7.4