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.buffer.UnpooledByteBufAllocator;
\r
7 import io.netty.channel.ChannelHandlerContext;
\r
8 import io.netty.channel.ChannelInboundHandlerAdapter;
\r
10 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
\r
11 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
\r
12 import org.slf4j.Logger;
\r
13 import org.slf4j.LoggerFactory;
\r
15 import com.google.common.util.concurrent.SettableFuture;
\r
19 * @author michal.polkorab
\r
21 public class SimpleClientHandler extends ChannelInboundHandlerAdapter {
\r
23 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleClientHandler.class);
\r
24 private SettableFuture<Boolean> isOnlineFuture;
\r
25 private int messagesReceived;
\r
28 * @param isOnlineFuture future notifier of connected channel
\r
30 public SimpleClientHandler(SettableFuture<Boolean> isOnlineFuture) {
\r
31 this.isOnlineFuture = isOnlineFuture;
\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
41 messagesReceived += readHeaders(bb);
\r
42 LOGGER.debug("Messages received: " + messagesReceived);
\r
43 switch (messagesReceived) {
\r
45 LOGGER.debug("FeaturesReply case");
\r
46 ByteBuf featuresReply = createFeaturesReplyBytebuf();
\r
47 ctx.write(featuresReply);
\r
48 LOGGER.debug("FeaturesReply sent");
\r
51 LOGGER.debug("Default case");
\r
56 LOGGER.info("end of read");
\r
59 private static ByteBuf createFeaturesReplyBytebuf() {
\r
60 ByteBuf featuresReply = UnpooledByteBufAllocator.DEFAULT.buffer();
\r
61 featuresReply.writeByte(4);
\r
62 featuresReply.writeByte(6);
\r
63 featuresReply.writeShort(32);
\r
64 ByteBuf featuresReplyBody = BufferHelper
\r
65 .buildBuffer("00 01 02 03 04 05 06 07 00 01 02 03 01 01 00 00 00"
\r
66 + " 01 02 03 00 01 02 03");
\r
67 featuresReply.writeBytes(featuresReplyBody);
\r
68 return featuresReply;
\r
72 public void channelActive(ChannelHandlerContext ctx) throws Exception {
\r
73 System.out.println("Client is active");
\r
74 if (isOnlineFuture != null) {
\r
75 isOnlineFuture.set(true);
\r
76 isOnlineFuture = null;
\r
80 private static int readHeaders(ByteBuf bb) {
\r
83 while (bb.readableBytes() > 0) {
\r
84 length = bb.getShort(2);
\r
85 bb.skipBytes(length);
\r