| 1 | package cz.vutbr.feec.packets.rsi; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | import java.util.LinkedList; |
| 5 | |
| 6 | import org.apache.commons.lang.builder.ToStringBuilder; |
| 7 | |
| 8 | import org.apache.commons.lang.builder.EqualsBuilder; |
| 9 | |
| 10 | import cz.vutbr.feec.packets.IPacket; |
| 11 | import cz.vutbr.feec.packets.PacketGenerateException; |
| 12 | import cz.vutbr.feec.packets.PacketParseException; |
| 13 | import cz.vutbr.feec.packets.PacketUtils; |
| 14 | import cz.vutbr.feec.packets.rsi.AddressBlock.Type; |
| 15 | import org.apache.commons.lang.builder.HashCodeBuilder; |
| 16 | |
| 17 | /** |
| 18 | * This class encapsulates all the necessary parameters of a RTCP Packet that |
| 19 | * needs to be handed to the Application when a RTCP Packet is received.This |
| 20 | * class represents the least common denominator in an RTCP Packet. It is |
| 21 | * equivalent to an abstract base class from which RTCPSenderReportPacket and |
| 22 | * RTCPReceiverReportPacket derive from. |
| 23 | */ |
| 24 | public class RSIPacket implements IPacket { |
| 25 | |
| 26 | /** The payload type. */ |
| 27 | private int payloadType = 208; |
| 28 | |
| 29 | // 2 bit |
| 30 | /** The version. */ |
| 31 | private int version = 2; |
| 32 | |
| 33 | // 1 bit |
| 34 | /** The padding. */ |
| 35 | private int padding = 0; |
| 36 | |
| 37 | /** The length of this RTCP packet. */ |
| 38 | private int length = -1; |
| 39 | |
| 40 | /** The SSRC of distribution source. */ |
| 41 | private long SSRC = -1; |
| 42 | /** The SSRC. */ |
| 43 | private long SSRC_summarized = -1; |
| 44 | |
| 45 | /** |
| 46 | * Indicates the wallclock time when this report was sent so that it may be |
| 47 | * used in combination with timestamps returned in reception reports from |
| 48 | * other receivers to measure round-trip propagation to those receivers. |
| 49 | * NTPTimeStampMostSignificant - Most significant 32 bits of the NTP Time |
| 50 | * stamp |
| 51 | */ |
| 52 | private long NTPTimeStampMostSignificant = -1; |
| 53 | |
| 54 | /** Least significant 32 bits of the NTP Time stamp. */ |
| 55 | private long NTPTimeStampLeastSignificant = -1; |
| 56 | |
| 57 | // max size = 31 (receptionReportCount) |
| 58 | /** The report blocks. */ |
| 59 | private LinkedList<SubReportBlock> reportBlocks = new LinkedList<SubReportBlock>(); |
| 60 | |
| 61 | // private ReportBlock reportBlock[]; |
| 62 | |
| 63 | /** |
| 64 | * Gets the length. |
| 65 | * |
| 66 | * @return the length |
| 67 | */ |
| 68 | public int getLength() { |
| 69 | return length; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets the length. |
| 74 | * |
| 75 | * @param length |
| 76 | * the length |
| 77 | */ |
| 78 | protected void setLength(int length) { |
| 79 | this.length = length; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Sets the padding. |
| 84 | * |
| 85 | * @param padding |
| 86 | * the padding |
| 87 | */ |
| 88 | public void setPadding(int padding) { |
| 89 | if (padding != 0) { |
| 90 | // musi se rozsirit delka packetu a na posledni octet vlozit |
| 91 | // delku vkladanych oktetu vyjma posledniho |
| 92 | System.err.println("It has not been implemented padding!!"); |
| 93 | throw new RuntimeException( |
| 94 | "Value other than 0 is has not been implemented yet"); |
| 95 | } |
| 96 | this.padding = padding; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gets the report block. |
| 101 | * |
| 102 | * @param i |
| 103 | * the i |
| 104 | * |
| 105 | * @return the report block |
| 106 | */ |
| 107 | public SubReportBlock getReportBlock(int i) { |
| 108 | return reportBlocks.get(i); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Adds the report block. |
| 113 | * |
| 114 | * @param reportBlock |
| 115 | * the report block |
| 116 | */ |
| 117 | public void addReportBlock(SubReportBlock reportBlock) { |
| 118 | this.reportBlocks.addLast(reportBlock); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Gets the version. |
| 123 | * |
| 124 | * @return the version |
| 125 | */ |
| 126 | public int getVersion() { |
| 127 | return version; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Generate header. |
| 132 | * |
| 133 | * @param resultArray |
| 134 | * the result array |
| 135 | * |
| 136 | * @return the int |
| 137 | */ |
| 138 | private int generateHeader(byte[] resultArray, int offset) { |
| 139 | // in 32-bit words |
| 140 | assert PacketUtils.checkSize(payloadType, 8) : "PT presahuje svou mez!! " |
| 141 | + payloadType; |
| 142 | assert PacketUtils.checkSize(this.length, 16) : "LENGTH presahuje svou mez!!"; |
| 143 | assert PacketUtils.checkSize(this.SSRC, 32) : "SSRC presahuje svou mez!!"; |
| 144 | assert PacketUtils.checkSize(version, 2) : "VERSION presahuje svou mez!!"; |
| 145 | assert PacketUtils.checkSize((long) SSRC_summarized, 32) : "SSRC pro RBlock presahuje svou mez!!"; |
| 146 | assert PacketUtils.checkSize((long) NTPTimeStampMostSignificant, 32); |
| 147 | assert PacketUtils.checkSize((long) NTPTimeStampLeastSignificant, 32); |
| 148 | |
| 149 | byte V_P_RC; |
| 150 | V_P_RC = (byte) ((getVersion() << 6) | (this.getPadding() << 5) | 0); |
| 151 | byte PT = (byte) getPayloadType(); |
| 152 | |
| 153 | resultArray[0 + offset] = V_P_RC; |
| 154 | resultArray[1 + offset] = PT; |
| 155 | |
| 156 | // compute total packet length |
| 157 | final int HEADER = 4 * (5 - 1); |
| 158 | int reportBlocksLength = 0; |
| 159 | for (SubReportBlock b : reportBlocks) { |
| 160 | reportBlocksLength += b.getLength(); |
| 161 | } |
| 162 | this.length = HEADER + reportBlocksLength; |
| 163 | |
| 164 | // length |
| 165 | PacketUtils.setOctet2(resultArray, offset + 2, this.length); |
| 166 | PacketUtils.setOctet4(resultArray, offset + 4, this.SSRC); |
| 167 | PacketUtils.setOctet4(resultArray, offset + 8, this.SSRC_summarized); |
| 168 | PacketUtils.setOctet4(resultArray, offset + 12, |
| 169 | this.NTPTimeStampMostSignificant); |
| 170 | PacketUtils.setOctet4(resultArray, offset + 16, |
| 171 | this.NTPTimeStampLeastSignificant); |
| 172 | return 5 * 4; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Generate report block. |
| 177 | * |
| 178 | * @param startPos |
| 179 | * position in array where start to generate bytes |
| 180 | * @param resultArray |
| 181 | * where will be stored output |
| 182 | * @param rp |
| 183 | * Report packet |
| 184 | * |
| 185 | * @return length of generated bytes |
| 186 | * @throws PacketGenerateException |
| 187 | */ |
| 188 | public int generate(byte[] resultArray, int startPos) |
| 189 | throws PacketGenerateException { |
| 190 | int len = generateHeader(resultArray, startPos); |
| 191 | |
| 192 | // generate report blocks |
| 193 | for (Iterator<SubReportBlock> it = reportBlocks.iterator(); it |
| 194 | .hasNext();) { |
| 195 | SubReportBlock subBlock = it.next(); |
| 196 | len += subBlock.generate(resultArray, startPos + len); |
| 197 | } |
| 198 | return len; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Parse byte array and fill packet. |
| 203 | * |
| 204 | * @param startIndex |
| 205 | * in array |
| 206 | * @param array |
| 207 | * input |
| 208 | * |
| 209 | * @return length in bytes |
| 210 | * @throws PacketGenerateException |
| 211 | */ |
| 212 | public int parseHeader(byte[] array, int startIndex) |
| 213 | throws PacketParseException { |
| 214 | if (array.length < startIndex + 5 * 4) { |
| 215 | throw new PacketParseException("Received packet too short !"); |
| 216 | } |
| 217 | |
| 218 | // 5 bits, only for parsing and validating # of ReportBlocks |
| 219 | this.version = (array[0 + startIndex] & 0xc0) >> 6; |
| 220 | this.padding = (array[0 + startIndex] & 0x20) >> 5; |
| 221 | this.payloadType = array[1 + startIndex] & 0xFF; |
| 222 | this.length = PacketUtils.getOctet2(array, startIndex + 2); |
| 223 | this.SSRC = PacketUtils.getOctet4(array, startIndex + 4); |
| 224 | this.SSRC_summarized = PacketUtils.getOctet4(array, startIndex + 8); |
| 225 | this.NTPTimeStampMostSignificant = PacketUtils.getOctet4(array, |
| 226 | startIndex + 12); |
| 227 | this.NTPTimeStampLeastSignificant = PacketUtils.getOctet4(array, |
| 228 | startIndex + 16); |
| 229 | return 5 * 4; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Parses the report block. |
| 234 | * |
| 235 | * @param packet |
| 236 | * the packet |
| 237 | * @param startIndex |
| 238 | * the start index |
| 239 | * @param array |
| 240 | * the array |
| 241 | * |
| 242 | * @return the int packet length |
| 243 | * @throws PacketParseException |
| 244 | */ |
| 245 | public int parse(byte[] array, int startIndex, int packetLength) |
| 246 | throws PacketParseException { |
| 247 | int len = parseHeader(array, startIndex); |
| 248 | SubReportBlock srb = new SubReportBlock(); |
| 249 | reportBlocks.clear(); |
| 250 | while ((len + 4) <= packetLength - startIndex) { |
| 251 | // parse subreport block header |
| 252 | srb.parse(array, startIndex + len, packetLength); |
| 253 | |
| 254 | // parse subreport block |
| 255 | switch (srb.getType()) { |
| 256 | case SubReportBlock.generalStatistics: |
| 257 | srb = new StatisticsBlock(); |
| 258 | break; |
| 259 | case SubReportBlock.jitter: |
| 260 | srb = new GenericBlockJitter(); |
| 261 | break; |
| 262 | case SubReportBlock.loss: |
| 263 | srb = new GenericBlockLoss(); |
| 264 | break; |
| 265 | case SubReportBlock.cummulLoss: |
| 266 | srb = new GenericBlockCummuLoss(); |
| 267 | break; |
| 268 | case SubReportBlock.groupAndAvgPktSize: |
| 269 | srb = new GroupAndAvgPktSizeBlock(); |
| 270 | break; |
| 271 | case SubReportBlock.ipv4Address: |
| 272 | srb = new AddressBlock(Type.IPv4); |
| 273 | break; |
| 274 | case SubReportBlock.ipv6Address: |
| 275 | srb = new AddressBlock(Type.IPv6); |
| 276 | break; |
| 277 | case SubReportBlock.collisionFeedback: |
| 278 | srb = new CollisionsBlock(); |
| 279 | break; |
| 280 | default: |
| 281 | throw new PacketParseException( |
| 282 | "Nebylo jeste implementovano anebo je neznamy typ!"); |
| 283 | } |
| 284 | len += srb.parse(array, startIndex + len, packetLength); |
| 285 | addReportBlock(srb); |
| 286 | } |
| 287 | |
| 288 | return len; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Gets the payload type. |
| 293 | * |
| 294 | * @return the payload type |
| 295 | */ |
| 296 | public int getPayloadType() { |
| 297 | return payloadType; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Sets the payload type. |
| 302 | * |
| 303 | * @param payloadType |
| 304 | * the payload type |
| 305 | */ |
| 306 | protected void setPayloadType(int payloadType) { |
| 307 | this.payloadType = payloadType; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Gets the padding. |
| 312 | * |
| 313 | * @return the padding |
| 314 | */ |
| 315 | public int getPadding() { |
| 316 | return padding; |
| 317 | } |
| 318 | |
| 319 | public int getReportBlockCount() { |
| 320 | return reportBlocks.size(); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Equals. |
| 325 | * |
| 326 | * @param object |
| 327 | * the object |
| 328 | * |
| 329 | * @return true, if equals |
| 330 | * |
| 331 | * @see java.lang.Object#equals(Object) |
| 332 | */ |
| 333 | public boolean equals(Object object) { |
| 334 | if (!(object instanceof RSIPacket)) { |
| 335 | return false; |
| 336 | } |
| 337 | RSIPacket rhs = (RSIPacket) object; |
| 338 | |
| 339 | return new EqualsBuilder().append(this.payloadType, rhs.payloadType) |
| 340 | .append(this.SSRC, rhs.SSRC).append(this.padding, rhs.padding) |
| 341 | .append(this.reportBlocks, rhs.reportBlocks).append( |
| 342 | this.length, rhs.length).append(this.version, |
| 343 | rhs.version).isEquals(); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * @see java.lang.Object#toString() |
| 348 | */ |
| 349 | public String toString() { |
| 350 | return new ToStringBuilder(this).append("version", this.version) |
| 351 | .append("padding", this.padding).append("payloadType", |
| 352 | this.payloadType).append("reportBlockCount", |
| 353 | this.getReportBlockCount()).append("length", |
| 354 | this.length).toString(); |
| 355 | } |
| 356 | |
| 357 | public long getNTPTimeStampMostSignificant() { |
| 358 | return NTPTimeStampMostSignificant; |
| 359 | } |
| 360 | |
| 361 | public void setNTPTimeStampMostSignificant(long timeStampMostSignificant) { |
| 362 | NTPTimeStampMostSignificant = timeStampMostSignificant; |
| 363 | } |
| 364 | |
| 365 | public long getNTPTimeStampLeastSignificant() { |
| 366 | return NTPTimeStampLeastSignificant; |
| 367 | } |
| 368 | |
| 369 | public void setNTPTimeStampLeastSignificant(long timeStampLeastSignificant) { |
| 370 | NTPTimeStampLeastSignificant = timeStampLeastSignificant; |
| 371 | } |
| 372 | |
| 373 | public long getSSRC() { |
| 374 | return SSRC; |
| 375 | } |
| 376 | |
| 377 | public void setSSRC(long ssrc) { |
| 378 | SSRC = ssrc; |
| 379 | } |
| 380 | |
| 381 | public long getSSRCSummarized() { |
| 382 | return SSRC_summarized; |
| 383 | } |
| 384 | |
| 385 | public void setSSRCSummarized(long ssrc_summarized) { |
| 386 | SSRC_summarized = ssrc_summarized; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * @see java.lang.Object#hashCode() |
| 391 | */ |
| 392 | public int hashCode() { |
| 393 | return new HashCodeBuilder(1241368359, -1433542427).append(this.reportBlocks).append( |
| 394 | this.payloadType).append(this.NTPTimeStampLeastSignificant) |
| 395 | .append(this.length).append(this.NTPTimeStampMostSignificant) |
| 396 | .append(this.SSRC_summarized).append(this.padding).append( |
| 397 | this.SSRC).append(this.version).toHashCode(); |
| 398 | } |
| 399 | } |