package org.kne.cloud.network.srv6;

import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
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 TLV {
	public static final int PAD1=0;
	public static final int PADN=4;
	public static final int HMAC=5;
	public static final int DETNET=6;
	public static final int SEQS=7;
	public static final int ACKSEQS=8;
	
	
	public IPv6SegmentRoutingTLV(ByteBuffer header ) {
		this(header,true);
	}
	
	

	public IPv6SegmentRoutingTLV(ByteBuffer header ,boolean isDefault ) {
		super(header,isDefault);
		
	}
	public IPv6SegmentRoutingTLV(int type) {
		this(type,true);
	}
	public IPv6SegmentRoutingTLV(int type,boolean isDefault) {
		super(type,isDefault);
		
	}

	
	public static IPv6SegmentRoutingTLV readIPv6SegmentRoutingTLVFromChannel(ReadableByteChannel din) throws IOException {
		ByteBuffer bbf= NetworkPacket.bufferAllocator.allocate(2);
		bbf.limit(1);
		while (bbf.hasRemaining()) {
			if (din.read(bbf) == -1) {
				return null;
			}
		}
		int type=bbf.get(0)&0xff;
		IPv6SegmentRoutingTLV rtlv;
		switch(type) {
		case PAD1:
			rtlv=new PAD1SegmentRoutingTLV(bbf);
			rtlv.readFromChannel(din);
			return rtlv;
		case PADN:
			rtlv=new PADNSegmentRoutingTLV(bbf);
			rtlv.readFromChannel(din);
			return rtlv;
		case ACKSEQS:
			rtlv=new ACKSEQSSegmentRoutingTLV(bbf);
			rtlv.readFromChannel(din);
			return rtlv;
		case DETNET:
			rtlv=new DetNetSRv6TLV(bbf);
			rtlv.readFromChannel(din);
			return rtlv;
		default:
			rtlv=new IPv6SegmentRoutingTLV(bbf);
			rtlv.readFromChannel(din);
			return rtlv;
		}
	}
	public static void writeIPv6SegmentRoutingTLVToChannel(WritableByteChannel dto,IPv6SegmentRoutingTLV tlv) throws IOException {
		tlv.writeToChannel(dto);
	}

}
