blob: c97ab026d71d0431c96a5a744e59aec3f741498f [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -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 */
tomf110fff2014-09-26 00:38:18 -070019package org.onlab.onos.foo;
20
21import org.onlab.nio.IOLoop;
22import org.onlab.nio.MessageStream;
23
24import java.nio.ByteBuffer;
25import java.nio.channels.ByteChannel;
26
tomf7e13b02014-09-26 11:12:25 -070027import static com.google.common.base.Preconditions.checkArgument;
tom0e0863f2014-09-26 09:02:33 -070028import static com.google.common.base.Preconditions.checkState;
29
tomf110fff2014-09-26 00:38:18 -070030/**
31 * Fixed-length message transfer buffer.
32 */
33public class TestMessageStream extends MessageStream<TestMessage> {
34
35 private static final String E_WRONG_LEN = "Illegal message length: ";
tom0e0863f2014-09-26 09:02:33 -070036 private static final long START_TAG = 0xfeedcafedeaddeedL;
37 private static final long END_TAG = 0xbeadcafedeaddeedL;
38 private static final int META_LENGTH = 40;
tomf110fff2014-09-26 00:38:18 -070039
40 private final int length;
tomf7e13b02014-09-26 11:12:25 -070041 private boolean isStrict = true;
tomf110fff2014-09-26 00:38:18 -070042
tom0e0863f2014-09-26 09:02:33 -070043 public TestMessageStream(int length, ByteChannel ch, IOLoop<TestMessage, ?> loop) {
tomf110fff2014-09-26 00:38:18 -070044 super(loop, ch, 64 * 1024, 500);
tomf7e13b02014-09-26 11:12:25 -070045 checkArgument(length >= META_LENGTH, "Length must be greater than header length of 40");
tomf110fff2014-09-26 00:38:18 -070046 this.length = length;
47 }
48
tomf7e13b02014-09-26 11:12:25 -070049 void setNonStrict() {
50 isStrict = false;
51 }
52
tomf110fff2014-09-26 00:38:18 -070053 @Override
54 protected TestMessage read(ByteBuffer rb) {
55 if (rb.remaining() < length) {
56 return null;
57 }
tom0e0863f2014-09-26 09:02:33 -070058
59 long startTag = rb.getLong();
tomf7e13b02014-09-26 11:12:25 -070060 if (isStrict) {
61 checkState(startTag == START_TAG, "Incorrect message start");
62 }
tom0e0863f2014-09-26 09:02:33 -070063
64 long size = rb.getLong();
65 long requestorTime = rb.getLong();
66 long responderTime = rb.getLong();
tomf7e13b02014-09-26 11:12:25 -070067 byte[] padding = padding();
tom0e0863f2014-09-26 09:02:33 -070068 rb.get(padding);
69
70 long endTag = rb.getLong();
tomf7e13b02014-09-26 11:12:25 -070071 if (isStrict) {
72 checkState(endTag == END_TAG, "Incorrect message end");
73 }
tom0e0863f2014-09-26 09:02:33 -070074
75 return new TestMessage((int) size, requestorTime, responderTime, padding);
tomf110fff2014-09-26 00:38:18 -070076 }
77
tomf110fff2014-09-26 00:38:18 -070078 @Override
79 protected void write(TestMessage message, ByteBuffer wb) {
80 if (message.length() != length) {
81 throw new IllegalArgumentException(E_WRONG_LEN + message.length());
82 }
tom0e0863f2014-09-26 09:02:33 -070083
84 wb.putLong(START_TAG);
85 wb.putLong(message.length());
86 wb.putLong(message.requestorTime());
87 wb.putLong(message.responderTime());
88 wb.put(message.padding(), 0, length - META_LENGTH);
89 wb.putLong(END_TAG);
tomf110fff2014-09-26 00:38:18 -070090 }
91
tomf7e13b02014-09-26 11:12:25 -070092 public byte[] padding() {
93 return new byte[length - META_LENGTH];
tom0e0863f2014-09-26 09:02:33 -070094 }
tomf110fff2014-09-26 00:38:18 -070095}