db339eb24fb5224e40a863fe826f1590265ba03a
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / SendEvent.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 package org.opendaylight.openflowjava.protocol.impl.clients;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.channel.ChannelHandlerContext;
13
14 import org.opendaylight.openflowjava.util.ByteBufUtils;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Class representing sending message event
20  * 
21  * @author michal.polkorab
22  */
23 public class SendEvent implements ClientEvent {
24
25     protected static final Logger LOGGER = LoggerFactory.getLogger(SendEvent.class);
26     protected byte[] msgToSend;
27     protected ChannelHandlerContext ctx;
28
29     /**
30      * @param msgToSend message to be sent
31      */
32     public SendEvent(byte[] msgToSend) {
33         this.msgToSend = new byte[msgToSend.length];
34         for (int i = 0; i < msgToSend.length; i++) {
35             this.msgToSend[i] = msgToSend[i];
36         }
37     }
38
39     @Override
40     public boolean eventExecuted() {
41         LOGGER.debug("sending message");
42         LOGGER.debug("start of run");
43         ByteBuf buffer = ctx.alloc().buffer();
44         buffer.writeBytes(msgToSend);
45         ctx.writeAndFlush(buffer);
46         LOGGER.debug(">> " + ByteBufUtils.bytesToHexString(msgToSend));
47         LOGGER.debug("message sent");
48         return true;
49     }
50
51     /**
52      * @param ctx context which will be used for sending messages (SendEvents)
53      */
54     public void setCtx(ChannelHandlerContext ctx) {
55         this.ctx = ctx;
56     }
57
58 }