blob: f18a5df1398097a3caf367c003a2dd0f94cfee6a [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -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 Vachuska781d18b2014-10-27 10:31:25 -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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
tomf110fff2014-09-26 00:38:18 -070016package org.onlab.onos.foo;
17
18import org.onlab.nio.IOLoop;
19import org.onlab.nio.MessageStream;
20
21import java.nio.ByteBuffer;
22import java.nio.channels.ByteChannel;
23
tomf7e13b02014-09-26 11:12:25 -070024import static com.google.common.base.Preconditions.checkArgument;
tom0e0863f2014-09-26 09:02:33 -070025import static com.google.common.base.Preconditions.checkState;
26
tomf110fff2014-09-26 00:38:18 -070027/**
28 * Fixed-length message transfer buffer.
29 */
30public class TestMessageStream extends MessageStream<TestMessage> {
31
32 private static final String E_WRONG_LEN = "Illegal message length: ";
tom0e0863f2014-09-26 09:02:33 -070033 private static final long START_TAG = 0xfeedcafedeaddeedL;
34 private static final long END_TAG = 0xbeadcafedeaddeedL;
35 private static final int META_LENGTH = 40;
tomf110fff2014-09-26 00:38:18 -070036
37 private final int length;
tomf7e13b02014-09-26 11:12:25 -070038 private boolean isStrict = true;
tomf110fff2014-09-26 00:38:18 -070039
tom0e0863f2014-09-26 09:02:33 -070040 public TestMessageStream(int length, ByteChannel ch, IOLoop<TestMessage, ?> loop) {
tomf110fff2014-09-26 00:38:18 -070041 super(loop, ch, 64 * 1024, 500);
tomf7e13b02014-09-26 11:12:25 -070042 checkArgument(length >= META_LENGTH, "Length must be greater than header length of 40");
tomf110fff2014-09-26 00:38:18 -070043 this.length = length;
44 }
45
tomf7e13b02014-09-26 11:12:25 -070046 void setNonStrict() {
47 isStrict = false;
48 }
49
tomf110fff2014-09-26 00:38:18 -070050 @Override
51 protected TestMessage read(ByteBuffer rb) {
52 if (rb.remaining() < length) {
53 return null;
54 }
tom0e0863f2014-09-26 09:02:33 -070055
56 long startTag = rb.getLong();
tomf7e13b02014-09-26 11:12:25 -070057 if (isStrict) {
58 checkState(startTag == START_TAG, "Incorrect message start");
59 }
tom0e0863f2014-09-26 09:02:33 -070060
61 long size = rb.getLong();
62 long requestorTime = rb.getLong();
63 long responderTime = rb.getLong();
tomf7e13b02014-09-26 11:12:25 -070064 byte[] padding = padding();
tom0e0863f2014-09-26 09:02:33 -070065 rb.get(padding);
66
67 long endTag = rb.getLong();
tomf7e13b02014-09-26 11:12:25 -070068 if (isStrict) {
69 checkState(endTag == END_TAG, "Incorrect message end");
70 }
tom0e0863f2014-09-26 09:02:33 -070071
72 return new TestMessage((int) size, requestorTime, responderTime, padding);
tomf110fff2014-09-26 00:38:18 -070073 }
74
tomf110fff2014-09-26 00:38:18 -070075 @Override
76 protected void write(TestMessage message, ByteBuffer wb) {
77 if (message.length() != length) {
78 throw new IllegalArgumentException(E_WRONG_LEN + message.length());
79 }
tom0e0863f2014-09-26 09:02:33 -070080
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);
tomf110fff2014-09-26 00:38:18 -070087 }
88
tomf7e13b02014-09-26 11:12:25 -070089 public byte[] padding() {
90 return new byte[length - META_LENGTH];
tom0e0863f2014-09-26 09:02:33 -070091 }
tomf110fff2014-09-26 00:38:18 -070092}