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