Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 16 | package org.onlab.onos.foo; |
| 17 | |
| 18 | import org.onlab.nio.IOLoop; |
| 19 | import org.onlab.nio.MessageStream; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.nio.channels.ByteChannel; |
| 23 | |
tom | f7e13b0 | 2014-09-26 11:12:25 -0700 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkArgument; |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkState; |
| 26 | |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Fixed-length message transfer buffer. |
| 29 | */ |
| 30 | public class TestMessageStream extends MessageStream<TestMessage> { |
| 31 | |
| 32 | private static final String E_WRONG_LEN = "Illegal message length: "; |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 33 | private static final long START_TAG = 0xfeedcafedeaddeedL; |
| 34 | private static final long END_TAG = 0xbeadcafedeaddeedL; |
| 35 | private static final int META_LENGTH = 40; |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 36 | |
| 37 | private final int length; |
tom | f7e13b0 | 2014-09-26 11:12:25 -0700 | [diff] [blame] | 38 | private boolean isStrict = true; |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 39 | |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 40 | public TestMessageStream(int length, ByteChannel ch, IOLoop<TestMessage, ?> loop) { |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 41 | super(loop, ch, 64 * 1024, 500); |
tom | f7e13b0 | 2014-09-26 11:12:25 -0700 | [diff] [blame] | 42 | checkArgument(length >= META_LENGTH, "Length must be greater than header length of 40"); |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 43 | this.length = length; |
| 44 | } |
| 45 | |
tom | f7e13b0 | 2014-09-26 11:12:25 -0700 | [diff] [blame] | 46 | void setNonStrict() { |
| 47 | isStrict = false; |
| 48 | } |
| 49 | |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 50 | @Override |
| 51 | protected TestMessage read(ByteBuffer rb) { |
| 52 | if (rb.remaining() < length) { |
| 53 | return null; |
| 54 | } |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 55 | |
| 56 | long startTag = rb.getLong(); |
tom | f7e13b0 | 2014-09-26 11:12:25 -0700 | [diff] [blame] | 57 | if (isStrict) { |
| 58 | checkState(startTag == START_TAG, "Incorrect message start"); |
| 59 | } |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 60 | |
| 61 | long size = rb.getLong(); |
| 62 | long requestorTime = rb.getLong(); |
| 63 | long responderTime = rb.getLong(); |
tom | f7e13b0 | 2014-09-26 11:12:25 -0700 | [diff] [blame] | 64 | byte[] padding = padding(); |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 65 | rb.get(padding); |
| 66 | |
| 67 | long endTag = rb.getLong(); |
tom | f7e13b0 | 2014-09-26 11:12:25 -0700 | [diff] [blame] | 68 | if (isStrict) { |
| 69 | checkState(endTag == END_TAG, "Incorrect message end"); |
| 70 | } |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 71 | |
| 72 | return new TestMessage((int) size, requestorTime, responderTime, padding); |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 73 | } |
| 74 | |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 75 | @Override |
| 76 | protected void write(TestMessage message, ByteBuffer wb) { |
| 77 | if (message.length() != length) { |
| 78 | throw new IllegalArgumentException(E_WRONG_LEN + message.length()); |
| 79 | } |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 80 | |
| 81 | wb.putLong(START_TAG); |
| 82 | wb.putLong(message.length()); |
| 83 | wb.putLong(message.requestorTime()); |
| 84 | wb.putLong(message.responderTime()); |
| 85 | wb.put(message.padding(), 0, length - META_LENGTH); |
| 86 | wb.putLong(END_TAG); |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 87 | } |
| 88 | |
tom | f7e13b0 | 2014-09-26 11:12:25 -0700 | [diff] [blame] | 89 | public byte[] padding() { |
| 90 | return new byte[length - META_LENGTH]; |
tom | 0e0863f | 2014-09-26 09:02:33 -0700 | [diff] [blame] | 91 | } |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 92 | } |