隧道链接改成标准URI格式
This commit is contained in:
@@ -7,16 +7,8 @@ import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.NoRouteToHostException;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.*;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -29,12 +21,11 @@ public class IPMulticastDiscovery implements Closeable, AutoCloseable {
|
||||
private NetworkInterface ninterface;
|
||||
private volatile boolean closed = false;
|
||||
private InetSocketAddress group;
|
||||
private InetSocketAddress bind;
|
||||
private int type;
|
||||
private List<MultipurposeSocketAddress> msas;
|
||||
private List<MultiProtocolSocketAddress> msas;
|
||||
private UUID selfUUID;
|
||||
|
||||
private volatile Consumer<MultipurposeSocketAddress> con;
|
||||
private volatile Consumer<MultiProtocolSocketAddress> con;
|
||||
private long timeInterval;
|
||||
|
||||
public long getTimeInterval() {
|
||||
@@ -45,7 +36,7 @@ public class IPMulticastDiscovery implements Closeable, AutoCloseable {
|
||||
this.timeInterval = timeInterval;
|
||||
}
|
||||
|
||||
public NetworkInterface getNinterface() {
|
||||
public NetworkInterface getInterface() {
|
||||
return ninterface;
|
||||
}
|
||||
|
||||
@@ -54,22 +45,24 @@ public class IPMulticastDiscovery implements Closeable, AutoCloseable {
|
||||
}
|
||||
|
||||
public InetSocketAddress getBind() {
|
||||
return bind;
|
||||
return (InetSocketAddress) soc.getLocalSocketAddress();
|
||||
}
|
||||
|
||||
public Consumer<MultipurposeSocketAddress> getCon() {
|
||||
public Consumer<MultiProtocolSocketAddress> getCon() {
|
||||
return con;
|
||||
}
|
||||
|
||||
public void setCon(Consumer<MultipurposeSocketAddress> con) {
|
||||
public void setCon(Consumer<MultiProtocolSocketAddress> con) {
|
||||
this.con = con;
|
||||
}
|
||||
|
||||
public IPMulticastDiscovery(InetSocketAddress bind, InetSocketAddress group, NetworkInterface ninterface,
|
||||
List<MultipurposeSocketAddress> msas,UUID node,long timeInterval) throws IOException {
|
||||
soc = new MulticastSocket(bind);
|
||||
List<MultiProtocolSocketAddress> msas, UUID node, long timeInterval) throws IOException {
|
||||
soc = new MulticastSocket(null);
|
||||
soc.setReuseAddress(true);
|
||||
soc.bind(bind);
|
||||
soc.setNetworkInterface(ninterface);
|
||||
soc.joinGroup(group, ninterface);
|
||||
this.bind = bind;
|
||||
this.group = group;
|
||||
this.ninterface = ninterface;
|
||||
this.msas = msas;
|
||||
@@ -82,46 +75,22 @@ public class IPMulticastDiscovery implements Closeable, AutoCloseable {
|
||||
ThreadTool.makeVDaemonThreadIfSupport("线路通知线程", () -> {
|
||||
try {
|
||||
while (!closed) {
|
||||
HashSet<MultipurposeSocketAddress> s = new HashSet<MultipurposeSocketAddress>();
|
||||
synchronized (msas) {
|
||||
for (Iterator<MultipurposeSocketAddress> iterator = msas.iterator(); iterator.hasNext();) {
|
||||
MultipurposeSocketAddress multipurposeSocketAddress = (MultipurposeSocketAddress) iterator
|
||||
.next();
|
||||
/*MultipurposeSocketAddress mpsa = new MultipurposeSocketAddress(
|
||||
multipurposeSocketAddress.getType(), "::0", multipurposeSocketAddress.getPort());*/
|
||||
if(msas!=null) {
|
||||
synchronized (msas) {
|
||||
for (Iterator<MultiProtocolSocketAddress> iterator = msas.iterator(); iterator.hasNext(); ) {
|
||||
MultiProtocolSocketAddress multiProtocolSocketAddress = (MultiProtocolSocketAddress) iterator
|
||||
.next();
|
||||
|
||||
MultipurposeSocketAddress mpsa = new MultipurposeSocketAddress(
|
||||
multipurposeSocketAddress.getType(), "::0", multipurposeSocketAddress.getPort());
|
||||
try {
|
||||
if (s.add(mpsa)) {
|
||||
boolean bf = true;
|
||||
Enumeration<InetAddress> ei = ninterface.getInetAddresses();
|
||||
while (ei.hasMoreElements()) {
|
||||
InetAddress inetAddress = (InetAddress) ei.nextElement();
|
||||
if (inetAddress.equals(multipurposeSocketAddress.getInetAddress())) {
|
||||
bf = false;
|
||||
break;
|
||||
try {
|
||||
send(multiProtocolSocketAddress);
|
||||
} catch (NoRouteToHostException | UnknownHostException e) {
|
||||
}
|
||||
}
|
||||
if (bf) {
|
||||
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) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Thread.sleep(timeInterval);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@@ -143,28 +112,9 @@ public class IPMulticastDiscovery implements Closeable, AutoCloseable {
|
||||
try {
|
||||
loop:while (!closed) {
|
||||
try {
|
||||
byte[] b = new byte[65535];
|
||||
DatagramPacket dp = new DatagramPacket(b, b.length);
|
||||
soc.receive(dp);
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(b, 0, dp.getLength());
|
||||
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);
|
||||
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();
|
||||
MultiProtocolSocketAddress mpsa = receive();
|
||||
if (mpsa == null) continue;
|
||||
InetAddress mpsai=mpsa.getInetAddress();
|
||||
Enumeration<InetAddress> ei = ninterface.getInetAddresses();
|
||||
while (ei.hasMoreElements()) {
|
||||
InetAddress inetAddress = (InetAddress) ei.nextElement();
|
||||
@@ -186,7 +136,7 @@ public class IPMulticastDiscovery implements Closeable, AutoCloseable {
|
||||
if(!isClosed())
|
||||
e.printStackTrace();
|
||||
}catch (IOException e) {
|
||||
System.out.println(bind + " " + group + " " + ninterface);
|
||||
System.out.println(getBind() + " " + group + " " + ninterface);
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
@@ -196,7 +146,48 @@ public class IPMulticastDiscovery implements Closeable, AutoCloseable {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}).start();
|
||||
}
|
||||
|
||||
private MultiProtocolSocketAddress receive() throws IOException {
|
||||
byte[] b = new byte[65535];
|
||||
DatagramPacket dp = new DatagramPacket(b, b.length);
|
||||
soc.receive(dp);
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(b, 0, dp.getLength());
|
||||
DataInputStream dis = new DataInputStream(bis);
|
||||
String val = dis.readUTF();
|
||||
long h=dis.readLong();
|
||||
long l=dis.readLong();
|
||||
UUID uid=new UUID(h, l);
|
||||
dis.close();
|
||||
MultiProtocolSocketAddress mpsa = new MultiProtocolSocketAddress(val);
|
||||
if(mpsa.getInetAddress().isAnyLocalAddress()){
|
||||
mpsa=new MultiProtocolSocketAddress(mpsa.getProtocol(),dp.getAddress().getHostAddress(),mpsa.getPort());
|
||||
}
|
||||
if(debug) {
|
||||
if(uid.equals(selfUUID)) {
|
||||
System.out.println(ninterface.getDisplayName()+" 丢弃广播:"+mpsa+" "+dp.getSocketAddress()+" "+uid);
|
||||
return null;
|
||||
}else {
|
||||
System.out.println(ninterface.getDisplayName()+ " 接收广播:"+mpsa+" "+dp.getSocketAddress()+" "+uid);
|
||||
}
|
||||
}
|
||||
return mpsa;
|
||||
}
|
||||
|
||||
private void send(MultiProtocolSocketAddress multiProtocolSocketAddress) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dops = new DataOutputStream(baos);
|
||||
dops.writeUTF(multiProtocolSocketAddress.toString());
|
||||
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(ninterface.getDisplayName()+" 发送广播:"+ multiProtocolSocketAddress +" "+group+" "+selfUUID);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user