tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.store.cluster.impl; |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 2 | |
| 3 | import org.onlab.nio.AbstractMessage; |
| 4 | |
| 5 | import java.util.Objects; |
| 6 | |
| 7 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 8 | |
| 9 | /** |
| 10 | * Base message for cluster-wide communications using TLVs. |
| 11 | */ |
| 12 | public class TLVMessage extends AbstractMessage { |
| 13 | |
| 14 | private final int type; |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame^] | 15 | private final byte[] data; |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Creates an immutable TLV message. |
| 19 | * |
| 20 | * @param type message type |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame^] | 21 | * @param data message data bytes |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 22 | */ |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame^] | 23 | public TLVMessage(int type, byte[] data) { |
| 24 | this.length = data.length + TLVMessageStream.METADATA_LENGTH; |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 25 | this.type = type; |
| 26 | this.data = data; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Returns the message type indicator. |
| 31 | * |
| 32 | * @return message type |
| 33 | */ |
| 34 | public int type() { |
| 35 | return type; |
| 36 | } |
| 37 | |
| 38 | /** |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame^] | 39 | * Returns the data bytes. |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 40 | * |
| 41 | * @return message data |
| 42 | */ |
tom | 7309483 | 2014-09-29 13:47:08 -0700 | [diff] [blame^] | 43 | public byte[] data() { |
tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 44 | return data; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public int hashCode() { |
| 49 | return Objects.hash(type, data); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public boolean equals(Object obj) { |
| 54 | if (this == obj) { |
| 55 | return true; |
| 56 | } |
| 57 | if (obj == null || getClass() != obj.getClass()) { |
| 58 | return false; |
| 59 | } |
| 60 | final TLVMessage other = (TLVMessage) obj; |
| 61 | return Objects.equals(this.type, other.type) && |
| 62 | Objects.equals(this.data, other.data); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public String toString() { |
| 67 | return toStringHelper(this).add("type", type).add("length", length).toString(); |
| 68 | } |
| 69 | |
| 70 | } |