forked from KNEMC/KLALB
110 lines
2.9 KiB
Java
110 lines
2.9 KiB
Java
package org.kne.cloud.network.klalb;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.BufferedOutputStream;
|
|
import java.io.DataInputStream;
|
|
import java.io.DataOutputStream;
|
|
import java.io.IOException;
|
|
import java.net.Socket;
|
|
import java.net.UnknownHostException;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
import java.util.Objects;
|
|
import java.util.Set;
|
|
import java.util.WeakHashMap;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
public class Tunnel {
|
|
private String name;
|
|
private IPPort ipport;
|
|
public IPPort getIpport() {
|
|
return ipport;
|
|
}
|
|
private long delay=-1;
|
|
public long getDelay() {
|
|
return delay;
|
|
}
|
|
public void setDelay(long delay) {
|
|
this.delay = delay;
|
|
}
|
|
private static Map<IPPort,Tunnel> tls=new ConcurrentHashMap<>();
|
|
private Tunnel(String name, IPPort ipport) throws UnknownHostException {
|
|
super();
|
|
this.name = name;
|
|
this.ipport=ipport;
|
|
}
|
|
public static Tunnel newTunnel(String name, IPPort ipport) throws UnknownHostException {
|
|
Tunnel tmp=tls.get(ipport);
|
|
if(tmp!=null) {
|
|
return tmp;
|
|
}
|
|
tmp=new Tunnel(name, ipport);
|
|
tls.put(ipport, tmp);
|
|
return tmp;
|
|
}
|
|
public static Tunnel newTunnel(String name, String ipport) throws UnknownHostException {
|
|
|
|
return newTunnel(name, new IPPort(ipport));
|
|
}
|
|
public static Tunnel newTunnel(String name, String ip, int port) throws UnknownHostException {
|
|
|
|
return newTunnel(name, new IPPort(ip, port));
|
|
}
|
|
|
|
public static Tunnel newTunnel(String s) throws UnknownHostException {
|
|
String[]t=s.split("\\$");
|
|
return newTunnel(t[0], t[1]);
|
|
}
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
public String getIp() {
|
|
return ipport.getIp().getHostAddress();
|
|
}
|
|
public int getPort() {
|
|
return ipport.getPort();
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return name+"$"+ipport;
|
|
}
|
|
public Socket connectClientSocket() throws UnknownHostException, IOException {
|
|
return new Socket(ipport.getIp(), ipport.getPort());
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(ipport, name);
|
|
}
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj)
|
|
return true;
|
|
if (obj == null)
|
|
return false;
|
|
if (getClass() != obj.getClass())
|
|
return false;
|
|
Tunnel other = (Tunnel) obj;
|
|
return Objects.equals(ipport, other.ipport) && Objects.equals(name, other.name);
|
|
}
|
|
public static void states() {
|
|
Set<Entry<IPPort, Tunnel>> s=tls.entrySet();
|
|
for (Iterator iterator = s.iterator(); iterator.hasNext();) {
|
|
Entry<IPPort, Tunnel> entry = (Entry<IPPort, Tunnel>) iterator.next();
|
|
Tunnel tll=entry.getValue();
|
|
System.out.println(tll.getName()+" "+((tll.getDelay()==-1)?"δ֪":tll.getDelay()/1000000+"ms")+" Á¬½ÓÊý"+tll.ati);
|
|
}
|
|
}
|
|
private AtomicInteger ati=new AtomicInteger(0);
|
|
public AtomicInteger getCCount() {
|
|
return ati;
|
|
}
|
|
|
|
|
|
|
|
}
|