View Javadoc
1   package org.apache.commons.jcs.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 org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import java.util.ArrayList;
26  
27  /**
28   * Used to periodically broadcast our location to other caches that might be listening.
29   */
30  public class UDPDiscoverySenderThread
31      implements Runnable
32  {
33      /** The logger. */
34      private static final Log log = LogFactory.getLog( UDPDiscoverySenderThread.class );
35  
36      /**
37       * details of the host, port, and service being advertised to listen for TCP socket connections
38       */
39      private final UDPDiscoveryAttributes attributes;
40  
41      /** List of known regions. */
42      private ArrayList<String> cacheNames = new ArrayList<String>();
43  
44      /**
45       * @param cacheNames The cacheNames to set.
46       */
47      protected void setCacheNames( ArrayList<String> cacheNames )
48      {
49          if ( log.isInfoEnabled() )
50          {
51              log.info( "Resetting cacheNames = [" + cacheNames + "]" );
52          }
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( UDPDiscoveryAttributes attributes, ArrayList<String> cacheNames )
72      {
73          this.attributes = attributes;
74  
75          this.cacheNames = cacheNames;
76  
77          if ( log.isDebugEnabled() )
78          {
79              log.debug( "Creating sender thread for discoveryAddress = [" + attributes.getUdpDiscoveryAddr()
80                  + "] and discoveryPort = [" + attributes.getUdpDiscoveryPort() + "] myHostName = ["
81                  + attributes.getServiceAddress() + "] and port = [" + attributes.getServicePort() + "]" );
82          }
83  
84          UDPDiscoverySender sender = null;
85          try
86          {
87              // move this to the run method and determine how often to call it.
88              sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(), attributes.getUdpDiscoveryPort() );
89              sender.requestBroadcast();
90  
91              if ( log.isDebugEnabled() )
92              {
93                  log.debug( "Sent a request broadcast to the group" );
94              }
95          }
96          catch ( Exception e )
97          {
98              log.error( "Problem sending a Request Broadcast", e );
99          }
100         finally
101         {
102             try
103             {
104                 if ( sender != null )
105                 {
106                     sender.destroy();
107                 }
108             }
109             catch ( Exception e )
110             {
111                 log.error( "Problem closing Request Broadcast sender", e );
112             }
113         }
114     }
115 
116     /**
117      * Send a message.
118      */
119     @Override
120     public void run()
121     {
122         UDPDiscoverySender sender = null;
123         try
124         {
125             // create this connection each time.
126             // more robust
127             sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(), attributes.getUdpDiscoveryPort() );
128 
129             sender.passiveBroadcast( attributes.getServiceAddress(), attributes.getServicePort(), cacheNames );
130 
131             // todo we should consider sending a request broadcast every so
132             // often.
133 
134             if ( log.isDebugEnabled() )
135             {
136                 log.debug( "Called sender to issue a passive broadcast" );
137             }
138 
139         }
140         catch ( Exception e )
141         {
142             log.error( "Problem calling the UDP Discovery Sender [" + attributes.getUdpDiscoveryAddr() + ":"
143                 + attributes.getUdpDiscoveryPort() + "]", e );
144         }
145         finally
146         {
147             if (sender != null)
148             {
149                 try
150                 {
151                     sender.destroy();
152                 }
153                 catch ( Exception e )
154                 {
155                     log.error( "Problem closing Passive Broadcast sender", e );
156                 }
157             }
158         }
159     }
160 
161     /**
162      * Issues a remove broadcast to the others.
163      */
164     protected void shutdown()
165     {
166         UDPDiscoverySender sender = null;
167         try
168         {
169             // create this connection each time.
170             // more robust
171             sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(), attributes.getUdpDiscoveryPort() );
172 
173             sender.removeBroadcast( attributes.getServiceAddress(), attributes.getServicePort(), cacheNames );
174 
175             if ( log.isDebugEnabled() )
176             {
177                 log.debug( "Called sender to issue a remove broadcast in shudown." );
178             }
179         }
180         catch ( Exception e )
181         {
182             log.error( "Problem calling the UDP Discovery Sender", e );
183         }
184         finally
185         {
186             try
187             {
188                 if ( sender != null )
189                 {
190                     sender.destroy();
191                 }
192             }
193             catch ( Exception e )
194             {
195                 log.error( "Problem closing Remote Broadcast sender", e );
196             }
197         }
198     }
199 }