package communication;
import BasicDataType.Direction;
/**
* DirectionPacket contains a Direction and packetNr
* 2 Packets is needed to distribute a complete Direction
* @author Marcus Fredriksson Dennis Sandmark
*/
public class DirectionPacket extends Packet{
/**
* Creates a AckPacket
* With a header and zero data
*/
public DirectionPacket(byte[] arr){
super(arr);
}
/** ID max 7
* PacketNr max 15
*
* @param dir
* @param packetNr
*/
public DirectionPacket(Direction dir,byte packetNr){
super(DIRECTION);
data[0]=(byte)pack(dir.getId(),packetNr);
/*System.out.println("Data[0]: "+data[0]);
System.out.println("ID :" +dir.getId());
System.out.println("PacketNr :" +packetNr);*/
data[1]= dir.getCoordX();
data[2]= dir.getCoordY();
}
public byte getId(){
int out[]=unpack(this.data[0]);
return (byte)out[0];
}
public byte getPacketNr(){
int out[]=unpack(this.data[0]);
//System.out.println("getPnr: "+(byte)out[1]);
return (byte)out[1];
}
/**
* Gets the x coordinate of the CoordPacket
* @return the square coordinate x of the CoordPacket
*/
public byte getCoordX(){
return this.data[1];
}
/**
* Gets the y coordinate of the CoordPacket
* @return the square coordinate y of the CoordPacket
*/
public byte getCoordY(){
return this.data[2];
}
public Direction toDirection() {
return new Direction(this.data[1],this.data[2],this.getId());
}
public int pack(int id,int packetNr){
return ((packetNr<<3)|id);
}
public int[] unpack(int packet){
int out[] = new int[2];
out[0] = (packet & 0x07);
out[1] = (packet>>>3);
return out;
}
public void print(){
System.out.println("Id: "+this.getId());
System.out.println("PacketNr: "+this.getPacketNr());
System.out.println("CoordX: "+this.getCoordX());
System.out.println("CoordY: "+this.getCoordY());
}
}