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.util.concurrent.atomic.AtomicInteger;

public class ACKTPacket extends KLALBPacket implements PortPacket {
	private int sport,dport;
	private long number;
	private boolean avaliable;
	private int sendcount;

	public ACKTPacket(int sport,int dport,long number,boolean avaliable,int sendcount) {
		super(ACKT);
		this.sport=sport;
		this.dport=dport;
		this.number=number;
		this.avaliable=avaliable;
		this.sendcount=sendcount;
	}

	public ACKTPacket() {
		super(ACKT);
	}

	@Override
	public String toString() {
		return "ACKT "+sport+"->"+dport+" "+number+"[] "+avaliable;
	}

	public int getSport() {
		return sport;
	}

	@Override
	public long getLength() {
		return super.getLength()+18;
	}

	public int getDport() {
		return dport;
	}

	public long getNumber() {
		return number;
	}

	public boolean isAvaliable() {
		return avaliable;
	}
	

	public int getSendcount() {
		return sendcount;
	}

	 private final byte[] writeBuffer = new byte[18];
	@Override
	protected void writeToStream(DataOutput dto) throws IOException {
		super.writeToStream(dto);
		/*dto.writeInt(sport);
		dto.writeInt(dport);
		dto.writeLong(number);
		dto.writeByte(sendcount);
		dto.writeBoolean(avaliable);*/
		writeBuffer[0] = (byte)(sport >>> 24);
        writeBuffer[1] = (byte)(sport >>> 16);
        writeBuffer[2] = (byte)(sport >>>  8);
        writeBuffer[3] = (byte)(sport >>>  0);
        
    	writeBuffer[4] = (byte)(dport >>> 24);
        writeBuffer[5] = (byte)(dport >>> 16);
        writeBuffer[6] = (byte)(dport >>>  8);
        writeBuffer[7] = (byte)(dport >>>  0);

        writeBuffer[8] = (byte)(number >>> 56);
        writeBuffer[9] = (byte)(number >>> 48);
        writeBuffer[10] = (byte)(number >>> 40);
        writeBuffer[11] = (byte)(number >>> 32);
        writeBuffer[12] = (byte)(number >>> 24);
        writeBuffer[13] = (byte)(number >>> 16);
        writeBuffer[14] = (byte)(number >>>  8);
        writeBuffer[15] = (byte)(number >>>  0);
        
        writeBuffer[16]=(byte) getSendRecord().size();
        
        writeBuffer[17]=(byte) (avaliable ? 1 : 0);
		dto.write(writeBuffer);
	}
	 private final byte[] readBuffer = new byte[18];
	@Override
	protected void readFromStream(DataInput din) throws IOException {
		super.readFromStream(din);
		/*sport=din.readInt();
		dport=din.readInt();
		number=din.readLong();
		sendcount=din.readUnsignedByte();
		avaliable=din.readBoolean();*/
		
		din.readFully(readBuffer);
		sport=((readBuffer[0] << 24) + (readBuffer[1] << 16) + (readBuffer[2] << 8) + (readBuffer[3] << 0));
		dport=((readBuffer[4] << 24) + (readBuffer[5] << 16) + (readBuffer[6] << 8) + (readBuffer[7] << 0));
		number=(((long)readBuffer[8] << 56) +
                ((long)(readBuffer[9] & 255) << 48) +
                ((long)(readBuffer[10] & 255) << 40) +
                ((long)(readBuffer[11] & 255) << 32) +
                ((long)(readBuffer[12] & 255) << 24) +
                ((readBuffer[13] & 255) << 16) +
                ((readBuffer[14] & 255) <<  8) +
                ((readBuffer[15] & 255) <<  0));
		sendcount=readBuffer[16]&0xff;
		avaliable=(readBuffer[17] != 0);
	}

}