FileDocCategorySizeDatePackage
WifiP2pDnsSdServiceResponse.javaAPI DocAndroid 5.1 API8966Thu Mar 12 22:22:44 GMT 2015android.net.wifi.p2p.nsd

WifiP2pDnsSdServiceResponse

public class WifiP2pDnsSdServiceResponse extends WifiP2pServiceResponse
A class for a response of bonjour service discovery.
hide

Fields Summary
private String
mDnsQueryName
DNS query name. e.g) for PTR "_ipp._tcp.local." for TXT "MyPrinter._ipp._tcp.local."
private String
mInstanceName
Service instance name. e.g) "MyPrinter" This field is only used when the dns type equals to {@link WifiP2pDnsSdServiceInfo#DNS_TYPE_PTR}.
private int
mDnsType
DNS Type. Should be {@link WifiP2pDnsSdServiceInfo#DNS_TYPE_PTR} or {@link WifiP2pDnsSdServiceInfo#DNS_TYPE_TXT}.
private int
mVersion
DnsSd version number. Should be {@link WifiP2pDnsSdServiceInfo#VERSION_1}.
private final HashMap
mTxtRecord
Txt record. This field is only used when the dns type equals to {@link WifiP2pDnsSdServiceInfo#DNS_TYPE_TXT}.
private static final Map
sVmpack
Virtual memory packet. see E.3 of the Wi-Fi Direct technical specification for the detail.
The spec can be obtained from wi-fi.org Key: pointer Value: domain name.
Constructors Summary
protected WifiP2pDnsSdServiceResponse(int status, int tranId, android.net.wifi.p2p.WifiP2pDevice dev, byte[] data)
This is only used in framework.

param
status status code.
param
dev source device.
param
data RDATA.
hide

        super(WifiP2pServiceInfo.SERVICE_TYPE_BONJOUR,
                status, tranId, dev, data);
        if (!parse()) {
            throw new IllegalArgumentException("Malformed bonjour service response");
        }
    
Methods Summary
public java.lang.StringgetDnsQueryName()
Returns query DNS name.

return
DNS name.


     
        sVmpack = new HashMap<Integer, String>();
        sVmpack.put(0x0c, "_tcp.local.");
        sVmpack.put(0x11, "local.");
        sVmpack.put(0x1c, "_udp.local.");
    
        return mDnsQueryName;
    
public intgetDnsType()
Return query DNS type.

return
DNS type.

        return mDnsType;
    
public java.lang.StringgetInstanceName()
Return instance name.

return

        return mInstanceName;
    
public java.util.MapgetTxtRecord()
Return TXT record data.

return
TXT record data.

        return mTxtRecord;
    
public intgetVersion()
Return bonjour version number.

return
version number.

        return mVersion;
    
static android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceResponsenewInstance(int status, int transId, android.net.wifi.p2p.WifiP2pDevice dev, byte[] data)
Creates DnsSd service response. This is only called from WifiP2pServiceResponse

param
status status code.
param
dev source device.
param
data DnsSd response data.
return
DnsSd service response data.
hide

        if (status != WifiP2pServiceResponse.Status.SUCCESS) {
            return new WifiP2pDnsSdServiceResponse(status,
                    transId, dev, null);
        }
        try {
            return new WifiP2pDnsSdServiceResponse(status,
                    transId, dev, data);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return null;
    
private booleanparse()
Parse DnsSd service discovery response.

return
{@code true} if the operation succeeded

        /*
         * The data format from Wi-Fi Direct spec is as follows.
         * ________________________________________________
         * |  encoded and compressed dns name (variable)  |
         * ________________________________________________
         * |       dnstype(2byte)      |  version(1byte)  |
         * ________________________________________________
         * |              RDATA (variable)                |
         */
        if (mData == null) {
            // the empty is OK.
            return true;
        }

        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(mData));

        mDnsQueryName = readDnsName(dis);
        if (mDnsQueryName == null) {
            return false;
        }

        try {
            mDnsType = dis.readUnsignedShort();
            mVersion = dis.readUnsignedByte();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        if (mDnsType == WifiP2pDnsSdServiceInfo.DNS_TYPE_PTR) {
            String rData = readDnsName(dis);
            if (rData == null) {
                return false;
            }
            if (rData.length() <= mDnsQueryName.length()) {
                return false;
            }

            mInstanceName = rData.substring(0,
                    rData.length() - mDnsQueryName.length() -1);
        } else if (mDnsType == WifiP2pDnsSdServiceInfo.DNS_TYPE_TXT) {
            return readTxtData(dis);
        } else {
            return false;
        }

        return true;
    
private java.lang.StringreadDnsName(java.io.DataInputStream dis)
Read dns name.

param
dis data input stream.
return
dns name

        StringBuffer sb = new StringBuffer();

        // copy virtual memory packet.
        HashMap<Integer, String> vmpack = new HashMap<Integer, String>(sVmpack);
        if (mDnsQueryName != null) {
            vmpack.put(0x27, mDnsQueryName);
        }
        try {
            while (true) {
                int i = dis.readUnsignedByte();
                if (i == 0x00) {
                    return sb.toString();
                } else if (i == 0xc0) {
                    // refer to pointer.
                    String ref = vmpack.get(dis.readUnsignedByte());
                    if (ref == null) {
                        //invalid.
                        return null;
                    }
                    sb.append(ref);
                    return sb.toString();
                } else {
                    byte[] data = new byte[i];
                    dis.readFully(data);
                    sb.append(new String(data));
                    sb.append(".");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    
private booleanreadTxtData(java.io.DataInputStream dis)
Read TXT record data.

param
dis
return
true if TXT data is valid

        try {
            while (dis.available() > 0) {
                int len = dis.readUnsignedByte();
                if (len == 0) {
                    break;
                }
                byte[] data = new byte[len];
                dis.readFully(data);
                String[] keyVal = new String(data).split("=");
                if (keyVal.length != 2) {
                    return false;
                }
                mTxtRecord.put(keyVal[0], keyVal[1]);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    
public java.lang.StringtoString()

        StringBuffer sbuf = new StringBuffer();
        sbuf.append("serviceType:DnsSd(").append(mServiceType).append(")");
        sbuf.append(" status:").append(Status.toString(mStatus));
        sbuf.append(" srcAddr:").append(mDevice.deviceAddress);
        sbuf.append(" version:").append(String.format("%02x", mVersion));
        sbuf.append(" dnsName:").append(mDnsQueryName);
        sbuf.append(" TxtRecord:");
        for (String key : mTxtRecord.keySet()) {
            sbuf.append(" key:").append(key).append(" value:").append(mTxtRecord.get(key));
        }
        if (mInstanceName != null) {
            sbuf.append(" InsName:").append(mInstanceName);
        }
        return sbuf.toString();