1 package org.apache.jcs.auxiliary.lateral.socket.tcp.discovery;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40 private String discoveryAddress = "";
41
42 private int discoveryPort = 0;
43
44
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
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
140
141
142
143 public void run()
144 {
145 UDPDiscoverySender sender = null;
146 try
147 {
148
149
150 sender = new UDPDiscoverySender( discoveryAddress, discoveryPort );
151
152 sender.passiveBroadcast( myHostName, myPort, cacheNames );
153
154
155
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 }