46472e6e1e7261aecdd6ec10644110509f6b2fb3
[openflowplugin.git] / openflow_netty / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / core / internal / OFMessageDecoder.java
1 package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
2
3 import java.util.List;
4
5 import org.jboss.netty.buffer.ChannelBuffer;
6 import org.jboss.netty.channel.Channel;
7 import org.jboss.netty.channel.ChannelHandlerContext;
8 import org.jboss.netty.handler.codec.frame.FrameDecoder;
9 import org.openflow.protocol.OFMessage;
10 import org.openflow.protocol.factory.BasicFactory;
11 import org.openflow.protocol.factory.OFMessageFactory;
12
13 /**
14  * Decode an openflow message from a Channel, for use in a netty
15  * pipeline
16  * @author readams
17  */
18 public class OFMessageDecoder extends FrameDecoder {
19
20     OFMessageFactory factory = new BasicFactory();
21
22     @Override
23     protected Object decode(ChannelHandlerContext ctx, Channel channel,
24                             ChannelBuffer buffer) throws Exception {
25         if (!channel.isConnected()) {
26             // In testing, I see decode being called AFTER decode last.
27             // This check avoids that from reading curroupted frames
28             return null;
29         }
30
31         List<OFMessage> message = factory.parseMessage(buffer);
32         return message;
33     }
34
35     @Override
36     protected Object decodeLast(ChannelHandlerContext ctx, Channel channel,
37                             ChannelBuffer buffer) throws Exception {
38         // This is not strictly needed atthis time. It is used to detect
39         // connection reset detection from netty (for debug)
40         return null;
41     }
42
43 }