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.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashSet; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.commons.jcs3.engine.behavior.IElementSerializer; import org.apache.commons.jcs3.engine.behavior.IRequireScheduler; 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.discovery.behavior.IDiscoveryListener; import org.apache.commons.jcs3.utils.net.HostNameUtil; import org.apache.commons.jcs3.utils.serialization.StandardSerializer; /** * This service creates a listener that can create lateral caches and add them to the no wait list. *
* It also creates a sender that periodically broadcasts its availability. *
** The sender also broadcasts a request for other caches to broadcast their addresses. *
*/ public class UDPDiscoveryService implements IShutdownObserver, IRequireScheduler { /** The logger */ private static final Log log = LogManager.getLog( UDPDiscoveryService.class ); /** thread that listens for messages */ private Thread udpReceiverThread; /** the runnable that the receiver thread runs */ private UDPDiscoveryReceiver receiver; /** attributes */ private UDPDiscoveryAttributes udpDiscoveryAttributes; /** Used to serialize messages */ private final IElementSerializer serializer; /** is this shut down? */ private final AtomicBoolean shutdown = new AtomicBoolean(false); /** This is a set of services that have been discovered. */ private final ConcurrentMap* @param cacheName */ public void addParticipatingCacheName( final String cacheName ) { cacheNames.add( cacheName ); } /** * Removes the discovered service from the list and calls the discovery listener. *
* @param service */ public void removeDiscoveredService( final DiscoveredService service ) { if (discoveredServices.remove(service.hashCode()) != null) { log.info( "Removing {0}", service ); } getDiscoveryListeners().forEach(listener -> listener.removeDiscoveredService(service)); } /** * Add a service to the list. Update the held copy if we already know about it. *
* @param discoveredService discovered service */ protected void addOrUpdateService( final DiscoveredService discoveredService ) { // We want to replace the old one, since we may add info that is not part of the equals. // The equals method on the object being added is intentionally restricted. discoveredServices.merge(discoveredService.hashCode(), discoveredService, (oldService, newService) -> { log.debug( "Set contains service." ); log.debug( "Updating service in the set {0}", newService ); // Update the list of cache names if it has changed. // need to update the time this sucks. add has no effect convert to a map if (!oldService.getCacheNames().equals(newService.getCacheNames())) { log.info( "List of cache names changed for service: {0}", newService ); // replace it, we want to reset the payload and the last heard from time. return newService; } if (oldService.getLastHearFromTime() != newService.getLastHearFromTime()) { return newService; } return oldService; }); // Always Notify the listeners // If we don't do this, then if a region using the default config is initialized after notification, // it will never get the service in it's no wait list. // Leave it to the listeners to decide what to do. getDiscoveryListeners().forEach(listener -> listener.addDiscoveredService(discoveredService)); } /** * Get all the cache names we have facades for. *
* @return ArrayList
*/
protected ArrayList
* @param listener
* @return true if it wasn't already in the set
*/
public boolean addDiscoveryListener( final IDiscoveryListener listener )
{
return getDiscoveryListeners().add( listener );
}
/**
* Removes a listener.
*
* @param listener
* @return true if it was in the set
*/
public boolean removeDiscoveryListener( final IDiscoveryListener listener )
{
return getDiscoveryListeners().remove( listener );
}
}