Make joinChannel() method asynchronous.
[xudocci.git] / src / main / java / net / pterodactylus / irc / Connection.java
index 6f0bd12..a50c946 100644 (file)
@@ -32,7 +32,8 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.SynchronousQueue;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import javax.net.SocketFactory;
 
 import net.pterodactylus.irc.event.ChannelJoined;
@@ -54,7 +55,6 @@ import com.beust.jcommander.internal.Maps;
 import com.beust.jcommander.internal.Sets;
 import com.google.common.base.Optional;
 import com.google.common.eventbus.EventBus;
-import com.google.common.eventbus.Subscribe;
 import com.google.common.io.Closeables;
 import com.google.common.util.concurrent.AbstractExecutionThreadService;
 import com.google.common.util.concurrent.Service;
@@ -66,6 +66,9 @@ import com.google.common.util.concurrent.Service;
  */
 public class Connection extends AbstractExecutionThreadService implements Service {
 
+       /* The logger. */
+       private static final Logger logger = Logger.getLogger(Connection.class.getName());
+
        /** The event bus. */
        private final EventBus eventBus;
 
@@ -198,42 +201,11 @@ public class Connection extends AbstractExecutionThreadService implements Servic
         * @param channel
         *              The channel to join
         * @return {@code true} if the channel was joined, {@code false} otherwise
+        * @throws IOException
+        *              if an I/O error occurs
         */
-       public boolean joinChannel(final String channel) {
-               final SynchronousQueue<Boolean> result = new SynchronousQueue<Boolean>();
-               Object eventHandler = new Object() {
-
-                       @Subscribe
-                       public void channelJoined(ChannelJoined channelJoined) throws InterruptedException {
-                               if (!channelJoined.channel().equalsIgnoreCase(channel)) {
-                                       return;
-                               }
-                               Optional<String> nickname = channelJoined.client().nick();
-                               if (!nickname.isPresent() || (nickname.isPresent() && nickname().equalsIgnoreCase(nickname.get()))) {
-                                       eventBus.unregister(this);
-                                       result.put(true);
-                               }
-                       }
-
-                       @Subscribe
-                       public void channelNotJoined(ChannelNotJoined channelNotJoined) throws InterruptedException {
-                               if (!channelNotJoined.channel().equalsIgnoreCase(channel)) {
-                                       return;
-                               }
-                               eventBus.unregister(this);
-                               result.put(false);
-                       }
-               };
-               eventBus.register(eventHandler);
-               try {
-                       connectionHandler.sendCommand("JOIN", channel);
-                       return result.take();
-               } catch (IOException ioe1) {
-                       eventBus.unregister(eventHandler);
-               } catch (InterruptedException ie1) {
-                       /* TODO - how to handle? */
-               }
-               return false;
+       public void joinChannel(final String channel) throws IOException {
+               connectionHandler.sendCommand("JOIN", channel);
        }
 
        //
@@ -282,7 +254,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
 
                        while (connected) {
                                Reply reply = connectionHandler.readReply();
-                               System.err.println("<< " + reply);
+                               logger.finest(String.format("<< %s", reply));
                                String command = reply.command();
                                List<String> parameters = reply.parameters();
 
@@ -317,11 +289,13 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                                                        char modeSymbol = parameter.charAt(closeParen + modeCharacterIndex);
                                                                        nickPrefixes.put(String.valueOf(modeSymbol), String.valueOf(modeCharacter));
                                                                }
+                                                               logger.fine(String.format("Parsed Prefixes: %s", nickPrefixes));
                                                        }
                                                } else if (parameter.startsWith("CHANTYPES=")) {
                                                        for (int typeIndex = 10; typeIndex < parameter.length(); ++typeIndex) {
                                                                channelTypes.add(parameter.charAt(typeIndex));
                                                        }
+                                                       logger.fine(String.format("Parsed Channel Types: %s", channelTypes));
                                                }
                                        }
 
@@ -365,7 +339,6 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                        }
                                } else if (command.equals("366")) {
                                        eventBus.post(new ChannelNicknames(this, parameters.get(1), nicks));
-                                       System.out.println("Found Nicknames: " + nicks);
                                        nicks.clear();
 
                                /* common channel join errors. */
@@ -392,9 +365,9 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                oldConnectionStatus = connectionStatus;
                        }
                } catch (IOException ioe1) {
-                       ioe1.printStackTrace();
+                       logger.log(Level.WARNING, "I/O error", ioe1);
                } finally {
-                       System.out.println("Closing Connection.");
+                       logger.info("Closing Connection.");
                        try {
                                Closeables.close(connectionHandler, true);
                        } catch (IOException ioe1) {
@@ -508,7 +481,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                commandBuilder.append(parameter);
                        }
 
-                       System.out.println(">> " + commandBuilder.toString());
+                       logger.finest(String.format(">> %s", commandBuilder));
                        outputStream.write((commandBuilder.toString() + "\r\n").getBytes("UTF-8"));
                        outputStream.flush();
                }