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 | */ |
| 28 | public class DnsSdServiceInfo implements NetworkServiceInfo, Parcelable { |
| 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 | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 40 | public DnsSdServiceInfo() { |
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 | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 44 | public DnsSdServiceInfo(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 | @Override |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 52 | public String getServiceName() { |
| 53 | return mServiceName; |
| 54 | } |
| 55 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame^] | 56 | /** Set the service name */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 57 | @Override |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 58 | public void setServiceName(String s) { |
| 59 | mServiceName = s; |
| 60 | } |
| 61 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame^] | 62 | /** Get the service type */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 63 | @Override |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 64 | public String getServiceType() { |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 65 | return mServiceType; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame^] | 68 | /** Set the service type */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 69 | @Override |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 70 | public void setServiceType(String s) { |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 71 | mServiceType = s; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame^] | 74 | /** @hide */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 75 | public DnsSdTxtRecord getTxtRecord() { |
| 76 | return mTxtRecord; |
| 77 | } |
| 78 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame^] | 79 | /** @hide */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 80 | public void setTxtRecord(DnsSdTxtRecord t) { |
| 81 | mTxtRecord = new DnsSdTxtRecord(t); |
| 82 | } |
| 83 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame^] | 84 | /** 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] | 85 | public InetAddress getHost() { |
| 86 | return mHost; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame^] | 89 | /** Set the host address */ |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 90 | public void setHost(InetAddress s) { |
| 91 | mHost = s; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame^] | 94 | /** Get port number. The port number is valid for a resolved service. */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 95 | public int getPort() { |
| 96 | return mPort; |
| 97 | } |
| 98 | |
Irfan Sheriff | 9278467 | 2012-04-13 12:15:41 -0700 | [diff] [blame^] | 99 | /** Set port number */ |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 100 | public void setPort(int p) { |
| 101 | mPort = p; |
| 102 | } |
| 103 | |
| 104 | public String toString() { |
| 105 | StringBuffer sb = new StringBuffer(); |
| 106 | |
| 107 | sb.append("name: ").append(mServiceName). |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 108 | append("type: ").append(mServiceType). |
| 109 | append("host: ").append(mHost). |
| 110 | append("port: ").append(mPort). |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 111 | append("txtRecord: ").append(mTxtRecord); |
| 112 | return sb.toString(); |
| 113 | } |
| 114 | |
| 115 | /** Implement the Parcelable interface */ |
| 116 | public int describeContents() { |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /** Implement the Parcelable interface */ |
| 121 | public void writeToParcel(Parcel dest, int flags) { |
| 122 | dest.writeString(mServiceName); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 123 | dest.writeString(mServiceType); |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 124 | dest.writeParcelable(mTxtRecord, flags); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 125 | if (mHost != null) { |
| 126 | dest.writeByte((byte)1); |
| 127 | dest.writeByteArray(mHost.getAddress()); |
| 128 | } else { |
| 129 | dest.writeByte((byte)0); |
| 130 | } |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 131 | dest.writeInt(mPort); |
| 132 | } |
| 133 | |
| 134 | /** Implement the Parcelable interface */ |
| 135 | public static final Creator<DnsSdServiceInfo> CREATOR = |
| 136 | new Creator<DnsSdServiceInfo>() { |
| 137 | public DnsSdServiceInfo createFromParcel(Parcel in) { |
| 138 | DnsSdServiceInfo info = new DnsSdServiceInfo(); |
| 139 | info.mServiceName = in.readString(); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 140 | info.mServiceType = in.readString(); |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 141 | info.mTxtRecord = in.readParcelable(null); |
Irfan Sheriff | 817388e | 2012-04-11 14:52:19 -0700 | [diff] [blame] | 142 | |
| 143 | if (in.readByte() == 1) { |
| 144 | try { |
| 145 | info.mHost = InetAddress.getByAddress(in.createByteArray()); |
| 146 | } catch (java.net.UnknownHostException e) {} |
| 147 | } |
| 148 | |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 149 | info.mPort = in.readInt(); |
| 150 | return info; |
| 151 | } |
| 152 | |
| 153 | public DnsSdServiceInfo[] newArray(int size) { |
| 154 | return new DnsSdServiceInfo[size]; |
| 155 | } |
| 156 | }; |
Irfan Sheriff | fa291e6 | 2012-04-04 13:18:17 -0700 | [diff] [blame] | 157 | } |