View Javadoc
1   package org.apache.commons.jcs3.utils.discovery;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  
25  import org.apache.commons.jcs3.log.Log;
26  import org.apache.commons.jcs3.log.LogManager;
27  
28  /**
29   * Used to periodically broadcast our location to other caches that might be listening.
30   * @deprecated Functionality moved to UDPDiscoveryService
31   */
32  @Deprecated
33  public class UDPDiscoverySenderThread
34      implements Runnable
35  {
36      /** The logger. */
37      private static final Log log = LogManager.getLog( UDPDiscoverySenderThread.class );
38  
39      /**
40       * details of the host, port, and service being advertised to listen for TCP socket connections
41       */
42      private final UDPDiscoveryAttributes attributes;
43  
44      /** List of known regions. */
45      private ArrayList<String> cacheNames = new ArrayList<>();
46  
47      /**
48       * @param cacheNames The cacheNames to set.
49       */
50      protected void setCacheNames( final ArrayList<String> cacheNames )
51      {
52          log.info( "Resetting cacheNames = [{0}]", cacheNames );
53          this.cacheNames = cacheNames;
54      }
55  
56      /**
57       * @return Returns the cacheNames.
58       */
59      protected ArrayList<String> getCacheNames()
60      {
61          return cacheNames;
62      }
63  
64      /**
65       * Constructs the sender with the port to tell others to connect to.
66       * <p>
67       * On construction the sender will request that the other caches let it know their addresses.
68       * @param attributes host, port, etc.
69       * @param cacheNames List of strings of the names of the region participating.
70       */
71      public UDPDiscoverySenderThread( final UDPDiscoveryAttributes attributes, final ArrayList<String> cacheNames )
72      {
73          this.attributes = attributes;
74  
75          this.cacheNames = cacheNames;
76  
77          log.debug( "Creating sender thread for discoveryAddress = [{0}] and "
78                  + "discoveryPort = [{1}] myHostName = [{2}] and port = [{3}]",
79                  attributes::getUdpDiscoveryAddr,
80                  attributes::getUdpDiscoveryPort,
81                  attributes::getServiceAddress,
82                  attributes::getServicePort);
83  
84          try (UDPDiscoverySender sender = new UDPDiscoverySender(
85                  attributes.getUdpDiscoveryAddr(),
86                  attributes.getUdpDiscoveryPort(),
87                  attributes.getUdpTTL()))
88          {
89              // move this to the run method and determine how often to call it.
90              sender.requestBroadcast();
91  
92              log.debug( "Sent a request broadcast to the group" );
93          }
94          catch ( final IOException e )
95          {
96              log.error( "Problem sending a Request Broadcast", e );
97          }
98      }
99  
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 }