KLALB3.2
This commit is contained in:
@@ -14,6 +14,7 @@ import java.net.NetworkInterface;
|
||||
import java.net.NoRouteToHostException;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.time.Duration;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
@@ -31,6 +32,15 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
private List<MultipurposeSocketAddress> msas;
|
||||
|
||||
private volatile Consumer<MultipurposeSocketAddress> con;
|
||||
private long timeInterval;
|
||||
|
||||
public long getTimeInterval() {
|
||||
return timeInterval;
|
||||
}
|
||||
|
||||
public void setTimeInterval(long timeInterval) {
|
||||
this.timeInterval = timeInterval;
|
||||
}
|
||||
|
||||
public NetworkInterface getNinterface() {
|
||||
return ninterface;
|
||||
@@ -53,13 +63,14 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
}
|
||||
|
||||
public IPMulticastDiscovery(InetSocketAddress bind, InetSocketAddress group, NetworkInterface ninterface,
|
||||
List<MultipurposeSocketAddress> msas) throws IOException {
|
||||
List<MultipurposeSocketAddress> msas,long timeInterval) throws IOException {
|
||||
soc = new MulticastSocket(bind);
|
||||
soc.joinGroup(group, ninterface);
|
||||
this.bind = bind;
|
||||
this.group = group;
|
||||
this.ninterface = ninterface;
|
||||
this.msas = msas;
|
||||
this.timeInterval=timeInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -104,7 +115,7 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
|
||||
}
|
||||
|
||||
Thread.sleep(10000L);
|
||||
Thread.sleep(timeInterval);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -133,11 +144,13 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
dis.close();
|
||||
MultipurposeSocketAddress mpsa = new MultipurposeSocketAddress(type, dp.getAddress().getHostAddress(),
|
||||
port);
|
||||
try {
|
||||
InetAddress mpsai=mpsa.getInetAddress();
|
||||
//System.out.println(mpsa);
|
||||
Enumeration<InetAddress> ei = ninterface.getInetAddresses();
|
||||
while (ei.hasMoreElements()) {
|
||||
InetAddress inetAddress = (InetAddress) ei.nextElement();
|
||||
if (inetAddress.equals(mpsa.getInetAddress())) {
|
||||
if (inetAddress.equals(mpsai)) {
|
||||
continue loop;
|
||||
}
|
||||
}
|
||||
@@ -145,13 +158,16 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
if (con != null) {
|
||||
con.accept(mpsa);
|
||||
}
|
||||
|
||||
}catch(UnknownHostException e) {
|
||||
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println(bind + " " + group + " " + ninterface);
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
|
||||
close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user