From 1dc5d9f0f3a7311363835b9db824e05c89039363 Mon Sep 17 00:00:00 2001 From: Marek Otahal Date: Fri, 6 Jan 2012 01:10:05 +0100 Subject: [PATCH] make ConnectionInfo comparable and sort list of connections in Connections_p ConnectionInfo compare by initTime Connections_p implement wish to sort connections, descending Signed-off-by: Marek Otahal --- htroot/Connections_p.java | 14 ++++++++++---- source/net/yacy/cora/protocol/ConnectionInfo.java | 9 ++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/htroot/Connections_p.java b/htroot/Connections_p.java index 74efaee55..047f54604 100644 --- a/htroot/Connections_p.java +++ b/htroot/Connections_p.java @@ -43,6 +43,9 @@ import de.anomic.server.serverCore; import de.anomic.server.serverCore.Session; import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; public final class Connections_p { @@ -141,11 +144,14 @@ public final class Connections_p { // client sessions final Set allConnections = ConnectionInfo.getAllConnections(); - // TODO sorting -// Arrays.sort(a, httpc.connectionTimeComparatorInstance); + // sorting: sort by initTime, decending + List allConnectionsSorted = new LinkedList(allConnections); + Collections.sort(allConnectionsSorted); + Collections.reverse(allConnectionsSorted); // toggle ascending/descending + int c = 0; - synchronized (allConnections) { - for (final ConnectionInfo conInfo: allConnections) { + synchronized (allConnectionsSorted) { + for (final ConnectionInfo conInfo: allConnectionsSorted) { prop.put("clientList_" + c + "_clientProtocol", conInfo.getProtocol()); prop.putNum("clientList_" + c + "_clientLifetime", conInfo.getLifetime()); prop.putNum("clientList_" + c + "_clientUpbytes", conInfo.getUpbytes()); diff --git a/source/net/yacy/cora/protocol/ConnectionInfo.java b/source/net/yacy/cora/protocol/ConnectionInfo.java index c47acb21f..6091a9702 100644 --- a/source/net/yacy/cora/protocol/ConnectionInfo.java +++ b/source/net/yacy/cora/protocol/ConnectionInfo.java @@ -36,7 +36,7 @@ import java.util.Set; * @author daniel * @author sixcooler */ -public class ConnectionInfo { +public class ConnectionInfo implements Comparable { /** * a list of all current connections to be shown in Connections_p */ @@ -270,4 +270,11 @@ public class ConnectionInfo { final ConnectionInfo other = (ConnectionInfo) obj; return this.id == other.id; } + + public int compareTo(ConnectionInfo o) { + if(o==null) throw new NullPointerException("ConnectionInfo: compare() : passed argument is null \n"); + if(this.initTime>o.initTime) return 1; + else if(this.initTime