Remove channel we’re kicked from from the correct collection.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index ab9804a..30da4a8 100644 (file)
@@ -41,6 +41,7 @@ import java.util.stream.Collectors;
 import net.pterodactylus.irc.Connection;
 import net.pterodactylus.irc.ConnectionFactory;
 import net.pterodactylus.irc.DccReceiver;
+import net.pterodactylus.irc.DefaultConnection;
 import net.pterodactylus.irc.event.ChannelJoined;
 import net.pterodactylus.irc.event.ChannelLeft;
 import net.pterodactylus.irc.event.ChannelMessageReceived;
@@ -104,6 +105,7 @@ public class Core extends AbstractExecutionThreadService {
        /** The logger. */
        private static final Logger logger = Logger.getLogger(Core.class.getName());
 
+       private final Object syncObject = new Object();
        /** The event bus. */
        private final EventBus eventBus;
        private final ConnectionFactory connectionFactory;
@@ -375,10 +377,15 @@ public class Core extends AbstractExecutionThreadService {
        @Override
        protected void run() throws Exception {
                while (isRunning()) {
-                       try {
-                               Thread.sleep(TimeUnit.MINUTES.toMillis(1));
-                       } catch (InterruptedException ie1) {
-                               /* ignore. */
+                       synchronized (syncObject) {
+                               try {
+                                       syncObject.wait(TimeUnit.MINUTES.toMillis(1));
+                               } catch (InterruptedException ie1) {
+                                       /* ignore. */
+                               }
+                       }
+                       if (!isRunning()) {
+                               break;
                        }
 
                        /* find channels that should be monitored but are not. */
@@ -407,7 +414,10 @@ public class Core extends AbstractExecutionThreadService {
        }
 
        @Override
-       protected void shutDown() {
+       protected void triggerShutdown() {
+               synchronized (syncObject) {
+                       syncObject.notifyAll();
+               }
        }
 
        //
@@ -430,10 +440,11 @@ public class Core extends AbstractExecutionThreadService {
                                return;
                        }
                        Server server = servers.get((int) (Math.random() * servers.size()));
-                       Connection connection = connectionFactory.createConnection(server.hostname(), server.unencryptedPorts().iterator().next());
+                       Connection connection = connectionFactory.createConnection(server.hostname(),
+                                       server.unencryptedPorts().iterator().next());
                        connection.username(RandomNickname.get()).realName(RandomNickname.get());
                        networkConnections.put(network, connection);
-                       connection.start();
+                       connection.open();
                }
        }
 
@@ -666,9 +677,9 @@ public class Core extends AbstractExecutionThreadService {
                                        return;
                                }
 
-                               extraChannels.remove(channel);
+                               extraChannels.remove(channel.get());
                        } else {
-                               channels.remove(channel.get());
+                               joinedChannels.remove(channel.get());
                        }
                        eventBus.post(new GenericMessage(format(
                                        "Kicked from %s by %s: %s",