tom | cb43d60 | 2014-09-28 22:46:16 -0700 | [diff] [blame] | 1 | package org.onlab.onos.ccc; |
| 2 | |
| 3 | import org.onlab.nio.IOLoop; |
| 4 | import org.onlab.nio.MessageStream; |
| 5 | |
| 6 | import java.nio.ByteBuffer; |
| 7 | import java.nio.channels.ByteChannel; |
| 8 | |
| 9 | import static com.google.common.base.Preconditions.checkState; |
| 10 | |
| 11 | /** |
| 12 | * Stream for transferring TLV messages between cluster members. |
| 13 | */ |
| 14 | public class TLVMessageStream extends MessageStream<TLVMessage> { |
| 15 | |
| 16 | private static final long MARKER = 0xfeedcafecafefeedL; |
| 17 | |
| 18 | /** |
| 19 | * Creates a message stream associated with the specified IO loop and |
| 20 | * backed by the given byte channel. |
| 21 | * |
| 22 | * @param loop IO loop |
| 23 | * @param byteChannel backing byte channel |
| 24 | * @param bufferSize size of the backing byte buffers |
| 25 | * @param maxIdleMillis maximum number of millis the stream can be idle |
| 26 | */ |
| 27 | protected TLVMessageStream(IOLoop<TLVMessage, ?> loop, ByteChannel byteChannel, |
| 28 | int bufferSize, int maxIdleMillis) { |
| 29 | super(loop, byteChannel, bufferSize, maxIdleMillis); |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | protected TLVMessage read(ByteBuffer buffer) { |
| 34 | long marker = buffer.getLong(); |
| 35 | checkState(marker == MARKER, "Incorrect message marker"); |
| 36 | |
| 37 | int type = buffer.getInt(); |
| 38 | int length = buffer.getInt(); |
| 39 | |
| 40 | // TODO: add deserialization hook here |
| 41 | |
| 42 | return new TLVMessage(type, length, null); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | protected void write(TLVMessage message, ByteBuffer buffer) { |
| 47 | buffer.putLong(MARKER); |
| 48 | buffer.putInt(message.type()); |
| 49 | buffer.putInt(message.length()); |
| 50 | |
| 51 | // TODO: add serialization hook here |
| 52 | } |
| 53 | } |