package org.apache.commons.jcs3.utils.discovery; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import java.io.IOException; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.NetworkInterface; import java.net.StandardProtocolFamily; import java.net.StandardSocketOptions; import java.nio.ByteBuffer; import java.nio.channels.DatagramChannel; import java.nio.channels.MembershipKey; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.util.Iterator; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.jcs3.engine.CacheInfo; import org.apache.commons.jcs3.engine.behavior.IElementSerializer; import org.apache.commons.jcs3.engine.behavior.IShutdownObserver; import org.apache.commons.jcs3.log.Log; import org.apache.commons.jcs3.log.LogManager; import org.apache.commons.jcs3.utils.net.HostNameUtil; import org.apache.commons.jcs3.utils.threadpool.PoolConfiguration; import org.apache.commons.jcs3.utils.threadpool.PoolConfiguration.WhenBlockedPolicy; import org.apache.commons.jcs3.utils.threadpool.ThreadPoolManager; /** Receives UDP Discovery messages. */ public class UDPDiscoveryReceiver implements Runnable, IShutdownObserver { /** The log factory */ private static final Log log = LogManager.getLog( UDPDiscoveryReceiver.class ); /** The channel used for communication. */ private DatagramChannel multicastChannel; /** The group membership key. */ private MembershipKey multicastGroupKey; /** The selector. */ private Selector selector; /** * TODO: Consider using the threadpool manager to get this thread pool. For now place a tight * restriction on the pool size */ private static final int maxPoolSize = 2; /** The processor */ private final ExecutorService pooledExecutor; /** number of messages received. For debugging and testing. */ private final AtomicInteger cnt = new AtomicInteger(0); /** Service to get cache names and handle request broadcasts */ private final UDPDiscoveryService service; /** Serializer */ private IElementSerializer serializer; /** Is it shutdown. */ private final AtomicBoolean shutdown = new AtomicBoolean(false); /** * Constructor for the UDPDiscoveryReceiver object. *
* We determine our own host using InetAddress *
* @param service * @param multicastInterfaceString * @param multicastAddressString * @param multicastPort * @throws IOException */ public UDPDiscoveryReceiver( final UDPDiscoveryService service, final String multicastInterfaceString, final String multicastAddressString, final int multicastPort ) throws IOException { this(service, multicastInterfaceString, InetAddress.getByName( multicastAddressString ), multicastPort); } /** * Constructor for the UDPDiscoveryReceiver object. *
* @param service * @param multicastInterfaceString * @param multicastAddress * @param multicastPort * @throws IOException * @since 3.1 */ public UDPDiscoveryReceiver( final UDPDiscoveryService service, final String multicastInterfaceString, final InetAddress multicastAddress, final int multicastPort ) throws IOException { this.service = service; if (service != null) { this.serializer = service.getSerializer(); } // create a small thread pool to handle a barrage this.pooledExecutor = ThreadPoolManager.getInstance().createPool( new PoolConfiguration(false, 0, maxPoolSize, maxPoolSize, 0, WhenBlockedPolicy.DISCARDOLDEST, maxPoolSize), "JCS-UDPDiscoveryReceiver-", Thread.MIN_PRIORITY); log.info( "Constructing listener, [{0}:{1}]", multicastAddress, multicastPort ); createSocket( multicastInterfaceString, multicastAddress, multicastPort ); } /** * Creates the socket for this class. *
* @param multicastInterfaceString
* @param multicastAddress
* @param multicastPort
* @throws IOException
*/
private void createSocket( final String multicastInterfaceString, final InetAddress multicastAddress,
final int multicastPort )
throws IOException
{
try
{
// Use dedicated interface if specified
NetworkInterface multicastInterface = null;
if (multicastInterfaceString != null)
{
multicastInterface = NetworkInterface.getByName(multicastInterfaceString);
}
else
{
multicastInterface = HostNameUtil.getMulticastNetworkInterface();
}
if (multicastInterface != null)
{
log.info("Using network interface {0}", multicastInterface::getDisplayName);
}
multicastChannel = DatagramChannel.open(
multicastAddress instanceof Inet6Address ?
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET)
.setOption(StandardSocketOptions.SO_REUSEADDR, true)
.setOption(StandardSocketOptions.IP_MULTICAST_IF, multicastInterface)
.bind(new InetSocketAddress(multicastPort));
multicastChannel.configureBlocking(false);
log.info("Joining Group: [{0}] on {1}", multicastAddress, multicastInterface);
multicastGroupKey = multicastChannel.join(multicastAddress, multicastInterface);
selector = Selector.open();
multicastChannel.register(selector, SelectionKey.OP_READ);
}
catch ( final IOException e )
{
log.error( "Could not bind to multicast address [{0}:{1}]", multicastAddress,
multicastPort, e );
throw e;
}
}
private final ArrayBlockingQueue