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;
15 import java.util.List;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
20 * Class for decoding incoming messages into message frames.
22 * @author michal.polkorab
24 public class SimpleClientFramer extends ByteToMessageDecoder {
26 /** Length of OpenFlow 1.3 header. */
27 public static final byte LENGTH_OF_HEADER = 8;
28 private static final byte LENGTH_INDEX_IN_HEADER = 2;
29 private static final Logger LOG = LoggerFactory.getLogger(SimpleClientFramer.class);
32 * Constructor of class.
34 public SimpleClientFramer() {
35 LOG.trace("Creating OFFrameDecoder");
39 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
40 LOG.warn("Unexpected exception from downstream.", cause);
45 protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) {
46 if (bb.readableBytes() < LENGTH_OF_HEADER) {
47 LOG.debug("skipping bb - too few data for header: {}", bb.readableBytes());
51 int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
52 if (bb.readableBytes() < length) {
53 LOG.debug("skipping bb - too few data for msg: {} < {}", bb.readableBytes(), length);
56 LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
58 ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
59 list.add(messageBuffer);
60 messageBuffer.retain();