GUACAMOLE-102: Deal with weights of 0, and properly dispose of connections with negative weights.
Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/commit/075e880a Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/tree/075e880a Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/diff/075e880a Branch: refs/heads/master Commit: 075e880acc7aa115f0a4b27fed3f182f8108affa Parents: 2363c63 Author: Nick Couchman <vn...@apache.org> Authored: Wed May 31 15:22:31 2017 -0400 Committer: Nick Couchman <nick.couch...@yahoo.com> Committed: Mon Jun 5 15:34:21 2017 -0400 ---------------------------------------------------------------------- .../auth/jdbc/connection/ModeledConnection.java | 3 +- .../RestrictedGuacamoleTunnelService.java | 45 +++++++++++--------- 2 files changed, 26 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/blob/075e880a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ModeledConnection.java ---------------------------------------------------------------------- diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ModeledConnection.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ModeledConnection.java index 1044a62..414a8a4 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ModeledConnection.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ModeledConnection.java @@ -409,6 +409,7 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod port != null ? port : defaultConfig.getPort(), encryptionMethod != null ? encryptionMethod : defaultConfig.getEncryptionMethod() ); + } /** * Returns the weight of the connection, or the default. @@ -417,7 +418,7 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod * The weight of the connection. * */ - public int getConnectionWeight() { + public Integer getConnectionWeight() { // Return the connection weight return getModel().getConnectionWeight(); http://git-wip-us.apache.org/repos/asf/incubator-guacamole-client/blob/075e880a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/tunnel/RestrictedGuacamoleTunnelService.java ---------------------------------------------------------------------- diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/tunnel/RestrictedGuacamoleTunnelService.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/tunnel/RestrictedGuacamoleTunnelService.java index b41ec91..49af081 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/tunnel/RestrictedGuacamoleTunnelService.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/tunnel/RestrictedGuacamoleTunnelService.java @@ -23,6 +23,7 @@ import com.google.common.collect.ConcurrentHashMultiset; import com.google.inject.Inject; import com.google.inject.Singleton; import java.util.Arrays; +import java.util.Iterator; import java.util.Comparator; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @@ -47,11 +48,6 @@ public class RestrictedGuacamoleTunnelService extends AbstractGuacamoleTunnelService { /** - * Logger for this class. - */ - private static final Logger logger = LoggerFactory.getLogger(RestrictedGuacamoleTunnelService.class); - - /** * The environment of the Guacamole server. */ @Inject @@ -180,6 +176,14 @@ public class RestrictedGuacamoleTunnelService // Get username String username = user.getIdentifier(); + // Remove connections where weight < 0 + Iterator<ModeledConnection> i = connections.iterator(); + while(i.hasNext()) { + Integer weight = i.next().getConnectionWeight(); + if (weight != null && weight.intValue() < 0) + i.remove(); + } + // Sort connections in ascending order of usage ModeledConnection[] sortedConnections = connections.toArray(new ModeledConnection[connections.size()]); Arrays.sort(sortedConnections, new Comparator<ModeledConnection>() { @@ -187,22 +191,21 @@ public class RestrictedGuacamoleTunnelService @Override public int compare(ModeledConnection a, ModeledConnection b) { - logger.debug("Calculating weights for connections {} and {}.", a.getName(), b.getName()); - int cw = 0; - int weightA = a.getConnectionWeight(); - // If the weight is null, we go ahead and sort, anyway - if (weightA == null) - weightA = 0; - - // If the weight is null, we go ahead and sort, anyway - int weightB = b.getConnectionWeight(); - if (weightB == null) - weightB = 0; - - int connsA = getActiveConnections(a).size(); - int connsB = getActiveConnections(b).size(); - logger.debug("Connection {} has computed weight of {}.", a.getName(), connsA * 10000 / weightA); - logger.debug("Connection {} has computed weight of {}.", b.getName(), connsB * 10000 / weightB); + int weightA, weightB; + // Check if weight of a is null, assign 1 if it is. + if (a.getConnectionWeight() == null) + weightA = 1; + else + weightA = a.getConnectionWeight().intValue() + 1; + + // Check if weight of b is null, assign 1 if it is. + if (b.getConnectionWeight() == null) + weightB = 1; + else + weightB = b.getConnectionWeight().intValue() + 1; + + int connsA = getActiveConnections(a).size() + 1; + int connsB = getActiveConnections(b).size() + 1; return (connsA * 10000 / weightA) - (connsB * 10000 / weightB);