Merge "Send OffloadServiceInfos when registerOffloadEngine is called" into main
This commit is contained in:
@@ -1191,7 +1191,7 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
// TODO: Limits the number of registrations created by a given class.
|
// TODO: Limits the number of registrations created by a given class.
|
||||||
mOffloadEngines.register(offloadEngineInfo.mOffloadEngine,
|
mOffloadEngines.register(offloadEngineInfo.mOffloadEngine,
|
||||||
offloadEngineInfo);
|
offloadEngineInfo);
|
||||||
// TODO: Sends all the existing OffloadServiceInfos back.
|
sendAllOffloadServiceInfos(offloadEngineInfo);
|
||||||
break;
|
break;
|
||||||
case NsdManager.UNREGISTER_OFFLOAD_ENGINE:
|
case NsdManager.UNREGISTER_OFFLOAD_ENGINE:
|
||||||
mOffloadEngines.unregister((IOffloadEngine) msg.obj);
|
mOffloadEngines.unregister((IOffloadEngine) msg.obj);
|
||||||
@@ -1877,6 +1877,21 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendAllOffloadServiceInfos(@NonNull OffloadEngineInfo offloadEngineInfo) {
|
||||||
|
final String targetInterface = offloadEngineInfo.mInterfaceName;
|
||||||
|
final IOffloadEngine offloadEngine = offloadEngineInfo.mOffloadEngine;
|
||||||
|
final List<MdnsAdvertiser.OffloadServiceInfoWrapper> offloadWrappers =
|
||||||
|
mAdvertiser.getAllInterfaceOffloadServiceInfos(targetInterface);
|
||||||
|
for (MdnsAdvertiser.OffloadServiceInfoWrapper wrapper : offloadWrappers) {
|
||||||
|
try {
|
||||||
|
offloadEngine.onOffloadServiceUpdated(wrapper.mOffloadServiceInfo);
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
// Can happen in regular cases, do not log a stacktrace
|
||||||
|
Log.i(TAG, "Failed to send offload callback, remote died: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void sendOffloadServiceInfosUpdate(@NonNull String targetInterfaceName,
|
private void sendOffloadServiceInfosUpdate(@NonNull String targetInterfaceName,
|
||||||
@NonNull OffloadServiceInfo offloadServiceInfo, boolean isRemove) {
|
@NonNull OffloadServiceInfo offloadServiceInfo, boolean isRemove) {
|
||||||
final int count = mOffloadEngines.beginBroadcast();
|
final int count = mOffloadEngines.beginBroadcast();
|
||||||
@@ -1900,7 +1915,7 @@ public class NsdService extends INsdManager.Stub {
|
|||||||
}
|
}
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
// Can happen in regular cases, do not log a stacktrace
|
// Can happen in regular cases, do not log a stacktrace
|
||||||
Log.i(TAG, "Failed to send offload callback, remote died", e);
|
Log.i(TAG, "Failed to send offload callback, remote died: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import com.android.net.module.util.SharedLog;
|
|||||||
import com.android.server.connectivity.mdns.util.MdnsUtils;
|
import com.android.server.connectivity.mdns.util.MdnsUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@@ -117,6 +118,17 @@ public class MdnsAdvertiser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the current status of the OffloadServiceInfos per interface.
|
||||||
|
* @param interfaceName the target interfaceName
|
||||||
|
* @return the list of current offloaded services.
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
public List<OffloadServiceInfoWrapper> getAllInterfaceOffloadServiceInfos(
|
||||||
|
@NonNull String interfaceName) {
|
||||||
|
return mInterfaceOffloadServices.getOrDefault(interfaceName, Collections.emptyList());
|
||||||
|
}
|
||||||
|
|
||||||
private final MdnsInterfaceAdvertiser.Callback mInterfaceAdvertiserCb =
|
private final MdnsInterfaceAdvertiser.Callback mInterfaceAdvertiserCb =
|
||||||
new MdnsInterfaceAdvertiser.Callback() {
|
new MdnsInterfaceAdvertiser.Callback() {
|
||||||
@Override
|
@Override
|
||||||
@@ -385,9 +397,12 @@ public class MdnsAdvertiser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class OffloadServiceInfoWrapper {
|
/**
|
||||||
private final @NonNull OffloadServiceInfo mOffloadServiceInfo;
|
* The wrapper class for OffloadServiceInfo including the serviceId.
|
||||||
private final int mServiceId;
|
*/
|
||||||
|
public static class OffloadServiceInfoWrapper {
|
||||||
|
public final @NonNull OffloadServiceInfo mOffloadServiceInfo;
|
||||||
|
public final int mServiceId;
|
||||||
|
|
||||||
OffloadServiceInfoWrapper(int serviceId, OffloadServiceInfo offloadServiceInfo) {
|
OffloadServiceInfoWrapper(int serviceId, OffloadServiceInfo offloadServiceInfo) {
|
||||||
mOffloadServiceInfo = offloadServiceInfo;
|
mOffloadServiceInfo = offloadServiceInfo;
|
||||||
|
|||||||
@@ -163,6 +163,7 @@ class NsdManagerTest {
|
|||||||
|
|
||||||
private val cm by lazy { context.getSystemService(ConnectivityManager::class.java)!! }
|
private val cm by lazy { context.getSystemService(ConnectivityManager::class.java)!! }
|
||||||
private val serviceName = "NsdTest%09d".format(Random().nextInt(1_000_000_000))
|
private val serviceName = "NsdTest%09d".format(Random().nextInt(1_000_000_000))
|
||||||
|
private val serviceName2 = "NsdTest%09d".format(Random().nextInt(1_000_000_000))
|
||||||
private val serviceType = "_nmt%09d._tcp".format(Random().nextInt(1_000_000_000))
|
private val serviceType = "_nmt%09d._tcp".format(Random().nextInt(1_000_000_000))
|
||||||
private val handlerThread = HandlerThread(NsdManagerTest::class.java.simpleName)
|
private val handlerThread = HandlerThread(NsdManagerTest::class.java.simpleName)
|
||||||
private val ctsNetUtils by lazy{ CtsNetUtils(context) }
|
private val ctsNetUtils by lazy{ CtsNetUtils(context) }
|
||||||
@@ -890,10 +891,11 @@ class NsdManagerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun checkOffloadServiceInfo(serviceInfo: OffloadServiceInfo) {
|
fun checkOffloadServiceInfo(serviceInfo: OffloadServiceInfo, si: NsdServiceInfo) {
|
||||||
assertEquals(serviceName, serviceInfo.key.serviceName)
|
val expectedServiceType = si.serviceType.split(",")[0]
|
||||||
assertEquals(serviceType, serviceInfo.key.serviceType)
|
assertEquals(si.serviceName, serviceInfo.key.serviceName)
|
||||||
assertEquals(listOf<String>("_subtype"), serviceInfo.subtypes)
|
assertEquals(expectedServiceType, serviceInfo.key.serviceType)
|
||||||
|
assertEquals(listOf("_subtype"), serviceInfo.subtypes)
|
||||||
assertTrue(serviceInfo.hostname.startsWith("Android_"))
|
assertTrue(serviceInfo.hostname.startsWith("Android_"))
|
||||||
assertTrue(serviceInfo.hostname.endsWith("local"))
|
assertTrue(serviceInfo.hostname.endsWith("local"))
|
||||||
assertEquals(0, serviceInfo.priority)
|
assertEquals(0, serviceInfo.priority)
|
||||||
@@ -907,36 +909,63 @@ class NsdManagerTest {
|
|||||||
// The offload callbacks are only supported with the new backend,
|
// The offload callbacks are only supported with the new backend,
|
||||||
// enabled with target SDK U+.
|
// enabled with target SDK U+.
|
||||||
assumeTrue(isAtLeastU() || targetSdkVersion > Build.VERSION_CODES.TIRAMISU)
|
assumeTrue(isAtLeastU() || targetSdkVersion > Build.VERSION_CODES.TIRAMISU)
|
||||||
|
|
||||||
|
// TODO: also have a test that use an executor that runs in a different thread, and pass
|
||||||
|
// in the thread ID NsdServiceInfo to check it
|
||||||
|
val si1 = NsdServiceInfo()
|
||||||
|
si1.serviceType = "$serviceType,_subtype"
|
||||||
|
si1.serviceName = serviceName
|
||||||
|
si1.network = testNetwork1.network
|
||||||
|
si1.port = 23456
|
||||||
|
val record1 = NsdRegistrationRecord()
|
||||||
|
|
||||||
|
val si2 = NsdServiceInfo()
|
||||||
|
si2.serviceType = "$serviceType,_subtype"
|
||||||
|
si2.serviceName = serviceName2
|
||||||
|
si2.network = testNetwork1.network
|
||||||
|
si2.port = 12345
|
||||||
|
val record2 = NsdRegistrationRecord()
|
||||||
val offloadEngine = TestNsdOffloadEngine()
|
val offloadEngine = TestNsdOffloadEngine()
|
||||||
runAsShell(NETWORK_SETTINGS) {
|
|
||||||
nsdManager.registerOffloadEngine(testNetwork1.iface.interfaceName,
|
|
||||||
OffloadEngine.OFFLOAD_TYPE_REPLY.toLong(),
|
|
||||||
OffloadEngine.OFFLOAD_CAPABILITY_BYPASS_MULTICAST_LOCK.toLong(),
|
|
||||||
{ it.run() }, offloadEngine)
|
|
||||||
}
|
|
||||||
|
|
||||||
val si = NsdServiceInfo()
|
tryTest {
|
||||||
si.serviceType = "$serviceType,_subtype"
|
// Register service before the OffloadEngine is registered.
|
||||||
si.serviceName = serviceName
|
nsdManager.registerService(si1, NsdManager.PROTOCOL_DNS_SD, record1)
|
||||||
si.network = testNetwork1.network
|
record1.expectCallback<ServiceRegistered>()
|
||||||
si.port = 12345
|
runAsShell(NETWORK_SETTINGS) {
|
||||||
val record = NsdRegistrationRecord()
|
nsdManager.registerOffloadEngine(testNetwork1.iface.interfaceName,
|
||||||
nsdManager.registerService(si, NsdManager.PROTOCOL_DNS_SD, record)
|
OffloadEngine.OFFLOAD_TYPE_REPLY.toLong(),
|
||||||
val addOrUpdateEvent = offloadEngine
|
OffloadEngine.OFFLOAD_CAPABILITY_BYPASS_MULTICAST_LOCK.toLong(),
|
||||||
.expectCallbackEventually<TestNsdOffloadEngine.OffloadEvent.AddOrUpdateEvent> {
|
{ it.run() }, offloadEngine)
|
||||||
it.info.key.serviceName == serviceName
|
|
||||||
}
|
}
|
||||||
checkOffloadServiceInfo(addOrUpdateEvent.info)
|
val addOrUpdateEvent1 = offloadEngine
|
||||||
|
.expectCallbackEventually<TestNsdOffloadEngine.OffloadEvent.AddOrUpdateEvent> {
|
||||||
|
it.info.key.serviceName == si1.serviceName
|
||||||
|
}
|
||||||
|
checkOffloadServiceInfo(addOrUpdateEvent1.info, si1)
|
||||||
|
|
||||||
nsdManager.unregisterService(record)
|
// Register service after OffloadEngine is registered.
|
||||||
val unregisterEvent = offloadEngine
|
nsdManager.registerService(si2, NsdManager.PROTOCOL_DNS_SD, record2)
|
||||||
.expectCallbackEventually<TestNsdOffloadEngine.OffloadEvent.RemoveEvent> {
|
record2.expectCallback<ServiceRegistered>()
|
||||||
it.info.key.serviceName == serviceName
|
val addOrUpdateEvent2 = offloadEngine
|
||||||
|
.expectCallbackEventually<TestNsdOffloadEngine.OffloadEvent.AddOrUpdateEvent> {
|
||||||
|
it.info.key.serviceName == si2.serviceName
|
||||||
|
}
|
||||||
|
checkOffloadServiceInfo(addOrUpdateEvent2.info, si2)
|
||||||
|
|
||||||
|
nsdManager.unregisterService(record2)
|
||||||
|
record2.expectCallback<ServiceUnregistered>()
|
||||||
|
val unregisterEvent = offloadEngine
|
||||||
|
.expectCallbackEventually<TestNsdOffloadEngine.OffloadEvent.RemoveEvent> {
|
||||||
|
it.info.key.serviceName == si2.serviceName
|
||||||
|
}
|
||||||
|
checkOffloadServiceInfo(unregisterEvent.info, si2)
|
||||||
|
} cleanupStep {
|
||||||
|
runAsShell(NETWORK_SETTINGS) {
|
||||||
|
nsdManager.unregisterOffloadEngine(offloadEngine)
|
||||||
}
|
}
|
||||||
checkOffloadServiceInfo(unregisterEvent.info)
|
} cleanup {
|
||||||
|
nsdManager.unregisterService(record1)
|
||||||
runAsShell(NETWORK_SETTINGS) {
|
record1.expectCallback<ServiceUnregistered>()
|
||||||
nsdManager.unregisterOffloadEngine(offloadEngine)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user