Add commands to show the current connections and close them.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 26 Apr 2013 05:14:08 +0000 (07:14 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 26 Apr 2013 05:14:08 +0000 (07:14 +0200)
src/main/java/net/pterodactylus/xdcc/core/Core.java
src/main/java/net/pterodactylus/xdcc/ui/stdin/CommandReader.java

index 5b883db..6af2f73 100644 (file)
@@ -147,6 +147,15 @@ public class Core extends AbstractExecutionThreadService {
        //
 
        /**
+        * Returns all currently known connections.
+        *
+        * @return All currently known connections
+        */
+       public Collection<Connection> connections() {
+               return networkConnections.values();
+       }
+
+       /**
         * Returns all configured channels. Due to various circumstances, configured
         * channels might not actually be joined.
         *
index 836f406..58ef4d0 100644 (file)
@@ -26,6 +26,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 
+import net.pterodactylus.irc.Connection;
 import net.pterodactylus.irc.DccReceiver;
 import net.pterodactylus.irc.util.MessageCleaner;
 import net.pterodactylus.xdcc.core.Core;
@@ -85,6 +86,7 @@ public class CommandReader extends AbstractExecutionThreadService {
                String lastLine = "";
                String line;
                final List<Result> lastResult = Lists.newArrayList();
+               final List<Connection> lastConnections = Lists.newArrayList();
                while ((line = reader.readLine()) != null) {
                        if (line.equals("")) {
                                line = lastLine;
@@ -158,6 +160,19 @@ public class CommandReader extends AbstractExecutionThreadService {
                                }
 
                                writeLine(String.format("%d channels (%d joined, %d extra), %d bots offering %d packs (%d unique).", configuredChannelsCount, joinedChannelsCount, extraChannelsCount, bots.size(), packsCount, packNames.size()));
+                       } else if (words[0].equalsIgnoreCase("connections")) {
+                               lastConnections.clear();
+                               int counter = 0;
+                               for (Connection connection : core.connections()) {
+                                       lastConnections.add(connection);
+                                       writer.write(String.format("[%d] %s:%d, %s/s\n", counter++, connection.hostname(), connection.port(), f(connection.getInputRate())));
+                               }
+                               writeLine("End of connections.");
+                       } else if (words[0].equalsIgnoreCase("disconnect")) {
+                               Integer index = Ints.tryParse(words[1]);
+                               if ((index != null) && (index < lastConnections.size())) {
+                                       core.closeConnection(lastConnections.get(index));
+                               }
                        }
 
                        lastLine = line;