88 lines
2.4 KiB
Java
88 lines
2.4 KiB
Java
package org.kne.cloud.network.klalb;
|
|
|
|
import java.io.DataInputStream;
|
|
import java.io.IOException;
|
|
import java.net.ConnectException;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.Socket;
|
|
import java.util.Collection;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import java.util.WeakHashMap;
|
|
|
|
import org.kne.cloud.network.mport.IPPort;
|
|
import org.kne.cloud.network.mport.ServiceElement;
|
|
|
|
public class KLALBServer {
|
|
|
|
WeakHashMap<UUID, IOThreadManager> whm=new WeakHashMap<>();
|
|
public KLALBServer(int port, Map<String,ServiceElement> services,List<Tunnel> tunnels) throws IOException {
|
|
TCPListener tcpl=new TCPListener(port);
|
|
tcpl.setCon((s)->{
|
|
RemoteTCPConnection tcc=null;
|
|
try {
|
|
//s.setSoTimeout(10000);
|
|
tcc=new RemoteTCPConnection(null,s);
|
|
DataInputStream din=tcc.getDin();
|
|
int val=din.readShort()&0xffff;
|
|
if(val!=59649) {
|
|
return;
|
|
}
|
|
int x=din.read();
|
|
if(x==0) {
|
|
synchronized (services) {
|
|
tcc.getDout().writeInt(services.size());
|
|
Collection<ServiceElement> cll=services.values();
|
|
for (Iterator<ServiceElement> iterator = cll.iterator(); iterator.hasNext();) {
|
|
ServiceElement tunnel = iterator.next();
|
|
tcc.getDout().writeUTF(tunnel.toString());
|
|
}
|
|
}
|
|
synchronized (tunnels) {
|
|
int counts=tunnels.size();
|
|
tcc.getDout().writeInt(counts);
|
|
for (int i = 0; i < counts; i++) {
|
|
tcc.getDout().writeUTF(tunnels.get(i).toString());
|
|
}
|
|
}
|
|
tcc.getDout().flush();
|
|
return;
|
|
}
|
|
|
|
String name = din.readUTF();
|
|
String ip = din.readUTF();
|
|
int portx=din.readInt();
|
|
Tunnel tll=Tunnel.newTunnel(name, ip, portx);
|
|
tcc.setTunnel(tll);
|
|
|
|
UUID uid=new UUID(din.readLong(),din.readLong());
|
|
IOThreadManager nx = null;
|
|
synchronized (tcpl) {
|
|
if(whm.containsKey(uid)) {
|
|
nx=whm.get(uid);
|
|
}else {
|
|
nx=new IOThreadManager();
|
|
whm.put(uid, nx);
|
|
}
|
|
}
|
|
nx.handleRemote(tcc);
|
|
}catch(IOException e) {
|
|
e.printStackTrace();
|
|
}finally {
|
|
if(tcc!=null)
|
|
tcc.close();
|
|
}
|
|
});
|
|
tcpl.open();
|
|
|
|
}
|
|
|
|
/*public static void main(String[] args) throws IOException {
|
|
KLALBServer kc=new KLALBServer(4569);
|
|
}*/
|
|
}
|