Fix waiting for connections.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index b020c1a..00d1fbd 100644 (file)
@@ -428,17 +428,17 @@ public class Core extends AbstractExecutionThreadService {
                        }
 
                        Map<Long, Network> timesForNextConnects = new TreeMap<>(missingNetworks.stream()
-                                       .collect(Collectors.toMap(connectionBackoff::getBackoff, Function.identity(), (network, ignore) -> network)));
+                                       .collect(Collectors.toMap(connectionBackoff::getConnectionTime, Function.identity(), (network, ignore) -> network)));
 
                        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())));
+                       if (firstNetwork.get().getKey() > System.currentTimeMillis()) {
+                               eventBus.post(new GenericMessage(String.format("Waiting %d minutes to connect to %s...", TimeUnit.MILLISECONDS.toMinutes(firstNetwork.get().getKey() - System.currentTimeMillis()), firstNetwork.get().getValue().name())));
                                synchronized (syncObject) {
                                        try {
-                                               syncObject.wait(firstNetwork.get().getKey());
+                                               syncObject.wait(firstNetwork.get().getKey() - System.currentTimeMillis());
                                        } catch (InterruptedException ie1) {
                                                /* ignore. */
                                        }
@@ -446,6 +446,9 @@ public class Core extends AbstractExecutionThreadService {
                                if (!isRunning()) {
                                        break;
                                }
+                               if (firstNetwork.get().getKey() > System.currentTimeMillis()) {
+                                       continue;
+                               }
                        }
 
                        connectNetwork(firstNetwork.get().getValue());