Improved logging in ScenarioHandler
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / SendEvent.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.clients;
3
4 import io.netty.buffer.ByteBuf;
5 import io.netty.channel.ChannelHandlerContext;
6
7 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 /**
12  * Class representing sending message event
13  * 
14  * @author michal.polkorab
15  */
16 public class SendEvent implements ClientEvent {
17
18     protected static final Logger LOGGER = LoggerFactory.getLogger(SendEvent.class);
19     protected byte[] msgToSend;
20     protected ChannelHandlerContext ctx;
21
22     /**
23      * @param msgToSend message to be sent
24      */
25     public SendEvent(byte[] msgToSend) {
26         this.msgToSend = msgToSend;
27     }
28
29     @Override
30     public boolean eventExecuted() {
31         LOGGER.debug("sending message");
32         Thread thread = new Thread(new Runnable() {
33             @Override
34             public void run() {
35                 LOGGER.debug("start of run");
36                 ByteBuf buffer = ctx.alloc().buffer();
37                 buffer.writeBytes(msgToSend);
38                 ctx.writeAndFlush(buffer);
39                 LOGGER.debug(">> " + ByteBufUtils.bytesToHexString(msgToSend));
40             }
41         });
42         thread.start();
43         try {
44             thread.join();
45         } catch (InterruptedException e) {
46             LOGGER.error(e.getMessage(), e);
47         }
48         LOGGER.debug("message sent");
49         return true;
50     }
51
52     /**
53      * @param ctx context which will be used for sending messages (SendEvents)
54      */
55     public void setCtx(ChannelHandlerContext ctx) {
56         this.ctx = ctx;
57     }
58
59 }