changeset 566:b4360eae631a

Fix for communication channel review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-August/002915.html reviewed-by: vanaltj PR 1124
author Mario Torre <neugens.limasoftware@gmail.com>
date Thu, 23 Aug 2012 20:04:43 +0200
parents 706785db6c08
children 8b4cc7f008bb
files agent/cli/src/main/java/com/redhat/thermostat/agent/cli/AgentApplication.java agent/command/src/main/java/com/redhat/thermostat/agent/command/ConfigurationServer.java agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImpl.java agent/command/src/test/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImplTest.java agent/core/src/main/java/com/redhat/thermostat/agent/Agent.java agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentConfigsUtils.java agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentProperties.java agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentStartupConfiguration.java client/command/src/main/java/com/redhat/thermostat/client/command/cli/PingCommand.java common/core/src/main/java/com/redhat/thermostat/common/storage/AgentInformation.java common/core/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java common/core/src/main/java/com/redhat/thermostat/common/storage/Storage.java common/core/src/main/java/com/redhat/thermostat/common/storage/StorageConstants.java distribution/config/agent.properties
diffstat 14 files changed, 45 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/AgentApplication.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/AgentApplication.java	Thu Aug 23 20:04:43 2012 +0200
@@ -139,7 +139,7 @@
         logger.fine("Connecting to storage...");
 
         final ConfigurationServer configServer = OSGIUtils.getInstance().getService(ConfigurationServer.class);
-        configServer.startListening(configuration.getConfigListenPort());
+        configServer.startListening(configuration.getConfigListenAddress());
 
         BackendRegistry backendRegistry = null;
         try {
--- a/agent/command/src/main/java/com/redhat/thermostat/agent/command/ConfigurationServer.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/agent/command/src/main/java/com/redhat/thermostat/agent/command/ConfigurationServer.java	Thu Aug 23 20:04:43 2012 +0200
@@ -38,7 +38,7 @@
 
 public interface ConfigurationServer {
 
-    void startListening(int configListenPort);
+    void startListening(String address);
 
     void stopListening();
 
--- a/agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImpl.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImpl.java	Thu Aug 23 20:04:43 2012 +0200
@@ -44,7 +44,6 @@
 
 class ConfigurationServerImpl implements ConfigurationServer {
 
-    private static final String HOST = "127.0.0.1";
     private final ConfigurationServerContext ctx;
 
     ConfigurationServerImpl(ConfigurationServerContext ctx) {
@@ -52,11 +51,13 @@
     }
 
     @Override
-    public void startListening(int configListenPort) {
+    public void startListening(String address) {
         ServerBootstrap bootstrap = (ServerBootstrap) ctx.getBootstrap();
 
+        String [] host = address.split(":");
+        
         // Bind and start to accept incoming connections.
-        bootstrap.bind(new InetSocketAddress(HOST, configListenPort));
+        bootstrap.bind(new InetSocketAddress(host[0], Integer.parseInt(host[1])));
     }
 
     @Override
--- a/agent/command/src/test/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImplTest.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/agent/command/src/test/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImplTest.java	Thu Aug 23 20:04:43 2012 +0200
@@ -72,12 +72,13 @@
 
     @Test
     public void testStartListening() {
-        InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 123);
         ConfigurationServerImpl server = new ConfigurationServerImpl(ctx);
-        server.startListening(addr.getPort());
+        server.startListening("127.0.0.1:123");
 
         ArgumentCaptor<InetSocketAddress> argument = ArgumentCaptor.forClass(InetSocketAddress.class);
         verify(bootstrap).bind(argument.capture());
+        
+        InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 123);
         assertEquals(addr, argument.getValue());
     }
 
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/Agent.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/agent/core/src/main/java/com/redhat/thermostat/agent/Agent.java	Thu Aug 23 20:04:43 2012 +0200
@@ -121,7 +121,7 @@
             agentInfo.addBackend(backendInfo);
         }
         agentInfo.setAlive(true);
-        agentInfo.setConfigListenPort(config.getConfigListenPort());
+        agentInfo.setConfigListenAddress(config.getConfigListenAddress());
         return agentInfo;
     }
 
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentConfigsUtils.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentConfigsUtils.java	Thu Aug 23 20:04:43 2012 +0200
@@ -87,10 +87,12 @@
             configuration.setPurge(!Boolean.parseBoolean(purge));
         }
 
-        configuration.setConfigListenPort(12000); // Needs some default value.
-        if (properties.containsKey(AgentProperties.CONFIG_LISTEN_PORT.name())) {
-            String port = properties.getProperty(AgentProperties.CONFIG_LISTEN_PORT.name());
-            configuration.setConfigListenPort(Integer.parseInt(port));
+        // TODO: we could avoid this, which means the agent doesn't want to
+        // accept any connection
+        configuration.setConfigListenAddress("127.0.0.1:12000");
+        if (properties.containsKey(AgentProperties.CONFIG_LISTEN_ADDRESS.name())) {
+            String address = properties.getProperty(AgentProperties.CONFIG_LISTEN_ADDRESS.name());
+            configuration.setConfigListenAddress(address);
         }
     }
     
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentProperties.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentProperties.java	Thu Aug 23 20:04:43 2012 +0200
@@ -43,5 +43,5 @@
     DEBUG_CONSOLE,
     DB_URL,
     SAVE_ON_EXIT,
-    CONFIG_LISTEN_PORT,
+    CONFIG_LISTEN_ADDRESS,
 }
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentStartupConfiguration.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentStartupConfiguration.java	Thu Aug 23 20:04:43 2012 +0200
@@ -62,7 +62,7 @@
     private String password;
 
     private long startTime;
-    private int configListenPort;
+    private String address;
     
     AgentStartupConfiguration() {
         this.backends = new ArrayList<>();
@@ -155,11 +155,11 @@
         return password;
     }
 
-    public void setConfigListenPort(int port) {
-        configListenPort = port;
+    public void setConfigListenAddress(String address) {
+        this.address = address;
     }
 
-    public int getConfigListenPort() {
-        return configListenPort;
+    public String getConfigListenAddress() {
+        return address;
     }
 }
--- a/client/command/src/main/java/com/redhat/thermostat/client/command/cli/PingCommand.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/client/command/src/main/java/com/redhat/thermostat/client/command/cli/PingCommand.java	Thu Aug 23 20:04:43 2012 +0200
@@ -116,8 +116,10 @@
             printCustomMessageWithUsage(out, "Invalid host ID or agent no longer running.  See \'help list-vms to obtain a valid host ID.");
             return;
         }
-        int port = df.getStorage().getConfigListenPort(targetHostRef);
-        InetSocketAddress target = new InetSocketAddress(targetHostRef.getHostName(), port);
+        String address = df.getStorage().getConfigListenAddress(targetHostRef);
+        
+        String [] host = address.split(":");
+        InetSocketAddress target = new InetSocketAddress(host[0], Integer.parseInt(host[1]));
         Request ping = new Request(RequestType.RESPONSE_EXPECTED, target);
         ping.setReceiver("com.redhat.thermostat.agent.command.internal.PingReceiver");
         final Semaphore responseBarrier = new Semaphore(0);
--- a/common/core/src/main/java/com/redhat/thermostat/common/storage/AgentInformation.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/AgentInformation.java	Thu Aug 23 20:04:43 2012 +0200
@@ -51,7 +51,7 @@
     private long stopTime;
 
     private boolean alive;
-    private int configListenPort;
+    private String address;
 
     private List<BackendInformation> backends = new ArrayList<BackendInformation>();
     
@@ -87,11 +87,11 @@
         backends.add(backend);
     }
 
-    public int getConfigListenPort() {
-        return configListenPort;
+    public String getConfigListenAddress() {
+        return address;
     }
 
-    public void setConfigListenPort(int configListenPort) {
-        this.configListenPort = configListenPort;
+    public void setConfigListenAddress(String address) {
+        this.address = address;
     }
 }
--- a/common/core/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java	Thu Aug 23 20:04:43 2012 +0200
@@ -256,7 +256,7 @@
         result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_START_TIME, agentInfo.getStartTime());
         result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_STOP_TIME, agentInfo.getStopTime());
         result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_ALIVE, agentInfo.isAlive());
-        result.put(StorageConstants.KEY_AGENT_CONFIG_LISTEN_PORT, agentInfo.getConfigListenPort());
+        result.put(StorageConstants.KEY_AGENT_CONFIG_LISTEN_ADDRESS, agentInfo.getConfigListenAddress());
         
         BasicDBObject backends = new BasicDBObject();
         for (BackendInformation backend : agentInfo.getBackends()) {
@@ -391,20 +391,20 @@
     }
 
     @Override
-    public int getConfigListenPort(HostRef ref) {
-        return getConfigListenPort(ref.getAgentId());
+    public String getConfigListenAddress(HostRef ref) {
+        return getConfigListenAddress(ref.getAgentId());
     }
 
-    private int getConfigListenPort(String id) {
-        int port = -1;
+    private String getConfigListenAddress(String id) {
+        String address = null;
         DBCollection configCollection = db.getCollection(StorageConstants.CATEGORY_AGENT_CONFIG);
         BasicDBObject query = new BasicDBObject(KEY_AGENT_ID, id);
         DBObject config = configCollection.findOne(query);
-        Object value = config.get(StorageConstants.KEY_AGENT_CONFIG_LISTEN_PORT);
-        if (value instanceof Integer) {
-            port = (int) value;
+        Object value = config.get(StorageConstants.KEY_AGENT_CONFIG_LISTEN_ADDRESS);
+        if (value instanceof String) {
+            address = (String) value;
         }
-        return port;
+        return address;
     }
 
     @Override
--- a/common/core/src/main/java/com/redhat/thermostat/common/storage/Storage.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/Storage.java	Thu Aug 23 20:04:43 2012 +0200
@@ -87,7 +87,7 @@
      */
     public abstract String getBackendConfig(String backendName, String configurationKey);
 
-    public abstract int getConfigListenPort(HostRef ref);
+    public abstract String getConfigListenAddress(HostRef ref);
 
     public abstract void saveFile(String filename, InputStream data);
 
--- a/common/core/src/main/java/com/redhat/thermostat/common/storage/StorageConstants.java	Thu Aug 23 20:02:48 2012 +0200
+++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/StorageConstants.java	Thu Aug 23 20:04:43 2012 +0200
@@ -52,5 +52,5 @@
     public static final String KEY_AGENT_CONFIG_AGENT_ALIVE = "alive";
     public static final String KEY_AGENT_CONFIG_AGENT_STOP_TIME = "stop-time";
 
-    public static final String KEY_AGENT_CONFIG_LISTEN_PORT = "config-listen-port";
+    public static final String KEY_AGENT_CONFIG_LISTEN_ADDRESS = "config-listen-address";
 }
--- a/distribution/config/agent.properties	Thu Aug 23 20:02:48 2012 +0200
+++ b/distribution/config/agent.properties	Thu Aug 23 20:04:43 2012 +0200
@@ -9,7 +9,7 @@
 SAVE_ON_EXIT=false
 
 # A netty-based side channel for accepting configuration/tuning
-# requests from the client will listen for connections on the port
+# requests from the client will listen for connections on the address
 # configured here.
-# If this is removed or commented out, the default port is 12000
-CONFIG_LISTEN_PORT=12000
+# If this is removed or commented out, the default port is 127.0.0.1:12000
+CONFIG_LISTEN_ADDRESS=127.0.0.1:12000