Skip to content

Commit 817388e

Browse files
committed
Bonjour fixes
Change-Id: I1df1dc470bb42c84abc7e1a46bedf9f206910b65
1 parent 63c115c commit 817388e

File tree

3 files changed

+418
-55
lines changed

3 files changed

+418
-55
lines changed

core/java/android/net/nsd/DnsSdServiceInfo.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import android.os.Parcelable;
2020
import android.os.Parcel;
2121

22+
import java.net.InetAddress;
23+
2224
/**
2325
* Defines a service based on DNS service discovery
2426
* {@hide}
@@ -27,20 +29,20 @@ public class DnsSdServiceInfo implements NetworkServiceInfo, Parcelable {
2729

2830
private String mServiceName;
2931

30-
private String mRegistrationType;
32+
private String mServiceType;
3133

3234
private DnsSdTxtRecord mTxtRecord;
3335

34-
private String mHostname;
36+
private InetAddress mHost;
3537

3638
private int mPort;
3739

38-
DnsSdServiceInfo() {
40+
public DnsSdServiceInfo() {
3941
}
4042

41-
DnsSdServiceInfo(String sn, String rt, DnsSdTxtRecord tr) {
43+
public DnsSdServiceInfo(String sn, String rt, DnsSdTxtRecord tr) {
4244
mServiceName = sn;
43-
mRegistrationType = rt;
45+
mServiceType = rt;
4446
mTxtRecord = tr;
4547
}
4648

@@ -59,13 +61,13 @@ public void setServiceName(String s) {
5961
@Override
6062
/** @hide */
6163
public String getServiceType() {
62-
return mRegistrationType;
64+
return mServiceType;
6365
}
6466

6567
@Override
6668
/** @hide */
6769
public void setServiceType(String s) {
68-
mRegistrationType = s;
70+
mServiceType = s;
6971
}
7072

7173
public DnsSdTxtRecord getTxtRecord() {
@@ -76,12 +78,12 @@ public void setTxtRecord(DnsSdTxtRecord t) {
7678
mTxtRecord = new DnsSdTxtRecord(t);
7779
}
7880

79-
public String getHostName() {
80-
return mHostname;
81+
public InetAddress getHost() {
82+
return mHost;
8183
}
8284

83-
public void setHostName(String s) {
84-
mHostname = s;
85+
public void setHost(InetAddress s) {
86+
mHost = s;
8587
}
8688

8789
public int getPort() {
@@ -96,7 +98,9 @@ public String toString() {
9698
StringBuffer sb = new StringBuffer();
9799

98100
sb.append("name: ").append(mServiceName).
99-
append("type: ").append(mRegistrationType).
101+
append("type: ").append(mServiceType).
102+
append("host: ").append(mHost).
103+
append("port: ").append(mPort).
100104
append("txtRecord: ").append(mTxtRecord);
101105
return sb.toString();
102106
}
@@ -109,9 +113,14 @@ public int describeContents() {
109113
/** Implement the Parcelable interface */
110114
public void writeToParcel(Parcel dest, int flags) {
111115
dest.writeString(mServiceName);
112-
dest.writeString(mRegistrationType);
116+
dest.writeString(mServiceType);
113117
dest.writeParcelable(mTxtRecord, flags);
114-
dest.writeString(mHostname);
118+
if (mHost != null) {
119+
dest.writeByte((byte)1);
120+
dest.writeByteArray(mHost.getAddress());
121+
} else {
122+
dest.writeByte((byte)0);
123+
}
115124
dest.writeInt(mPort);
116125
}
117126

@@ -121,9 +130,15 @@ public void writeToParcel(Parcel dest, int flags) {
121130
public DnsSdServiceInfo createFromParcel(Parcel in) {
122131
DnsSdServiceInfo info = new DnsSdServiceInfo();
123132
info.mServiceName = in.readString();
124-
info.mRegistrationType = in.readString();
133+
info.mServiceType = in.readString();
125134
info.mTxtRecord = in.readParcelable(null);
126-
info.mHostname = in.readString();
135+
136+
if (in.readByte() == 1) {
137+
try {
138+
info.mHost = InetAddress.getByAddress(in.createByteArray());
139+
} catch (java.net.UnknownHostException e) {}
140+
}
141+
127142
info.mPort = in.readInt();
128143
return info;
129144
}

core/java/android/net/nsd/NsdManager.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ public class NsdManager {
9393
/** @hide */
9494
public static final int RESOLVE_SERVICE_SUCCEEDED = BASE + 17;
9595

96+
/** @hide */
97+
public static final int STOP_RESOLVE = BASE + 18;
98+
/** @hide */
99+
public static final int STOP_RESOLVE_FAILED = BASE + 19;
100+
/** @hide */
101+
public static final int STOP_RESOLVE_SUCCEEDED = BASE + 20;
102+
103+
104+
96105
/**
97106
* Create a new Nsd instance. Applications use
98107
* {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
@@ -117,10 +126,23 @@ public NsdManager(INsdManager service) {
117126

118127
/**
119128
* Indicates that the operation failed because the framework is busy and
120-
* unable to service the request
129+
* unable to service the request.
121130
*/
122131
public static final int BUSY = 2;
123132

133+
/**
134+
* Indicates that the operation failed because it is already active.
135+
*/
136+
public static final int ALREADY_ACTIVE = 3;
137+
138+
/**
139+
* Indicates that the operation failed because maximum limit on
140+
* service registrations has reached.
141+
*/
142+
public static final int MAX_REGS_REACHED = 4;
143+
144+
145+
124146
/** Interface for callback invocation when framework channel is connected or lost */
125147
public interface ChannelListener {
126148
public void onChannelConnected(Channel c);
@@ -188,6 +210,7 @@ public static class Channel {
188210
private DnsSdRegisterListener mDnsSdRegisterListener;
189211
private DnsSdUpdateRegistrationListener mDnsSdUpdateListener;
190212
private DnsSdResolveListener mDnsSdResolveListener;
213+
private ActionListener mDnsSdStopResolveListener;
191214

192215
AsyncChannel mAsyncChannel;
193216
ServiceHandler mHandler;
@@ -278,6 +301,16 @@ public void handleMessage(Message message) {
278301
(DnsSdServiceInfo) message.obj);
279302
}
280303
break;
304+
case STOP_RESOLVE_FAILED:
305+
if (mDnsSdStopResolveListener!= null) {
306+
mDnsSdStopResolveListener.onFailure(message.arg1);
307+
}
308+
break;
309+
case STOP_RESOLVE_SUCCEEDED:
310+
if (mDnsSdStopResolveListener != null) {
311+
mDnsSdStopResolveListener.onSuccess();
312+
}
313+
break;
281314
default:
282315
Log.d(TAG, "Ignored " + message);
283316
break;
@@ -345,6 +378,14 @@ public void setResolveListener(Channel c, DnsSdResolveListener b) {
345378
c.mDnsSdResolveListener = b;
346379
}
347380

381+
/**
382+
* Set the listener for stopping service resolution. Can be null.
383+
*/
384+
public void setStopResolveListener(Channel c, ActionListener b) {
385+
if (c == null) throw new IllegalArgumentException("Channel needs to be initialized");
386+
c.mDnsSdStopResolveListener = b;
387+
}
388+
348389
public void registerService(Channel c, DnsSdServiceInfo serviceInfo) {
349390
if (c == null) throw new IllegalArgumentException("Channel needs to be initialized");
350391
if (serviceInfo == null) throw new IllegalArgumentException("Null serviceInfo");
@@ -378,6 +419,13 @@ public void resolveService(Channel c, DnsSdServiceInfo serviceInfo) {
378419
c.mAsyncChannel.sendMessage(RESOLVE_SERVICE, serviceInfo);
379420
}
380421

422+
public void stopServiceResolve(Channel c) {
423+
if (c == null) throw new IllegalArgumentException("Channel needs to be initialized");
424+
if (c.mDnsSdResolveListener == null) throw new
425+
IllegalStateException("Resolve listener needs to be set first");
426+
c.mAsyncChannel.sendMessage(STOP_RESOLVE);
427+
}
428+
381429
/**
382430
* Get a reference to NetworkService handler. This is used to establish
383431
* an AsyncChannel communication with the service

0 commit comments

Comments
 (0)