View Javadoc

1   package org.apache.jcs.auxiliary.lateral.socket.tcp.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.util.ArrayList;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /***
28   * Used to periodically broadcast our location to other caches that might be
29   * listening.
30   *
31   * @author Aaron Smuts
32   *
33   */
34  public class UDPDiscoverySenderThread
35      implements Runnable
36  {
37      private final static Log log = LogFactory.getLog( UDPDiscoverySenderThread.class );
38  
39      // the UDP multicast port
40      private String discoveryAddress = "";
41  
42      private int discoveryPort = 0;
43  
44      // the host and port we listen on for TCP socket connections
45      private String myHostName = null;
46  
47      private int myPort = 0;
48  
49      private ArrayList cacheNames = new ArrayList();
50  
51      /***
52       * @param cacheNames
53       *            The cacheNames to set.
54       */
55      protected void setCacheNames( ArrayList cacheNames )
56      {
57          if ( log.isDebugEnabled() )
58          {
59              log.debug( "Resetting cacheNames = [" + cacheNames + "]" );
60          }
61          this.cacheNames = cacheNames;
62      }
63  
64      /***
65       * @return Returns the cacheNames.
66       */
67      protected ArrayList getCacheNames()
68      {
69          return cacheNames;
70      }
71  
72      /***
73       * Constructs the sender with the port to tell others to connect to.
74       * <p>
75       * On construction the sender will request that the other caches let it know
76       * their addresses.
77       *
78       * @param discoveryAddress
79       *            host to broadcast to
80       * @param discoveryPort
81       *            port to broadcast to
82       * @param myHostName
83       *            host name we can be found at
84       * @param myPort
85       *            port we are listening on
86       * @param cacheNames
87       *            List of strings of the names of the regiond participating.
88       */
89      public UDPDiscoverySenderThread( String discoveryAddress, int discoveryPort, String myHostName, int myPort,
90                                      ArrayList cacheNames )
91      {
92          this.discoveryAddress = discoveryAddress;
93          this.discoveryPort = discoveryPort;
94  
95          this.myHostName = myHostName;
96          this.myPort = myPort;
97  
98          this.cacheNames = cacheNames;
99  
100         if ( log.isDebugEnabled() )
101         {
102             log.debug( "Creating sender thread for discoveryAddress = [" + discoveryAddress + "] and discoveryPort = ["
103                 + discoveryPort + "] myHostName = [" + myHostName + "] and port = [" + myPort + "]" );
104         }
105 
106         UDPDiscoverySender sender = null;
107         try
108         {
109             // move this to the run method and determine how often to call it.
110             sender = new UDPDiscoverySender( discoveryAddress, discoveryPort );
111             sender.requestBroadcast();
112 
113             if ( log.isDebugEnabled() )
114             {
115                 log.debug( "Sent a request broadcast to the group" );
116             }
117         }
118         catch ( Exception e )
119         {
120             log.error( "Problem sending a Request Broadcast", e );
121         }
122         finally
123         {
124             try
125             {
126                 if ( sender != null )
127                 {
128                     sender.destroy();
129                 }
130             }
131             catch ( Exception e )
132             {
133                 log.error( "Problem closing Request Broadcast sender", e );
134             }
135         }
136     }
137 
138     /*
139      * (non-Javadoc)
140      *
141      * @see java.lang.Runnable#run()
142      */
143     public void run()
144     {
145         UDPDiscoverySender sender = null;
146         try
147         {
148             // create this connection each time.
149             // more robust
150             sender = new UDPDiscoverySender( discoveryAddress, discoveryPort );
151 
152             sender.passiveBroadcast( myHostName, myPort, cacheNames );
153 
154             // todo we should consider sending a request broadcast every so
155             // often.
156 
157             if ( log.isDebugEnabled() )
158             {
159                 log.debug( "Called sender to issue a passive broadcast" );
160             }
161 
162         }
163         catch ( Exception e )
164         {
165             log.error( "Problem calling the UDP Discovery Sender [" + discoveryAddress + ":" + discoveryPort + "]", e );
166         }
167         finally
168         {
169             try
170             {
171                 sender.destroy();
172             }
173             catch ( Exception e )
174             {
175                 log.error( "Problem closing Passive Broadcast sender", e );
176             }
177         }
178     }
179 }