| 1 | package cz.vutbr.feec.packets.ttp; |
| 2 | |
| 3 | import cz.vutbr.feec.packets.PacketGenerateException; |
| 4 | import cz.vutbr.feec.packets.PacketParseException; |
| 5 | import cz.vutbr.feec.packets.PacketUtils; |
| 6 | import org.apache.commons.lang.builder.ToStringBuilder; |
| 7 | |
| 8 | public class FTCPacket extends GeneralTTPHeader { |
| 9 | |
| 10 | private long feedbackTargetPort; |
| 11 | private int activationStatus; |
| 12 | private int reservedEnd; |
| 13 | |
| 14 | public FTCPacket() { |
| 15 | super(3); |
| 16 | } |
| 17 | /** |
| 18 | * Generate buffer of values in FTC header. <i>This method generates |
| 19 | * specific size of bytes into the buffer.</i> |
| 20 | * @param resultArray - memory bytes for generating buffer |
| 21 | * @param startPos - starting position in resultArray |
| 22 | */ |
| 23 | @Override |
| 24 | public int generate(byte[] resultArray, int startPos) |
| 25 | throws PacketGenerateException { |
| 26 | int length = super.generate(resultArray, startPos); |
| 27 | length += generateRestFTC(resultArray, startPos+length); |
| 28 | return length; |
| 29 | } |
| 30 | |
| 31 | private int generateRestFTC(byte[] buffer, int offset){ |
| 32 | |
| 33 | assert PacketUtils.checkSize(this.getFeedbackTargetPort(), 16); |
| 34 | assert PacketUtils.checkSize(this.getActivationStatus(), 8); |
| 35 | assert PacketUtils.checkSize(this.getReservedEnd(), 8); |
| 36 | |
| 37 | |
| 38 | //adding Feedback target port to buffer |
| 39 | |
| 40 | PacketUtils.setOctet2(buffer, offset, this.getFeedbackTargetPort()); |
| 41 | //adding Activation status |
| 42 | |
| 43 | PacketUtils.setOctet1(buffer, offset+2, this.getActivationStatus()); |
| 44 | //adding ReservedEnd |
| 45 | |
| 46 | PacketUtils.setOctet1(buffer, offset+3, this.getReservedEnd()); |
| 47 | // zbytek hlavicky zabere celkem 32 bitu |
| 48 | return 4; |
| 49 | } |
| 50 | /** |
| 51 | * Parsing FTC header values from buffer. <i>This method takes specific |
| 52 | * number of bytes from buffer and parse them into FTC header values. </i> |
| 53 | * @param resultArray - memory bytes for parsing the values |
| 54 | * @param startPos - starting position in resultArray |
| 55 | * @param length - length of header |
| 56 | */ |
| 57 | @Override |
| 58 | public int parse(byte[] resultArray, int startPos, int length) |
| 59 | throws PacketParseException { |
| 60 | int len = super.parse(resultArray, startPos, length); |
| 61 | len += this.parseRestFTC(resultArray, startPos, length); |
| 62 | return len; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | private int parseRestFTC(byte[] resultArray, int startPos, int length) throws PacketParseException{ |
| 67 | this.setFeedbackTargetPort(PacketUtils.getOctet2(resultArray, 4+ startPos)); |
| 68 | this.setActivationStatus(PacketUtils.getOctet1(resultArray, 6 + startPos)); |
| 69 | this.setReservedEnd(PacketUtils.getOctet1(resultArray, 7 + startPos)); |
| 70 | |
| 71 | return 4; |
| 72 | } |
| 73 | |
| 74 | public long getFeedbackTargetPort() { |
| 75 | return feedbackTargetPort; |
| 76 | } |
| 77 | |
| 78 | public void setFeedbackTargetPort(long feedbackTargetPort) { |
| 79 | this.feedbackTargetPort = feedbackTargetPort; |
| 80 | } |
| 81 | |
| 82 | public int getActivationStatus() { |
| 83 | return activationStatus; |
| 84 | } |
| 85 | |
| 86 | public void setActivationStatus(int activationStatus) { |
| 87 | this.activationStatus = activationStatus; |
| 88 | } |
| 89 | |
| 90 | public int getReservedEnd() { |
| 91 | return reservedEnd; |
| 92 | } |
| 93 | |
| 94 | public void setReservedEnd(int reservedEnd) { |
| 95 | this.reservedEnd = reservedEnd; |
| 96 | } |
| 97 | /** |
| 98 | * @see java.lang.Object#toString() |
| 99 | */ |
| 100 | public String toString() { |
| 101 | return new ToStringBuilder(this).append("activationStatus", |
| 102 | this.activationStatus).append("feedbackTargetPort", |
| 103 | this.feedbackTargetPort).append("version", this.getVersion()) |
| 104 | .append("padding", this.getPadding()).append("reservedEnd", |
| 105 | this.reservedEnd).append("packetType", |
| 106 | this.getPacketType()) |
| 107 | .append("length", this.getLength()).append("feedbackTreeId", |
| 108 | this.getFeedbackTreeId()).toString(); |
| 109 | } |
| 110 | |
| 111 | } |