This is an automated email from the ASF dual-hosted git repository. tv pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
commit a842bfa0515f68ad9d3e225b7713cf772083c293 Author: Thomas Vandahl <[email protected]> AuthorDate: Mon Apr 13 16:03:19 2020 +0200 JCS-166 Specify interface to listen on --- .../lateral/socket/tcp/LateralTCPListener.java | 28 ++++++++++++----- .../socket/tcp/TCPLateralCacheAttributes.java | 28 ++++++++++++++++- .../tcp/behavior/ITCPLateralCacheAttributes.java | 15 ++++++++++ .../lateral/socket/tcp/TestTCPLateralUnitTest.java | 35 +++++++++++----------- .../src/test/test-conf/TestTCPLateralCache.ccf | 1 + xdocs/LateralTCPProperties.xml | 6 ++++ 6 files changed, 88 insertions(+), 25 deletions(-) diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java index a2c2c36..e33e82e 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java @@ -25,8 +25,10 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; +import java.net.SocketAddress; import java.net.SocketException; import java.net.SocketTimeoutException; import java.util.Map; @@ -76,9 +78,6 @@ public class LateralTCPListener<K, V> /** Configuration attributes */ private ITCPLateralCacheAttributes tcpLateralCacheAttributes; - /** Listening port */ - private int port; - /** The processor. We should probably use an event queue here. */ private ExecutorService pooledExecutor; @@ -149,16 +148,31 @@ public class LateralTCPListener<K, V> { try { - this.port = getTcpLateralCacheAttributes().getTcpListenerPort(); + int port = getTcpLateralCacheAttributes().getTcpListenerPort(); + String host = getTcpLateralCacheAttributes().getTcpListenerHost(); pooledExecutor = Executors.newCachedThreadPool( new DaemonThreadFactory("JCS-LateralTCPListener-")); terminated = new AtomicBoolean(false); shutdown = new AtomicBoolean(false); - log.info( "Listening on port {0}", port ); - - ServerSocket serverSocket = new ServerSocket( port ); + ServerSocket serverSocket; + if (host != null && host.length() > 0) + { + log.info( "Listening on {0}:{1}", host, port ); + // Resolve host name + InetAddress inetAddress = InetAddress.getByName(host); + //Bind the SocketAddress with inetAddress and port + SocketAddress endPoint = new InetSocketAddress(inetAddress, port); + + serverSocket = new ServerSocket(); + serverSocket.bind(endPoint); + } + else + { + log.info( "Listening on port {0}", port ); + serverSocket = new ServerSocket( port ); + } serverSocket.setSoTimeout( acceptTimeOut ); receiver = new ListenerThread(serverSocket); diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java index cac34db..26ed199 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TCPLateralCacheAttributes.java @@ -66,9 +66,12 @@ public class TCPLateralCacheAttributes /** used to identify the service that this manager will be operating on */ private String tcpServer = ""; - /** The pot */ + /** The port */ private int tcpListenerPort = 0; + /** The host */ + private String tcpListenerHost = ""; + /** udp discovery for tcp server */ private String udpDiscoveryAddr = DEFAULT_UDP_DISCOVERY_ADDRESS; @@ -163,6 +166,29 @@ public class TCPLateralCacheAttributes } /** + * Sets the tcpListenerHost attribute of the ILateralCacheAttributes object + * <p> + * @param val + * The new tcpListenerHost value + */ + @Override + public void setTcpListenerHost( String val ) + { + this.tcpListenerHost = val; + } + + /** + * Gets the tcpListenerHost attribute of the ILateralCacheAttributes object + * <p> + * @return The tcpListenerHost value + */ + @Override + public String getTcpListenerHost() + { + return this.tcpListenerHost; + } + + /** * Can setup UDP Discovery. This only works for TCp laterals right now. It allows TCP laterals * to find each other by broadcasting to a multicast port. * <p> diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java index db12800..7ffd3bc 100644 --- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java +++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/behavior/ITCPLateralCacheAttributes.java @@ -77,6 +77,21 @@ public interface ITCPLateralCacheAttributes int getTcpListenerPort(); /** + * Sets the tcpListenerHost attribute of the ILateralCacheAttributes object + * <p> + * @param val + * The new tcpListenerHost value + */ + void setTcpListenerHost( String val ); + + /** + * Gets the tcpListenerHost attribute of the ILateralCacheAttributes object + * <p> + * @return The tcpListenerHost value + */ + String getTcpListenerHost(); + + /** * Can setup UDP Discovery. This only works for TCp laterals right now. It * allows TCP laterals to find each other by broadcasting to a multicast * port. diff --git a/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TestTCPLateralUnitTest.java b/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TestTCPLateralUnitTest.java index 4f34f80..631ff0a 100644 --- a/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TestTCPLateralUnitTest.java +++ b/commons-jcs-core/src/test/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/TestTCPLateralUnitTest.java @@ -1,5 +1,22 @@ package org.apache.commons.jcs.auxiliary.lateral.socket.tcp; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.jcs.JCS; +import org.apache.commons.jcs.auxiliary.lateral.LateralCacheAttributes; +import org.apache.commons.jcs.auxiliary.lateral.LateralCommand; +import org.apache.commons.jcs.auxiliary.lateral.LateralElementDescriptor; +import org.apache.commons.jcs.engine.CacheElement; +import org.apache.commons.jcs.engine.behavior.ICacheElement; +import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager; +import org.apache.commons.jcs.engine.control.CompositeCache; +import org.apache.commons.jcs.engine.control.CompositeCacheManager; +import org.apache.commons.jcs.engine.control.MockCompositeCacheManager; +import org.apache.commons.jcs.engine.control.group.GroupAttrName; +import org.apache.commons.jcs.engine.control.group.GroupId; +import org.apache.commons.jcs.utils.timing.SleepUtil; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -20,22 +37,6 @@ package org.apache.commons.jcs.auxiliary.lateral.socket.tcp; */ import junit.framework.TestCase; -import org.apache.commons.jcs.JCS; -import org.apache.commons.jcs.auxiliary.lateral.LateralCacheAttributes; -import org.apache.commons.jcs.auxiliary.lateral.LateralCommand; -import org.apache.commons.jcs.auxiliary.lateral.LateralElementDescriptor; -import org.apache.commons.jcs.engine.CacheElement; -import org.apache.commons.jcs.engine.behavior.ICacheElement; -import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager; -import org.apache.commons.jcs.engine.control.CompositeCache; -import org.apache.commons.jcs.engine.control.CompositeCacheManager; -import org.apache.commons.jcs.engine.control.MockCompositeCacheManager; -import org.apache.commons.jcs.engine.control.group.GroupAttrName; -import org.apache.commons.jcs.engine.control.group.GroupId; -import org.apache.commons.jcs.utils.timing.SleepUtil; - -import java.util.Map; -import java.util.Set; /** * Basic unit tests for the sending and receiving portions of the lateral cache. @@ -313,7 +314,7 @@ public class TestTCPLateralUnitTest // VERIFY assertNotNull( "Result should not be null.", result ); - assertEquals( "Didn't get the correct object", "key", result.toArray()[0] ); + assertEquals( "Didn't get the correct object", "key", result.iterator().next().attrName ); } /** diff --git a/commons-jcs-core/src/test/test-conf/TestTCPLateralCache.ccf b/commons-jcs-core/src/test/test-conf/TestTCPLateralCache.ccf index 230c1cc..bf6e941 100644 --- a/commons-jcs-core/src/test/test-conf/TestTCPLateralCache.ccf +++ b/commons-jcs-core/src/test/test-conf/TestTCPLateralCache.ccf @@ -38,6 +38,7 @@ jcs.auxiliary.LTCP=org.apache.commons.jcs.auxiliary.lateral.socket.tcp.LateralTC jcs.auxiliary.LTCP.attributes=org.apache.commons.jcs.auxiliary.lateral.socket.tcp.TCPLateralCacheAttributes jcs.auxiliary.LTCP.attributes.TcpServers=localhost:1111 jcs.auxiliary.LTCP.attributes.TcpListenerPort=1110 +jcs.auxiliary.LTCP.attributes.TcpListenerHost=localhost jcs.auxiliary.LTCP.attributes.AllowGet=false jcs.auxiliary.LTCP.attributes.SocketTimeOut=1001 jcs.auxiliary.LTCP.attributes.OpenTimeOut=2002 diff --git a/xdocs/LateralTCPProperties.xml b/xdocs/LateralTCPProperties.xml index 9444d8e..7c265a0 100644 --- a/xdocs/LateralTCPProperties.xml +++ b/xdocs/LateralTCPProperties.xml @@ -37,6 +37,12 @@ <td>N</td> <td>none</td> </tr> + <tr> + <td>TcpListenerHost</td> + <td> This is the host this cache should listen on (for multi-homed hosts).</td> + <td>Y</td> + <td>listen on all interfaces</td> + </tr> <tr> <td>TcpListenerPort</td> <td> This is the port this cache should listen on.</td>
