Support discover/resolve on specific networks

This adds a Network member to NsdServiceInfo, allowing discovered
services to report which network they were discovered on, and clients to
specify which network to resolve the service on. If clients use the
discovered NsdServiceInfo to resolve a service, it will be resolved on
the network where it was discovered instead of an unspecified network.

Also add a network parameter to a new overload of
NsdManager#discoverServices, so that clients can discover on specific
networks.

Bug: 190249673
Test: atest NsdManagerTest

Change-Id: Idc4bf9fde0f4b0328204a8cd2eedc12fffbbbdba
This commit is contained in:
Remi NGUYEN VAN
2021-12-16 15:31:16 +09:00
parent 271548c96d
commit bca7618ded
3 changed files with 167 additions and 22 deletions

View File

@@ -16,6 +16,8 @@
package android.net.nsd;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemService;
@@ -23,6 +25,7 @@ import android.app.compat.CompatChanges;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
import android.content.Context;
import android.net.Network;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
@@ -633,6 +636,14 @@ public final class NsdManager {
}
}
/**
* Same as {@link #discoverServices(String, int, Network, DiscoveryListener)} with a null
* {@link Network}.
*/
public void discoverServices(String serviceType, int protocolType, DiscoveryListener listener) {
discoverServices(serviceType, protocolType, null, listener);
}
/**
* Initiate service discovery to browse for instances of a service type. Service discovery
* consumes network bandwidth and will continue until the application calls
@@ -657,11 +668,13 @@ public final class NsdManager {
* @param serviceType The service type being discovered. Examples include "_http._tcp" for
* http services or "_ipp._tcp" for printers
* @param protocolType The service discovery protocol
* @param network Network to discover services on, or null to discover on all available networks
* @param listener The listener notifies of a successful discovery and is used
* to stop discovery on this serviceType through a call on {@link #stopServiceDiscovery}.
* Cannot be null. Cannot be in use for an active service discovery.
*/
public void discoverServices(String serviceType, int protocolType, DiscoveryListener listener) {
public void discoverServices(@NonNull String serviceType, int protocolType,
@Nullable Network network, @NonNull DiscoveryListener listener) {
if (TextUtils.isEmpty(serviceType)) {
throw new IllegalArgumentException("Service type cannot be empty");
}
@@ -669,6 +682,7 @@ public final class NsdManager {
NsdServiceInfo s = new NsdServiceInfo();
s.setServiceType(serviceType);
s.setNetwork(network);
int key = putListener(listener, s);
try {

View File

@@ -17,7 +17,9 @@
package android.net.nsd;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
import android.net.Network;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
@@ -49,6 +51,9 @@ public final class NsdServiceInfo implements Parcelable {
private int mPort;
@Nullable
private Network mNetwork;
public NsdServiceInfo() {
}
@@ -307,18 +312,37 @@ public final class NsdServiceInfo implements Parcelable {
return txtRecord;
}
public String toString() {
StringBuffer sb = new StringBuffer();
/**
* Get the network where the service can be found.
*
* This is never null if this {@link NsdServiceInfo} was obtained from
* {@link NsdManager#discoverServices} or {@link NsdManager#resolveService}.
*/
@Nullable
public Network getNetwork() {
return mNetwork;
}
/**
* Set the network where the service can be found.
* @param network The network, or null to search for, or to announce, the service on all
* connected networks.
*/
public void setNetwork(@Nullable Network network) {
mNetwork = network;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("name: ").append(mServiceName)
.append(", type: ").append(mServiceType)
.append(", host: ").append(mHost)
.append(", port: ").append(mPort);
.append(", port: ").append(mPort)
.append(", network: ").append(mNetwork);
byte[] txtRecord = getTxtRecord();
if (txtRecord != null) {
sb.append(", txtRecord: ").append(new String(txtRecord, StandardCharsets.UTF_8));
}
sb.append(", txtRecord: ").append(new String(txtRecord, StandardCharsets.UTF_8));
return sb.toString();
}
@@ -352,6 +376,8 @@ public final class NsdServiceInfo implements Parcelable {
}
dest.writeString(key);
}
dest.writeParcelable(mNetwork, 0);
}
/** Implement the Parcelable interface */
@@ -381,6 +407,7 @@ public final class NsdServiceInfo implements Parcelable {
}
info.mTxtRecord.put(in.readString(), valueArray);
}
info.mNetwork = in.readParcelable(null, Network.class);
return info;
}