Improved logging in ScenarioHandler
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClientHandler.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 \r
3 package org.opendaylight.openflowjava.protocol.impl.clients;\r
4 \r
5 import io.netty.buffer.ByteBuf;\r
6 import io.netty.channel.ChannelHandlerContext;\r
7 import io.netty.channel.ChannelInboundHandlerAdapter;\r
8 \r
9 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
10 import org.slf4j.Logger;\r
11 import org.slf4j.LoggerFactory;\r
12 \r
13 import com.google.common.util.concurrent.SettableFuture;\r
14 \r
15 /**\r
16  *\r
17  * @author michal.polkorab\r
18  */\r
19 public class SimpleClientHandler extends ChannelInboundHandlerAdapter {\r
20 \r
21     protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleClientHandler.class);\r
22     private SettableFuture<Boolean> isOnlineFuture;\r
23     protected ScenarioHandler scenarioHandler;\r
24 \r
25     /**\r
26      * @param isOnlineFuture future notifier of connected channel\r
27      * @param scenarioHandler handler of scenario events\r
28      */\r
29     public SimpleClientHandler(SettableFuture<Boolean> isOnlineFuture, ScenarioHandler scenarioHandler) {\r
30         this.isOnlineFuture = isOnlineFuture;\r
31         this.scenarioHandler = scenarioHandler;\r
32     }\r
33 \r
34     @Override\r
35     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {\r
36         LOGGER.info("SimpleClientHandler - start of read");\r
37         ByteBuf bb = (ByteBuf) msg;\r
38         if (LOGGER.isDebugEnabled()) {\r
39             LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(bb));\r
40         }\r
41         byte[] message = new byte[8];\r
42         bb.readBytes(message);\r
43         scenarioHandler.addOfMsg(message);\r
44         skipMsg(bb);\r
45         LOGGER.info("end of read");\r
46     }\r
47 \r
48     @Override\r
49     public void channelActive(ChannelHandlerContext ctx) throws Exception {\r
50         LOGGER.info("Client is active");\r
51         if (isOnlineFuture != null) {\r
52             isOnlineFuture.set(true);\r
53             isOnlineFuture = null;\r
54         }\r
55         scenarioHandler.setCtx(ctx);\r
56         scenarioHandler.start();\r
57         \r
58     }\r
59 \r
60     private static void skipMsg(ByteBuf bb) {\r
61         if (bb.readableBytes() > 0) {\r
62             bb.skipBytes(bb.getShort(2));\r
63         }\r
64     }\r
65 \r
66     /**\r
67      * @param scenarioHandler handler of scenario events\r
68      */\r
69     public void setScenario(ScenarioHandler scenarioHandler) {\r
70         this.scenarioHandler = scenarioHandler;\r
71     }\r
72 \r
73 }\r