1
2
3
4 package javax.jmdns.impl;
5
6 import java.util.Date;
7 import java.util.Timer;
8 import java.util.TimerTask;
9 import java.util.concurrent.ConcurrentHashMap;
10 import java.util.concurrent.ConcurrentMap;
11 import java.util.concurrent.atomic.AtomicReference;
12
13 import javax.jmdns.impl.tasks.RecordReaper;
14 import javax.jmdns.impl.tasks.Responder;
15 import javax.jmdns.impl.tasks.resolver.ServiceInfoResolver;
16 import javax.jmdns.impl.tasks.resolver.ServiceResolver;
17 import javax.jmdns.impl.tasks.resolver.TypeResolver;
18 import javax.jmdns.impl.tasks.state.Announcer;
19 import javax.jmdns.impl.tasks.state.Canceler;
20 import javax.jmdns.impl.tasks.state.Prober;
21 import javax.jmdns.impl.tasks.state.Renewer;
22
23
24
25
26
27
28
29
30
31 public interface DNSTaskStarter {
32
33
34
35
36 public static final class Factory {
37
38 private static volatile Factory _instance;
39 private final ConcurrentMap<JmDNSImpl, DNSTaskStarter> _instances;
40
41
42
43
44 public static interface ClassDelegate {
45
46
47
48
49
50
51
52
53
54
55 public DNSTaskStarter newDNSTaskStarter(JmDNSImpl jmDNSImpl);
56 }
57
58 private static final AtomicReference<Factory.ClassDelegate> _databaseClassDelegate = new AtomicReference<Factory.ClassDelegate>();
59
60 private Factory() {
61 super();
62 _instances = new ConcurrentHashMap<JmDNSImpl, DNSTaskStarter>(20);
63 }
64
65
66
67
68
69
70
71
72
73 public static void setClassDelegate(Factory.ClassDelegate delegate) {
74 _databaseClassDelegate.set(delegate);
75 }
76
77
78
79
80
81
82
83
84 public static Factory.ClassDelegate classDelegate() {
85 return _databaseClassDelegate.get();
86 }
87
88
89
90
91
92
93
94
95 protected static DNSTaskStarter newDNSTaskStarter(JmDNSImpl jmDNSImpl) {
96 DNSTaskStarter instance = null;
97 Factory.ClassDelegate delegate = _databaseClassDelegate.get();
98 if (delegate != null) {
99 instance = delegate.newDNSTaskStarter(jmDNSImpl);
100 }
101 return (instance != null ? instance : new DNSTaskStarterImpl(jmDNSImpl));
102 }
103
104
105
106
107
108
109 public static Factory getInstance() {
110 if (_instance == null) {
111 synchronized (DNSTaskStarter.Factory.class) {
112 if (_instance == null) {
113 _instance = new Factory();
114 }
115 }
116 }
117 return _instance;
118 }
119
120
121
122
123
124
125
126
127 public DNSTaskStarter getStarter(JmDNSImpl jmDNSImpl) {
128 DNSTaskStarter starter = _instances.get(jmDNSImpl);
129 if (starter == null) {
130 _instances.putIfAbsent(jmDNSImpl, newDNSTaskStarter(jmDNSImpl));
131 starter = _instances.get(jmDNSImpl);
132 }
133 return starter;
134 }
135
136 }
137
138 public static final class DNSTaskStarterImpl implements DNSTaskStarter {
139
140 private final JmDNSImpl _jmDNSImpl;
141
142
143
144
145 private final Timer _timer;
146
147
148
149
150 private final Timer _stateTimer;
151
152 public static class StarterTimer extends Timer {
153
154
155 private volatile boolean _cancelled;
156
157
158
159
160 public StarterTimer() {
161 super();
162 _cancelled = false;
163 }
164
165
166
167
168 public StarterTimer(boolean isDaemon) {
169 super(isDaemon);
170 _cancelled = false;
171 }
172
173
174
175
176
177 public StarterTimer(String name, boolean isDaemon) {
178 super(name, isDaemon);
179 _cancelled = false;
180 }
181
182
183
184
185 public StarterTimer(String name) {
186 super(name);
187 _cancelled = false;
188 }
189
190
191
192
193
194 @Override
195 public synchronized void cancel() {
196 if (_cancelled) return;
197 _cancelled = true;
198 super.cancel();
199 }
200
201
202
203
204
205 @Override
206 public synchronized void schedule(TimerTask task, long delay) {
207 if (_cancelled) return;
208 super.schedule(task, delay);
209 }
210
211
212
213
214
215 @Override
216 public synchronized void schedule(TimerTask task, Date time) {
217 if (_cancelled) return;
218 super.schedule(task, time);
219 }
220
221
222
223
224
225 @Override
226 public synchronized void schedule(TimerTask task, long delay, long period) {
227 if (_cancelled) return;
228 super.schedule(task, delay, period);
229 }
230
231
232
233
234
235 @Override
236 public synchronized void schedule(TimerTask task, Date firstTime, long period) {
237 if (_cancelled) return;
238 super.schedule(task, firstTime, period);
239 }
240
241
242
243
244
245 @Override
246 public synchronized void scheduleAtFixedRate(TimerTask task, long delay, long period) {
247 if (_cancelled) return;
248 super.scheduleAtFixedRate(task, delay, period);
249 }
250
251
252
253
254
255 @Override
256 public synchronized void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) {
257 if (_cancelled) return;
258 super.scheduleAtFixedRate(task, firstTime, period);
259 }
260
261 }
262
263 public DNSTaskStarterImpl(JmDNSImpl jmDNSImpl) {
264 super();
265 _jmDNSImpl = jmDNSImpl;
266 _timer = new StarterTimer("JmDNS(" + _jmDNSImpl.getName() + ").Timer", true);
267 _stateTimer = new StarterTimer("JmDNS(" + _jmDNSImpl.getName() + ").State.Timer", false);
268 }
269
270
271
272
273
274 @Override
275 public void purgeTimer() {
276 _timer.purge();
277 }
278
279
280
281
282
283 @Override
284 public void purgeStateTimer() {
285 _stateTimer.purge();
286 }
287
288
289
290
291
292 @Override
293 public void cancelTimer() {
294 _timer.cancel();
295 }
296
297
298
299
300
301 @Override
302 public void cancelStateTimer() {
303 _stateTimer.cancel();
304 }
305
306
307
308
309
310 @Override
311 public void startProber() {
312 new Prober(_jmDNSImpl).start(_stateTimer);
313 }
314
315
316
317
318
319 @Override
320 public void startAnnouncer() {
321 new Announcer(_jmDNSImpl).start(_stateTimer);
322 }
323
324
325
326
327
328 @Override
329 public void startRenewer() {
330 new Renewer(_jmDNSImpl).start(_stateTimer);
331 }
332
333
334
335
336
337 @Override
338 public void startCanceler() {
339 new Canceler(_jmDNSImpl).start(_stateTimer);
340 }
341
342
343
344
345
346 @Override
347 public void startReaper() {
348 new RecordReaper(_jmDNSImpl).start(_timer);
349 }
350
351
352
353
354
355 @Override
356 public void startServiceInfoResolver(ServiceInfoImpl info) {
357 new ServiceInfoResolver(_jmDNSImpl, info).start(_timer);
358 }
359
360
361
362
363
364 @Override
365 public void startTypeResolver() {
366 new TypeResolver(_jmDNSImpl).start(_timer);
367 }
368
369
370
371
372
373 @Override
374 public void startServiceResolver(String type) {
375 new ServiceResolver(_jmDNSImpl, type).start(_timer);
376 }
377
378
379
380
381
382 @Override
383 public void startResponder(DNSIncoming in, int port) {
384 new Responder(_jmDNSImpl, in, port).start(_timer);
385 }
386 }
387
388
389
390
391 public void purgeTimer();
392
393
394
395
396 public void purgeStateTimer();
397
398
399
400
401 public void cancelTimer();
402
403
404
405
406 public void cancelStateTimer();
407
408
409
410
411 public void startProber();
412
413
414
415
416 public void startAnnouncer();
417
418
419
420
421 public void startRenewer();
422
423
424
425
426 public void startCanceler();
427
428
429
430
431 public void startReaper();
432
433
434
435
436
437
438
439 public void startServiceInfoResolver(ServiceInfoImpl info);
440
441
442
443
444 public void startTypeResolver();
445
446
447
448
449
450
451
452 public void startServiceResolver(String type);
453
454
455
456
457
458
459
460
461
462 public void startResponder(DNSIncoming in, int port);
463
464 }