package org.kne.cloud.network.klalb;

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.ipv6.IPv6Packet;

public class IPv6OverKLALBPacket extends KLALBPacket {
	private static final int HEADER_LENGTH=9;


	@Override
	public long getTotalLength() {
		return HEADER_LENGTH+ipv6Packet.getTotalLength();
	}
	
	private IPv6Packet ipv6Packet;
	
	public IPv6OverKLALBPacket(IPv6Packet ipv6Packet) {
		super(IPV6OVERKLALB,HEADER_LENGTH);
		klalbHeader.putLong((int) ipv6Packet.getTotalLength());
		this.ipv6Packet=ipv6Packet;
	}

	

	public IPv6OverKLALBPacket() {
		super(IPV6OVERKLALB,HEADER_LENGTH);
		this.ipv6Packet=new IPv6Packet();
	}

	public IPv6OverKLALBPacket(ByteBuffer bb) {
		super(bb,HEADER_LENGTH);
		this.ipv6Packet=new IPv6Packet();
	}
	
	public IPv6Packet getIPv6Packet() {
		return ipv6Packet;
	}

	/*@Override
	public String toString() {
		return "IPv6OverKLALBPacket "+ipv6Packet.getSourceAddress().getHostAddress()+"->"+ipv6Packet.getDestinationAddress().getHostAddress()+" type:"+ipv6Packet.getPayload().getProtocolNumber()+"["+getSize()+"]";
	}*/
	
	
	

	public long getSize() {
		return ipv6Packet.getTotalLength();
	}

	@Override
	public String toString() {
		return "IPv6OverKLALBPacket [ipv6Packet=" + ipv6Packet + "]";
	}



	@Override
	public void writeToChannel(WritableByteChannel dto) throws IOException {
		klalbHeader.putLong(1, ipv6Packet.getTotalLength());
		super.writeToChannel(dto);
		ipv6Packet.writeToChannel(dto);
	}

	@Override
	public void readFromChannel(ReadableByteChannel din,long length) throws IOException {
		super.readFromChannel(din,length);
		ipv6Packet.readFromChannel(din, klalbHeader.getLong(1));
	}

}
