Integration test, SimpleClient bundle
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClientHandler.java
diff --git a/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientHandler.java b/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/SimpleClientHandler.java
new file mode 100644 (file)
index 0000000..f0990da
--- /dev/null
@@ -0,0 +1,69 @@
+/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
+\r
+package org.opendaylight.openflowjava.protocol.impl.clients;\r
+\r
+import io.netty.buffer.ByteBuf;\r
+import io.netty.channel.ChannelHandlerContext;\r
+import io.netty.channel.ChannelInboundHandlerAdapter;\r
+\r
+import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import com.google.common.util.concurrent.SettableFuture;\r
+\r
+/**\r
+ *\r
+ * @author michal.polkorab\r
+ */\r
+public class SimpleClientHandler extends ChannelInboundHandlerAdapter {\r
+\r
+    protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleClientHandler.class);\r
+    private SettableFuture<Boolean> isOnlineFuture;\r
+    protected ScenarioHandler scenarioHandler;\r
+\r
+    /**\r
+     * @param isOnlineFuture future notifier of connected channel\r
+     */\r
+    public SimpleClientHandler(SettableFuture<Boolean> isOnlineFuture, ScenarioHandler scenarioHandler) {\r
+        this.isOnlineFuture = isOnlineFuture;\r
+        this.scenarioHandler = scenarioHandler;\r
+    }\r
+\r
+    @Override\r
+    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {\r
+        LOGGER.info("SimpleClientHandler - start of read");\r
+        ByteBuf bb = (ByteBuf) msg;\r
+        if (LOGGER.isDebugEnabled()) {\r
+            LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(bb));\r
+        }\r
+        byte[] message = new byte[8];\r
+        bb.readBytes(message);\r
+        scenarioHandler.addOfMsg(message);\r
+        skipMsg(bb);\r
+        LOGGER.info("end of read");\r
+    }\r
+\r
+    @Override\r
+    public void channelActive(ChannelHandlerContext ctx) throws Exception {\r
+        LOGGER.info("Client is active");\r
+        if (isOnlineFuture != null) {\r
+            isOnlineFuture.set(true);\r
+            isOnlineFuture = null;\r
+        }\r
+        scenarioHandler.setCtx(ctx);\r
+        scenarioHandler.start();\r
+        \r
+    }\r
+\r
+    private static void skipMsg(ByteBuf bb) {\r
+        if (bb.readableBytes() > 0) {\r
+            bb.skipBytes(bb.getShort(2));\r
+        }\r
+    }\r
+\r
+    public void setScenario(ScenarioHandler scenarioHandler) {\r
+        this.scenarioHandler = scenarioHandler;\r
+    }\r
+\r
+}\r