| 1 | package cz.vutbr.feec.packets.rsi; |
| 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 BandwidthBlock extends SubReportBlock { |
| 9 | public boolean receiver = false; |
| 10 | public boolean sender = false; |
| 11 | public long bandwidth = -1; |
| 12 | |
| 13 | public BandwidthBlock() { |
| 14 | this.setType(receiverBandwidth); |
| 15 | } |
| 16 | |
| 17 | public int parse(byte[] array, int offset, int packetLength) throws PacketParseException { |
| 18 | if(packetLength < 4*2) { |
| 19 | throw new PacketParseException("array too short!"); |
| 20 | } |
| 21 | // SBRT + Length |
| 22 | super.parse(array, offset, packetLength); |
| 23 | assert this.getType() == receiverBandwidth; |
| 24 | |
| 25 | sender = (array[2] & 0x80 ) != 0; |
| 26 | receiver = (array[2] & 0x40 ) != 0; |
| 27 | |
| 28 | // Bandwidth |
| 29 | this.bandwidth = ((array[4 + offset] & 0xff) << 24) |
| 30 | | ((array[5 + offset] & 0xff) << 16) |
| 31 | | ((array[6 + offset] & 0xff) << 8) |
| 32 | | (array[7 + offset] & 0xff); |
| 33 | |
| 34 | return 4*2; |
| 35 | } |
| 36 | |
| 37 | public int generate(byte[] array, int offset) throws PacketGenerateException { |
| 38 | assert this.getType() == receiverBandwidth; |
| 39 | this.length = 2*4; |
| 40 | // SBRT + length |
| 41 | super.generate(array, offset); |
| 42 | |
| 43 | if(sender) { |
| 44 | array[2] |= 0x80; // 0b10000000 |
| 45 | } else { |
| 46 | array[2] &= 0x7F; // 0b01111111 |
| 47 | } |
| 48 | if(receiver) { |
| 49 | array[2] |= 0x40; // 0b01000000 |
| 50 | } else { |
| 51 | array[2] &= 0xBF; // 0b10111111 |
| 52 | } |
| 53 | |
| 54 | // Median jitter |
| 55 | PacketUtils.setOctet4(array, offset+4, bandwidth); |
| 56 | return 4*2; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @see java.lang.Object#toString() |
| 61 | */ |
| 62 | public String toString() { |
| 63 | return new ToStringBuilder(this).append("type", this.getType()).append( |
| 64 | "length", this.length).toString(); |
| 65 | } |
| 66 | |
| 67 | } |