001package org.apache.commons.jcs3.utils.discovery;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.IOException;
023import java.util.ArrayList;
024
025import org.apache.commons.jcs3.log.Log;
026import org.apache.commons.jcs3.log.LogManager;
027
028/**
029 * Used to periodically broadcast our location to other caches that might be listening.
030 * @deprecated Functionality moved to UDPDiscoveryService
031 */
032@Deprecated
033public class UDPDiscoverySenderThread
034    implements Runnable
035{
036    /** The logger. */
037    private static final Log log = LogManager.getLog( UDPDiscoverySenderThread.class );
038
039    /**
040     * details of the host, port, and service being advertised to listen for TCP socket connections
041     */
042    private final UDPDiscoveryAttributes attributes;
043
044    /** List of known regions. */
045    private ArrayList<String> cacheNames = new ArrayList<>();
046
047    /**
048     * @param cacheNames The cacheNames to set.
049     */
050    protected void setCacheNames( final ArrayList<String> cacheNames )
051    {
052        log.info( "Resetting cacheNames = [{0}]", cacheNames );
053        this.cacheNames = cacheNames;
054    }
055
056    /**
057     * @return Returns the cacheNames.
058     */
059    protected ArrayList<String> getCacheNames()
060    {
061        return cacheNames;
062    }
063
064    /**
065     * Constructs the sender with the port to tell others to connect to.
066     * <p>
067     * On construction the sender will request that the other caches let it know their addresses.
068     * @param attributes host, port, etc.
069     * @param cacheNames List of strings of the names of the region participating.
070     */
071    public UDPDiscoverySenderThread( final UDPDiscoveryAttributes attributes, final ArrayList<String> cacheNames )
072    {
073        this.attributes = attributes;
074
075        this.cacheNames = cacheNames;
076
077        log.debug( "Creating sender thread for discoveryAddress = [{0}] and "
078                + "discoveryPort = [{1}] myHostName = [{2}] and port = [{3}]",
079                attributes::getUdpDiscoveryAddr,
080                attributes::getUdpDiscoveryPort,
081                attributes::getServiceAddress,
082                attributes::getServicePort);
083
084        try (UDPDiscoverySender sender = new UDPDiscoverySender(
085                attributes.getUdpDiscoveryAddr(),
086                attributes.getUdpDiscoveryPort(),
087                attributes.getUdpTTL()))
088        {
089            // move this to the run method and determine how often to call it.
090            sender.requestBroadcast();
091
092            log.debug( "Sent a request broadcast to the group" );
093        }
094        catch ( final IOException e )
095        {
096            log.error( "Problem sending a Request Broadcast", e );
097        }
098    }
099
100    /**
101     * Send a message.
102     */
103    @Override
104    public void run()
105    {
106        // create this connection each time.
107        // more robust
108        try (UDPDiscoverySender sender = new UDPDiscoverySender(
109                attributes.getUdpDiscoveryAddr(),
110                attributes.getUdpDiscoveryPort(),
111                attributes.getUdpTTL()))
112        {
113            sender.passiveBroadcast( attributes.getServiceAddress(), attributes.getServicePort(), cacheNames );
114
115            // todo we should consider sending a request broadcast every so
116            // often.
117
118            log.debug( "Called sender to issue a passive broadcast" );
119        }
120        catch ( final IOException e )
121        {
122            log.error( "Problem calling the UDP Discovery Sender [{0}:{1}]",
123                    attributes.getUdpDiscoveryAddr(),
124                    attributes.getUdpDiscoveryPort(), e );
125        }
126    }
127
128    /**
129     * Issues a remove broadcast to the others.
130     */
131    protected void shutdown()
132    {
133        // create this connection each time.
134        // more robust
135        try (UDPDiscoverySender sender = new UDPDiscoverySender(
136                attributes.getUdpDiscoveryAddr(),
137                attributes.getUdpDiscoveryPort(),
138                attributes.getUdpTTL()))
139        {
140            sender.removeBroadcast( attributes.getServiceAddress(), attributes.getServicePort(), cacheNames );
141
142            log.debug( "Called sender to issue a remove broadcast in shutdown." );
143        }
144        catch ( final IOException e )
145        {
146            log.error( "Problem calling the UDP Discovery Sender", e );
147        }
148    }
149}