blob: 66abd3a321881542a70dcb562ae8a9662baabeaf [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 */
28public class DnsSdServiceInfo implements NetworkServiceInfo, Parcelable {
29
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 Sheriff817388e2012-04-11 14:52:19 -070040 public DnsSdServiceInfo() {
Irfan Sherifffa291e62012-04-04 13:18:17 -070041 }
42
Irfan Sheriff92784672012-04-13 12:15:41 -070043 /** @hide */
Irfan Sheriff817388e2012-04-11 14:52:19 -070044 public DnsSdServiceInfo(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 @Override
Irfan Sherifffa291e62012-04-04 13:18:17 -070052 public String getServiceName() {
53 return mServiceName;
54 }
55
Irfan Sheriff92784672012-04-13 12:15:41 -070056 /** Set the service name */
Irfan Sherifffa291e62012-04-04 13:18:17 -070057 @Override
Irfan Sherifffa291e62012-04-04 13:18:17 -070058 public void setServiceName(String s) {
59 mServiceName = s;
60 }
61
Irfan Sheriff92784672012-04-13 12:15:41 -070062 /** Get the service type */
Irfan Sherifffa291e62012-04-04 13:18:17 -070063 @Override
Irfan Sherifffa291e62012-04-04 13:18:17 -070064 public String getServiceType() {
Irfan Sheriff817388e2012-04-11 14:52:19 -070065 return mServiceType;
Irfan Sherifffa291e62012-04-04 13:18:17 -070066 }
67
Irfan Sheriff92784672012-04-13 12:15:41 -070068 /** Set the service type */
Irfan Sherifffa291e62012-04-04 13:18:17 -070069 @Override
Irfan Sherifffa291e62012-04-04 13:18:17 -070070 public void setServiceType(String s) {
Irfan Sheriff817388e2012-04-11 14:52:19 -070071 mServiceType = s;
Irfan Sherifffa291e62012-04-04 13:18:17 -070072 }
73
Irfan Sheriff92784672012-04-13 12:15:41 -070074 /** @hide */
Irfan Sherifffa291e62012-04-04 13:18:17 -070075 public DnsSdTxtRecord getTxtRecord() {
76 return mTxtRecord;
77 }
78
Irfan Sheriff92784672012-04-13 12:15:41 -070079 /** @hide */
Irfan Sherifffa291e62012-04-04 13:18:17 -070080 public void setTxtRecord(DnsSdTxtRecord t) {
81 mTxtRecord = new DnsSdTxtRecord(t);
82 }
83
Irfan Sheriff92784672012-04-13 12:15:41 -070084 /** Get the host address. The host address is valid for a resolved service. */
Irfan Sheriff817388e2012-04-11 14:52:19 -070085 public InetAddress getHost() {
86 return mHost;
Irfan Sherifffa291e62012-04-04 13:18:17 -070087 }
88
Irfan Sheriff92784672012-04-13 12:15:41 -070089 /** Set the host address */
Irfan Sheriff817388e2012-04-11 14:52:19 -070090 public void setHost(InetAddress s) {
91 mHost = s;
Irfan Sherifffa291e62012-04-04 13:18:17 -070092 }
93
Irfan Sheriff92784672012-04-13 12:15:41 -070094 /** Get port number. The port number is valid for a resolved service. */
Irfan Sherifffa291e62012-04-04 13:18:17 -070095 public int getPort() {
96 return mPort;
97 }
98
Irfan Sheriff92784672012-04-13 12:15:41 -070099 /** Set port number */
Irfan Sherifffa291e62012-04-04 13:18:17 -0700100 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 Sheriff817388e2012-04-11 14:52:19 -0700108 append("type: ").append(mServiceType).
109 append("host: ").append(mHost).
110 append("port: ").append(mPort).
Irfan Sherifffa291e62012-04-04 13:18:17 -0700111 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 Sheriff817388e2012-04-11 14:52:19 -0700123 dest.writeString(mServiceType);
Irfan Sherifffa291e62012-04-04 13:18:17 -0700124 dest.writeParcelable(mTxtRecord, flags);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700125 if (mHost != null) {
126 dest.writeByte((byte)1);
127 dest.writeByteArray(mHost.getAddress());
128 } else {
129 dest.writeByte((byte)0);
130 }
Irfan Sherifffa291e62012-04-04 13:18:17 -0700131 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 Sheriff817388e2012-04-11 14:52:19 -0700140 info.mServiceType = in.readString();
Irfan Sherifffa291e62012-04-04 13:18:17 -0700141 info.mTxtRecord = in.readParcelable(null);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700142
143 if (in.readByte() == 1) {
144 try {
145 info.mHost = InetAddress.getByAddress(in.createByteArray());
146 } catch (java.net.UnknownHostException e) {}
147 }
148
Irfan Sherifffa291e62012-04-04 13:18:17 -0700149 info.mPort = in.readInt();
150 return info;
151 }
152
153 public DnsSdServiceInfo[] newArray(int size) {
154 return new DnsSdServiceInfo[size];
155 }
156 };
Irfan Sherifffa291e62012-04-04 13:18:17 -0700157}