Merge "Share NetworkInterfaceProvider between MdnsSockets" into main
This commit is contained in:
commit
df0a16ccf4
3 changed files with 20 additions and 5 deletions
|
|
@ -58,7 +58,6 @@ public class MdnsSocket {
|
|||
MdnsSocket(@NonNull MulticastNetworkInterfaceProvider multicastNetworkInterfaceProvider,
|
||||
MulticastSocket multicastSocket, SharedLog sharedLog) throws IOException {
|
||||
this.multicastNetworkInterfaceProvider = multicastNetworkInterfaceProvider;
|
||||
this.multicastNetworkInterfaceProvider.startWatchingConnectivityChanges();
|
||||
this.multicastSocket = multicastSocket;
|
||||
this.sharedLog = sharedLog;
|
||||
// RFC Spec: https://tools.ietf.org/html/rfc6762
|
||||
|
|
@ -120,7 +119,6 @@ public class MdnsSocket {
|
|||
public void close() {
|
||||
// This is a race with the use of the file descriptor (b/27403984).
|
||||
multicastSocket.close();
|
||||
multicastNetworkInterfaceProvider.stopWatchingConnectivityChanges();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -106,6 +106,7 @@ public class MdnsSocketClient implements MdnsSocketClientBase {
|
|||
@Nullable private Timer checkMulticastResponseTimer;
|
||||
private final SharedLog sharedLog;
|
||||
@NonNull private final MdnsFeatureFlags mdnsFeatureFlags;
|
||||
private final MulticastNetworkInterfaceProvider interfaceProvider;
|
||||
|
||||
public MdnsSocketClient(@NonNull Context context, @NonNull MulticastLock multicastLock,
|
||||
SharedLog sharedLog, @NonNull MdnsFeatureFlags mdnsFeatureFlags) {
|
||||
|
|
@ -118,6 +119,7 @@ public class MdnsSocketClient implements MdnsSocketClientBase {
|
|||
unicastReceiverBuffer = null;
|
||||
}
|
||||
this.mdnsFeatureFlags = mdnsFeatureFlags;
|
||||
this.interfaceProvider = new MulticastNetworkInterfaceProvider(context, sharedLog);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -138,6 +140,7 @@ public class MdnsSocketClient implements MdnsSocketClientBase {
|
|||
cannotReceiveMulticastResponse.set(false);
|
||||
|
||||
shouldStopSocketLoop = false;
|
||||
interfaceProvider.startWatchingConnectivityChanges();
|
||||
try {
|
||||
// TODO (changed when importing code): consider setting thread stats tag
|
||||
multicastSocket = createMdnsSocket(MdnsConstants.MDNS_PORT, sharedLog);
|
||||
|
|
@ -183,6 +186,7 @@ public class MdnsSocketClient implements MdnsSocketClientBase {
|
|||
}
|
||||
|
||||
multicastLock.release();
|
||||
interfaceProvider.stopWatchingConnectivityChanges();
|
||||
|
||||
shouldStopSocketLoop = true;
|
||||
waitForSendThreadToStop();
|
||||
|
|
@ -482,8 +486,7 @@ public class MdnsSocketClient implements MdnsSocketClientBase {
|
|||
|
||||
@VisibleForTesting
|
||||
MdnsSocket createMdnsSocket(int port, SharedLog sharedLog) throws IOException {
|
||||
return new MdnsSocket(new MulticastNetworkInterfaceProvider(context, sharedLog), port,
|
||||
sharedLog);
|
||||
return new MdnsSocket(interfaceProvider, port, sharedLog);
|
||||
}
|
||||
|
||||
private void sendPackets(List<DatagramPacket> packets, MdnsSocket socket) {
|
||||
|
|
|
|||
|
|
@ -26,14 +26,17 @@ import static org.mockito.ArgumentMatchers.anyInt;
|
|||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.Manifest.permission;
|
||||
import android.annotation.RequiresPermission;
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.net.wifi.WifiManager.MulticastLock;
|
||||
import android.text.format.DateUtils;
|
||||
|
|
@ -48,6 +51,7 @@ import org.junit.Before;
|
|||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.ArgumentMatchers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
|
@ -71,6 +75,7 @@ public class MdnsSocketClientTests {
|
|||
|
||||
@Mock private Context mContext;
|
||||
@Mock private WifiManager mockWifiManager;
|
||||
@Mock private ConnectivityManager mockConnectivityManager;
|
||||
@Mock private MdnsSocket mockMulticastSocket;
|
||||
@Mock private MdnsSocket mockUnicastSocket;
|
||||
@Mock private MulticastLock mockMulticastLock;
|
||||
|
|
@ -84,6 +89,9 @@ public class MdnsSocketClientTests {
|
|||
public void setup() throws RuntimeException, IOException {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
doReturn(mockConnectivityManager).when(mContext).getSystemService(
|
||||
Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
when(mockWifiManager.createMulticastLock(ArgumentMatchers.anyString()))
|
||||
.thenReturn(mockMulticastLock);
|
||||
|
||||
|
|
@ -320,19 +328,25 @@ public class MdnsSocketClientTests {
|
|||
|
||||
@Test
|
||||
public void testStartStop() throws IOException {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
mdnsClient.startDiscovery();
|
||||
|
||||
Thread multicastReceiverThread = mdnsClient.multicastReceiveThread;
|
||||
Thread socketThread = mdnsClient.sendThread;
|
||||
final ArgumentCaptor<ConnectivityManager.NetworkCallback> cbCaptor =
|
||||
ArgumentCaptor.forClass(ConnectivityManager.NetworkCallback.class);
|
||||
|
||||
assertTrue(multicastReceiverThread.isAlive());
|
||||
assertTrue(socketThread.isAlive());
|
||||
verify(mockConnectivityManager, times(i))
|
||||
.registerNetworkCallback(any(), cbCaptor.capture());
|
||||
|
||||
mdnsClient.stopDiscovery();
|
||||
|
||||
assertFalse(multicastReceiverThread.isAlive());
|
||||
assertFalse(socketThread.isAlive());
|
||||
verify(mockConnectivityManager, times(i))
|
||||
.unregisterNetworkCallback(cbCaptor.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue