forked from KNEMC/KLALB
KLALB V3.6.0 写了一半
This commit is contained in:
@@ -5,6 +5,7 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
@@ -20,9 +21,12 @@ import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class IPMulticastDiscovery extends Thread implements Closeable, AutoCloseable {
|
||||
private static final boolean debug=false;
|
||||
|
||||
private MulticastSocket soc;
|
||||
private NetworkInterface ninterface;
|
||||
private volatile boolean closed = false;
|
||||
@@ -30,6 +34,7 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
private InetSocketAddress bind;
|
||||
private int type;
|
||||
private List<MultipurposeSocketAddress> msas;
|
||||
private UUID selfUUID;
|
||||
|
||||
private volatile Consumer<MultipurposeSocketAddress> con;
|
||||
private long timeInterval;
|
||||
@@ -63,13 +68,14 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
}
|
||||
|
||||
public IPMulticastDiscovery(InetSocketAddress bind, InetSocketAddress group, NetworkInterface ninterface,
|
||||
List<MultipurposeSocketAddress> msas,long timeInterval) throws IOException {
|
||||
List<MultipurposeSocketAddress> msas,UUID node,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.selfUUID=node;
|
||||
this.timeInterval=timeInterval;
|
||||
}
|
||||
|
||||
@@ -100,16 +106,19 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
}
|
||||
}
|
||||
if (bf) {
|
||||
continue;
|
||||
}
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dops = new DataOutputStream(baos);
|
||||
dops.writeInt(multipurposeSocketAddress.getPort());
|
||||
dops.writeUTF(multipurposeSocketAddress.getType());
|
||||
dops.writeLong(selfUUID.getMostSignificantBits());
|
||||
dops.writeLong(selfUUID.getLeastSignificantBits());
|
||||
dops.close();
|
||||
byte[] b = baos.toByteArray();
|
||||
DatagramPacket dp = new DatagramPacket(b, b.length, group);
|
||||
soc.send(dp);
|
||||
if(debug)
|
||||
System.out.println("发送广播:"+multipurposeSocketAddress+" "+group+" "+selfUUID);
|
||||
}
|
||||
}
|
||||
} catch (NoRouteToHostException|UnknownHostException e) {
|
||||
}
|
||||
@@ -136,6 +145,7 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
|
||||
try {
|
||||
loop:while (!closed) {
|
||||
try {
|
||||
byte[] b = new byte[65535];
|
||||
DatagramPacket dp = new DatagramPacket(b, b.length);
|
||||
soc.receive(dp);
|
||||
@@ -143,12 +153,21 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
DataInputStream dis = new DataInputStream(bis);
|
||||
int port = dis.readInt();
|
||||
String type = dis.readUTF();
|
||||
long h=dis.readLong();
|
||||
long l=dis.readLong();
|
||||
UUID uid=new UUID(h, l);
|
||||
dis.close();
|
||||
MultipurposeSocketAddress mpsa = new MultipurposeSocketAddress(type, dp.getAddress().getHostAddress(),
|
||||
port);
|
||||
try {
|
||||
if(debug) {
|
||||
if(uid.equals(selfUUID)) {
|
||||
System.out.println("丢弃广播:"+mpsa+" "+dp.getSocketAddress()+" "+uid);
|
||||
continue;
|
||||
}else {
|
||||
System.out.println("接收广播:"+mpsa+" "+dp.getSocketAddress()+" "+uid);
|
||||
}
|
||||
}
|
||||
InetAddress mpsai=mpsa.getInetAddress();
|
||||
//System.out.println(mpsa);
|
||||
Enumeration<InetAddress> ei = ninterface.getInetAddresses();
|
||||
while (ei.hasMoreElements()) {
|
||||
InetAddress inetAddress = (InetAddress) ei.nextElement();
|
||||
@@ -162,9 +181,14 @@ public class IPMulticastDiscovery extends Thread implements Closeable, AutoClose
|
||||
}
|
||||
}catch(UnknownHostException e) {
|
||||
|
||||
}catch(EOFException e) {
|
||||
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
} catch(SocketException e) {
|
||||
if(!isClosed())
|
||||
e.printStackTrace();
|
||||
}catch (IOException e) {
|
||||
System.out.println(bind + " " + group + " " + ninterface);
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user