Clover Coverage Report - JmDNS 3.4.1
Coverage timestamp: Thu Aug 25 2011 13:06:33 CEST
../../../../../img/srcFileCovDistChart9.png 2% of files have more coverage
47   188   16   4.7
10   112   0.34   10
10     1.6  
1    
 
  DNSStateTask       Line # 24 47 0% 16 7 89.6% 0.8955224
 
  (21)
 
1    // Licensed under Apache License version 2.0
2    package javax.jmdns.impl.tasks.state;
3   
4    import java.io.IOException;
5    import java.util.ArrayList;
6    import java.util.List;
7    import java.util.logging.Level;
8    import java.util.logging.Logger;
9   
10    import javax.jmdns.ServiceInfo;
11    import javax.jmdns.impl.DNSOutgoing;
12    import javax.jmdns.impl.DNSStatefulObject;
13    import javax.jmdns.impl.JmDNSImpl;
14    import javax.jmdns.impl.ServiceInfoImpl;
15    import javax.jmdns.impl.constants.DNSConstants;
16    import javax.jmdns.impl.constants.DNSState;
17    import javax.jmdns.impl.tasks.DNSTask;
18   
19    /**
20    * This is the root class for all state tasks. These tasks work with objects that implements the {@link javax.jmdns.impl.DNSStatefulObject} interface and therefore participate in the state machine.
21    *
22    * @author Pierre Frisch
23    */
 
24    public abstract class DNSStateTask extends DNSTask {
25    static Logger logger1 = Logger.getLogger(DNSStateTask.class.getName());
26   
27    /**
28    * By setting a 0 ttl we effectively expire the record.
29    */
30    private final int _ttl;
31   
32    private static int _defaultTTL = DNSConstants.DNS_TTL;
33   
34    /**
35    * The state of the task.
36    */
37    private DNSState _taskState = null;
38   
39    public abstract String getTaskDescription();
40   
 
41  151 toggle public static int defaultTTL() {
42  151 return _defaultTTL;
43    }
44   
45    /**
46    * For testing only do not use in production.
47    *
48    * @param value
49    */
 
50  2 toggle public static void setDefaultTTL(int value) {
51  2 _defaultTTL = value;
52    }
53   
54    /**
55    * @param jmDNSImpl
56    * @param ttl
57    */
 
58  184 toggle public DNSStateTask(JmDNSImpl jmDNSImpl, int ttl) {
59  184 super(jmDNSImpl);
60  184 _ttl = ttl;
61    }
62   
63    /**
64    * @return the ttl
65    */
 
66  302 toggle public int getTTL() {
67  303 return _ttl;
68    }
69   
70    /**
71    * Associate the DNS host and the service infos with this task if not already associated and in the same state.
72    *
73    * @param state
74    * target state
75    */
 
76  184 toggle protected void associate(DNSState state) {
77  184 synchronized (this.getDns()) {
78  184 this.getDns().associateWithTask(this, state);
79    }
80  184 for (ServiceInfo serviceInfo : this.getDns().getServices().values()) {
81  89 ((ServiceInfoImpl) serviceInfo).associateWithTask(this, state);
82    }
83    }
84   
85    /**
86    * Remove the DNS host and service info association with this task.
87    */
 
88  133 toggle protected void removeAssociation() {
89    // Remove association from host to this
90  133 synchronized (this.getDns()) {
91  133 this.getDns().removeAssociationWithTask(this);
92    }
93   
94    // Remove associations from services to this
95  133 for (ServiceInfo serviceInfo : this.getDns().getServices().values()) {
96  64 ((ServiceInfoImpl) serviceInfo).removeAssociationWithTask(this);
97    }
98    }
99   
 
100  318 toggle @Override
101    public void run() {
102  318 DNSOutgoing out = this.createOugoing();
103  318 try {
104  317 if (!this.checkRunCondition()) {
105  0 this.cancel();
106  0 return;
107    }
108  318 List<DNSStatefulObject> stateObjects = new ArrayList<DNSStatefulObject>();
109    // send probes for JmDNS itself
110  318 synchronized (this.getDns()) {
111  317 if (this.getDns().isAssociatedWithTask(this, this.getTaskState())) {
112  134 logger1.finer(this.getName() + ".run() JmDNS " + this.getTaskDescription() + " " + this.getDns().getName());
113  134 stateObjects.add(this.getDns());
114  135 out = this.buildOutgoingForDNS(out);
115    }
116    }
117    // send probes for services
118  318 for (ServiceInfo serviceInfo : this.getDns().getServices().values()) {
119  168 ServiceInfoImpl info = (ServiceInfoImpl) serviceInfo;
120   
121  168 synchronized (info) {
122  168 if (info.isAssociatedWithTask(this, this.getTaskState())) {
123  168 logger1.fine(this.getName() + ".run() JmDNS " + this.getTaskDescription() + " " + info.getQualifiedName());
124  168 stateObjects.add(info);
125  168 out = this.buildOutgoingForInfo(info, out);
126    }
127    }
128    }
129  317 if (!out.isEmpty()) {
130  302 logger1.finer(this.getName() + ".run() JmDNS " + this.getTaskDescription() + " #" + this.getTaskState());
131  303 this.getDns().send(out);
132   
133    // Advance the state of objects.
134  303 this.advanceObjectsState(stateObjects);
135    } else {
136    // Advance the state of objects.
137  15 this.advanceObjectsState(stateObjects);
138   
139    // If we have nothing to send, another timer taskState ahead of us has done the job for us. We can cancel.
140  15 cancel();
141  15 return;
142    }
143    } catch (Throwable e) {
144  0 logger1.log(Level.WARNING, this.getName() + ".run() exception ", e);
145  0 this.recoverTask(e);
146    }
147   
148  303 this.advanceTask();
149    }
150   
151    protected abstract boolean checkRunCondition();
152   
153    protected abstract DNSOutgoing buildOutgoingForDNS(DNSOutgoing out) throws IOException;
154   
155    protected abstract DNSOutgoing buildOutgoingForInfo(ServiceInfoImpl info, DNSOutgoing out) throws IOException;
156   
157    protected abstract DNSOutgoing createOugoing();
158   
 
159  318 toggle protected void advanceObjectsState(List<DNSStatefulObject> list) {
160  318 if (list != null) {
161  318 for (DNSStatefulObject object : list) {
162  303 synchronized (object) {
163  303 object.advanceState(this);
164    }
165    }
166    }
167    }
168   
169    protected abstract void recoverTask(Throwable e);
170   
171    protected abstract void advanceTask();
172   
173    /**
174    * @return the taskState
175    */
 
176  1395 toggle protected DNSState getTaskState() {
177  1395 return this._taskState;
178    }
179   
180    /**
181    * @param taskState
182    * the taskState to set
183    */
 
184  487 toggle protected void setTaskState(DNSState taskState) {
185  487 this._taskState = taskState;
186    }
187   
188    }