1 |
|
|
2 |
|
package javax.jmdns.impl; |
3 |
|
|
4 |
|
import java.util.Collection; |
5 |
|
import java.util.concurrent.ConcurrentHashMap; |
6 |
|
import java.util.concurrent.ConcurrentMap; |
7 |
|
import java.util.concurrent.Semaphore; |
8 |
|
import java.util.concurrent.TimeUnit; |
9 |
|
import java.util.concurrent.locks.ReentrantLock; |
10 |
|
import java.util.logging.Level; |
11 |
|
import java.util.logging.Logger; |
12 |
|
|
13 |
|
import javax.jmdns.impl.constants.DNSState; |
14 |
|
import javax.jmdns.impl.tasks.DNSTask; |
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
@author |
21 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
22 |
|
public interface DNSStatefulObject { |
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
@link |
28 |
|
|
29 |
|
|
30 |
|
@author |
31 |
|
|
|
|
| 52.6% |
Uncovered Elements: 18 (38) |
Complexity: 7 |
Complexity Density: 0.23 |
|
32 |
|
public static final class DNSStatefulObjectSemaphore { |
33 |
|
private static Logger logger = Logger.getLogger(DNSStatefulObjectSemaphore.class.getName()); |
34 |
|
|
35 |
|
private final String _name; |
36 |
|
|
37 |
|
private final ConcurrentMap<Thread, Semaphore> _semaphores; |
38 |
|
|
39 |
|
|
40 |
|
@param |
41 |
|
|
42 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
43 |
939
|
public DNSStatefulObjectSemaphore(String name) {... |
44 |
936
|
super(); |
45 |
940
|
_name = name; |
46 |
936
|
_semaphores = new ConcurrentHashMap<Thread, Semaphore>(50); |
47 |
|
} |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@param |
53 |
|
|
54 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
55 |
90
|
public void waitForEvent(long timeout) {... |
56 |
90
|
Thread thread = Thread.currentThread(); |
57 |
90
|
Semaphore semaphore = _semaphores.get(thread); |
58 |
90
|
if (semaphore == null) { |
59 |
90
|
semaphore = new Semaphore(1, true); |
60 |
90
|
semaphore.drainPermits(); |
61 |
90
|
_semaphores.putIfAbsent(thread, semaphore); |
62 |
|
} |
63 |
90
|
semaphore = _semaphores.get(thread); |
64 |
90
|
try { |
65 |
90
|
semaphore.tryAcquire(timeout, TimeUnit.MILLISECONDS); |
66 |
|
} catch (InterruptedException exception) { |
67 |
0
|
logger.log(Level.FINER, "Exception ", exception); |
68 |
|
} |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
74 |
92
|
public void signalEvent() {... |
75 |
92
|
Collection<Semaphore> semaphores = _semaphores.values(); |
76 |
92
|
for (Semaphore semaphore : semaphores) { |
77 |
60
|
semaphore.release(); |
78 |
60
|
semaphores.remove(semaphore); |
79 |
|
} |
80 |
|
} |
81 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 2 |
Complexity Density: 0.15 |
|
82 |
0
|
@Override... |
83 |
|
public String toString() { |
84 |
0
|
StringBuilder aLog = new StringBuilder(1000); |
85 |
0
|
aLog.append("Semaphore: "); |
86 |
0
|
aLog.append(this._name); |
87 |
0
|
if (_semaphores.size() == 0) { |
88 |
0
|
aLog.append(" no semaphores."); |
89 |
|
} else { |
90 |
0
|
aLog.append(" semaphores:\n"); |
91 |
0
|
for (Thread thread : _semaphores.keySet()) { |
92 |
0
|
aLog.append("\tThread: "); |
93 |
0
|
aLog.append(thread.getName()); |
94 |
0
|
aLog.append(' '); |
95 |
0
|
aLog.append(_semaphores.get(thread)); |
96 |
0
|
aLog.append('\n'); |
97 |
|
} |
98 |
|
} |
99 |
0
|
return aLog.toString(); |
100 |
|
} |
101 |
|
|
102 |
|
} |
103 |
|
|
|
|
| 79.5% |
Uncovered Elements: 34 (166) |
Complexity: 50 |
Complexity Density: 0.5 |
|
104 |
|
public static class DefaultImplementation extends ReentrantLock implements DNSStatefulObject { |
105 |
|
private static Logger logger = Logger.getLogger(DefaultImplementation.class.getName()); |
106 |
|
|
107 |
|
private static final long serialVersionUID = -3264781576883412227L; |
108 |
|
|
109 |
|
private volatile JmDNSImpl _dns; |
110 |
|
|
111 |
|
protected volatile DNSTask _task; |
112 |
|
|
113 |
|
protected volatile DNSState _state; |
114 |
|
|
115 |
|
private final DNSStatefulObjectSemaphore _announcing; |
116 |
|
|
117 |
|
private final DNSStatefulObjectSemaphore _canceling; |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
119 |
467
|
public DefaultImplementation() {... |
120 |
467
|
super(); |
121 |
468
|
_dns = null; |
122 |
467
|
_task = null; |
123 |
469
|
_state = DNSState.PROBING_1; |
124 |
466
|
_announcing = new DNSStatefulObjectSemaphore("Announce"); |
125 |
468
|
_canceling = new DNSStatefulObjectSemaphore("Cancel"); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
@inheritDoc |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
99
|
@Override... |
132 |
|
public JmDNSImpl getDns() { |
133 |
99
|
return this._dns; |
134 |
|
} |
135 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
136 |
178
|
protected void setDns(JmDNSImpl dns) {... |
137 |
179
|
this._dns = dns; |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
@inheritDoc |
142 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 5 |
Complexity Density: 0.83 |
|
143 |
273
|
@Override... |
144 |
|
public void associateWithTask(DNSTask task, DNSState state) { |
145 |
273
|
if (this._task == null && this._state == state) { |
146 |
170
|
this.lock(); |
147 |
170
|
try { |
148 |
170
|
if (this._task == null && this._state == state) { |
149 |
170
|
this.setTask(task); |
150 |
|
} |
151 |
|
} finally { |
152 |
170
|
this.unlock(); |
153 |
|
} |
154 |
|
} |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
|
@inheritDoc |
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
160 |
197
|
@Override... |
161 |
|
public void removeAssociationWithTask(DNSTask task) { |
162 |
197
|
if (this._task == task) { |
163 |
118
|
this.lock(); |
164 |
118
|
try { |
165 |
118
|
if (this._task == task) { |
166 |
117
|
this.setTask(null); |
167 |
|
} |
168 |
|
} finally { |
169 |
118
|
this.unlock(); |
170 |
|
} |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
|
@inheritDoc |
176 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
177 |
486
|
@Override... |
178 |
|
public boolean isAssociatedWithTask(DNSTask task, DNSState state) { |
179 |
486
|
this.lock(); |
180 |
486
|
try { |
181 |
485
|
return this._task == task && this._state == state; |
182 |
|
} finally { |
183 |
485
|
this.unlock(); |
184 |
|
} |
185 |
|
} |
186 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
187 |
360
|
protected void setTask(DNSTask task) {... |
188 |
360
|
this._task = task; |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
@param |
193 |
|
|
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
195 |
405
|
protected void setState(DNSState state) {... |
196 |
405
|
this.lock(); |
197 |
405
|
try { |
198 |
405
|
this._state = state; |
199 |
405
|
if (this.isAnnounced()) { |
200 |
51
|
_announcing.signalEvent(); |
201 |
|
} |
202 |
405
|
if (this.isCanceled()) { |
203 |
20
|
_canceling.signalEvent(); |
204 |
|
|
205 |
20
|
_announcing.signalEvent(); |
206 |
|
} |
207 |
|
} finally { |
208 |
405
|
this.unlock(); |
209 |
|
} |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
|
@inheritDoc |
214 |
|
|
|
|
| 76.9% |
Uncovered Elements: 3 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
215 |
332
|
@Override... |
216 |
|
public boolean advanceState(DNSTask task) { |
217 |
332
|
boolean result = true; |
218 |
332
|
if (this._task == task) { |
219 |
332
|
this.lock(); |
220 |
332
|
try { |
221 |
332
|
if (this._task == task) { |
222 |
332
|
this.setState(this._state.advance()); |
223 |
|
} else { |
224 |
0
|
logger.warning("Trying to advance state whhen not the owner. owner: " + this._task + " perpetrator: " + task); |
225 |
|
} |
226 |
|
} finally { |
227 |
332
|
this.unlock(); |
228 |
|
} |
229 |
|
} |
230 |
332
|
return result; |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
@inheritDoc |
235 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
236 |
0
|
@Override... |
237 |
|
public boolean revertState() { |
238 |
0
|
boolean result = true; |
239 |
0
|
if (!this.willCancel()) { |
240 |
0
|
this.lock(); |
241 |
0
|
try { |
242 |
0
|
if (!this.willCancel()) { |
243 |
0
|
this.setState(this._state.revert()); |
244 |
0
|
this.setTask(null); |
245 |
|
} |
246 |
|
} finally { |
247 |
0
|
this.unlock(); |
248 |
|
} |
249 |
|
} |
250 |
0
|
return result; |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
|
@inheritDoc |
255 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
256 |
20
|
@Override... |
257 |
|
public boolean cancelState() { |
258 |
20
|
boolean result = false; |
259 |
20
|
if (!this.willCancel()) { |
260 |
20
|
this.lock(); |
261 |
20
|
try { |
262 |
20
|
if (!this.willCancel()) { |
263 |
20
|
this.setState(DNSState.CANCELING_1); |
264 |
20
|
this.setTask(null); |
265 |
20
|
result = true; |
266 |
|
} |
267 |
|
} finally { |
268 |
20
|
this.unlock(); |
269 |
|
} |
270 |
|
} |
271 |
20
|
return result; |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
@inheritDoc |
276 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
277 |
29
|
@Override... |
278 |
|
public boolean closeState() { |
279 |
29
|
boolean result = false; |
280 |
29
|
if (!this.willClose()) { |
281 |
29
|
this.lock(); |
282 |
29
|
try { |
283 |
29
|
if (!this.willClose()) { |
284 |
29
|
this.setState(DNSState.CLOSING); |
285 |
29
|
this.setTask(null); |
286 |
29
|
result = true; |
287 |
|
} |
288 |
|
} finally { |
289 |
29
|
this.unlock(); |
290 |
|
} |
291 |
|
} |
292 |
29
|
return result; |
293 |
|
} |
294 |
|
|
295 |
|
|
296 |
|
@inheritDoc |
297 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
298 |
20
|
@Override... |
299 |
|
public boolean recoverState() { |
300 |
20
|
boolean result = false; |
301 |
20
|
this.lock(); |
302 |
20
|
try { |
303 |
20
|
this.setState(DNSState.PROBING_1); |
304 |
20
|
this.setTask(null); |
305 |
|
} finally { |
306 |
20
|
this.unlock(); |
307 |
|
} |
308 |
20
|
return result; |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
|
@inheritDoc |
313 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
314 |
0
|
@Override... |
315 |
|
public boolean isProbing() { |
316 |
0
|
return this._state.isProbing(); |
317 |
|
} |
318 |
|
|
319 |
|
|
320 |
|
@inheritDoc |
321 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
322 |
60
|
@Override... |
323 |
|
public boolean isAnnouncing() { |
324 |
60
|
return this._state.isAnnouncing(); |
325 |
|
} |
326 |
|
|
327 |
|
|
328 |
|
@inheritDoc |
329 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
330 |
1187
|
@Override... |
331 |
|
public boolean isAnnounced() { |
332 |
1187
|
return this._state.isAnnounced(); |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
|
@inheritDoc |
337 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
338 |
2378
|
@Override... |
339 |
|
public boolean isCanceling() { |
340 |
2409
|
return this._state.isCanceling(); |
341 |
|
} |
342 |
|
|
343 |
|
|
344 |
|
@inheritDoc |
345 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
346 |
2946
|
@Override... |
347 |
|
public boolean isCanceled() { |
348 |
2968
|
return this._state.isCanceled(); |
349 |
|
} |
350 |
|
|
351 |
|
|
352 |
|
@inheritDoc |
353 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
354 |
707
|
@Override... |
355 |
|
public boolean isClosing() { |
356 |
732
|
return this._state.isClosing(); |
357 |
|
} |
358 |
|
|
359 |
|
|
360 |
|
@inheritDoc |
361 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
362 |
646
|
@Override... |
363 |
|
public boolean isClosed() { |
364 |
672
|
return this._state.isClosed(); |
365 |
|
} |
366 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
367 |
79
|
private boolean willCancel() {... |
368 |
79
|
return this._state.isCanceled() || this._state.isCanceling(); |
369 |
|
} |
370 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
371 |
87
|
private boolean willClose() {... |
372 |
87
|
return this._state.isClosed() || this._state.isClosing(); |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
|
@inheritDoc |
377 |
|
|
|
|
| 53.8% |
Uncovered Elements: 6 (13) |
Complexity: 6 |
Complexity Density: 0.86 |
|
378 |
40
|
@Override... |
379 |
|
public boolean waitForAnnounced(long timeout) { |
380 |
40
|
if (!this.isAnnounced() && !this.willCancel()) { |
381 |
39
|
_announcing.waitForEvent(timeout); |
382 |
|
} |
383 |
40
|
if (!this.isAnnounced()) { |
384 |
0
|
if (this.willCancel() || this.willClose()) { |
385 |
0
|
logger.fine("Wait for announced cancelled: " + this); |
386 |
|
} else { |
387 |
0
|
logger.warning("Wait for announced timed out: " + this); |
388 |
|
} |
389 |
|
} |
390 |
40
|
return this.isAnnounced(); |
391 |
|
} |
392 |
|
|
393 |
|
|
394 |
|
@inheritDoc |
395 |
|
|
|
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
396 |
49
|
@Override... |
397 |
|
public boolean waitForCanceled(long timeout) { |
398 |
49
|
if (!this.isCanceled()) { |
399 |
49
|
_canceling.waitForEvent(timeout); |
400 |
|
} |
401 |
49
|
if (!this.isCanceled() && !this.willClose()) { |
402 |
0
|
logger.warning("Wait for canceled timed out: " + this); |
403 |
|
} |
404 |
49
|
return this.isCanceled(); |
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
|
@inheritDoc |
409 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
410 |
7
|
@Override... |
411 |
|
public String toString() { |
412 |
7
|
return (_dns != null ? "DNS: " + _dns.getName() : "NO DNS") + " state: " + _state + " task: " + _task; |
413 |
|
} |
414 |
|
|
415 |
|
} |
416 |
|
|
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
@return |
421 |
|
|
422 |
|
public JmDNSImpl getDns(); |
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
|
427 |
|
@param |
428 |
|
|
429 |
|
@param |
430 |
|
|
431 |
|
|
432 |
|
public void associateWithTask(DNSTask task, DNSState state); |
433 |
|
|
434 |
|
|
435 |
|
|
436 |
|
|
437 |
|
@param |
438 |
|
|
439 |
|
|
440 |
|
public void removeAssociationWithTask(DNSTask task); |
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
@param |
446 |
|
|
447 |
|
@param |
448 |
|
|
449 |
|
@return |
450 |
|
|
451 |
|
public boolean isAssociatedWithTask(DNSTask task, DNSState state); |
452 |
|
|
453 |
|
|
454 |
|
|
455 |
|
|
456 |
|
@param |
457 |
|
|
458 |
|
@return |
459 |
|
@see |
460 |
|
|
461 |
|
public boolean advanceState(DNSTask task); |
462 |
|
|
463 |
|
|
464 |
|
|
465 |
|
|
466 |
|
@return |
467 |
|
@see |
468 |
|
|
469 |
|
public boolean revertState(); |
470 |
|
|
471 |
|
|
472 |
|
|
473 |
|
|
474 |
|
@return |
475 |
|
|
476 |
|
public boolean cancelState(); |
477 |
|
|
478 |
|
|
479 |
|
|
480 |
|
|
481 |
|
@return |
482 |
|
|
483 |
|
public boolean closeState(); |
484 |
|
|
485 |
|
|
486 |
|
|
487 |
|
|
488 |
|
@return |
489 |
|
|
490 |
|
public boolean recoverState(); |
491 |
|
|
492 |
|
|
493 |
|
|
494 |
|
|
495 |
|
@return |
496 |
|
|
497 |
|
public boolean isProbing(); |
498 |
|
|
499 |
|
|
500 |
|
|
501 |
|
|
502 |
|
@return |
503 |
|
|
504 |
|
public boolean isAnnouncing(); |
505 |
|
|
506 |
|
|
507 |
|
|
508 |
|
|
509 |
|
@return |
510 |
|
|
511 |
|
public boolean isAnnounced(); |
512 |
|
|
513 |
|
|
514 |
|
|
515 |
|
|
516 |
|
@return |
517 |
|
|
518 |
|
public boolean isCanceling(); |
519 |
|
|
520 |
|
|
521 |
|
|
522 |
|
|
523 |
|
@return |
524 |
|
|
525 |
|
public boolean isCanceled(); |
526 |
|
|
527 |
|
|
528 |
|
|
529 |
|
|
530 |
|
@return |
531 |
|
|
532 |
|
public boolean isClosing(); |
533 |
|
|
534 |
|
|
535 |
|
|
536 |
|
|
537 |
|
@return |
538 |
|
|
539 |
|
public boolean isClosed(); |
540 |
|
|
541 |
|
|
542 |
|
|
543 |
|
|
544 |
|
@param |
545 |
|
|
546 |
|
@return |
547 |
|
|
548 |
|
public boolean waitForAnnounced(long timeout); |
549 |
|
|
550 |
|
|
551 |
|
|
552 |
|
|
553 |
|
@param |
554 |
|
|
555 |
|
@return |
556 |
|
|
557 |
|
public boolean waitForCanceled(long timeout); |
558 |
|
|
559 |
|
} |