# HG changeset patch # User Omair Majid # Date 1367942181 14400 # Node ID 17aed647ccffdf8cb24445fff810867642902646 # Parent dd39c8d202fb5003d71b9c4fc28d3a358833c212 Cleanup ConfigurationServer class Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-May/006516.html diff -r dd39c8d202fb -r 17aed647ccff agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/AgentApplication.java --- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/AgentApplication.java Tue Apr 23 11:44:28 2013 +0200 +++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/AgentApplication.java Tue May 07 11:56:21 2013 -0400 @@ -122,7 +122,8 @@ @Override public Object addingService(ServiceReference reference) { final ConfigurationServer configServer = (ConfigurationServer) super.addingService(reference); - configServer.startListening(configuration.getConfigListenAddress()); + String [] host = configuration.getConfigListenAddress().split(":"); + configServer.startListening(host[0], Integer.valueOf(host[1])); ConnectionListener connectionListener = new ConnectionListener() { @Override diff -r dd39c8d202fb -r 17aed647ccff agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/AgentApplicationTest.java --- a/agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/AgentApplicationTest.java Tue Apr 23 11:44:28 2013 +0200 +++ b/agent/cli/src/test/java/com/redhat/thermostat/agent/cli/impl/AgentApplicationTest.java Tue May 07 11:56:21 2013 -0400 @@ -39,6 +39,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; @@ -87,6 +88,8 @@ @RunWith(PowerMockRunner.class) public class AgentApplicationTest { + private static final String COMMAND_CHANNLE_BIND_HOST = "test"; + private static final int COMMAND_CHANNEL_BIND_PORT = 10101; // TODO: Test i18nized versions when they come. private StubBundleContext context; @@ -104,6 +107,7 @@ AgentStartupConfiguration config = mock(AgentStartupConfiguration.class); when(config.getDBConnectionString()).thenReturn("test string; please ignore"); + when(config.getConfigListenAddress()).thenReturn(COMMAND_CHANNLE_BIND_HOST + ":" + COMMAND_CHANNEL_BIND_PORT); configCreator = mock(ConfigurationCreator.class); when(configCreator.create()).thenReturn(config); @@ -238,7 +242,7 @@ latch.countDown(); return null; } - }).when(configServer).startListening(anyString()); + }).when(configServer).startListening(COMMAND_CHANNLE_BIND_HOST, COMMAND_CHANNEL_BIND_PORT); try { agent.run(commandContext); diff -r dd39c8d202fb -r 17aed647ccff agent/command/src/main/java/com/redhat/thermostat/agent/command/ConfigurationServer.java --- a/agent/command/src/main/java/com/redhat/thermostat/agent/command/ConfigurationServer.java Tue Apr 23 11:44:28 2013 +0200 +++ b/agent/command/src/main/java/com/redhat/thermostat/agent/command/ConfigurationServer.java Tue May 07 11:56:21 2013 -0400 @@ -36,12 +36,30 @@ package com.redhat.thermostat.agent.command; -// FIXME document this class +import com.redhat.thermostat.annotations.Service; +import com.redhat.thermostat.common.command.Request; + +/** + * An agent-side service that allows starting and stopping the command-channel + * server. + * + * The command-channel server allows the agent to receive and process + * {@link Request}s. + */ +@Service public interface ConfigurationServer { - void startListening(String address); + /** + * Starts the configuration server so it listens at the interface specified + * by the {@code host} and {@code port}. + */ + void startListening(String host, int port); + /** + * Shuts down the configuration server. No additional {@link Request}s will + * be accepted after this is called, but existing ones may be processed to + * completion. + */ void stopListening(); } - diff -r dd39c8d202fb -r 17aed647ccff agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImpl.java --- a/agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImpl.java Tue Apr 23 11:44:28 2013 +0200 +++ b/agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImpl.java Tue May 07 11:56:21 2013 -0400 @@ -56,11 +56,10 @@ } @Override - public void startListening(String address) { + public void startListening(String hostname, int port) { ServerBootstrap bootstrap = (ServerBootstrap) ctx.getBootstrap(); - String [] host = address.split(":"); - InetSocketAddress addr = new InetSocketAddress(host[0], Integer.parseInt(host[1])); + InetSocketAddress addr = new InetSocketAddress(hostname, port); logger.log(Level.FINE, "Starting command channel server on " + addr.toString()); try { diff -r dd39c8d202fb -r 17aed647ccff agent/command/src/test/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImplTest.java --- a/agent/command/src/test/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImplTest.java Tue Apr 23 11:44:28 2013 +0200 +++ b/agent/command/src/test/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImplTest.java Tue May 07 11:56:21 2013 -0400 @@ -73,7 +73,7 @@ @Test public void testStartListening() { ConfigurationServerImpl server = new ConfigurationServerImpl(ctx); - server.startListening("127.0.0.1:123"); + server.startListening("127.0.0.1", 123); ArgumentCaptor argument = ArgumentCaptor.forClass(InetSocketAddress.class); verify(bootstrap).bind(argument.capture()); @@ -90,7 +90,7 @@ when(bootstrap.bind(any(InetSocketAddress.class))).thenThrow(ChannelException.class); try { - server.startListening("does-not-resolve.example.com:123"); + server.startListening("does-not-resolve.example.com", 123); fail("Should have thrown exception"); } catch (ChannelException e) { // pass