2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.openflowjava.protocol.impl.clients;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.ByteToMessageDecoder;
16 import java.util.List;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Class for decoding incoming messages into message frames.
24 * @author michal.polkorab
26 public class SimpleClientFramer extends ByteToMessageDecoder {
28 /** Length of OpenFlow 1.3 header */
29 public static final byte LENGTH_OF_HEADER = 8;
30 private static final byte LENGTH_INDEX_IN_HEADER = 2;
31 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleClientFramer.class);
34 * Constructor of class.
36 public SimpleClientFramer() {
37 LOGGER.trace("Creating OFFrameDecoder");
41 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
42 LOGGER.warn("Unexpected exception from downstream.", cause);
47 protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
48 if (bb.readableBytes() < LENGTH_OF_HEADER) {
49 LOGGER.debug("skipping bb - too few data for header: " + bb.readableBytes());
53 int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
54 if (bb.readableBytes() < length) {
55 LOGGER.debug("skipping bb - too few data for msg: " +
56 bb.readableBytes() + " < " + length);
59 LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
61 ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
62 list.add(messageBuffer);
63 messageBuffer.retain();