Remove trailing whitespace
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClientHandler.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9
10 package org.opendaylight.openflowjava.protocol.impl.clients;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelInboundHandlerAdapter;
15
16 import org.opendaylight.openflowjava.util.ByteBufUtils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.google.common.util.concurrent.SettableFuture;
21
22 /**
23  *
24  * @author michal.polkorab
25  */
26 public class SimpleClientHandler extends ChannelInboundHandlerAdapter {
27
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;
32
33     /**
34      * @param isOnlineFuture future notifier of connected channel
35      * @param scenarioHandler handler of scenario events
36      */
37     public SimpleClientHandler(SettableFuture<Boolean> isOnlineFuture, ScenarioHandler scenarioHandler) {
38         this.isOnlineFuture = isOnlineFuture;
39         this.scenarioHandler = scenarioHandler;
40     }
41
42     @Override
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));
47         }
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");
54     }
55
56     @Override
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;
62         }
63         scenarioHandler.setCtx(ctx);
64         scenarioHandler.start();
65
66     }
67
68     /**
69      * @param scenarioHandler handler of scenario events
70      */
71     public void setScenario(ScenarioHandler scenarioHandler) {
72         this.scenarioHandler = scenarioHandler;
73     }
74
75 }