forked from KNEMC/KLALB
KLALB V3.4
This commit is contained in:
@@ -7,68 +7,38 @@ import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
|
||||
import org.kne.cloud.network.NetworkPacket;
|
||||
import org.kne.cloud.network.ipv6.TLV;
|
||||
import org.kne.cloud.network.klalb.KLALBPacket;
|
||||
|
||||
public class IPv6SegmentRoutingTLV extends NetworkPacket {
|
||||
public class IPv6SegmentRoutingTLV extends TLV {
|
||||
public static final int PAD1=0;
|
||||
public static final int PADN=4;
|
||||
public static final int HMAC=5;
|
||||
public static final int SEQS=7;
|
||||
public static final int ACKSEQS=8;
|
||||
|
||||
private int headerLength=2;
|
||||
|
||||
public int getHeaderLength() {
|
||||
return headerLength;
|
||||
}
|
||||
|
||||
private boolean isDefault;
|
||||
|
||||
protected volatile ByteBuffer header;
|
||||
|
||||
private ByteBuffer data;
|
||||
|
||||
public IPv6SegmentRoutingTLV(ByteBuffer header ) {
|
||||
this(header,true);
|
||||
}
|
||||
|
||||
public ByteBuffer getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
public IPv6SegmentRoutingTLV(ByteBuffer header ,boolean isDefault ) {
|
||||
super();
|
||||
this.header = header;
|
||||
this.isDefault=isDefault;
|
||||
if(getType()==PAD1)
|
||||
headerLength=1;
|
||||
if(isDefault&&(headerLength>1)) {
|
||||
data=ByteBuffer.allocate(512);
|
||||
}
|
||||
super(header,isDefault);
|
||||
|
||||
}
|
||||
public IPv6SegmentRoutingTLV(int type) {
|
||||
this(type,true);
|
||||
}
|
||||
public IPv6SegmentRoutingTLV(int type,boolean isDefault) {
|
||||
super();
|
||||
this.header=ByteBuffer.allocate(2);
|
||||
this.isDefault=isDefault;
|
||||
header.put((byte) type);
|
||||
if(type==PAD1) {
|
||||
headerLength=1;
|
||||
}
|
||||
if(isDefault&&(headerLength>1)) {
|
||||
data=ByteBuffer.allocate(512);
|
||||
}
|
||||
super(type,isDefault);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLength() {
|
||||
return headerLength+(isDefault?data.limit():0);
|
||||
}
|
||||
|
||||
public static IPv6SegmentRoutingTLV readIPv6SegmentRoutingTLVFromChannel(ReadableByteChannel din) throws IOException {
|
||||
ByteBuffer bbf= ByteBuffer.allocate(2);
|
||||
ByteBuffer bbf= NetworkPacket.bufferAllocator.allocate(2);
|
||||
bbf.limit(1);
|
||||
while (bbf.hasRemaining()) {
|
||||
if (din.read(bbf) == -1) {
|
||||
@@ -103,65 +73,5 @@ public class IPv6SegmentRoutingTLV extends NetworkPacket {
|
||||
public static void writeIPv6SegmentRoutingTLVToChannel(WritableByteChannel dto,IPv6SegmentRoutingTLV tlv) throws IOException {
|
||||
tlv.writeToChannel(dto);
|
||||
}
|
||||
@Override
|
||||
public void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
if(isDefault&&(headerLength>1)) {
|
||||
setDataLength(data.limit());
|
||||
}
|
||||
header.limit(headerLength);
|
||||
dto.write(header.slice(0,header.limit()));
|
||||
if(isDefault&&(headerLength>1)) {
|
||||
dto.write(data.slice(0, data.limit()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromChannel(ReadableByteChannel din, long length) throws IOException {
|
||||
header.limit(1);
|
||||
while (header.hasRemaining()) {
|
||||
if (din.read(header) == -1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
if(headerLength>1) {
|
||||
header.limit(2);
|
||||
while (header.hasRemaining()) {
|
||||
if (din.read(header) == -1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
}
|
||||
header.flip();
|
||||
|
||||
if(isDefault&&(headerLength>1)) {
|
||||
data.clear();
|
||||
data.limit(getDataLength());
|
||||
while(data.hasRemaining()){
|
||||
if(din.read(data)==-1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
data.flip();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean needEndPosition() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return header.get(0)&0xff;
|
||||
}
|
||||
public void setType(int type) {
|
||||
header.put(0,(byte) type);
|
||||
}
|
||||
|
||||
public int getDataLength() {
|
||||
return header.get(1)&0xff;
|
||||
}
|
||||
|
||||
public void setDataLength(int dataLength) {
|
||||
header.put(1,(byte) dataLength);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package org.kne.cloud.network.srv6;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.Externalizable;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectOutput;
|
||||
import java.io.Serializable;
|
||||
import java.net.Inet6Address;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import org.kne.cloud.network.MultipurposeSocketAddress;
|
||||
import org.kne.cloud.network.NetworkPacket;
|
||||
import org.kne.cloud.network.ipv6.Inet6AddressGroup;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class JsonDataPacket extends KLALBRoutingProtocolPacket implements Serializable{
|
||||
|
||||
private Object userCallback;
|
||||
|
||||
|
||||
public Object getUserCallback() {
|
||||
return userCallback;
|
||||
}
|
||||
|
||||
public void setUserCallback(Object userCallback) {
|
||||
this.userCallback = userCallback;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected long getHeaderSize() {
|
||||
return HEADER_LENGTH;
|
||||
}
|
||||
|
||||
private String data;
|
||||
|
||||
private static final int HEADER_LENGTH=3;
|
||||
|
||||
private static Gson gson;
|
||||
static{
|
||||
GsonBuilder gb=new GsonBuilder();
|
||||
MultipurposeSocketAddress.registerToGsonBuilder(gb);
|
||||
gson=gb.create();
|
||||
}
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public KLALBRoutingProtocolJsonData getDecodedData() {
|
||||
return gson.fromJson(data,KLALBRoutingProtocolJsonData.class );
|
||||
}
|
||||
|
||||
public JsonDataPacket(String data) {
|
||||
super(JSON_DATA);
|
||||
this.data=data;
|
||||
}
|
||||
|
||||
public JsonDataPacket(KLALBRoutingProtocolJsonData data) {
|
||||
this(gson.toJson(data));
|
||||
}
|
||||
|
||||
|
||||
public JsonDataPacket(ByteBuffer bb) {
|
||||
super(bb);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "JsonDATA "+getData();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
byte[]bta=data.getBytes(Charset.forName("UTF-8"));
|
||||
getHeader().putChar(1,(char) bta.length);
|
||||
super.writeToChannel(dto);
|
||||
dto.write(ByteBuffer.wrap(bta));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromChannel(ReadableByteChannel din) throws IOException {
|
||||
super.readFromChannel(din);
|
||||
int lth=getHeader().getChar(1);
|
||||
byte[]b=new byte[lth];
|
||||
ByteBuffer wp= ByteBuffer.wrap(b);
|
||||
while(wp.hasRemaining()){
|
||||
if(din.read(wp)==-1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
data=new String(b, Charset.forName("UTF-8"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getTotalLength() {
|
||||
return HEADER_LENGTH+data.getBytes(Charset.forName("UTF-8")).length;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -27,8 +27,12 @@ import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.kne.cloud.network.ByteBufferAllocator;
|
||||
import org.kne.cloud.network.ipv6.IPv6NetworkLink;
|
||||
import org.kne.cloud.network.ipv6.Inet6AddressGroup;
|
||||
import org.kne.cloud.network.ipv6.Neighbor;
|
||||
@@ -69,7 +73,7 @@ public class KLALBRoutingProtocol extends Thread{
|
||||
|
||||
public static final long HOTSOPT_TIMEOUT = 1000000000;
|
||||
|
||||
public static final int HOTSOPT_REPORT_INTERVAL = 50000000;
|
||||
public static final int HOTSOPT_REPORT_INTERVAL = 1000000;
|
||||
private SRv6Router router;
|
||||
public SRv6Router getRouter() {
|
||||
return router;
|
||||
@@ -83,15 +87,27 @@ public class KLALBRoutingProtocol extends Thread{
|
||||
private long floodTimer=System.nanoTime();
|
||||
private long floodTimer2=System.nanoTime();
|
||||
private long requestTimer=System.nanoTime();
|
||||
KLALBVirtualRawSocket ds = null;
|
||||
private KLALBVirtualRawSocket ds = null;
|
||||
private ReentrantLock sendLock=new ReentrantLock();
|
||||
|
||||
private Set<BiConsumer<SocketAddress,JsonDataPacket>>receivers=new CopyOnWriteArraySet<BiConsumer<SocketAddress,JsonDataPacket>>();
|
||||
|
||||
public void addReceiver(BiConsumer<SocketAddress,JsonDataPacket> rec) {
|
||||
receivers.add(rec);
|
||||
}
|
||||
|
||||
public void removeReceiver(BiConsumer<SocketAddress,JsonDataPacket> rec) {
|
||||
receivers.remove(rec);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Thread.currentThread().setName("KLALB路由协议接收线程");
|
||||
getSelfRouterInfo();
|
||||
try{
|
||||
ds=new KLALBVirtualRawSocket(router,router.getLocator().getAddress(), DEFAULT_PROTOCOL_NUMBER);
|
||||
ds=new KLALBVirtualRawSocket(router.getinLoopback(),router.getLocator().getAddress(), DEFAULT_PROTOCOL_NUMBER);
|
||||
new Thread(()->{
|
||||
Thread.currentThread().setName("KLALB路由协议接收线程");
|
||||
Thread.currentThread().setName("KLALB路由协议发送线程");
|
||||
|
||||
while(true) {
|
||||
try {
|
||||
@@ -115,25 +131,29 @@ public class KLALBRoutingProtocol extends Thread{
|
||||
}*/
|
||||
|
||||
long cur=System.nanoTime();
|
||||
if(cur-floodTimer>60000000000L) {
|
||||
if(cur-floodTimer>20000000000L) {
|
||||
if(debug)
|
||||
System.out.println("update60");
|
||||
floodTimer=cur;
|
||||
floodPacket(ds, null, new RouterInfoPacket(selfRouterInfo,true,-1));
|
||||
floodPacket( null, new RouterInfoPacket(selfRouterInfo,true,-1));
|
||||
}
|
||||
|
||||
if(cur-floodTimer2>1000000000L) {
|
||||
if(debug)
|
||||
System.out.println("update1");
|
||||
floodTimer2=cur;
|
||||
floodPacket(ds, null, new RouterInfoPacket(selfRouterInfo,true,router.getASN()));
|
||||
floodPacket(null, new RouterInfoPacket(selfRouterInfo,true,router.getASN()));
|
||||
}
|
||||
|
||||
//Set<Inet6Address> requestSet=new HashSet<>();
|
||||
for (Iterator<Entry<Inet6Address, RouterInfo>> iterator = netmap.entrySet().iterator(); iterator.hasNext();) {
|
||||
Entry<Inet6Address, RouterInfo> type = (Entry<Inet6Address, RouterInfo>) iterator.next();
|
||||
RouterInfo val=type.getValue();
|
||||
if(val.checkTimeOut()) {
|
||||
long timeout=30000000000L;
|
||||
if(val.getAsn()==router.getASN()) {
|
||||
timeout=2000000000L;
|
||||
}
|
||||
if(val.checkTimeOut(timeout)) {
|
||||
iterator.remove();
|
||||
noticeUpdate();
|
||||
}
|
||||
@@ -197,21 +217,20 @@ public class KLALBRoutingProtocol extends Thread{
|
||||
while(true) {
|
||||
|
||||
try {
|
||||
byte[]ca=new byte[65535];
|
||||
byte[]ca=ByteBufferAllocator.allocateArray(65535);
|
||||
DatagramPacket dgp=new DatagramPacket(ca, ca.length);
|
||||
ds.receive(dgp);
|
||||
//System.out.println(Arrays.toString( Arrays.copyOf( dgp.getData(),dgp.getLength())));
|
||||
ByteArrayInputStream bi=new ByteArrayInputStream(dgp.getData(),0,dgp.getLength());
|
||||
KLALBRoutingProtocolPacket kp=KLALBRoutingProtocolPacket.readKLALBPacketFromChannel(Channels.newChannel(bi));
|
||||
|
||||
//System.out.println(kp);
|
||||
switch(kp.getType()) {
|
||||
case KLALBRoutingProtocolPacket.RINFO_REQ:
|
||||
RouterInfo rifr=netmap.get(router.getLocator().getAddress());
|
||||
if(debug)
|
||||
System.out.println("get Request from:"+dgp.getSocketAddress());
|
||||
if(rifr!=null) {
|
||||
writePacket(ds, new RouterInfoPacket( rifr,false,-1), dgp.getSocketAddress());
|
||||
writePacket( new RouterInfoPacket( rifr,false,-1), dgp.getSocketAddress());
|
||||
if(debug)
|
||||
System.out.println("response router info:"+rifr);
|
||||
}else {
|
||||
@@ -233,7 +252,7 @@ public class KLALBRoutingProtocol extends Thread{
|
||||
netmap.put(rif.getLocator().getAddress(), rif);
|
||||
noticeUpdate();
|
||||
if(rifp.isFlood()) {
|
||||
floodPacket(ds, dgp.getSocketAddress(), rifp);
|
||||
floodPacket( dgp.getSocketAddress(), rifp);
|
||||
|
||||
}
|
||||
|
||||
@@ -244,6 +263,10 @@ public class KLALBRoutingProtocol extends Thread{
|
||||
}
|
||||
computeShortestPathIfUpdated();
|
||||
break;
|
||||
case KLALBRoutingProtocolPacket.JSON_DATA:
|
||||
JsonDataPacket data=(JsonDataPacket) kp;
|
||||
receivers.forEach((v)->{v.accept(dgp.getSocketAddress(),data);});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -260,43 +283,44 @@ public class KLALBRoutingProtocol extends Thread{
|
||||
}
|
||||
}
|
||||
|
||||
private void floodPacket(KLALBVirtualRawSocket ds2,SocketAddress except,RouterInfoPacket rifp) throws IOException {
|
||||
private void floodPacket(SocketAddress except,RouterInfoPacket rifp) throws IOException {
|
||||
long asn=rifp.getASN();
|
||||
List<IPv6NetworkLink>links=router.getLinkTabel();
|
||||
Object[] nls=links.toArray();
|
||||
for(int i=0;i<nls.length;i++) {
|
||||
IPv6NetworkLink nl=(IPv6NetworkLink) nls[i];
|
||||
if(!nl.isLoopBack()) {
|
||||
List<Neighbor>neis=nl.getNeighborsInfo();
|
||||
for (Iterator<Neighbor> iteratorx = neis.iterator(); iteratorx.hasNext();) {
|
||||
Neighbor addresses = (Neighbor) iteratorx.next();
|
||||
List<Neighbor>ln= router.getNeighbors();
|
||||
for(Neighbor addresses :ln) {
|
||||
if(addresses.getLocator()==null)
|
||||
continue;
|
||||
InetSocketAddress isa=new InetSocketAddress(addresses.getLocator().getAddress(),DEFAULT_PORT);
|
||||
if(!isa.equals(except)) {
|
||||
if(asn==-1) {
|
||||
writePacket(ds2, rifp, isa);
|
||||
writePacket( rifp, isa);
|
||||
}else{
|
||||
RouterInfo nif= netmap.get(isa.getAddress());
|
||||
if(nif!=null&&nif.getAsn()==asn) {
|
||||
writePacket(ds2, rifp, isa);
|
||||
writePacket( rifp, isa);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void writePacket(KLALBVirtualRawSocket ds2,KLALBRoutingProtocolPacket pkt,SocketAddress dest) throws IOException {
|
||||
public void sendJsonPacketToAddress(JsonDataPacket pkt,SocketAddress dest) throws IOException {
|
||||
writePacket(pkt,dest);
|
||||
}
|
||||
|
||||
private void writePacket(KLALBRoutingProtocolPacket pkt,SocketAddress dest) throws IOException {
|
||||
ByteArrayOutputStream bos=new ByteArrayOutputStream(65535);
|
||||
KLALBRoutingProtocolPacket.writeKLALBPacketToChannel(Channels.newChannel(bos),pkt);
|
||||
byte[]to=bos.toByteArray();
|
||||
DatagramPacket dgpx=new DatagramPacket(to,to.length);
|
||||
dgpx.setSocketAddress(dest);
|
||||
sendLock.lock();
|
||||
try {
|
||||
ds2.send(dgpx);
|
||||
ds.send(dgpx);
|
||||
}catch(BindException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Cannot assign:"+dest);
|
||||
}finally {
|
||||
sendLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -550,7 +574,7 @@ public class KLALBRoutingProtocol extends Thread{
|
||||
}
|
||||
}
|
||||
}
|
||||
NeighborInfo ni=new NeighborInfo(nl.getAddressGroup(),addresses.getAddress(),addresses.getLocator(),updelay ,downdelay,updelayMin ,downdelayMin ,uploadspeed ,downloadspeed,uploadspeedmax ,downloadspeedmax);
|
||||
NeighborInfo ni=new NeighborInfo(nl.getAddressGroups().get(0),addresses.getAddress(),addresses.getLocator(),updelay ,downdelay,updelayMin ,downdelayMin ,uploadspeed ,downloadspeed,uploadspeedmax ,downloadspeedmax);
|
||||
ni.setContext(addresses);
|
||||
ri.getNeighborAddresses().add(ni);
|
||||
|
||||
@@ -624,9 +648,9 @@ public class KLALBRoutingProtocol extends Thread{
|
||||
hat.refreshTimeout();
|
||||
}
|
||||
if(hat.checkReportTime()) {
|
||||
System.out.println("hotspot address:"+sourceAddress);
|
||||
//System.out.println("hotspot address:"+sourceAddress);
|
||||
try {
|
||||
writePacket(ds, new RouterInfoPacket( netmap.get(router.getLocator().getAddress()),false,-1), new InetSocketAddress(sourceAddress, DEFAULT_PORT));
|
||||
writePacket( new RouterInfoPacket( netmap.get(router.getLocator().getAddress()),false,-1), new InetSocketAddress(sourceAddress, DEFAULT_PORT));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package org.kne.cloud.network.srv6;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.Cleaner;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.kne.cloud.network.MultipurposeSocketAddress;
|
||||
import org.kne.cloud.network.ThreadTool;
|
||||
import org.kne.cloud.network.congress.ECNCongressAlgorithm;
|
||||
import org.kne.cloud.network.congress.EmptyCongressAlgorithm;
|
||||
import org.kne.cloud.network.congress.SendPacketSlidingWindow;
|
||||
import org.kne.opencl64.Releaser;
|
||||
|
||||
public class KLALBRoutingProtocolAPIClient {
|
||||
private KLALBRoutingProtocol routingProtocol;
|
||||
private SendPacketSlidingWindow<UUID, JsonDataPacket> window = new SendPacketSlidingWindow<UUID, JsonDataPacket>(
|
||||
new EmptyCongressAlgorithm(3000000000L), 1024 * 1024);
|
||||
|
||||
private static final Cleaner clr = Cleaner.create();
|
||||
|
||||
private BiConsumer<SocketAddress, JsonDataPacket> rec = (addr, data) -> {
|
||||
KLALBRoutingProtocolJsonData dataobj = data.getDecodedData();
|
||||
InetSocketAddress addrs = (InetSocketAddress) addr;
|
||||
UUID ruid = dataobj.getUuid();
|
||||
JsonDataPacket relate = null;
|
||||
//System.out.println(ruid + " " + window.getSendmap());
|
||||
switch (dataobj.getType()) {
|
||||
case KLALBRoutingProtocolJsonData.OPEN_LINES_RESP:
|
||||
|
||||
if ((relate = window.ack(ruid)) != null) {
|
||||
List<?> connects = (List<?>) dataobj.getData();
|
||||
List<MultipurposeSocketAddress> connectsm = new ArrayList<MultipurposeSocketAddress>(connects.size());
|
||||
for (Object open : connects) {
|
||||
if (open instanceof MultipurposeSocketAddress) {
|
||||
connectsm.add((MultipurposeSocketAddress) open);
|
||||
} else {
|
||||
connectsm.add(new MultipurposeSocketAddress((String) open));
|
||||
|
||||
}
|
||||
}
|
||||
((Consumer<List<MultipurposeSocketAddress>>) relate.getUserCallback()).accept(connectsm);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public KLALBRoutingProtocolAPIClient(KLALBRoutingProtocol routingProtocol) {
|
||||
this.routingProtocol = routingProtocol;
|
||||
routingProtocol.addReceiver(rec);
|
||||
this.releaser = new KLALBRoutingProtocolAPIClientReleaser(this.routingProtocol, rec, window);
|
||||
clr.register(this, releaser);
|
||||
}
|
||||
|
||||
public void requestOpenLines(SocketAddress addr, Consumer<List<MultipurposeSocketAddress>> callback)
|
||||
throws IOException {
|
||||
UUID suid = UUID.randomUUID();
|
||||
KLALBRoutingProtocolJsonData json = new KLALBRoutingProtocolJsonData(
|
||||
KLALBRoutingProtocolJsonData.OPEN_LINES_REQ, suid, null);
|
||||
JsonDataPacket packet = new JsonDataPacket(json);
|
||||
packet.setUserCallback(callback);
|
||||
window.put(suid, packet);
|
||||
routingProtocol.sendJsonPacketToAddress(packet, addr);
|
||||
|
||||
}
|
||||
|
||||
public KLALBRoutingProtocol getRoutingProtocol() {
|
||||
return routingProtocol;
|
||||
}
|
||||
|
||||
private KLALBRoutingProtocolAPIClientReleaser releaser;
|
||||
|
||||
public void close() {
|
||||
releaser.run();
|
||||
}
|
||||
|
||||
public boolean isClosed() {
|
||||
return releaser.isReleased();
|
||||
}
|
||||
}
|
||||
|
||||
class KLALBRoutingProtocolAPIClientReleaser extends Releaser<BiConsumer<SocketAddress, JsonDataPacket>> {
|
||||
|
||||
private KLALBRoutingProtocol routingProtocol;
|
||||
private SendPacketSlidingWindow<UUID, JsonDataPacket> window;
|
||||
|
||||
public KLALBRoutingProtocolAPIClientReleaser(KLALBRoutingProtocol routingProtocol,
|
||||
BiConsumer<SocketAddress, JsonDataPacket> resource, SendPacketSlidingWindow<UUID, JsonDataPacket> window) {
|
||||
super(resource);
|
||||
this.routingProtocol = routingProtocol;
|
||||
this.window = window;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void release(BiConsumer<SocketAddress, JsonDataPacket> resource) {
|
||||
window.close();
|
||||
routingProtocol.removeReceiver(resource);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package org.kne.cloud.network.srv6;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.Cleaner;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import org.kne.cloud.network.MultipurposeSocketAddress;
|
||||
import org.kne.cloud.network.ThreadTool;
|
||||
import org.kne.cloud.network.congress.SendPacketSlidingWindow;
|
||||
import org.kne.cloud.network.klalb.KLALBController;
|
||||
import org.kne.opencl64.Releaser;
|
||||
|
||||
public class KLALBRoutingProtocolAPIServer {
|
||||
private KLALBController controller;
|
||||
private KLALBRoutingProtocol routingProtocol;
|
||||
|
||||
|
||||
private static final Cleaner clr=Cleaner.create();
|
||||
|
||||
private BiConsumer<SocketAddress, JsonDataPacket> rec=(addr,data)->{
|
||||
try {
|
||||
KLALBRoutingProtocolJsonData dataobj= data.getDecodedData();
|
||||
InetSocketAddress addrs=(InetSocketAddress) addr;
|
||||
switch(dataobj.getType()){
|
||||
case KLALBRoutingProtocolJsonData.OPEN_LINES_REQ:
|
||||
KLALBRoutingProtocolJsonData json=new KLALBRoutingProtocolJsonData(KLALBRoutingProtocolJsonData.OPEN_LINES_RESP,dataobj.getUuid(),controller.getSelflineTable());
|
||||
routingProtocol.sendJsonPacketToAddress(new JsonDataPacket(json),addr);
|
||||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
};
|
||||
|
||||
public KLALBRoutingProtocolAPIServer(KLALBRoutingProtocol routingProtocol,KLALBController controller) {
|
||||
this.controller=controller;
|
||||
this.routingProtocol=routingProtocol;
|
||||
routingProtocol.addReceiver(rec);
|
||||
this.releaser=new KLALBRoutingProtocolAPIServerReleaser(this.routingProtocol,rec);
|
||||
clr.register(this, releaser);
|
||||
}
|
||||
|
||||
public KLALBController getController() {
|
||||
return controller;
|
||||
}
|
||||
|
||||
public KLALBRoutingProtocol getRoutingProtocol() {
|
||||
return routingProtocol;
|
||||
}
|
||||
|
||||
|
||||
private KLALBRoutingProtocolAPIServerReleaser releaser;
|
||||
|
||||
public void close() {
|
||||
releaser.run();
|
||||
}
|
||||
|
||||
|
||||
public boolean isClosed() {
|
||||
return releaser.isReleased();
|
||||
}
|
||||
|
||||
}
|
||||
class KLALBRoutingProtocolAPIServerReleaser extends Releaser<BiConsumer<SocketAddress, JsonDataPacket>>{
|
||||
|
||||
private KLALBRoutingProtocol routingProtocol;
|
||||
|
||||
public KLALBRoutingProtocolAPIServerReleaser(KLALBRoutingProtocol routingProtocol,BiConsumer<SocketAddress, JsonDataPacket> resource) {
|
||||
super(resource);
|
||||
this.routingProtocol=routingProtocol;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void release(BiConsumer<SocketAddress, JsonDataPacket> resource) {
|
||||
routingProtocol.removeReceiver(resource);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.kne.cloud.network.srv6;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class KLALBRoutingProtocolJsonData {
|
||||
public static final String OPEN_LINES_REQ="openlinesreq";
|
||||
public static final String OPEN_LINES_RESP="openlinesresp";
|
||||
private String type;
|
||||
private UUID uuid;
|
||||
private Object data;
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
public UUID getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((data == null) ? 0 : data.hashCode());
|
||||
result = prime * result + ((type == null) ? 0 : type.hashCode());
|
||||
result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
KLALBRoutingProtocolJsonData other = (KLALBRoutingProtocolJsonData) obj;
|
||||
if (data == null) {
|
||||
if (other.data != null)
|
||||
return false;
|
||||
} else if (!data.equals(other.data))
|
||||
return false;
|
||||
if (type == null) {
|
||||
if (other.type != null)
|
||||
return false;
|
||||
} else if (!type.equals(other.type))
|
||||
return false;
|
||||
if (uuid == null) {
|
||||
if (other.uuid != null)
|
||||
return false;
|
||||
} else if (!uuid.equals(other.uuid))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
public KLALBRoutingProtocolJsonData(String type, UUID uuid, Object data) {
|
||||
super();
|
||||
this.type = type;
|
||||
this.uuid = uuid;
|
||||
this.data = data;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "KLALBRoutingProtocolJsonData [type=" + type + ", uuid=" + uuid + ", data=" + data + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import org.kne.cloud.network.NetworkPacket;
|
||||
public abstract class KLALBRoutingProtocolPacket extends NetworkPacket {
|
||||
public static final int RINFO_REQ=0;
|
||||
public static final int RINFO=1;
|
||||
public static final int JSON_DATA=2;
|
||||
|
||||
private static final int HEADER_CAPACITY = 32;
|
||||
|
||||
@@ -79,7 +80,7 @@ public abstract class KLALBRoutingProtocolPacket extends NetworkPacket {
|
||||
public long getRcvtime() {
|
||||
return rcvtime;
|
||||
}
|
||||
public long getLength() {
|
||||
public long getTotalLength() {
|
||||
return getHeaderSize();
|
||||
}
|
||||
|
||||
@@ -116,6 +117,10 @@ public abstract class KLALBRoutingProtocolPacket extends NetworkPacket {
|
||||
klp=new RouterInfoPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
case JSON_DATA:
|
||||
klp=new JsonDataPacket(bb);
|
||||
klp.readFromChannel(in);
|
||||
return klp;
|
||||
}
|
||||
//throw new StreamCorruptedException("unknown package type:"+type);
|
||||
System.err.println("ignore unknown KLALBRoutingProtocolPacket type:"+type);
|
||||
|
||||
@@ -16,17 +16,4 @@ public class PADNSegmentRoutingTLV extends IPv6SegmentRoutingTLV {
|
||||
getData().limit(dataLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
|
||||
super.writeToChannel(dto);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromChannel(ReadableByteChannel din, long length) throws IOException {
|
||||
super.readFromChannel(din, length);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,5 +6,5 @@ import org.kne.cloud.network.ipv6.IPv6Packet;
|
||||
import org.pcap4j.packet.IpV6Packet;
|
||||
|
||||
public interface PacketConsumer {
|
||||
public void accept(IPv6Packet packx)throws IOException;
|
||||
public boolean accept(IPv6Packet packx)throws IOException;
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ public class RouterInfo implements Serializable{
|
||||
return createTime;
|
||||
}
|
||||
private static final long INFO_UPDATETIME=10000000000L;
|
||||
private static final long INFO_TIMEOUT=120000000000L;
|
||||
private static final long INFO_TIMEOUT=60000000000L;
|
||||
private long putTime=System.nanoTime();
|
||||
public boolean checkUpdateTime() {
|
||||
return System.nanoTime()-putTime>INFO_UPDATETIME;
|
||||
@@ -77,6 +77,10 @@ public class RouterInfo implements Serializable{
|
||||
return System.nanoTime()-putTime>INFO_TIMEOUT;
|
||||
}
|
||||
|
||||
public boolean checkTimeOut(long timeout) {
|
||||
return System.nanoTime()-putTime>timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RINFO [locator=" + locator + ", neighborAddresses=" + neighborAddresses + ", putTime=" + putTime
|
||||
@@ -123,4 +127,5 @@ public class RouterInfo implements Serializable{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -52,8 +52,8 @@ public class RouterInfoPacket extends KLALBRoutingProtocolPacket implements Seri
|
||||
return super.getHeaderSize()+9;
|
||||
}
|
||||
@Override
|
||||
public long getLength() {
|
||||
return super.getLength();
|
||||
public long getTotalLength() {
|
||||
return super.getTotalLength();
|
||||
}
|
||||
public void setRinfo(RouterInfo rinfo) {
|
||||
this.rinfo = rinfo;
|
||||
|
||||
@@ -25,6 +25,7 @@ public class SRv6PacketReorder {
|
||||
|
||||
private class PacketTimeEntry{
|
||||
private IPv6Packet packet;
|
||||
private boolean accepted=false;
|
||||
private long addTime;
|
||||
public PacketTimeEntry(IPv6Packet packet) {
|
||||
super();
|
||||
@@ -59,10 +60,20 @@ public class SRv6PacketReorder {
|
||||
//System.out.println("Ord:"+i6p.getFlowLabel()+" "+seqptr.get());
|
||||
seqptr.getAndIncrement();
|
||||
orderTime=System.nanoTime();
|
||||
if(packetConsumer!=null) {
|
||||
if(packetConsumer!=null&&(!i6p.accepted)) {
|
||||
i6p.accepted=true;
|
||||
packetConsumer.accept(i6p.packet);
|
||||
}
|
||||
}else {
|
||||
for(int i=1;i<5;i++) {
|
||||
PacketTimeEntry i6px=map.get(seqptr.get()+i);
|
||||
if(i6px!=null) {
|
||||
if(packetConsumer!=null&&(!i6px.accepted)) {
|
||||
i6px.accepted=true;
|
||||
packetConsumer.accept(i6px.packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,7 @@
|
||||
package org.kne.cloud.network.srv6;
|
||||
|
||||
import org.kne.cloud.network.ipv6.IPv6NetworkLink;
|
||||
|
||||
public interface SRv6RouterListener {
|
||||
public void onLinkChanged(SRv6Router router,IPv6NetworkLink link) ;
|
||||
}
|
||||
Reference in New Issue
Block a user