DNSEntry.java
Go to the documentation of this file.
00001 // Copyright 2003-2005 Arthur van Hoff, Rick Blair
00002 // Licensed under Apache License version 2.0
00003 // Original license LGPL
00004 
00005 package javax.jmdns.impl;
00006 
00007 import java.io.ByteArrayOutputStream;
00008 import java.io.DataOutputStream;
00009 import java.io.IOException;
00010 import java.util.Collections;
00011 import java.util.Map;
00012 
00013 import javax.jmdns.ServiceInfo.Fields;
00014 import javax.jmdns.impl.constants.DNSRecordClass;
00015 import javax.jmdns.impl.constants.DNSRecordType;
00016 
00022 public abstract class DNSEntry {
00023     // private static Logger logger = Logger.getLogger(DNSEntry.class.getName());
00024     private final String         _key;
00025 
00026     private final String         _name;
00027 
00028     private final String         _type;
00029 
00030     private final DNSRecordType  _recordType;
00031 
00032     private final DNSRecordClass _dnsClass;
00033 
00034     private final boolean        _unique;
00035 
00036     final Map<Fields, String>    _qualifiedNameMap;
00037 
00041     DNSEntry(String name, DNSRecordType type, DNSRecordClass recordClass, boolean unique) {
00042         _name = name;
00043         // _key = (name != null ? name.trim().toLowerCase() : null);
00044         _recordType = type;
00045         _dnsClass = recordClass;
00046         _unique = unique;
00047         _qualifiedNameMap = ServiceInfoImpl.decodeQualifiedNameMapForType(this.getName());
00048         String domain = _qualifiedNameMap.get(Fields.Domain);
00049         String protocol = _qualifiedNameMap.get(Fields.Protocol);
00050         String application = _qualifiedNameMap.get(Fields.Application);
00051         String instance = _qualifiedNameMap.get(Fields.Instance).toLowerCase();
00052         _type = (application.length() > 0 ? "_" + application + "." : "") + (protocol.length() > 0 ? "_" + protocol + "." : "") + domain + ".";
00053         _key = ((instance.length() > 0 ? instance + "." : "") + _type).toLowerCase();
00054     }
00055 
00056     /*
00057      * (non-Javadoc)
00058      * @see java.lang.Object#equals(java.lang.Object)
00059      */
00060     @Override
00061     public boolean equals(Object obj) {
00062         boolean result = false;
00063         if (obj instanceof DNSEntry) {
00064             DNSEntry other = (DNSEntry) obj;
00065             result = this.getKey().equals(other.getKey()) && this.getRecordType().equals(other.getRecordType()) && this.getRecordClass() == other.getRecordClass();
00066         }
00067         return result;
00068     }
00069 
00076     public boolean isSameEntry(DNSEntry entry) {
00077         return this.getKey().equals(entry.getKey()) && this.getRecordType().equals(entry.getRecordType()) && ((DNSRecordClass.CLASS_ANY == entry.getRecordClass()) || this.getRecordClass().equals(entry.getRecordClass()));
00078     }
00079 
00086     public boolean sameSubtype(DNSEntry other) {
00087         return this.getSubtype().equals(other.getSubtype());
00088     }
00089 
00095     public String getSubtype() {
00096         String subtype = this.getQualifiedNameMap().get(Fields.Subtype);
00097         return (subtype != null ? subtype : "");
00098     }
00099 
00105     public String getName() {
00106         return (_name != null ? _name : "");
00107     }
00108 
00112     public String getType() {
00113         return (_type != null ? _type : "");
00114     }
00115 
00121     public String getKey() {
00122         return (_key != null ? _key : "");
00123     }
00124 
00128     public DNSRecordType getRecordType() {
00129         return (_recordType != null ? _recordType : DNSRecordType.TYPE_IGNORE);
00130     }
00131 
00135     public DNSRecordClass getRecordClass() {
00136         return (_dnsClass != null ? _dnsClass : DNSRecordClass.CLASS_UNKNOWN);
00137     }
00138 
00142     public boolean isUnique() {
00143         return _unique;
00144     }
00145 
00146     public Map<Fields, String> getQualifiedNameMap() {
00147         return Collections.unmodifiableMap(_qualifiedNameMap);
00148     }
00149 
00150     public boolean isServicesDiscoveryMetaQuery() {
00151         return _qualifiedNameMap.get(Fields.Application).equals("dns-sd") && _qualifiedNameMap.get(Fields.Instance).equals("_services");
00152     }
00153 
00154     public boolean isDomainDiscoveryQuery() {
00155         // b._dns-sd._udp.<domain>.
00156         // db._dns-sd._udp.<domain>.
00157         // r._dns-sd._udp.<domain>.
00158         // dr._dns-sd._udp.<domain>.
00159         // lb._dns-sd._udp.<domain>.
00160 
00161         if (_qualifiedNameMap.get(Fields.Application).equals("dns-sd")) {
00162             String name = _qualifiedNameMap.get(Fields.Instance);
00163             return "b".equals(name) || "db".equals(name) || "r".equals(name) || "dr".equals(name) || "lb".equals(name);
00164         }
00165         return false;
00166     }
00167 
00168     public boolean isReverseLookup() {
00169         return this.isV4ReverseLookup() || this.isV6ReverseLookup();
00170     }
00171 
00172     public boolean isV4ReverseLookup() {
00173         return _qualifiedNameMap.get(Fields.Domain).endsWith("in-addr.arpa");
00174     }
00175 
00176     public boolean isV6ReverseLookup() {
00177         return _qualifiedNameMap.get(Fields.Domain).endsWith("ip6.arpa");
00178     }
00179 
00187     public abstract boolean isStale(long now);
00188 
00196     public abstract boolean isExpired(long now);
00197 
00204     public boolean isSameRecordClass(DNSEntry entry) {
00205         return (entry != null) && (entry.getRecordClass() == this.getRecordClass());
00206     }
00207 
00214     public boolean isSameType(DNSEntry entry) {
00215         return (entry != null) && (entry.getRecordType() == this.getRecordType());
00216     }
00217 
00222     protected void toByteArray(DataOutputStream dout) throws IOException {
00223         dout.write(this.getName().getBytes("UTF8"));
00224         dout.writeShort(this.getRecordType().indexValue());
00225         dout.writeShort(this.getRecordClass().indexValue());
00226     }
00227 
00233     protected byte[] toByteArray() {
00234         try {
00235             ByteArrayOutputStream bout = new ByteArrayOutputStream();
00236             DataOutputStream dout = new DataOutputStream(bout);
00237             this.toByteArray(dout);
00238             dout.close();
00239             return bout.toByteArray();
00240         } catch (IOException e) {
00241             throw new InternalError();
00242         }
00243     }
00244 
00251     public int compareTo(DNSEntry that) {
00252         byte[] thisBytes = this.toByteArray();
00253         byte[] thatBytes = that.toByteArray();
00254         for (int i = 0, n = Math.min(thisBytes.length, thatBytes.length); i < n; i++) {
00255             if (thisBytes[i] > thatBytes[i]) {
00256                 return 1;
00257             } else if (thisBytes[i] < thatBytes[i]) {
00258                 return -1;
00259             }
00260         }
00261         return thisBytes.length - thatBytes.length;
00262     }
00263 
00267     @Override
00268     public int hashCode() {
00269         return this.getKey().hashCode() + this.getRecordType().indexValue() + this.getRecordClass().indexValue();
00270     }
00271 
00272     /*
00273      * (non-Javadoc)
00274      * @see java.lang.Object#toString()
00275      */
00276     @Override
00277     public String toString() {
00278         StringBuilder aLog = new StringBuilder(200);
00279         aLog.append("[" + this.getClass().getSimpleName() + "@" + System.identityHashCode(this));
00280         aLog.append(" type: " + this.getRecordType());
00281         aLog.append(", class: " + this.getRecordClass());
00282         aLog.append((_unique ? "-unique," : ","));
00283         aLog.append(" name: " + _name);
00284         this.toString(aLog);
00285         aLog.append("]");
00286         return aLog.toString();
00287     }
00288 
00292     protected void toString(StringBuilder aLog) {
00293         // Stub
00294     }
00295 
00296 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


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