Used Splitter instead of String.split() - performance improvement
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFFrameDecoder.java
index 4a81194be39cddea47ce09508c2394df7b836ff7..dab263a2da458df32ce07fea8c0ff6af6e5fc188 100644 (file)
@@ -1,4 +1,11 @@
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 
 package org.opendaylight.openflowjava.protocol.impl.core;
 
@@ -8,12 +15,12 @@ import io.netty.handler.codec.ByteToMessageDecoder;
 
 import java.util.List;
 
+import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Class for decoding incoming messages into message frames.
- *
+ * Decodes incoming messages into message frames.
  * @author michal.polkorab
  */
 public class OFFrameDecoder extends ByteToMessageDecoder {
@@ -27,7 +34,7 @@ public class OFFrameDecoder extends ByteToMessageDecoder {
      * Constructor of class.
      */
     public OFFrameDecoder() {
-        LOGGER.debug("Creating OFFrameDecoder");
+        LOGGER.trace("Creating OFFrameDecoder");
     }
 
     @Override
@@ -38,19 +45,25 @@ public class OFFrameDecoder extends ByteToMessageDecoder {
 
     @Override
     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
-        if (bb.readableBytes() < LENGTH_OF_HEADER) {
-            LOGGER.debug("skipping bb - too few data for header: " + bb.readableBytes());
+        int readableBytes = bb.readableBytes();
+        if (readableBytes < LENGTH_OF_HEADER) {
+            LOGGER.debug("skipping bb - too few data for header: " + readableBytes);
             return;
         }
-
-        int length = bb.getUnsignedShort(LENGTH_INDEX_IN_HEADER);
-        if (bb.readableBytes() < length) {
-            LOGGER.debug("skipping bb - too few data for msg: " +
-                    bb.readableBytes() + " < " + length);
+        
+        int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
+        LOGGER.debug("length of actual message: {}", length);
+        
+        if (readableBytes < length) {
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug("skipping bb - too few data for msg: " +
+                        readableBytes + " < " + length);
+                LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));
+            }
             return;
         }
-        LOGGER.info("OF Protocol message received, type:{}", bb.getByte(1));
-
+        LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
+        
         ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
         list.add(messageBuffer);
         messageBuffer.retain();