tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 1 | package org.onlab.nio; |
| 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | import java.nio.channels.ByteChannel; |
| 5 | |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 6 | import static com.google.common.base.Preconditions.checkArgument; |
| 7 | import static com.google.common.base.Preconditions.checkState; |
| 8 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 9 | /** |
| 10 | * Fixed-length message transfer buffer. |
| 11 | */ |
| 12 | public class TestMessageStream extends MessageStream<TestMessage> { |
| 13 | |
| 14 | private static final String E_WRONG_LEN = "Illegal message length: "; |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 15 | private static final long START_TAG = 0xfeedcafedeaddeedL; |
| 16 | private static final long END_TAG = 0xbeadcafedeaddeedL; |
| 17 | private static final int META_LENGTH = 40; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 18 | |
| 19 | private final int length; |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 20 | private boolean isStrict = true; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 21 | |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 22 | public TestMessageStream(int length, ByteChannel ch, IOLoop<TestMessage, ?> loop) { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 23 | super(loop, ch, 64 * 1024, 500); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 24 | checkArgument(length >= META_LENGTH, "Length must be greater than header length of 40"); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 25 | this.length = length; |
| 26 | } |
| 27 | |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 28 | void setNonStrict() { |
| 29 | isStrict = false; |
| 30 | } |
| 31 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 32 | @Override |
| 33 | protected TestMessage read(ByteBuffer rb) { |
| 34 | if (rb.remaining() < length) { |
| 35 | return null; |
| 36 | } |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 37 | |
| 38 | long startTag = rb.getLong(); |
| 39 | if (isStrict) { |
| 40 | checkState(startTag == START_TAG, "Incorrect message start"); |
| 41 | } |
| 42 | |
| 43 | long size = rb.getLong(); |
| 44 | long requestorTime = rb.getLong(); |
| 45 | long responderTime = rb.getLong(); |
| 46 | byte[] padding = padding(); |
| 47 | rb.get(padding); |
| 48 | |
| 49 | long endTag = rb.getLong(); |
| 50 | if (isStrict) { |
| 51 | checkState(endTag == END_TAG, "Incorrect message end"); |
| 52 | } |
| 53 | |
| 54 | return new TestMessage((int) size, requestorTime, responderTime, padding); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 55 | } |
| 56 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 57 | @Override |
| 58 | protected void write(TestMessage message, ByteBuffer wb) { |
| 59 | if (message.length() != length) { |
| 60 | throw new IllegalArgumentException(E_WRONG_LEN + message.length()); |
| 61 | } |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 62 | |
| 63 | wb.putLong(START_TAG); |
| 64 | wb.putLong(message.length()); |
| 65 | wb.putLong(message.requestorTime()); |
| 66 | wb.putLong(message.responderTime()); |
| 67 | wb.put(message.padding(), 0, length - META_LENGTH); |
| 68 | wb.putLong(END_TAG); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 69 | } |
| 70 | |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame] | 71 | public byte[] padding() { |
| 72 | return new byte[length - META_LENGTH]; |
| 73 | } |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 74 | } |