| 1 | package cz.vutbr.feec.packets.rsi; |
| 2 | |
| 3 | import java.util.Vector; |
| 4 | |
| 5 | import cz.vutbr.feec.packets.IPacket; |
| 6 | import cz.vutbr.feec.packets.PacketGenerateException; |
| 7 | import cz.vutbr.feec.packets.PacketParseException; |
| 8 | import cz.vutbr.feec.packets.PacketUtils; |
| 9 | import org.apache.commons.lang.builder.ToStringBuilder; |
| 10 | |
| 11 | abstract public class GenericBlock extends SubReportBlock implements IPacket { |
| 12 | private int numberOfDistributionBuckets = -1; |
| 13 | private int multiplicativeFactor = -1; |
| 14 | private long minimumDistributionValue = -1; |
| 15 | private long maximumDistributionValue = -1; |
| 16 | private Vector<Integer> buckets = new Vector<Integer>(); |
| 17 | |
| 18 | public int parse(byte[] resultArray, int offset, int packetLength) |
| 19 | throws PacketParseException { |
| 20 | super.parse(resultArray, offset, packetLength); |
| 21 | this.numberOfDistributionBuckets = ((((resultArray[2 + offset] & 0xff) << 4) | (resultArray[3 + offset] & 0xff)) >> 4) & 0xfff; |
| 22 | |
| 23 | this.multiplicativeFactor = resultArray[offset + 3] & 0xf; |
| 24 | |
| 25 | this.minimumDistributionValue = ((resultArray[4 + offset] & 0xff) << 24) |
| 26 | | ((resultArray[5 + offset] & 0xff) << 16) |
| 27 | | ((resultArray[6 + offset] & 0xff) << 8) |
| 28 | | (resultArray[7 + offset] & 0xff); |
| 29 | |
| 30 | this.maximumDistributionValue = ((resultArray[8 + offset] & 0xff) << 24) |
| 31 | | ((resultArray[9 + offset] & 0xff) << 16) |
| 32 | | ((resultArray[10 + offset] & 0xff) << 8) |
| 33 | | (resultArray[11 + offset] & 0xff); |
| 34 | |
| 35 | assert ((this.length * 4) - 12) * 8 / numberOfDistributionBuckets == 4; |
| 36 | |
| 37 | buckets.removeAllElements(); |
| 38 | for (int i = 0; i < numberOfDistributionBuckets/2; i++) { |
| 39 | byte actByte = resultArray[offset + 12]; |
| 40 | byte b1 = (byte) ((actByte >> 4) & 0xf); |
| 41 | byte b2 = (byte) (actByte & 0xf); |
| 42 | buckets.add(Integer.valueOf(b1)); |
| 43 | buckets.add(Integer.valueOf(b2)); |
| 44 | } |
| 45 | return 12 + (numberOfDistributionBuckets * 4) / 8; |
| 46 | } |
| 47 | |
| 48 | public void addBucket(int bucket) { |
| 49 | buckets.add(bucket); |
| 50 | } |
| 51 | |
| 52 | public int generate(byte[] array, int offset) |
| 53 | throws PacketGenerateException { |
| 54 | assert numberOfDistributionBuckets != 0 : "Paket bez Buckets nema zadny vyznam."; |
| 55 | assert PacketUtils.checkSize(getNumberOfDistributionBuckets(), 12); |
| 56 | assert PacketUtils.checkSize(multiplicativeFactor, 4); |
| 57 | assert PacketUtils.checkSize(buckets.size(), 12); |
| 58 | assert PacketUtils.checkSize(minimumDistributionValue, 32); |
| 59 | assert PacketUtils.checkSize(maximumDistributionValue, 32); |
| 60 | this.length = 3 + (buckets.size() + 7) / 8; |
| 61 | |
| 62 | assert ((length * 4 - 12) % numberOfDistributionBuckets) == 0 : "Paket je spatne nakongfigurovan. Delka a pocet bucketu neodopovidaji"; |
| 63 | |
| 64 | numberOfDistributionBuckets = buckets.size(); |
| 65 | // int buckBitLength = ((this.length * 4) - 12) * 8 / |
| 66 | // numberOfDistributionBuckets; |
| 67 | // length = (buckBitLength * numberOfDistributionBuckets / 8) / 8; |
| 68 | super.generate(array, offset); |
| 69 | assert ((this.length * 4) - 12) * 8 / numberOfDistributionBuckets == 4 : " Je podporovano pouze 4 bity na jeden bucket."; |
| 70 | PacketUtils.setOctet2(array, offset + 2, numberOfDistributionBuckets << 4); |
| 71 | // set to zeroes |
| 72 | array[offset + 3] ^= array[offset + 3] & 0x0f; |
| 73 | array[offset + 3] |= multiplicativeFactor & 0x0f; |
| 74 | PacketUtils.setOctet4(array, offset + 4, minimumDistributionValue); |
| 75 | PacketUtils.setOctet4(array, offset + 8, maximumDistributionValue); |
| 76 | |
| 77 | return length * 4; |
| 78 | } |
| 79 | |
| 80 | public int getNumberOfDistributionBuckets() { |
| 81 | return buckets.size(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @see java.lang.Object#toString() |
| 86 | */ |
| 87 | public String toString() { |
| 88 | return new ToStringBuilder(this).append("numberOfDistributionBuckets", |
| 89 | this.numberOfDistributionBuckets) |
| 90 | .append("type", this.getType()).append("length", this.length) |
| 91 | .toString(); |
| 92 | } |
| 93 | |
| 94 | public int getMultiplicativeFactor() { |
| 95 | return multiplicativeFactor; |
| 96 | } |
| 97 | |
| 98 | public void setMultiplicativeFactor(int multiplicativeFactor) { |
| 99 | this.multiplicativeFactor = multiplicativeFactor; |
| 100 | } |
| 101 | |
| 102 | public long getMinimumDistributionValue() { |
| 103 | return minimumDistributionValue; |
| 104 | } |
| 105 | |
| 106 | public void setMinimumDistributionValue(long minimumDistributionValue) { |
| 107 | this.minimumDistributionValue = minimumDistributionValue; |
| 108 | } |
| 109 | |
| 110 | public long getMaximumDistributionValue() { |
| 111 | return maximumDistributionValue; |
| 112 | } |
| 113 | |
| 114 | public void setMaximumDistributionValue(long maximumDistributionValue) { |
| 115 | this.maximumDistributionValue = maximumDistributionValue; |
| 116 | } |
| 117 | |
| 118 | } |