2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.openflowjava.protocol.impl.clients;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelInboundHandlerAdapter;
16 import org.opendaylight.openflowjava.util.ByteBufUtils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import com.google.common.util.concurrent.SettableFuture;
24 * @author michal.polkorab
26 public class SimpleClientHandler extends ChannelInboundHandlerAdapter {
28 protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleClientHandler.class);
29 private static final int LENGTH_INDEX_IN_HEADER = 2;
30 private SettableFuture<Boolean> isOnlineFuture;
31 protected ScenarioHandler scenarioHandler;
34 * @param isOnlineFuture future notifier of connected channel
35 * @param scenarioHandler handler of scenario events
37 public SimpleClientHandler(SettableFuture<Boolean> isOnlineFuture, ScenarioHandler scenarioHandler) {
38 this.isOnlineFuture = isOnlineFuture;
39 this.scenarioHandler = scenarioHandler;
43 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
44 ByteBuf bb = (ByteBuf) msg;
45 if (LOGGER.isDebugEnabled()) {
46 LOGGER.debug("<< {}", ByteBufUtils.byteBufToHexString(bb));
48 int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
49 LOGGER.trace("SimpleClientHandler - start of read");
50 byte[] message = new byte[length];
51 bb.readBytes(message);
52 scenarioHandler.addOfMsg(message);
53 LOGGER.trace("end of read");
57 public void channelActive(ChannelHandlerContext ctx) throws Exception {
58 LOGGER.debug("Client is active");
59 if (isOnlineFuture != null) {
60 isOnlineFuture.set(true);
61 isOnlineFuture = null;
63 scenarioHandler.setCtx(ctx);
64 scenarioHandler.start();
69 * @param scenarioHandler handler of scenario events
71 public void setScenario(ScenarioHandler scenarioHandler) {
72 this.scenarioHandler = scenarioHandler;