blob: 14a691aa8e91f3e03df2f9e13e6c9dbb138d2934 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
toma7083182014-09-25 21:38:03 -070019package org.onlab.nio;
20
21import java.nio.ByteBuffer;
22import java.nio.channels.ByteChannel;
23
tom1ae3d162014-09-26 09:38:16 -070024import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkState;
26
toma7083182014-09-25 21:38:03 -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: ";
tom1ae3d162014-09-26 09:38:16 -070033 private static final long START_TAG = 0xfeedcafedeaddeedL;
34 private static final long END_TAG = 0xbeadcafedeaddeedL;
35 private static final int META_LENGTH = 40;
toma7083182014-09-25 21:38:03 -070036
37 private final int length;
tom1ae3d162014-09-26 09:38:16 -070038 private boolean isStrict = true;
toma7083182014-09-25 21:38:03 -070039
tom1ae3d162014-09-26 09:38:16 -070040 public TestMessageStream(int length, ByteChannel ch, IOLoop<TestMessage, ?> loop) {
toma7083182014-09-25 21:38:03 -070041 super(loop, ch, 64 * 1024, 500);
tom1ae3d162014-09-26 09:38:16 -070042 checkArgument(length >= META_LENGTH, "Length must be greater than header length of 40");
toma7083182014-09-25 21:38:03 -070043 this.length = length;
44 }
45
tom1ae3d162014-09-26 09:38:16 -070046 void setNonStrict() {
47 isStrict = false;
48 }
49
toma7083182014-09-25 21:38:03 -070050 @Override
51 protected TestMessage read(ByteBuffer rb) {
52 if (rb.remaining() < length) {
53 return null;
54 }
tom1ae3d162014-09-26 09:38:16 -070055
56 long startTag = rb.getLong();
57 if (isStrict) {
58 checkState(startTag == START_TAG, "Incorrect message start");
59 }
60
61 long size = rb.getLong();
62 long requestorTime = rb.getLong();
63 long responderTime = rb.getLong();
64 byte[] padding = padding();
65 rb.get(padding);
66
67 long endTag = rb.getLong();
68 if (isStrict) {
69 checkState(endTag == END_TAG, "Incorrect message end");
70 }
71
72 return new TestMessage((int) size, requestorTime, responderTime, padding);
toma7083182014-09-25 21:38:03 -070073 }
74
toma7083182014-09-25 21:38:03 -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 }
tom1ae3d162014-09-26 09:38:16 -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);
toma7083182014-09-25 21:38:03 -070087 }
88
tom1ae3d162014-09-26 09:38:16 -070089 public byte[] padding() {
90 return new byte[length - META_LENGTH];
91 }
toma7083182014-09-25 21:38:03 -070092}