%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jcs.auxiliary.lateral.socket.tcp.discovery.UDPDiscoverySenderThread |
|
|
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 | 26 | private final static Log log = LogFactory.getLog( UDPDiscoverySenderThread.class ); |
38 | ||
39 | // the UDP multicast port |
|
40 | 13 | private String discoveryAddress = ""; |
41 | ||
42 | 13 | private int discoveryPort = 0; |
43 | ||
44 | // the host and port we listen on for TCP socket connections |
|
45 | 13 | private String myHostName = null; |
46 | ||
47 | 13 | private int myPort = 0; |
48 | ||
49 | 13 | private ArrayList cacheNames = new ArrayList(); |
50 | ||
51 | /** |
|
52 | * @param cacheNames |
|
53 | * The cacheNames to set. |
|
54 | */ |
|
55 | protected void setCacheNames( ArrayList cacheNames ) |
|
56 | { |
|
57 | 26 | if ( log.isDebugEnabled() ) |
58 | { |
|
59 | 0 | log.debug( "Resetting cacheNames = [" + cacheNames + "]" ); |
60 | } |
|
61 | 26 | this.cacheNames = cacheNames; |
62 | 26 | } |
63 | ||
64 | /** |
|
65 | * @return Returns the cacheNames. |
|
66 | */ |
|
67 | protected ArrayList getCacheNames() |
|
68 | { |
|
69 | 0 | 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, class="keyword">int myPort, |
|
90 | ArrayList cacheNames ) |
|
91 | 13 | { |
92 | 13 | this.discoveryAddress = discoveryAddress; |
93 | 13 | this.discoveryPort = discoveryPort; |
94 | ||
95 | 13 | this.myHostName = myHostName; |
96 | 13 | this.myPort = myPort; |
97 | ||
98 | 13 | this.cacheNames = cacheNames; |
99 | ||
100 | 13 | if ( log.isDebugEnabled() ) |
101 | { |
|
102 | 0 | log.debug( "Creating sender thread for discoveryAddress = [" + discoveryAddress + "] and discoveryPort = [" |
103 | + discoveryPort + "] myHostName = [" + myHostName + "] and port = [" + myPort + "]" ); |
|
104 | } |
|
105 | ||
106 | 13 | UDPDiscoverySender sender = null; |
107 | try |
|
108 | { |
|
109 | // move this to the run method and determine how often to call it. |
|
110 | 13 | sender = new UDPDiscoverySender( discoveryAddress, discoveryPort ); |
111 | 13 | sender.requestBroadcast(); |
112 | ||
113 | 13 | if ( log.isDebugEnabled() ) |
114 | { |
|
115 | 0 | log.debug( "Sent a request broadcast to the group" ); |
116 | } |
|
117 | } |
|
118 | 0 | catch ( Exception e ) |
119 | { |
|
120 | 0 | log.error( "Problem sending a Request Broadcast", e ); |
121 | } |
|
122 | finally |
|
123 | { |
|
124 | 0 | try |
125 | { |
|
126 | 13 | if ( sender != null ) |
127 | { |
|
128 | 13 | sender.destroy(); |
129 | } |
|
130 | } |
|
131 | 0 | catch ( Exception e ) |
132 | { |
|
133 | 0 | log.error( "Problem closing Request Broadcast sender", e ); |
134 | 13 | } |
135 | 0 | } |
136 | 13 | } |
137 | ||
138 | /* |
|
139 | * (non-Javadoc) |
|
140 | * |
|
141 | * @see java.lang.Runnable#run() |
|
142 | */ |
|
143 | public void run() |
|
144 | { |
|
145 | 0 | UDPDiscoverySender sender = null; |
146 | try |
|
147 | { |
|
148 | // create this connection each time. |
|
149 | // more robust |
|
150 | 0 | sender = new UDPDiscoverySender( discoveryAddress, discoveryPort ); |
151 | ||
152 | 0 | sender.passiveBroadcast( myHostName, myPort, cacheNames ); |
153 | ||
154 | // todo we should consider sending a request broadcast every so |
|
155 | // often. |
|
156 | ||
157 | 0 | if ( log.isDebugEnabled() ) |
158 | { |
|
159 | 0 | log.debug( "Called sender to issue a passive broadcast" ); |
160 | } |
|
161 | ||
162 | } |
|
163 | 0 | catch ( Exception e ) |
164 | { |
|
165 | 0 | log.error( "Problem calling the UDP Discovery Sender [" + discoveryAddress + ":" + discoveryPort + "]", e ); |
166 | } |
|
167 | finally |
|
168 | { |
|
169 | 0 | try |
170 | { |
|
171 | 0 | sender.destroy(); |
172 | } |
|
173 | 0 | catch ( Exception e ) |
174 | { |
|
175 | 0 | log.error( "Problem closing Passive Broadcast sender", e ); |
176 | 0 | } |
177 | 0 | } |
178 | 0 | } |
179 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |