| 1 | package cz.vutbr.feec.packets.rtcp; |
| 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 | import org.apache.commons.lang.builder.EqualsBuilder; |
| 9 | import org.apache.commons.lang.builder.HashCodeBuilder; |
| 10 | |
| 11 | /** |
| 12 | * This class encapsulates all the necessary parameters of a SDES Item that |
| 13 | * needs to be handed to the Application when a SDES Packet is received. |
| 14 | */ |
| 15 | public class SDESItem implements IPacket { |
| 16 | |
| 17 | public int generate(byte[] buffer, int offset) |
| 18 | throws PacketGenerateException { |
| 19 | assert getType() > 0 && getType() < 8; |
| 20 | assert PacketUtils.checkSize(getType(), 8); |
| 21 | assert PacketUtils.checkSize(getLengthOctets(), 8); |
| 22 | assert PacketUtils.checkSize(getText().getBytes().length, |
| 23 | getTextLength()); |
| 24 | |
| 25 | int len = 0; |
| 26 | PacketUtils.setOctet1(buffer, offset + len, getType()); |
| 27 | len++; |
| 28 | PacketUtils.setOctet1(buffer, offset + len, getLengthOctets()); |
| 29 | len++; |
| 30 | PacketUtils.setOctets(buffer, offset + len, getText(), |
| 31 | getTextLength()); |
| 32 | |
| 33 | // TODO overit zdali funguje spravne |
| 34 | len += getLengthOctets(); |
| 35 | return len; |
| 36 | } |
| 37 | |
| 38 | public int parse(byte[] buffer, int offset, int length) |
| 39 | throws PacketParseException { |
| 40 | int len = 0; |
| 41 | setType(PacketUtils.getOctet1(buffer, offset + len)); |
| 42 | len++; |
| 43 | textLength = PacketUtils.getOctet1(buffer, offset + len); |
| 44 | len++; |
| 45 | if(type == PRIV) { |
| 46 | this.prefixLength = PacketUtils.getOctet1(buffer, offset+len); |
| 47 | for(int i=0; i<prefixLength; ++i){ |
| 48 | text += (char)buffer[len + offset + i]; |
| 49 | } |
| 50 | len += 1 + prefixLength; |
| 51 | } |
| 52 | setText(PacketUtils.getOctets(buffer, offset + len, |
| 53 | textLength)); |
| 54 | len = len + textLength; |
| 55 | |
| 56 | return len; |
| 57 | } |
| 58 | |
| 59 | /** The Constant END. */ |
| 60 | final static public int END = 0; |
| 61 | |
| 62 | /** The Constant CNAME. */ |
| 63 | final static public int CNAME = 1; |
| 64 | |
| 65 | /** The Constant NAME. */ |
| 66 | final static public int NAME = 2; |
| 67 | |
| 68 | /** The Constant EMAIL. */ |
| 69 | final static public int EMAIL = 3; |
| 70 | |
| 71 | /** The Constant PHONE. */ |
| 72 | final static public int PHONE = 4; |
| 73 | |
| 74 | /** The Constant LOC. */ |
| 75 | final static public int LOC = 5; |
| 76 | |
| 77 | /** The Constant TOOL. */ |
| 78 | final static public int TOOL = 6; |
| 79 | |
| 80 | /** The Constant NOTE. */ |
| 81 | final static public int NOTE = 7; |
| 82 | |
| 83 | /** The Constant PRIV. */ |
| 84 | final static public int PRIV = 8; |
| 85 | |
| 86 | /** The type. */ |
| 87 | private int type = -1; |
| 88 | |
| 89 | /** The text length. */ |
| 90 | private int textLength; |
| 91 | |
| 92 | /** The prefix length. */ |
| 93 | private int prefixLength; |
| 94 | |
| 95 | /** The text. */ |
| 96 | private String text = ""; |
| 97 | |
| 98 | /** The prefix text. */ |
| 99 | private String prefixText = ""; |
| 100 | |
| 101 | /** |
| 102 | * Gets the length octets. |
| 103 | * |
| 104 | * @return length of packet (aligned to 32 bits) |
| 105 | */ |
| 106 | public int getLengthOctets() { |
| 107 | // header (SSRC+TYPE+LEN) |
| 108 | // text char = 1 octet |
| 109 | // prvni dva bytey se dle rfc neberou v potaz |
| 110 | return text.length(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Gets the text length. |
| 115 | * |
| 116 | * @return the text length |
| 117 | */ |
| 118 | public int getTextLength() { |
| 119 | return text.length(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Gets the text. |
| 124 | * |
| 125 | * @return the text |
| 126 | */ |
| 127 | public String getText() { |
| 128 | return text; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * String needs to be not longer than 255 characters. |
| 133 | * |
| 134 | * @param text |
| 135 | * the text |
| 136 | */ |
| 137 | public void setText(String text) { |
| 138 | // max. 255 chars |
| 139 | assert (text.length() <= 255); |
| 140 | this.textLength = text.length(); |
| 141 | this.text = text; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Gets the type. |
| 146 | * |
| 147 | * @return the type |
| 148 | */ |
| 149 | public int getType() { |
| 150 | return type; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Sets the type. |
| 155 | * |
| 156 | * @param type |
| 157 | * the type |
| 158 | */ |
| 159 | public void setType(int type) { |
| 160 | assert type > 0 && type < 8; |
| 161 | this.type = type; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @see java.lang.Object#toString() |
| 166 | */ |
| 167 | public String toString() { |
| 168 | return new ToStringBuilder(this).append("text", this.text).append( |
| 169 | "type", this.type).append("textLength", this.textLength) |
| 170 | .append("lengthOctets", this.getLengthOctets()).toString(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @see java.lang.Object#equals(Object) |
| 175 | */ |
| 176 | public boolean equals(Object object) { |
| 177 | if (!(object instanceof SDESItem)) { |
| 178 | return false; |
| 179 | } |
| 180 | SDESItem rhs = (SDESItem) object; |
| 181 | return new EqualsBuilder().append(this.text, rhs.text) |
| 182 | .append(this.textLength, rhs.textLength).append(this.type, |
| 183 | rhs.type).append(this.prefixLength, rhs.prefixLength) |
| 184 | .append(this.prefixText, rhs.prefixText) |
| 185 | .isEquals(); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @see java.lang.Object#hashCode() |
| 190 | */ |
| 191 | public int hashCode() { |
| 192 | return new HashCodeBuilder(1034310855, -1906885237).append(this.prefixText).append(this.text) |
| 193 | .append(this.textLength).append(this.type).append( |
| 194 | this.prefixLength).toHashCode(); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | } |