Merge changes I987ab866,I73fa9ca7

* changes:
  Drop dot at the end of service type
  Implement the stop resolution with MdnsDiscoveryManager
This commit is contained in:
Paul Hu
2023-03-03 12:56:38 +00:00
committed by Gerrit Code Review
2 changed files with 102 additions and 44 deletions

View File

@@ -525,31 +525,6 @@ public class NsdService extends INsdManager.Stub {
clientInfo.mClientIdForServiceUpdates = 0;
}
/**
* Check the given service type is valid and construct it to a service type
* which can use for discovery / resolution service.
*
* <p> The valid service type should be 2 labels, or 3 labels if the query is for a
* subtype (see RFC6763 7.1). Each label is up to 63 characters and must start with an
* underscore; they are alphanumerical characters or dashes or underscore, except the
* last one that is just alphanumerical. The last label must be _tcp or _udp.
*
* @param serviceType the request service type for discovery / resolution service
* @return constructed service type or null if the given service type is invalid.
*/
@Nullable
private String constructServiceType(String serviceType) {
if (TextUtils.isEmpty(serviceType)) return null;
final Pattern serviceTypePattern = Pattern.compile(
"^(_[a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]\\.)?"
+ "(_[a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]\\._(?:tcp|udp))$");
final Matcher matcher = serviceTypePattern.matcher(serviceType);
if (!matcher.matches()) return null;
return matcher.group(1) == null
? serviceType
: matcher.group(1) + "_sub." + matcher.group(2);
}
/**
* Truncate a service name to up to 63 UTF-8 bytes.
@@ -574,6 +549,12 @@ public class NsdService extends INsdManager.Stub {
return new String(out.array(), 0, out.position(), utf8);
}
private void stopDiscoveryManagerRequest(ClientRequest request, int clientId, int id,
ClientInfo clientInfo) {
clientInfo.unregisterMdnsListenerFromRequest(request);
removeRequestMap(clientId, id, clientInfo);
}
@Override
public boolean processMessage(Message msg) {
final ClientInfo clientInfo;
@@ -661,11 +642,7 @@ public class NsdService extends INsdManager.Stub {
// point, so this needs to check the type of the original request to
// unregister instead of looking at the flag value.
if (request instanceof DiscoveryManagerRequest) {
final MdnsListener listener =
((DiscoveryManagerRequest) request).mListener;
mMdnsDiscoveryManager.unregisterListener(
listener.getListenedServiceType(), listener);
removeRequestMap(clientId, id, clientInfo);
stopDiscoveryManagerRequest(request, clientId, id, clientInfo);
clientInfo.onStopDiscoverySucceeded(clientId);
} else {
removeRequestMap(clientId, id, clientInfo);
@@ -836,15 +813,22 @@ public class NsdService extends INsdManager.Stub {
break;
}
id = request.mGlobalId;
removeRequestMap(clientId, id, clientInfo);
if (stopResolveService(id)) {
// Note isMdnsDiscoveryManagerEnabled may have changed to false at this
// point, so this needs to check the type of the original request to
// unregister instead of looking at the flag value.
if (request instanceof DiscoveryManagerRequest) {
stopDiscoveryManagerRequest(request, clientId, id, clientInfo);
clientInfo.onStopResolutionSucceeded(clientId);
} else {
clientInfo.onStopResolutionFailed(
clientId, NsdManager.FAILURE_OPERATION_NOT_RUNNING);
removeRequestMap(clientId, id, clientInfo);
if (stopResolveService(id)) {
clientInfo.onStopResolutionSucceeded(clientId);
} else {
clientInfo.onStopResolutionFailed(
clientId, NsdManager.FAILURE_OPERATION_NOT_RUNNING);
}
clientInfo.mResolvedService = null;
}
clientInfo.mResolvedService = null;
// TODO: Implement the stop resolution with MdnsDiscoveryManager.
break;
}
case NsdManager.REGISTER_SERVICE_CALLBACK:
@@ -1197,10 +1181,7 @@ public class NsdService extends INsdManager.Stub {
Log.wtf(TAG, "non-DiscoveryManager request in DiscoveryManager event");
break;
}
final MdnsListener listener = ((DiscoveryManagerRequest) request).mListener;
mMdnsDiscoveryManager.unregisterListener(
listener.getListenedServiceType(), listener);
removeRequestMap(clientId, transactionId, clientInfo);
stopDiscoveryManagerRequest(request, clientId, transactionId, clientInfo);
break;
}
default:
@@ -1260,6 +1241,34 @@ public class NsdService extends INsdManager.Stub {
return sb.toString();
}
/**
* Check the given service type is valid and construct it to a service type
* which can use for discovery / resolution service.
*
* <p> The valid service type should be 2 labels, or 3 labels if the query is for a
* subtype (see RFC6763 7.1). Each label is up to 63 characters and must start with an
* underscore; they are alphanumerical characters or dashes or underscore, except the
* last one that is just alphanumerical. The last label must be _tcp or _udp.
*
* @param serviceType the request service type for discovery / resolution service
* @return constructed service type or null if the given service type is invalid.
*/
@Nullable
public static String constructServiceType(String serviceType) {
if (TextUtils.isEmpty(serviceType)) return null;
final Pattern serviceTypePattern = Pattern.compile(
"^(_[a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]\\.)?"
+ "(_[a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]\\._(?:tcp|udp))"
// Drop '.' at the end of service type that is compatible with old backend.
+ "\\.?$");
final Matcher matcher = serviceTypePattern.matcher(serviceType);
if (!matcher.matches()) return null;
return matcher.group(1) == null
? matcher.group(2)
: matcher.group(1) + "_sub." + matcher.group(2);
}
@VisibleForTesting
NsdService(Context ctx, Handler handler, long cleanupDelayMs) {
this(ctx, handler, cleanupDelayMs, new Dependencies());
@@ -1780,6 +1789,13 @@ public class NsdService extends INsdManager.Stub {
mIsPreSClient = true;
}
private void unregisterMdnsListenerFromRequest(ClientRequest request) {
final MdnsListener listener =
((DiscoveryManagerRequest) request).mListener;
mMdnsDiscoveryManager.unregisterListener(
listener.getListenedServiceType(), listener);
}
// Remove any pending requests from the global map when we get rid of a client,
// and send cancellations to the daemon.
private void expungeAllRequests() {
@@ -1795,10 +1811,7 @@ public class NsdService extends INsdManager.Stub {
}
if (request instanceof DiscoveryManagerRequest) {
final MdnsListener listener =
((DiscoveryManagerRequest) request).mListener;
mMdnsDiscoveryManager.unregisterListener(
listener.getListenedServiceType(), listener);
unregisterMdnsListenerFromRequest(request);
continue;
}