OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / samples / 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 import org.opendaylight.openflowjava.util.ByteBufUtils;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * Class representing sending message event.
19  *
20  * @author michal.polkorab
21  */
22 public class SendEvent implements ClientEvent {
23
24     private static final Logger LOG = LoggerFactory.getLogger(SendEvent.class);
25     protected byte[] msgToSend;
26     protected ChannelHandlerContext ctx;
27
28     /**
29      * Constructor.
30      *
31      * @param msgToSend message to be sent
32      */
33     public SendEvent(byte[] msgToSend) {
34         this.msgToSend = new byte[msgToSend.length];
35         System.arraycopy(msgToSend, 0, this.msgToSend, 0, msgToSend.length);
36     }
37
38     @Override
39     public boolean eventExecuted() {
40         LOG.debug("sending message");
41         LOG.debug("start of run");
42         ByteBuf buffer = ctx.alloc().buffer();
43         buffer.writeBytes(msgToSend);
44         ctx.writeAndFlush(buffer);
45
46         if (LOG.isDebugEnabled()) {
47             LOG.debug("Message to send {}", ByteBufUtils.bytesToHexString(msgToSend));
48             LOG.debug("message sent");
49         }
50         return true;
51     }
52
53     /**
54      * Sets the ChannelHandlerContext.
55      *
56      * @param ctx context which will be used for sending messages (SendEvents)
57      */
58     public void setCtx(ChannelHandlerContext ctx) {
59         this.ctx = ctx;
60     }
61
62 }