1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.clients;
4 import io.netty.bootstrap.Bootstrap;
5 import io.netty.buffer.ByteBuf;
6 import io.netty.channel.Channel;
7 import io.netty.channel.EventLoopGroup;
8 import io.netty.channel.nio.NioEventLoopGroup;
9 import io.netty.channel.socket.nio.NioSocketChannel;
10 import io.netty.util.concurrent.Future;
12 import java.io.BufferedReader;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.net.InetAddress;
19 import java.nio.charset.Charset;
21 import org.opendaylight.openflowjava.protocol.impl.integration.IntegrationTest;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.common.util.concurrent.SettableFuture;
28 * Simple client for testing purposes
30 * @author michal.polkorab
32 public class SimpleClient extends Thread {
34 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleClient.class);
35 private final String host;
36 private final int port;
37 private boolean securedClient = false;
38 private InputStream fis;
39 private EventLoopGroup group;
40 private SettableFuture<Boolean> isOnlineFuture;
41 private SettableFuture<Boolean> automatedPartDone;
44 * Constructor of class
46 * @param host address of host
47 * @param port host listening port
48 * @param filename name of input file containing binary data to be send
50 public SimpleClient(String host, int port, String filename) {
53 if (filename != null) {
55 fis = new FileInputStream(filename);
56 } catch (FileNotFoundException ex) {
57 LOGGER.error(ex.getMessage(), ex);
68 public SimpleClient(String host, int port, InputStream filename) {
76 isOnlineFuture = SettableFuture.create();
77 automatedPartDone = SettableFuture.create();
81 * Starting class of {@link SimpleClient}
85 group = new NioEventLoopGroup();
87 Bootstrap b = new Bootstrap();
89 .channel(NioSocketChannel.class)
90 .handler(new SimpleClientInitializer(isOnlineFuture, securedClient));
92 Channel ch = b.connect(host, port).sync().channel();
94 byte[] bytearray = new byte[64];
95 ByteBuf buffy = ch.alloc().buffer(128);
97 LOGGER.debug("Before fis != null - fis == " + fis);
100 LOGGER.debug("Size to read (in bytes) : " + fis.available());
102 while ((lenght = fis.read(bytearray)) != -1) {
103 buffy.writeBytes(bytearray, 0, lenght);
105 ch.writeAndFlush(buffy);
107 } catch (IOException e) {
108 LOGGER.error(e.getMessage(), e);
109 automatedPartDone.setException(e);
112 automatedPartDone.set(true);
114 BufferedReader in = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
116 String line = in.readLine();
120 buffy = ch.alloc().buffer(128);
121 buffy.writeBytes(line.getBytes(Charset.defaultCharset()));
122 ch.writeAndFlush(buffy);
124 if ("bye".equals(line.toLowerCase())) {
130 LOGGER.debug("after stdin reading done");
131 } catch (Exception ex) {
132 LOGGER.error(ex.getMessage(), ex);
134 group.shutdownGracefully();
139 * @return close future
141 public Future<?> disconnect() {
142 LOGGER.debug("disconnecting client");
143 return group.shutdownGracefully();
147 * @param securedClient
149 public void setSecuredClient(boolean securedClient) {
150 this.securedClient = securedClient;
154 * Sets up {@link SimpleClient} and fires run()
159 public static void main(String[] args) throws Exception {
163 if (args.length != 4) {
164 LOGGER.error("Usage: " + SimpleClient.class.getSimpleName()
165 + " <host> <port> <secured> <filename>");
166 LOGGER.error("Trying to use default setting.");
167 InetAddress ia = InetAddress.getLocalHost();
168 InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
169 host = all[0].getHostAddress();
171 InputStream filenamearg = IntegrationTest.class.getResourceAsStream(
172 IntegrationTest.OF_BINARY_MESSAGE_INPUT_TXT);
173 sc = new SimpleClient(host, port, filenamearg);
174 sc.setSecuredClient(true);
177 port = Integer.parseInt(args[1]);
178 String filenamearg = args[3];
179 sc = new SimpleClient(host, port, filenamearg);
180 sc.setSecuredClient(Boolean.parseBoolean(args[2]));
187 * @return the isOnlineFuture
189 public SettableFuture<Boolean> getIsOnlineFuture() {
190 return isOnlineFuture;
194 * @return the automatedPartDone
196 public SettableFuture<Boolean> getAutomatedPartDone() {
197 return automatedPartDone;