Fix connection to network.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 13 Jan 2015 21:09:15 +0000 (22:09 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 13 Jan 2015 21:10:03 +0000 (22:10 +0100)
src/main/java/net/pterodactylus/xdcc/core/Core.java

index 1038b4f..0db311c 100644 (file)
@@ -392,10 +392,8 @@ public class Core extends AbstractExecutionThreadService {
                                if (channelBanManager.isBanned(channel)) {
                                        continue;
                                }
-                               if (networkConnections.containsKey(channel.network())) {
-                                       if (networkConnections.get(channel.network()).established()) {
-                                               missingChannels.add(channel);
-                                       }
+                               if (!networkConnections.containsKey(channel.network()) || networkConnections.get(channel.network()).established()) {
+                                       missingChannels.add(channel);
                                }
                        }
                        Set<Network> missingNetworks = missingChannels.stream()
@@ -404,23 +402,25 @@ public class Core extends AbstractExecutionThreadService {
                                        .filter((network) -> !networkConnections.containsKey(network))
                                        .collect(Collectors.toSet());
 
-                       if (!missingChannels.isEmpty()) {
-                               for (Channel missingChannel : missingChannels) {
-                                       Network network = missingChannel.network();
-                                       eventBus.post(new GenericMessage(String.format("Trying to join %s on %s...", missingChannel.name(), network)));
-                                       try {
-                                               channelsBeingJoined.add(missingChannel);
-                                               networkConnections.get(network).joinChannel(missingChannel.name());
-                                       } catch (IOException ioe1) {
-                                               logger.warn(String.format("Could not join %s on %s!", missingChannel.name(), network.name()), ioe1);
+                       if (missingNetworks.isEmpty()) {
+                               if (!missingChannels.isEmpty()) {
+                                       for (Channel missingChannel : missingChannels) {
+                                               Network network = missingChannel.network();
+                                               eventBus.post(new GenericMessage(String.format("Trying to join %s on %s...", missingChannel.name(), network)));
+                                               try {
+                                                       channelsBeingJoined.add(missingChannel);
+                                                       networkConnections.get(network).joinChannel(missingChannel.name());
+                                               } catch (IOException ioe1) {
+                                                       logger.warn(String.format("Could not join %s on %s!", missingChannel.name(), network.name()), ioe1);
+                                               }
                                        }
-                               }
-                       } else if (missingNetworks.isEmpty()) {
-                               synchronized (syncObject) {
-                                       try {
-                                               syncObject.wait(TimeUnit.MINUTES.toMillis(1));
-                                       } catch (InterruptedException ie1) {
+                               } else {
+                                       synchronized (syncObject) {
+                                               try {
+                                                       syncObject.wait(TimeUnit.MINUTES.toMillis(1));
+                                               } catch (InterruptedException ie1) {
                                                /* ignore. */
+                                               }
                                        }
                                }
                                continue;
@@ -429,12 +429,15 @@ public class Core extends AbstractExecutionThreadService {
                        Map<Long, Network> timesForNextConnects = new TreeMap<>(missingNetworks.stream()
                                        .collect(Collectors.toMap(connectionBackoff::getBackoff, Function.identity(), (network, ignore) -> network)));
 
-                       Entry<Long, Network> firstNetwork = timesForNextConnects.entrySet().stream().findFirst().get();
-                       if (firstNetwork.getKey() > 0) {
-                               eventBus.post(new GenericMessage(String.format("Waiting %d seconds to connect to %s...", TimeUnit.MILLISECONDS.toMinutes(firstNetwork.getKey()), firstNetwork.getValue().name())));
+                       Optional<Entry<Long, Network>> firstNetwork = Optional.fromNullable(timesForNextConnects.entrySet().stream().findFirst().orElse(null));
+                       if (!firstNetwork.isPresent()) {
+                               continue;
+                       }
+                       if (firstNetwork.get().getKey() > 0) {
+                               eventBus.post(new GenericMessage(String.format("Waiting %d minutes to connect to %s...", TimeUnit.MILLISECONDS.toMinutes(firstNetwork.get().getKey()), firstNetwork.get().getValue().name())));
                                synchronized (syncObject) {
                                        try {
-                                               syncObject.wait(firstNetwork.getKey());
+                                               syncObject.wait(firstNetwork.get().getKey());
                                        } catch (InterruptedException ie1) {
                                                /* ignore. */
                                        }
@@ -444,7 +447,7 @@ public class Core extends AbstractExecutionThreadService {
                                }
                        }
 
-                       connectNetwork(firstNetwork.getValue());
+                       connectNetwork(firstNetwork.get().getValue());
                }
        }
 
@@ -630,15 +633,15 @@ public class Core extends AbstractExecutionThreadService {
                }
 
                Optional<Channel> channel = getChannel(network.get(), channelNotJoined.channel());
+               synchronized (syncObject) {
+                       syncObject.notifyAll();
+               }
                if (!channel.isPresent()) {
                        eventBus.post(new GenericMessage(format("Could not join %s but didn’t try to join, either.", channelNotJoined.channel())));
                        return;
                }
-
                channelsBeingJoined.remove(channel.get());
-               synchronized (syncObject) {
-                       syncObject.notifyAll();
-               }
+
 
                /* remove all bots for this channel, we might have been kicked. */
                Collection<Bot> botsToRemove = networkBots.row(network.get())