Clover Coverage Report - JmDNS 3.4.1
Coverage timestamp: Thu Aug 25 2011 13:06:33 CEST
../../../img/srcFileCovDistChart6.png 31% of files have more coverage
65   296   41   2.6
30   144   0.63   25
25     1.64  
1    
 
  DNSEntry       Line # 22 65 0% 41 52 56.7% 0.56666666
 
  (21)
 
1    // Copyright 2003-2005 Arthur van Hoff, Rick Blair
2    // Licensed under Apache License version 2.0
3    // Original license LGPL
4   
5    package javax.jmdns.impl;
6   
7    import java.io.ByteArrayOutputStream;
8    import java.io.DataOutputStream;
9    import java.io.IOException;
10    import java.util.Collections;
11    import java.util.Map;
12   
13    import javax.jmdns.ServiceInfo.Fields;
14    import javax.jmdns.impl.constants.DNSRecordClass;
15    import javax.jmdns.impl.constants.DNSRecordType;
16   
17    /**
18    * DNS entry with a name, type, and class. This is the base class for questions and records.
19    *
20    * @author Arthur van Hoff, Pierre Frisch, Rick Blair
21    */
 
22    public abstract class DNSEntry {
23    // private static Logger logger = Logger.getLogger(DNSEntry.class.getName());
24    private final String _key;
25   
26    private final String _name;
27   
28    private final String _type;
29   
30    private final DNSRecordType _recordType;
31   
32    private final DNSRecordClass _dnsClass;
33   
34    private final boolean _unique;
35   
36    final Map<Fields, String> _qualifiedNameMap;
37   
38    /**
39    * Create an entry.
40    */
 
41  2597 toggle DNSEntry(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
42  2639 _name = name;
43    // _key = (name != null ? name.trim().toLowerCase() : null);
44  2636 _recordType = type;
45  2624 _dnsClass = recordClass;
46  2640 _unique = unique;
47  2627 _qualifiedNameMap = ServiceInfoImpl.decodeQualifiedNameMapForType(this.getName());
48  2641 String domain = _qualifiedNameMap.get(Fields.Domain);
49  2640 String protocol = _qualifiedNameMap.get(Fields.Protocol);
50  2648 String application = _qualifiedNameMap.get(Fields.Application);
51  2649 String instance = _qualifiedNameMap.get(Fields.Instance).toLowerCase();
52  2643 _type = (application.length() > 0 ? "_" + application + "." : "") + (protocol.length() > 0 ? "_" + protocol + "." : "") + domain + ".";
53  2650 _key = ((instance.length() > 0 ? instance + "." : "") + _type).toLowerCase();
54    }
55   
56    /*
57    * (non-Javadoc)
58    * @see java.lang.Object#equals(java.lang.Object)
59    */
 
60  106 toggle @Override
61    public boolean equals(Object obj) {
62  107 boolean result = false;
63  106 if (obj instanceof DNSEntry) {
64  108 DNSEntry other = (DNSEntry) obj;
65  108 result = this.getKey().equals(other.getKey()) && this.getRecordType().equals(other.getRecordType()) && this.getRecordClass() == other.getRecordClass();
66    }
67  108 return result;
68    }
69   
70    /**
71    * Check if two entries have exactly the same name, type, and class.
72    *
73    * @param entry
74    * @return <code>true</code> if the two entries have are for the same record, <code>false</code> otherwise
75    */
 
76  963 toggle public boolean isSameEntry(DNSEntry entry) {
77  968 return this.getKey().equals(entry.getKey()) && this.getRecordType().equals(entry.getRecordType()) && ((DNSRecordClass.CLASS_ANY == entry.getRecordClass()) || this.getRecordClass().equals(entry.getRecordClass()));
78    }
79   
80    /**
81    * Check if two entries have the same subtype.
82    *
83    * @param other
84    * @return <code>true</code> if the two entries have are for the same subtype, <code>false</code> otherwise
85    */
 
86  503 toggle public boolean sameSubtype(DNSEntry other) {
87  508 return this.getSubtype().equals(other.getSubtype());
88    }
89   
90    /**
91    * Returns the subtype of this entry
92    *
93    * @return subtype of this entry
94    */
 
95  1015 toggle public String getSubtype() {
96  1029 String subtype = this.getQualifiedNameMap().get(Fields.Subtype);
97  1018 return (subtype != null ? subtype : "");
98    }
99   
100    /**
101    * Returns the name of this entry
102    *
103    * @return name of this entry
104    */
 
105  5762 toggle public String getName() {
106  5781 return (_name != null ? _name : "");
107    }
108   
109    /**
110    * @return the type
111    */
 
112  16 toggle public String getType() {
113  16 return (_type != null ? _type : "");
114    }
115   
116    /**
117    * Returns the key for this entry. The key is the lower case name.
118    *
119    * @return key for this entry
120    */
 
121  4125 toggle public String getKey() {
122  4131 return (_key != null ? _key : "");
123    }
124   
125    /**
126    * @return record type
127    */
 
128  9386 toggle public DNSRecordType getRecordType() {
129  9374 return (_recordType != null ? _recordType : DNSRecordType.TYPE_IGNORE);
130    }
131   
132    /**
133    * @return record class
134    */
 
135  4736 toggle public DNSRecordClass getRecordClass() {
136  4737 return (_dnsClass != null ? _dnsClass : DNSRecordClass.CLASS_UNKNOWN);
137    }
138   
139    /**
140    * @return true if unique
141    */
 
142  2458 toggle public boolean isUnique() {
143  2465 return _unique;
144    }
145   
 
146  1282 toggle public Map<Fields, String> getQualifiedNameMap() {
147  1292 return Collections.unmodifiableMap(_qualifiedNameMap);
148    }
149   
 
150  1020 toggle public boolean isServicesDiscoveryMetaQuery() {
151  1032 return _qualifiedNameMap.get(Fields.Application).equals("dns-sd") && _qualifiedNameMap.get(Fields.Instance).equals("_services");
152    }
153   
 
154  848 toggle public boolean isDomainDiscoveryQuery() {
155    // b._dns-sd._udp.<domain>.
156    // db._dns-sd._udp.<domain>.
157    // r._dns-sd._udp.<domain>.
158    // dr._dns-sd._udp.<domain>.
159    // lb._dns-sd._udp.<domain>.
160   
161  855 if (_qualifiedNameMap.get(Fields.Application).equals("dns-sd")) {
162  0 String name = _qualifiedNameMap.get(Fields.Instance);
163  0 return "b".equals(name) || "db".equals(name) || "r".equals(name) || "dr".equals(name) || "lb".equals(name);
164    }
165  857 return false;
166    }
167   
 
168  151 toggle public boolean isReverseLookup() {
169  151 return this.isV4ReverseLookup() || this.isV6ReverseLookup();
170    }
171   
 
172  150 toggle public boolean isV4ReverseLookup() {
173  152 return _qualifiedNameMap.get(Fields.Domain).endsWith("in-addr.arpa");
174    }
175   
 
176  143 toggle public boolean isV6ReverseLookup() {
177  143 return _qualifiedNameMap.get(Fields.Domain).endsWith("ip6.arpa");
178    }
179   
180    /**
181    * Check if the record is stale, i.e. it has outlived more than half of its TTL.
182    *
183    * @param now
184    * update date
185    * @return <code>true</code> is the record is stale, <code>false</code> otherwise.
186    */
187    public abstract boolean isStale(long now);
188   
189    /**
190    * Check if the record is expired.
191    *
192    * @param now
193    * update date
194    * @return <code>true</code> is the record is expired, <code>false</code> otherwise.
195    */
196    public abstract boolean isExpired(long now);
197   
198    /**
199    * Check that 2 entries are of the same class.
200    *
201    * @param entry
202    * @return <code>true</code> is the two class are the same, <code>false</code> otherwise.
203    */
 
204  24 toggle public boolean isSameRecordClass(DNSEntry entry) {
205  24 return (entry != null) && (entry.getRecordClass() == this.getRecordClass());
206    }
207   
208    /**
209    * Check that 2 entries are of the same type.
210    *
211    * @param entry
212    * @return <code>true</code> is the two type are the same, <code>false</code> otherwise.
213    */
 
214  0 toggle public boolean isSameType(DNSEntry entry) {
215  0 return (entry != null) && (entry.getRecordType() == this.getRecordType());
216    }
217   
218    /**
219    * @param dout
220    * @exception IOException
221    */
 
222  0 toggle protected void toByteArray(DataOutputStream dout) throws IOException {
223  0 dout.write(this.getName().getBytes("UTF8"));
224  0 dout.writeShort(this.getRecordType().indexValue());
225  0 dout.writeShort(this.getRecordClass().indexValue());
226    }
227   
228    /**
229    * Creates a byte array representation of this record. This is needed for tie-break tests according to draft-cheshire-dnsext-multicastdns-04.txt chapter 9.2.
230    *
231    * @return byte array representation
232    */
 
233  0 toggle protected byte[] toByteArray() {
234  0 try {
235  0 ByteArrayOutputStream bout = new ByteArrayOutputStream();
236  0 DataOutputStream dout = new DataOutputStream(bout);
237  0 this.toByteArray(dout);
238  0 dout.close();
239  0 return bout.toByteArray();
240    } catch (IOException e) {
241  0 throw new InternalError();
242    }
243    }
244   
245    /**
246    * Does a lexicographic comparison of the byte array representation of this record and that record. This is needed for tie-break tests according to draft-cheshire-dnsext-multicastdns-04.txt chapter 9.2.
247    *
248    * @param that
249    * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
250    */
 
251  0 toggle public int compareTo(DNSEntry that) {
252  0 byte[] thisBytes = this.toByteArray();
253  0 byte[] thatBytes = that.toByteArray();
254  0 for (int i = 0, n = Math.min(thisBytes.length, thatBytes.length); i < n; i++) {
255  0 if (thisBytes[i] > thatBytes[i]) {
256  0 return 1;
257  0 } else if (thisBytes[i] < thatBytes[i]) {
258  0 return -1;
259    }
260    }
261  0 return thisBytes.length - thatBytes.length;
262    }
263   
264    /**
265    * Overriden, to return a value which is consistent with the value returned by equals(Object).
266    */
 
267  123 toggle @Override
268    public int hashCode() {
269  123 return this.getKey().hashCode() + this.getRecordType().indexValue() + this.getRecordClass().indexValue();
270    }
271   
272    /*
273    * (non-Javadoc)
274    * @see java.lang.Object#toString()
275    */
 
276  0 toggle @Override
277    public String toString() {
278  0 StringBuilder aLog = new StringBuilder(200);
279  0 aLog.append("[" + this.getClass().getSimpleName() + "@" + System.identityHashCode(this));
280  0 aLog.append(" type: " + this.getRecordType());
281  0 aLog.append(", class: " + this.getRecordClass());
282  0 aLog.append((_unique ? "-unique," : ","));
283  0 aLog.append(" name: " + _name);
284  0 this.toString(aLog);
285  0 aLog.append("]");
286  0 return aLog.toString();
287    }
288   
289    /**
290    * @param aLog
291    */
 
292  0 toggle protected void toString(StringBuilder aLog) {
293    // Stub
294    }
295   
296    }