1 package org.apache.commons.jcs.utils.discovery;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
29
30 public class UDPDiscoverySenderThread
31 implements Runnable
32 {
33
34 private static final Log log = LogFactory.getLog( UDPDiscoverySenderThread.class );
35
36
37
38
39 private final UDPDiscoveryAttributes attributes;
40
41
42 private ArrayList<String> cacheNames = new ArrayList<String>();
43
44
45
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
58
59 protected ArrayList<String> getCacheNames()
60 {
61 return cacheNames;
62 }
63
64
65
66
67
68
69
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
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
118
119 @Override
120 public void run()
121 {
122 UDPDiscoverySender sender = null;
123 try
124 {
125
126
127 sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(), attributes.getUdpDiscoveryPort() );
128
129 sender.passiveBroadcast( attributes.getServiceAddress(), attributes.getServicePort(), cacheNames );
130
131
132
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
163
164 protected void shutdown()
165 {
166 UDPDiscoverySender sender = null;
167 try
168 {
169
170
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 }