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 | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 9 | import static com.google.common.base.Preconditions.checkState; |
| 10 | |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 11 | /** |
| 12 | * Fixed-length message transfer buffer. |
| 13 | */ |
| 14 | public class TestMessageStream extends MessageStream<TestMessage> { |
| 15 | |
| 16 | private static final String E_WRONG_LEN = "Illegal message length: "; |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 17 | private static final long START_TAG = 0xfeedcafedeaddeedL; |
| 18 | private static final long END_TAG = 0xbeadcafedeaddeedL; |
| 19 | private static final int META_LENGTH = 40; |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 20 | |
| 21 | private final int length; |
| 22 | |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 23 | public TestMessageStream(int length, ByteChannel ch, IOLoop<TestMessage, ?> loop) { |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 24 | super(loop, ch, 64 * 1024, 500); |
| 25 | this.length = length; |
| 26 | } |
| 27 | |
| 28 | @Override |
| 29 | protected TestMessage read(ByteBuffer rb) { |
| 30 | if (rb.remaining() < length) { |
| 31 | return null; |
| 32 | } |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 33 | |
| 34 | long startTag = rb.getLong(); |
| 35 | checkState(startTag == START_TAG, "Incorrect message start"); |
| 36 | |
| 37 | long size = rb.getLong(); |
| 38 | long requestorTime = rb.getLong(); |
| 39 | long responderTime = rb.getLong(); |
| 40 | byte[] padding = padding(length); |
| 41 | rb.get(padding); |
| 42 | |
| 43 | long endTag = rb.getLong(); |
| 44 | checkState(endTag == END_TAG, "Incorrect message end"); |
| 45 | |
| 46 | return new TestMessage((int) size, requestorTime, responderTime, padding); |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 47 | } |
| 48 | |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 49 | @Override |
| 50 | protected void write(TestMessage message, ByteBuffer wb) { |
| 51 | if (message.length() != length) { |
| 52 | throw new IllegalArgumentException(E_WRONG_LEN + message.length()); |
| 53 | } |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 54 | |
| 55 | wb.putLong(START_TAG); |
| 56 | wb.putLong(message.length()); |
| 57 | wb.putLong(message.requestorTime()); |
| 58 | wb.putLong(message.responderTime()); |
| 59 | wb.put(message.padding(), 0, length - META_LENGTH); |
| 60 | wb.putLong(END_TAG); |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 61 | } |
| 62 | |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 63 | public byte[] padding(int msgLength) { |
| 64 | return new byte[msgLength - META_LENGTH]; |
| 65 | } |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 66 | } |