| 1 | package cz.vutbr.feec.packets.rsi; |
| 2 | |
| 3 | import cz.vutbr.feec.packets.IPacket; |
| 4 | import cz.vutbr.feec.packets.PacketGenerateException; |
| 5 | import cz.vutbr.feec.packets.PacketParseException; |
| 6 | import cz.vutbr.feec.packets.PacketUtils; |
| 7 | import org.apache.commons.lang.builder.ToStringBuilder; |
| 8 | |
| 9 | public class SubReportBlock implements IPacket { |
| 10 | // Feedback Address Target sub-report block |
| 11 | public static final int ipv4Address = 0; |
| 12 | public static final int ipv6Address = 1; |
| 13 | public static final int dnsUnicastFeedback = 2; |
| 14 | // Generic Sub-report Block |
| 15 | public static final int loss = 4; |
| 16 | public static final int jitter = 5; |
| 17 | public static final int rtt = 6; |
| 18 | public static final int cummulLoss = 7; |
| 19 | // Collisions sub-report block |
| 20 | public static final int collisionFeedback = 8; |
| 21 | // General Statistics sub-report block |
| 22 | public static final int generalStatistics = 10; |
| 23 | // RTCP Bandwidth Indication sub-report block |
| 24 | public static final int receiverBandwidth = 11; |
| 25 | // RTCP Group and Average Packet Size Sub-report Block |
| 26 | public static final int groupAndAvgPktSize = 12; |
| 27 | |
| 28 | private int type = -1; |
| 29 | protected int length = -1; |
| 30 | |
| 31 | public int generate(byte[] array, int offset) throws PacketGenerateException { |
| 32 | assert PacketUtils.checkSize(type, 8); |
| 33 | assert PacketUtils.checkSize(length, 8); |
| 34 | |
| 35 | array[offset + 0] = (byte)this.type; |
| 36 | array[offset + 1] = (byte)this.length; |
| 37 | return 2; |
| 38 | } |
| 39 | |
| 40 | public int parse(byte[] array, int offset, int packetLength) throws PacketParseException { |
| 41 | assert array.length >= packetLength+offset; |
| 42 | if(packetLength < 4){ |
| 43 | throw new PacketParseException("Parsed data is too short!"); |
| 44 | } |
| 45 | this.type = array[offset]; |
| 46 | this.length = array[offset+1]; |
| 47 | if(this.type < 0 || this.type > 12 || this.type == 3|| this.type == 9) { |
| 48 | throw new PacketParseException("Detected type:"+this.type+ " is unknown"); |
| 49 | } |
| 50 | return 2; |
| 51 | } |
| 52 | public int getLength() { |
| 53 | return length; |
| 54 | } |
| 55 | |
| 56 | public int getType() { |
| 57 | return type; |
| 58 | } |
| 59 | |
| 60 | protected void setType(int type) { |
| 61 | this.type = type; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @see java.lang.Object#toString() |
| 66 | */ |
| 67 | public String toString() { |
| 68 | return new ToStringBuilder(this).append("type", this.type).append( |
| 69 | "length", this.length).toString(); |
| 70 | } |
| 71 | } |