package org.kne.cloud.network.klalb;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.concurrent.atomic.AtomicInteger;

public class ACKTPacket extends KLALBPacket implements PortPacket {
	
	private static final int HEADER_LENGTH=35;
	
	public ACKTPacket(int sport,int dport,long number,long avaliableRcvWindow,boolean congress,long rcvSpeed,int sendcount) {
		super(ACKT,HEADER_LENGTH);
		klalbHeader.putInt(sport);
		klalbHeader.putInt(dport);
		klalbHeader.putLong(number);
		klalbHeader.put((byte) sendcount);
		klalbHeader.putLong(avaliableRcvWindow);
		klalbHeader.put((byte) (congress?1:0));
		klalbHeader.putLong(rcvSpeed);
	}

	public ACKTPacket(ByteBuffer bb) {
		super(bb,HEADER_LENGTH);
	}

	@Override
	public String toString() {
		return "ACKT "+getSrcPort()+"->"+getDstPort()+" "+getNumber()+"[] avaliable:"+getAvaliableRcvWindow();
	}

	public int getSrcPort() {
		return klalbHeader.getInt(1);
	}

	public boolean isCongress() {
		return klalbHeader.get(26)!=0;
	}
	
	public int getDstPort() {
		return klalbHeader.getInt(5);
	}

	public long getNumber() {
		return klalbHeader.getLong(9);
	}

	public long getAvaliableRcvWindow() {
		return klalbHeader.getLong(18);
	}
	

	public int getSendcount() {
		return klalbHeader.getInt(17);
	}
	public long getRcvSpeed() {
		return klalbHeader.getLong(27);
	}
}