blob: 33c3eb98aaea27fc57fafeec5cb6d8feb5b48c94 [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/**
25 * Defines a service based on DNS service discovery
26 * {@hide}
27 */
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 Sheriff817388e2012-04-11 14:52:19 -070043 public DnsSdServiceInfo(String sn, String rt, DnsSdTxtRecord tr) {
Irfan Sherifffa291e62012-04-04 13:18:17 -070044 mServiceName = sn;
Irfan Sheriff817388e2012-04-11 14:52:19 -070045 mServiceType = rt;
Irfan Sherifffa291e62012-04-04 13:18:17 -070046 mTxtRecord = tr;
47 }
48
49 @Override
50 /** @hide */
51 public String getServiceName() {
52 return mServiceName;
53 }
54
55 @Override
56 /** @hide */
57 public void setServiceName(String s) {
58 mServiceName = s;
59 }
60
61 @Override
62 /** @hide */
63 public String getServiceType() {
Irfan Sheriff817388e2012-04-11 14:52:19 -070064 return mServiceType;
Irfan Sherifffa291e62012-04-04 13:18:17 -070065 }
66
67 @Override
68 /** @hide */
69 public void setServiceType(String s) {
Irfan Sheriff817388e2012-04-11 14:52:19 -070070 mServiceType = s;
Irfan Sherifffa291e62012-04-04 13:18:17 -070071 }
72
73 public DnsSdTxtRecord getTxtRecord() {
74 return mTxtRecord;
75 }
76
77 public void setTxtRecord(DnsSdTxtRecord t) {
78 mTxtRecord = new DnsSdTxtRecord(t);
79 }
80
Irfan Sheriff817388e2012-04-11 14:52:19 -070081 public InetAddress getHost() {
82 return mHost;
Irfan Sherifffa291e62012-04-04 13:18:17 -070083 }
84
Irfan Sheriff817388e2012-04-11 14:52:19 -070085 public void setHost(InetAddress s) {
86 mHost = s;
Irfan Sherifffa291e62012-04-04 13:18:17 -070087 }
88
89 public int getPort() {
90 return mPort;
91 }
92
93 public void setPort(int p) {
94 mPort = p;
95 }
96
97 public String toString() {
98 StringBuffer sb = new StringBuffer();
99
100 sb.append("name: ").append(mServiceName).
Irfan Sheriff817388e2012-04-11 14:52:19 -0700101 append("type: ").append(mServiceType).
102 append("host: ").append(mHost).
103 append("port: ").append(mPort).
Irfan Sherifffa291e62012-04-04 13:18:17 -0700104 append("txtRecord: ").append(mTxtRecord);
105 return sb.toString();
106 }
107
108 /** Implement the Parcelable interface */
109 public int describeContents() {
110 return 0;
111 }
112
113 /** Implement the Parcelable interface */
114 public void writeToParcel(Parcel dest, int flags) {
115 dest.writeString(mServiceName);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700116 dest.writeString(mServiceType);
Irfan Sherifffa291e62012-04-04 13:18:17 -0700117 dest.writeParcelable(mTxtRecord, flags);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700118 if (mHost != null) {
119 dest.writeByte((byte)1);
120 dest.writeByteArray(mHost.getAddress());
121 } else {
122 dest.writeByte((byte)0);
123 }
Irfan Sherifffa291e62012-04-04 13:18:17 -0700124 dest.writeInt(mPort);
125 }
126
127 /** Implement the Parcelable interface */
128 public static final Creator<DnsSdServiceInfo> CREATOR =
129 new Creator<DnsSdServiceInfo>() {
130 public DnsSdServiceInfo createFromParcel(Parcel in) {
131 DnsSdServiceInfo info = new DnsSdServiceInfo();
132 info.mServiceName = in.readString();
Irfan Sheriff817388e2012-04-11 14:52:19 -0700133 info.mServiceType = in.readString();
Irfan Sherifffa291e62012-04-04 13:18:17 -0700134 info.mTxtRecord = in.readParcelable(null);
Irfan Sheriff817388e2012-04-11 14:52:19 -0700135
136 if (in.readByte() == 1) {
137 try {
138 info.mHost = InetAddress.getByAddress(in.createByteArray());
139 } catch (java.net.UnknownHostException e) {}
140 }
141
Irfan Sherifffa291e62012-04-04 13:18:17 -0700142 info.mPort = in.readInt();
143 return info;
144 }
145
146 public DnsSdServiceInfo[] newArray(int size) {
147 return new DnsSdServiceInfo[size];
148 }
149 };
150
151}