Home | History | Annotate | Download | only in nsd
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.net.nsd;
     18 
     19 import android.os.Parcelable;
     20 import android.os.Parcel;
     21 
     22 import java.net.InetAddress;
     23 
     24 /**
     25  * A class representing service information for network service discovery
     26  * {@see NsdManager}
     27  */
     28 public final class NsdServiceInfo implements Parcelable {
     29 
     30     private String mServiceName;
     31 
     32     private String mServiceType;
     33 
     34     private DnsSdTxtRecord mTxtRecord;
     35 
     36     private InetAddress mHost;
     37 
     38     private int mPort;
     39 
     40     public NsdServiceInfo() {
     41     }
     42 
     43     /** @hide */
     44     public NsdServiceInfo(String sn, String rt, DnsSdTxtRecord tr) {
     45         mServiceName = sn;
     46         mServiceType = rt;
     47         mTxtRecord = tr;
     48     }
     49 
     50     /** Get the service name */
     51     public String getServiceName() {
     52         return mServiceName;
     53     }
     54 
     55     /** Set the service name */
     56     public void setServiceName(String s) {
     57         mServiceName = s;
     58     }
     59 
     60     /** Get the service type */
     61     public String getServiceType() {
     62         return mServiceType;
     63     }
     64 
     65     /** Set the service type */
     66     public void setServiceType(String s) {
     67         mServiceType = s;
     68     }
     69 
     70     /** @hide */
     71     public DnsSdTxtRecord getTxtRecord() {
     72         return mTxtRecord;
     73     }
     74 
     75     /** @hide */
     76     public void setTxtRecord(DnsSdTxtRecord t) {
     77         mTxtRecord = new DnsSdTxtRecord(t);
     78     }
     79 
     80     /** Get the host address. The host address is valid for a resolved service. */
     81     public InetAddress getHost() {
     82         return mHost;
     83     }
     84 
     85     /** Set the host address */
     86     public void setHost(InetAddress s) {
     87         mHost = s;
     88     }
     89 
     90     /** Get port number. The port number is valid for a resolved service. */
     91     public int getPort() {
     92         return mPort;
     93     }
     94 
     95     /** Set port number */
     96     public void setPort(int p) {
     97         mPort = p;
     98     }
     99 
    100     public String toString() {
    101         StringBuffer sb = new StringBuffer();
    102 
    103         sb.append("name: ").append(mServiceName).
    104             append("type: ").append(mServiceType).
    105             append("host: ").append(mHost).
    106             append("port: ").append(mPort).
    107             append("txtRecord: ").append(mTxtRecord);
    108         return sb.toString();
    109     }
    110 
    111     /** Implement the Parcelable interface */
    112     public int describeContents() {
    113         return 0;
    114     }
    115 
    116     /** Implement the Parcelable interface */
    117     public void writeToParcel(Parcel dest, int flags) {
    118         dest.writeString(mServiceName);
    119         dest.writeString(mServiceType);
    120         dest.writeParcelable(mTxtRecord, flags);
    121         if (mHost != null) {
    122             dest.writeByte((byte)1);
    123             dest.writeByteArray(mHost.getAddress());
    124         } else {
    125             dest.writeByte((byte)0);
    126         }
    127         dest.writeInt(mPort);
    128     }
    129 
    130     /** Implement the Parcelable interface */
    131     public static final Creator<NsdServiceInfo> CREATOR =
    132         new Creator<NsdServiceInfo>() {
    133             public NsdServiceInfo createFromParcel(Parcel in) {
    134                 NsdServiceInfo info = new NsdServiceInfo();
    135                 info.mServiceName = in.readString();
    136                 info.mServiceType = in.readString();
    137                 info.mTxtRecord = in.readParcelable(null);
    138 
    139                 if (in.readByte() == 1) {
    140                     try {
    141                         info.mHost = InetAddress.getByAddress(in.createByteArray());
    142                     } catch (java.net.UnknownHostException e) {}
    143                 }
    144 
    145                 info.mPort = in.readInt();
    146                 return info;
    147             }
    148 
    149             public NsdServiceInfo[] newArray(int size) {
    150                 return new NsdServiceInfo[size];
    151             }
    152         };
    153 }
    154