From 3c5338e5025b97d85f8ab9aef33ebf36124cbb8c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 13 Jan 2015 22:09:15 +0100 Subject: [PATCH] Fix connection to network. --- .../java/net/pterodactylus/xdcc/core/Core.java | 59 ++++++++++++---------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index 1038b4f..0db311c 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -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 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 timesForNextConnects = new TreeMap<>(missingNetworks.stream() .collect(Collectors.toMap(connectionBackoff::getBackoff, Function.identity(), (network, ignore) -> network))); - Entry 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> 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 = 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 botsToRemove = networkBots.row(network.get()) -- 2.7.4