Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 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 | |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 22 | import java.net.InetAddress; |
| 23 | |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 24 | /** |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 25 | * A class representing service information for network service discovery |
| 26 | * {@see NsdManager} |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 27 | */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame^] | 28 | public final class NsdServiceInfo implements Parcelable { |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 29 | |
| 30 | private String mServiceName; |
| 31 | |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 32 | private String mServiceType; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 33 | |
| 34 | private DnsSdTxtRecord mTxtRecord; |
| 35 | |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 36 | private InetAddress mHost; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 37 | |
| 38 | private int mPort; |
| 39 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame^] | 40 | public NsdServiceInfo() { |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 43 | /** @hide */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame^] | 44 | public NsdServiceInfo(String sn, String rt, DnsSdTxtRecord tr) { |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 45 | mServiceName = sn; |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 46 | mServiceType = rt; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 47 | mTxtRecord = tr; |
| 48 | } |
| 49 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 50 | /** Get the service name */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 51 | public String getServiceName() { |
| 52 | return mServiceName; |
| 53 | } |
| 54 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 55 | /** Set the service name */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 56 | public void setServiceName(String s) { |
| 57 | mServiceName = s; |
| 58 | } |
| 59 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 60 | /** Get the service type */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 61 | public String getServiceType() { |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 62 | return mServiceType; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 65 | /** Set the service type */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 66 | public void setServiceType(String s) { |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 67 | mServiceType = s; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 70 | /** @hide */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 71 | public DnsSdTxtRecord getTxtRecord() { |
| 72 | return mTxtRecord; |
| 73 | } |
| 74 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 75 | /** @hide */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 76 | public void setTxtRecord(DnsSdTxtRecord t) { |
| 77 | mTxtRecord = new DnsSdTxtRecord(t); |
| 78 | } |
| 79 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 80 | /** Get the host address. The host address is valid for a resolved service. */ |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 81 | public InetAddress getHost() { |
| 82 | return mHost; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 85 | /** Set the host address */ |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 86 | public void setHost(InetAddress s) { |
| 87 | mHost = s; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 90 | /** Get port number. The port number is valid for a resolved service. */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 91 | public int getPort() { |
| 92 | return mPort; |
| 93 | } |
| 94 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame] | 95 | /** Set port number */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 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). |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 104 | append("type: ").append(mServiceType). |
| 105 | append("host: ").append(mHost). |
| 106 | append("port: ").append(mPort). |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 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); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 119 | dest.writeString(mServiceType); |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 120 | dest.writeParcelable(mTxtRecord, flags); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 121 | if (mHost != null) { |
| 122 | dest.writeByte((byte)1); |
| 123 | dest.writeByteArray(mHost.getAddress()); |
| 124 | } else { |
| 125 | dest.writeByte((byte)0); |
| 126 | } |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 127 | dest.writeInt(mPort); |
| 128 | } |
| 129 | |
| 130 | /** Implement the Parcelable interface */ |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame^] | 131 | public static final Creator<NsdServiceInfo> CREATOR = |
| 132 | new Creator<NsdServiceInfo>() { |
| 133 | public NsdServiceInfo createFromParcel(Parcel in) { |
| 134 | NsdServiceInfo info = new NsdServiceInfo(); |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 135 | info.mServiceName = in.readString(); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 136 | info.mServiceType = in.readString(); |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 137 | info.mTxtRecord = in.readParcelable(null); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 138 | |
| 139 | if (in.readByte() == 1) { |
| 140 | try { |
| 141 | info.mHost = InetAddress.getByAddress(in.createByteArray()); |
| 142 | } catch (java.net.UnknownHostException e) {} |
| 143 | } |
| 144 | |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 145 | info.mPort = in.readInt(); |
| 146 | return info; |
| 147 | } |
| 148 | |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame^] | 149 | public NsdServiceInfo[] newArray(int size) { |
| 150 | return new NsdServiceInfo[size]; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 151 | } |
| 152 | }; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 153 | } |