X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fxdcc%2Fcore%2FCore.java;h=649db75114993bbd6d2896db7c311e2f2e99a3fe;hb=82dbd8dd4022906970f8851080df22c77d0111af;hp=0db311cc9e9cdc4666cbda09d0a52dbdc380c7a1;hpb=3c5338e5025b97d85f8ab9aef33ebf36124cbb8c;p=xudocci.git diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index 0db311c..649db75 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -19,6 +19,7 @@ package net.pterodactylus.xdcc.core; import static java.lang.String.format; import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned; +import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.inviteOnly; import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly; import static net.pterodactylus.irc.util.MessageCleaner.getDefaultInstance; import static net.pterodactylus.xdcc.data.Channel.TO_NETWORK; @@ -29,6 +30,8 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.time.Duration; +import java.time.Instant; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -109,6 +112,7 @@ public class Core extends AbstractExecutionThreadService { /** The logger. */ private static final Logger logger = Logger.getLogger(Core.class.getName()); + private final Instant startup = Instant.now(); private final Object syncObject = new Object(); /** The event bus. */ private final EventBus eventBus; @@ -195,8 +199,11 @@ public class Core extends AbstractExecutionThreadService { Network network = entry.getKey(); Collection bots = networkBots.row(network).values(); int packCount = bots.stream().mapToInt((bot) -> bot.packs().size()).reduce((a, b) -> a + b).orElse(0); - return new ConnectedNetwork(network, entry.getValue().hostname(), - entry.getValue().port(), entry.getValue().nickname(), + Connection connection = entry.getValue(); + return new ConnectedNetwork(network, connection.hostname(), + connection.port(), + connection.getUptime().orElse(Duration.ofSeconds(0)), + connection.nickname(), channels.stream() .filter((channel) -> channel.network() .equals(network)) @@ -257,6 +264,10 @@ public class Core extends AbstractExecutionThreadService { return downloads.values(); } + public Duration getUptime() { + return Duration.between(startup, Instant.now()); + } + // // ACTIONS // @@ -328,13 +339,6 @@ public class Core extends AbstractExecutionThreadService { return; } - /* get connection. */ - Connection connection = networkConnections.get(bot.network()); - if (connection == null) { - /* request for unknown network? */ - return; - } - /* stop the DCC receiver. */ if (download.get().dccReceiver() != null) { download.get().dccReceiver().stop(); @@ -343,6 +347,13 @@ public class Core extends AbstractExecutionThreadService { downloads.remove(pack.name(), download.get()); } + /* get connection. */ + Connection connection = networkConnections.get(bot.network()); + if (connection == null) { + /* request for unknown network? */ + return; + } + /* remove the request from the bot, too. */ try { connection.sendMessage(bot.name(), String.format("XDCC %s", (download.get().dccReceiver() != null) ? "CANCEL" : "REMOVE")); @@ -427,17 +438,17 @@ public class Core extends AbstractExecutionThreadService { } Map 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> 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("Will connect to %2$s at %1$tH:%1$tM...", firstNetwork.get().getKey(), firstNetwork.get().getValue().name()))); synchronized (syncObject) { try { - syncObject.wait(firstNetwork.get().getKey()); + syncObject.wait(firstNetwork.get().getKey() - System.currentTimeMillis()); } catch (InterruptedException ie1) { /* ignore. */ } @@ -445,6 +456,9 @@ public class Core extends AbstractExecutionThreadService { if (!isRunning()) { break; } + if (firstNetwork.get().getKey() > System.currentTimeMillis()) { + continue; + } } connectNetwork(firstNetwork.get().getValue()); @@ -478,6 +492,7 @@ public class Core extends AbstractExecutionThreadService { return; } Server server = servers.get((int) (Math.random() * servers.size())); + eventBus.post(new GenericMessage(String.format("Connecting to %s on %s...", network.name(), server.hostname()))); Connection connection = connectionFactory.createConnection(server.hostname(), server.unencryptedPorts().iterator().next()); connection.username(RandomNickname.get()).realName(RandomNickname.get()); @@ -493,14 +508,14 @@ public class Core extends AbstractExecutionThreadService { * The connection to remove */ private void removeConnection(Connection connection) { + logger.debug(String.format("Removing Connection %s...", connection)); Optional network = getNetwork(connection); if (!network.isPresent()) { + logger.debug(String.format("Connection %s did not belong to any network.", connection)); return; } + logger.debug(String.format("Connection %s belongs to network %s.", connection, network.get())); networkConnections.remove(network.get()); - if (!connection.established()) { - return; - } /* find all channels that need to be removed. */ for (Collection channels : ImmutableList.of(joinedChannels, extraChannels)) { @@ -509,7 +524,7 @@ public class Core extends AbstractExecutionThreadService { if (!joinedChannel.network().equals(network.get())) { continue; } - + logger.debug(String.format("Channel %s will be removed.", joinedChannel)); channelIterator.remove(); } } @@ -653,23 +668,24 @@ public class Core extends AbstractExecutionThreadService { .forEach(bot -> networkBots.row(network.get()) .remove(bot.name())); + channelBanManager.ban(channel.get()); if (channelNotJoined.reason() == registeredNicknamesOnly) { - channels.remove(channel.get()); eventBus.post(new GenericMessage( - format("Not trying to join %s anymore.", channel.get()))); - return; - } - if (channelNotJoined.reason() == banned) { - channelBanManager.ban(channel.get()); + format("%s requires nickname registration, suspending join for a day.", + channel.get()))); + } else if (channelNotJoined.reason() == inviteOnly) { + eventBus.post(new GenericMessage( + format("%s is invite-only, suspending join for a day.", + channel.get()))); + } else if (channelNotJoined.reason() == banned) { eventBus.post(new GenericMessage( format("Banned from %s, suspending join for a day.", channel.get()))); - return; + } else { + eventBus.post(new GenericMessage( + format("Could not join %s: %s", channelNotJoined.channel(), + channelNotJoined.reason()))); } - - eventBus.post(new GenericMessage( - format("Could not join %s: %s", channelNotJoined.channel(), - channelNotJoined.reason()))); } /** @@ -744,7 +760,7 @@ public class Core extends AbstractExecutionThreadService { "Kicked from %s by %s: %s", kickedFromChannel.channel(), kickedFromChannel.kicker(), - kickedFromChannel.reason().or("") + kickedFromChannel.reason().orElse("") ))); } }