Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 3 | * |
| 4 | * 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 |
| 7 | * |
| 8 | * 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. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.routing.fpm; |
| 18 | |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.jboss.netty.channel.Channel; |
| 21 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 22 | import org.jboss.netty.handler.codec.frame.FrameDecoder; |
| 23 | import org.onosproject.routing.fpm.protocol.FpmHeader; |
| 24 | |
| 25 | /** |
| 26 | * Frame decoder for FPM connections. |
| 27 | */ |
| 28 | public class FpmFrameDecoder extends FrameDecoder { |
| 29 | |
| 30 | @Override |
| 31 | protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) |
| 32 | throws Exception { |
| 33 | |
| 34 | if (!channel.isConnected()) { |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | if (buffer.readableBytes() < FpmHeader.FPM_HEADER_LENGTH) { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | buffer.markReaderIndex(); |
| 43 | |
| 44 | short version = buffer.readUnsignedByte(); |
| 45 | short type = buffer.readUnsignedByte(); |
| 46 | int length = buffer.readUnsignedShort(); |
| 47 | |
| 48 | buffer.resetReaderIndex(); |
| 49 | |
| 50 | if (buffer.readableBytes() < length) { |
| 51 | // Not enough bytes to read a whole message |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | byte[] fpmMessage = new byte[length]; |
| 56 | buffer.readBytes(fpmMessage); |
| 57 | |
| 58 | return FpmHeader.decode(fpmMessage, 0, fpmMessage.length); |
| 59 | } |
| 60 | } |