blob: fc86beb96c332e91f4d816ce7aa31f203091e830 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
toma7083182014-09-25 21:38:03 -070016package org.onlab.nio;
17
18import java.nio.ByteBuffer;
19import java.nio.channels.ByteChannel;
20
tom1ae3d162014-09-26 09:38:16 -070021import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkState;
23
toma7083182014-09-25 21:38:03 -070024/**
25 * Fixed-length message transfer buffer.
26 */
27public class TestMessageStream extends MessageStream<TestMessage> {
28
29 private static final String E_WRONG_LEN = "Illegal message length: ";
tom1ae3d162014-09-26 09:38:16 -070030 private static final long START_TAG = 0xfeedcafedeaddeedL;
31 private static final long END_TAG = 0xbeadcafedeaddeedL;
32 private static final int META_LENGTH = 40;
toma7083182014-09-25 21:38:03 -070033
34 private final int length;
tom1ae3d162014-09-26 09:38:16 -070035 private boolean isStrict = true;
toma7083182014-09-25 21:38:03 -070036
tom1ae3d162014-09-26 09:38:16 -070037 public TestMessageStream(int length, ByteChannel ch, IOLoop<TestMessage, ?> loop) {
toma7083182014-09-25 21:38:03 -070038 super(loop, ch, 64 * 1024, 500);
tom1ae3d162014-09-26 09:38:16 -070039 checkArgument(length >= META_LENGTH, "Length must be greater than header length of 40");
toma7083182014-09-25 21:38:03 -070040 this.length = length;
41 }
42
tom1ae3d162014-09-26 09:38:16 -070043 void setNonStrict() {
44 isStrict = false;
45 }
46
toma7083182014-09-25 21:38:03 -070047 @Override
48 protected TestMessage read(ByteBuffer rb) {
49 if (rb.remaining() < length) {
50 return null;
51 }
tom1ae3d162014-09-26 09:38:16 -070052
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);
toma7083182014-09-25 21:38:03 -070070 }
71
toma7083182014-09-25 21:38:03 -070072 @Override
73 protected void write(TestMessage message, ByteBuffer wb) {
74 if (message.length() != length) {
75 throw new IllegalArgumentException(E_WRONG_LEN + message.length());
76 }
tom1ae3d162014-09-26 09:38:16 -070077
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);
toma7083182014-09-25 21:38:03 -070084 }
85
tom1ae3d162014-09-26 09:38:16 -070086 public byte[] padding() {
87 return new byte[length - META_LENGTH];
88 }
toma7083182014-09-25 21:38:03 -070089}