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