2 * Copyright (c) 2013 Cisco Systems, Inc. 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
8 package org.opendaylight.protocol.framework;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.ByteBufUtil;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.handler.codec.ByteToMessageDecoder;
15 import java.util.List;
17 import com.google.common.base.Preconditions;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public final class ProtocolMessageDecoder<T> extends ByteToMessageDecoder {
24 private final static Logger LOG = LoggerFactory.getLogger(ProtocolMessageDecoder.class);
26 private final ProtocolMessageFactory<T> factory;
28 public ProtocolMessageDecoder(final ProtocolMessageFactory<T> factory) {
29 this.factory = Preconditions.checkNotNull(factory);
33 protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception {
34 if (in.readableBytes() == 0) {
35 LOG.debug("No more content in incoming buffer.");
40 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
41 final byte[] bytes = new byte[in.readableBytes()];
43 out.add(this.factory.parse(bytes));
44 } catch (DeserializerException | DocumentedException e) {
45 LOG.debug("Failed to decode protocol message", e);
46 this.exceptionCaught(ctx, e);
48 in.discardReadBytes();