blob: 205a21d95b25d4158ee198c880e80152cc78cbd4 [file] [log] [blame]
Irfan Sherifffa291e62012-04-04 13:18:17 -07001/*
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
17package android.net.nsd;
18
19import android.os.Parcelable;
20import android.os.Parcel;
21
Irfan Sheriff817388e2012-04-11 14:52:19 -070022import java.net.InetAddress;
23
Irfan Sherifffa291e62012-04-04 13:18:17 -070024/**
Irfan Sheriff92784672012-04-13 12:15:41 -070025 * A class representing service information for network service discovery
26 * {@see NsdManager}
Irfan Sherifffa291e62012-04-04 13:18:17 -070027 */
Irfan Sheriff22af38c2012-05-03 16:44:27 -070028public final class NsdServiceInfo implements Parcelable {
Irfan Sherifffa291e62012-04-04 13:18:17 -070029
30 private String mServiceName;
31
Irfan Sheriff817388e2012-04-11 14:52:19 -070032 private String mServiceType;
Irfan Sherifffa291e62012-04-04 13:18:17 -070033
34 private DnsSdTxtRecord mTxtRecord;
35
Irfan Sheriff817388e2012-04-11 14:52:19 -070036 private InetAddress mHost;
Irfan Sherifffa291e62012-04-04 13:18:17 -070037
38 private int mPort;
39
Irfan Sheriff22af38c2012-05-03 16:44:27 -070040 public NsdServiceInfo() {
Irfan Sherifffa291e62012-04-04 13:18:17 -070041 }
42
Irfan Sheriff92784672012-04-13 12:15:41 -070043 /** @hide */
Irfan Sheriff22af38c2012-05-03 16:44:27 -070044 public NsdServiceInfo(String sn, String rt, DnsSdTxtRecord tr) {
Irfan Sherifffa291e62012-04-04 13:18:17 -070045 mServiceName = sn;
Irfan Sheriff817388e2012-04-11 14:52:19 -070046 mServiceType = rt;
Irfan Sherifffa291e62012-04-04 13:18:17 -070047 mTxtRecord = tr;
48 }
49
Irfan Sheriff92784672012-04-13 12:15:41 -070050 /** Get the service name */
Irfan Sherifffa291e62012-04-04 13:18:17 -070051 public String getServiceName() {
52 return mServiceName;
53 }
54
Irfan Sheriff92784672012-04-13 12:15:41 -070055 /** Set the service name */
Irfan Sherifffa291e62012-04-04 13:18:17 -070056 public void setServiceName(String s) {
57 mServiceName = s;
58 }
59
Irfan Sheriff92784672012-04-13 12:15:41 -070060 /** Get the service type */
Irfan Sherifffa291e62012-04-04 13:18:17 -070061 public String getServiceType() {
Irfan Sheriff817388e2012-04-11 14:52:19 -070062 return mServiceType;
Irfan Sherifffa291e62012-04-04 13:18:17 -070063 }
64
Irfan Sheriff92784672012-04-13 12:15:41 -070065 /** Set the service type */
Irfan Sherifffa291e62012-04-04 13:18:17 -070066 public void setServiceType(String s) {
Irfan Sheriff817388e2012-04-11 14:52:19 -070067 mServiceType = s;
Irfan Sherifffa291e62012-04-04 13:18:17 -070068 }
69
Irfan Sheriff92784672012-04-13 12:15:41 -070070 /** @hide */
Irfan Sherifffa291e62012-04-04 13:18:17 -070071 public DnsSdTxtRecord getTxtRecord() {
72 return mTxtRecord;
73 }
74
Irfan Sheriff92784672012-04-13 12:15:41 -070075 /** @hide */
Irfan Sherifffa291e62012-04-04 13:18:17 -070076 public void setTxtRecord(DnsSdTxtRecord t) {
77 mTxtRecord = new DnsSdTxtRecord(t);
78 }
79
Irfan Sheriff92784672012-04-13 12:15:41 -070080 /** Get the host address. The host address is valid for a resolved service. */
Irfan Sheriff817388e2012-04-11 14:52:19 -070081 public InetAddress getHost() {
82 return mHost;
Irfan Sherifffa291e62012-04-04 13:18:17 -070083 }
84
Irfan Sheriff92784672012-04-13 12:15:41 -070085 /** Set the host address */
Irfan Sheriff817388e2012-04-11 14:52:19 -070086 public void setHost(InetAddress s) {
87 mHost = s;
Irfan Sherifffa291e62012-04-04 13:18:17 -070088 }
89
Irfan Sheriff92784672012-04-13 12:15:41 -070090 /** Get port number. The port number is valid for a resolved service. */
Irfan Sherifffa291e62012-04-04 13:18:17 -070091 public int getPort() {
92 return mPort;
93 }
94
Irfan Sheriff92784672012-04-13 12:15:41 -070095 /** Set port number */
Irfan Sherifffa291e62012-04-04 13:18:17 -070096 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 Sheriff817388e2012-04-11 14:52:19 -0700104 append("type: ").append(mServiceType).
105 append("host: ").append(mHost).
106 append("port: ").append(mPort).
Irfan Sherifffa291e62012-04-04 13:18:17 -0700107 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 Sheriff817388e2012-04-11 14:52:19 -0700119 dest.writeString(mServiceType);
Irfan Sherifffa291e62012-04-04 13:18:17 -0700120 dest.writeParcelable(mTxtRecord, flags);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700121 if (mHost != null) {
122 dest.writeByte((byte)1);
123 dest.writeByteArray(mHost.getAddress());
124 } else {
125 dest.writeByte((byte)0);
126 }
Irfan Sherifffa291e62012-04-04 13:18:17 -0700127 dest.writeInt(mPort);
128 }
129
130 /** Implement the Parcelable interface */
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700131 public static final Creator<NsdServiceInfo> CREATOR =
132 new Creator<NsdServiceInfo>() {
133 public NsdServiceInfo createFromParcel(Parcel in) {
134 NsdServiceInfo info = new NsdServiceInfo();
Irfan Sherifffa291e62012-04-04 13:18:17 -0700135 info.mServiceName = in.readString();
Irfan Sheriff817388e2012-04-11 14:52:19 -0700136 info.mServiceType = in.readString();
Irfan Sherifffa291e62012-04-04 13:18:17 -0700137 info.mTxtRecord = in.readParcelable(null);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700138
139 if (in.readByte() == 1) {
140 try {
141 info.mHost = InetAddress.getByAddress(in.createByteArray());
142 } catch (java.net.UnknownHostException e) {}
143 }
144
Irfan Sherifffa291e62012-04-04 13:18:17 -0700145 info.mPort = in.readInt();
146 return info;
147 }
148
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700149 public NsdServiceInfo[] newArray(int size) {
150 return new NsdServiceInfo[size];
Irfan Sherifffa291e62012-04-04 13:18:17 -0700151 }
152 };
Irfan Sherifffa291e62012-04-04 13:18:17 -0700153}