DNSTaskStarter.java
Go to the documentation of this file.
00001 
00004 package javax.jmdns.impl;
00005 
00006 import java.util.Date;
00007 import java.util.Timer;
00008 import java.util.TimerTask;
00009 import java.util.concurrent.ConcurrentHashMap;
00010 import java.util.concurrent.ConcurrentMap;
00011 import java.util.concurrent.atomic.AtomicReference;
00012 
00013 import javax.jmdns.impl.tasks.RecordReaper;
00014 import javax.jmdns.impl.tasks.Responder;
00015 import javax.jmdns.impl.tasks.resolver.ServiceInfoResolver;
00016 import javax.jmdns.impl.tasks.resolver.ServiceResolver;
00017 import javax.jmdns.impl.tasks.resolver.TypeResolver;
00018 import javax.jmdns.impl.tasks.state.Announcer;
00019 import javax.jmdns.impl.tasks.state.Canceler;
00020 import javax.jmdns.impl.tasks.state.Prober;
00021 import javax.jmdns.impl.tasks.state.Renewer;
00022 
00031 public interface DNSTaskStarter {
00032 
00036     public static final class Factory {
00037 
00038         private static volatile Factory                        _instance;
00039         private final ConcurrentMap<JmDNSImpl, DNSTaskStarter> _instances;
00040 
00044         public static interface ClassDelegate {
00045 
00055             public DNSTaskStarter newDNSTaskStarter(JmDNSImpl jmDNSImpl);
00056         }
00057 
00058         private static final AtomicReference<Factory.ClassDelegate> _databaseClassDelegate = new AtomicReference<Factory.ClassDelegate>();
00059 
00060         private Factory() {
00061             super();
00062             _instances = new ConcurrentHashMap<JmDNSImpl, DNSTaskStarter>(20);
00063         }
00064 
00073         public static void setClassDelegate(Factory.ClassDelegate delegate) {
00074             _databaseClassDelegate.set(delegate);
00075         }
00076 
00084         public static Factory.ClassDelegate classDelegate() {
00085             return _databaseClassDelegate.get();
00086         }
00087 
00095         protected static DNSTaskStarter newDNSTaskStarter(JmDNSImpl jmDNSImpl) {
00096             DNSTaskStarter instance = null;
00097             Factory.ClassDelegate delegate = _databaseClassDelegate.get();
00098             if (delegate != null) {
00099                 instance = delegate.newDNSTaskStarter(jmDNSImpl);
00100             }
00101             return (instance != null ? instance : new DNSTaskStarterImpl(jmDNSImpl));
00102         }
00103 
00109         public static Factory getInstance() {
00110             if (_instance == null) {
00111                 synchronized (DNSTaskStarter.Factory.class) {
00112                     if (_instance == null) {
00113                         _instance = new Factory();
00114                     }
00115                 }
00116             }
00117             return _instance;
00118         }
00119 
00127         public DNSTaskStarter getStarter(JmDNSImpl jmDNSImpl) {
00128             DNSTaskStarter starter = _instances.get(jmDNSImpl);
00129             if (starter == null) {
00130                 _instances.putIfAbsent(jmDNSImpl, newDNSTaskStarter(jmDNSImpl));
00131                 starter = _instances.get(jmDNSImpl);
00132             }
00133             return starter;
00134         }
00135 
00136     }
00137 
00138     public static final class DNSTaskStarterImpl implements DNSTaskStarter {
00139 
00140         private final JmDNSImpl _jmDNSImpl;
00141 
00145         private final Timer     _timer;
00146 
00150         private final Timer     _stateTimer;
00151 
00152         public static class StarterTimer extends Timer {
00153 
00154             // This is needed because in some case we cancel the timers before all the task have finished running and in some case they will try to reschedule
00155             private volatile boolean _cancelled;
00156 
00160             public StarterTimer() {
00161                 super();
00162                 _cancelled = false;
00163             }
00164 
00168             public StarterTimer(boolean isDaemon) {
00169                 super(isDaemon);
00170                 _cancelled = false;
00171             }
00172 
00177             public StarterTimer(String name, boolean isDaemon) {
00178                 super(name, isDaemon);
00179                 _cancelled = false;
00180             }
00181 
00185             public StarterTimer(String name) {
00186                 super(name);
00187                 _cancelled = false;
00188             }
00189 
00190             /*
00191              * (non-Javadoc)
00192              * @see java.util.Timer#cancel()
00193              */
00194             @Override
00195             public synchronized void cancel() {
00196                 if (_cancelled) return;
00197                 _cancelled = true;
00198                 super.cancel();
00199             }
00200 
00201             /*
00202              * (non-Javadoc)
00203              * @see java.util.Timer#schedule(java.util.TimerTask, long)
00204              */
00205             @Override
00206             public synchronized void schedule(TimerTask task, long delay) {
00207                 if (_cancelled) return;
00208                 super.schedule(task, delay);
00209             }
00210 
00211             /*
00212              * (non-Javadoc)
00213              * @see java.util.Timer#schedule(java.util.TimerTask, java.util.Date)
00214              */
00215             @Override
00216             public synchronized void schedule(TimerTask task, Date time) {
00217                 if (_cancelled) return;
00218                 super.schedule(task, time);
00219             }
00220 
00221             /*
00222              * (non-Javadoc)
00223              * @see java.util.Timer#schedule(java.util.TimerTask, long, long)
00224              */
00225             @Override
00226             public synchronized void schedule(TimerTask task, long delay, long period) {
00227                 if (_cancelled) return;
00228                 super.schedule(task, delay, period);
00229             }
00230 
00231             /*
00232              * (non-Javadoc)
00233              * @see java.util.Timer#schedule(java.util.TimerTask, java.util.Date, long)
00234              */
00235             @Override
00236             public synchronized void schedule(TimerTask task, Date firstTime, long period) {
00237                 if (_cancelled) return;
00238                 super.schedule(task, firstTime, period);
00239             }
00240 
00241             /*
00242              * (non-Javadoc)
00243              * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long)
00244              */
00245             @Override
00246             public synchronized void scheduleAtFixedRate(TimerTask task, long delay, long period) {
00247                 if (_cancelled) return;
00248                 super.scheduleAtFixedRate(task, delay, period);
00249             }
00250 
00251             /*
00252              * (non-Javadoc)
00253              * @see java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, java.util.Date, long)
00254              */
00255             @Override
00256             public synchronized void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) {
00257                 if (_cancelled) return;
00258                 super.scheduleAtFixedRate(task, firstTime, period);
00259             }
00260 
00261         }
00262 
00263         public DNSTaskStarterImpl(JmDNSImpl jmDNSImpl) {
00264             super();
00265             _jmDNSImpl = jmDNSImpl;
00266             _timer = new StarterTimer("JmDNS(" + _jmDNSImpl.getName() + ").Timer", true);
00267             _stateTimer = new StarterTimer("JmDNS(" + _jmDNSImpl.getName() + ").State.Timer", false);
00268         }
00269 
00270         /*
00271          * (non-Javadoc)
00272          * @see javax.jmdns.impl.DNSTaskStarter#purgeTimer()
00273          */
00274         @Override
00275         public void purgeTimer() {
00276             _timer.purge();
00277         }
00278 
00279         /*
00280          * (non-Javadoc)
00281          * @see javax.jmdns.impl.DNSTaskStarter#purgeStateTimer()
00282          */
00283         @Override
00284         public void purgeStateTimer() {
00285             _stateTimer.purge();
00286         }
00287 
00288         /*
00289          * (non-Javadoc)
00290          * @see javax.jmdns.impl.DNSTaskStarter#cancelTimer()
00291          */
00292         @Override
00293         public void cancelTimer() {
00294             _timer.cancel();
00295         }
00296 
00297         /*
00298          * (non-Javadoc)
00299          * @see javax.jmdns.impl.DNSTaskStarter#cancelStateTimer()
00300          */
00301         @Override
00302         public void cancelStateTimer() {
00303             _stateTimer.cancel();
00304         }
00305 
00306         /*
00307          * (non-Javadoc)
00308          * @see javax.jmdns.impl.DNSTaskStarter#startProber()
00309          */
00310         @Override
00311         public void startProber() {
00312             new Prober(_jmDNSImpl).start(_stateTimer);
00313         }
00314 
00315         /*
00316          * (non-Javadoc)
00317          * @see javax.jmdns.impl.DNSTaskStarter#startAnnouncer()
00318          */
00319         @Override
00320         public void startAnnouncer() {
00321             new Announcer(_jmDNSImpl).start(_stateTimer);
00322         }
00323 
00324         /*
00325          * (non-Javadoc)
00326          * @see javax.jmdns.impl.DNSTaskStarter#startRenewer()
00327          */
00328         @Override
00329         public void startRenewer() {
00330             new Renewer(_jmDNSImpl).start(_stateTimer);
00331         }
00332 
00333         /*
00334          * (non-Javadoc)
00335          * @see javax.jmdns.impl.DNSTaskStarter#startCanceler()
00336          */
00337         @Override
00338         public void startCanceler() {
00339             new Canceler(_jmDNSImpl).start(_stateTimer);
00340         }
00341 
00342         /*
00343          * (non-Javadoc)
00344          * @see javax.jmdns.impl.DNSTaskStarter#startReaper()
00345          */
00346         @Override
00347         public void startReaper() {
00348             new RecordReaper(_jmDNSImpl).start(_timer);
00349         }
00350 
00351         /*
00352          * (non-Javadoc)
00353          * @see javax.jmdns.impl.DNSTaskStarter#startServiceInfoResolver(javax.jmdns.impl.ServiceInfoImpl)
00354          */
00355         @Override
00356         public void startServiceInfoResolver(ServiceInfoImpl info) {
00357             new ServiceInfoResolver(_jmDNSImpl, info).start(_timer);
00358         }
00359 
00360         /*
00361          * (non-Javadoc)
00362          * @see javax.jmdns.impl.DNSTaskStarter#startTypeResolver()
00363          */
00364         @Override
00365         public void startTypeResolver() {
00366             new TypeResolver(_jmDNSImpl).start(_timer);
00367         }
00368 
00369         /*
00370          * (non-Javadoc)
00371          * @see javax.jmdns.impl.DNSTaskStarter#startServiceResolver(java.lang.String)
00372          */
00373         @Override
00374         public void startServiceResolver(String type) {
00375             new ServiceResolver(_jmDNSImpl, type).start(_timer);
00376         }
00377 
00378         /*
00379          * (non-Javadoc)
00380          * @see javax.jmdns.impl.DNSTaskStarter#startResponder(javax.jmdns.impl.DNSIncoming, int)
00381          */
00382         @Override
00383         public void startResponder(DNSIncoming in, int port) {
00384             new Responder(_jmDNSImpl, in, port).start(_timer);
00385         }
00386     }
00387 
00391     public void purgeTimer();
00392 
00396     public void purgeStateTimer();
00397 
00401     public void cancelTimer();
00402 
00406     public void cancelStateTimer();
00407 
00411     public void startProber();
00412 
00416     public void startAnnouncer();
00417 
00421     public void startRenewer();
00422 
00426     public void startCanceler();
00427 
00431     public void startReaper();
00432 
00439     public void startServiceInfoResolver(ServiceInfoImpl info);
00440 
00444     public void startTypeResolver();
00445 
00452     public void startServiceResolver(String type);
00453 
00462     public void startResponder(DNSIncoming in, int port);
00463 
00464 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


zeroconf_jmdns
Author(s): Daniel Stonier
autogenerated on Tue Nov 6 2012 13:08:26