Fix self discovery
Check address on broadcast reception and use only ipv4
This commit is contained in:
父節點
3b9f4f0a41
當前提交
c4b2b55887
共有 2 個文件被更改,包括 38 次插入 和 8 次删除
|
@ -7,6 +7,8 @@ import org.jetbrains.annotations.Nullable;
|
|||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
|
||||
import static fr.insa.clavardator.network.NetUtil.isLocalAddress;
|
||||
|
||||
public class NetDiscoverer {
|
||||
private static final short DISCOVERY_PORT = 31593;
|
||||
private static final short RESPONSE_PORT = 31594;
|
||||
|
@ -51,10 +53,20 @@ public class NetDiscoverer {
|
|||
broadcastListener.stopListening();
|
||||
|
||||
broadcastListener = new BroadcastListener((ipAddr, data) -> {
|
||||
if (onBroadcastReceived != null)
|
||||
onBroadcastReceived.onBroadcastReceived(ipAddr, data);
|
||||
responseSender = new ResponseSender(ipAddr, responseMessage, errorCallback);
|
||||
responseSender.start();
|
||||
try {
|
||||
if (!isLocalAddress(ipAddr)) {
|
||||
if (onBroadcastReceived != null)
|
||||
onBroadcastReceived.onBroadcastReceived(ipAddr, data);
|
||||
responseSender = new ResponseSender(ipAddr, responseMessage, errorCallback);
|
||||
responseSender.start();
|
||||
} else {
|
||||
Log.w(this.getClass().getSimpleName(), "Received broadcast from self");
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
Log.e(this.getClass().getSimpleName(), "Error getting local adresses", e);
|
||||
if (errorCallback != null)
|
||||
errorCallback.onError(e);
|
||||
}
|
||||
}, e -> {
|
||||
if (errorCallback != null)
|
||||
errorCallback.onError(e);
|
||||
|
|
|
@ -1,15 +1,20 @@
|
|||
package fr.insa.clavardator.network;
|
||||
|
||||
import fr.insa.clavardator.util.Log;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
|
||||
public class NetUtil {
|
||||
/**
|
||||
* Lists all ipv4 broadcast addresses
|
||||
*
|
||||
* @return
|
||||
* @throws SocketException
|
||||
*/
|
||||
public static List<InetAddress> listAllBroadcastAddresses() throws SocketException {
|
||||
List<InetAddress> broadcastList = new ArrayList<>();
|
||||
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||
|
@ -26,6 +31,12 @@ public class NetUtil {
|
|||
return broadcastList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all local Ipv4 addresses
|
||||
*
|
||||
* @return
|
||||
* @throws SocketException
|
||||
*/
|
||||
public static List<InetAddress> listAllLocalAddresses() throws SocketException {
|
||||
List<InetAddress> localList = new ArrayList<>();
|
||||
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||
|
@ -34,14 +45,21 @@ public class NetUtil {
|
|||
NetworkInterface networkInterface = interfaces.nextElement();
|
||||
if (!networkInterface.isLoopback() && networkInterface.isUp()) {
|
||||
networkInterface.getInterfaceAddresses().stream()
|
||||
.filter((i) -> Objects.nonNull(i.getBroadcast()))
|
||||
.map(InterfaceAddress::getAddress)
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(localList::add);
|
||||
}
|
||||
}
|
||||
Log.v("NetUtil", "Local addresses: " + Arrays.toString(new List[]{localList}));
|
||||
return localList;
|
||||
}
|
||||
|
||||
public static boolean isLocalAddress(InetAddress address) throws SocketException {
|
||||
final List<InetAddress> list = listAllLocalAddresses();
|
||||
return list.stream().anyMatch((a) -> Arrays.equals(address.getAddress(), a.getAddress()));
|
||||
}
|
||||
|
||||
public static int getIdFromIp(InetAddress ipAddr) {
|
||||
byte[] addr = ipAddr.getAddress();
|
||||
int id = 0;
|
||||
|
|
載入中…
Reference in a new issue