Initial framework migration to netty.
[bgpcep.git] / framework / src / main / java / org / opendaylight / protocol / framework / ProtocolMessageDecoder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.framework;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.channel.ChannelHandlerContext;
12 import io.netty.channel.MessageList;
13 import io.netty.handler.codec.ByteToMessageDecoder;
14
15 final class ProtocolMessageDecoder extends ByteToMessageDecoder {
16
17         private final ProtocolMessageFactory factory;
18
19         public ProtocolMessageDecoder(final ProtocolMessageFactory factory) {
20                 this.factory = factory;
21         }
22
23         @Override
24         public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final MessageList<Object> out) throws Exception {
25                 ProtocolMessage msg = null;
26                 try {
27                         msg = this.factory.parse(in.array());
28                 } catch (DeserializerException | DocumentedException e) {
29                         this.exceptionCaught(ctx, e);
30                 }
31                 out.add(msg);
32         }
33
34         @Override
35         public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception {
36                 // TODO:
37                 ctx.close();
38         }
39 }