1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
3 package org.opendaylight.openflowjava.protocol.impl.clients;
\r
5 import io.netty.buffer.ByteBuf;
\r
6 import io.netty.channel.ChannelHandlerContext;
\r
7 import io.netty.channel.ChannelInboundHandlerAdapter;
\r
9 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
\r
10 import org.slf4j.Logger;
\r
11 import org.slf4j.LoggerFactory;
\r
13 import com.google.common.util.concurrent.SettableFuture;
\r
17 * @author michal.polkorab
\r
19 public class SimpleClientHandler extends ChannelInboundHandlerAdapter {
\r
21 protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleClientHandler.class);
\r
22 private static final int LENGTH_INDEX_IN_HEADER = 2;
\r
23 private SettableFuture<Boolean> isOnlineFuture;
\r
24 protected ScenarioHandler scenarioHandler;
\r
27 * @param isOnlineFuture future notifier of connected channel
\r
28 * @param scenarioHandler handler of scenario events
\r
30 public SimpleClientHandler(SettableFuture<Boolean> isOnlineFuture, ScenarioHandler scenarioHandler) {
\r
31 this.isOnlineFuture = isOnlineFuture;
\r
32 this.scenarioHandler = scenarioHandler;
\r
36 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
\r
37 ByteBuf bb = (ByteBuf) msg;
\r
38 if (LOGGER.isDebugEnabled()) {
\r
39 LOGGER.debug("<< " + ByteBufUtils.byteBufToHexString(bb));
\r
41 int length = bb.getUnsignedShort(LENGTH_INDEX_IN_HEADER);
\r
42 LOGGER.info("SimpleClientHandler - start of read");
\r
43 byte[] message = new byte[length];
\r
44 bb.readBytes(message);
\r
45 scenarioHandler.addOfMsg(message);
\r
46 LOGGER.info("end of read");
\r
50 public void channelActive(ChannelHandlerContext ctx) throws Exception {
\r
51 LOGGER.info("Client is active");
\r
52 if (isOnlineFuture != null) {
\r
53 isOnlineFuture.set(true);
\r
54 isOnlineFuture = null;
\r
56 scenarioHandler.setCtx(ctx);
\r
57 scenarioHandler.start();
\r
62 * @param scenarioHandler handler of scenario events
\r
64 public void setScenario(ScenarioHandler scenarioHandler) {
\r
65 this.scenarioHandler = scenarioHandler;
\r