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.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.common.util.concurrent.SettableFuture;
27 * Simple client for testing purposes
29 * @author michal.polkorab
31 public class SimpleClient extends Thread {
33 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleClient.class);
34 private final String host;
35 private final int port;
36 private boolean securedClient = false;
37 private InputStream fis;
38 private EventLoopGroup group;
39 private SettableFuture<Boolean> isOnlineFuture;
42 * Constructor of class
44 * @param host address of host
45 * @param port host listening port
46 * @param filename name of input file containing binary data to be send
48 public SimpleClient(String host, int port, String filename) {
51 if (filename != null) {
53 fis = new FileInputStream(filename);
54 } catch (FileNotFoundException ex) {
55 LOGGER.error(ex.getMessage(), ex);
66 public SimpleClient(String host, int port, InputStream filename) {
74 isOnlineFuture = SettableFuture.create();
78 * Starting class of {@link SimpleClient}
82 group = new NioEventLoopGroup();
84 Bootstrap b = new Bootstrap();
87 .channel(NioSocketChannel.class)
88 .handler(new SimpleClientInitializer(isOnlineFuture));
91 .channel(NioSocketChannel.class)
92 .handler(new SimpleClientHandler(isOnlineFuture));
95 Channel ch = b.connect(host, port).sync().channel();
97 byte[] bytearray = new byte[64];
98 ByteBuf buffy = ch.alloc().buffer(128);
100 LOGGER.debug("Before fis != null " + fis);
103 LOGGER.debug("Size to read (in bytes) : " + fis.available());
105 while ((lenght = fis.read(bytearray)) != -1) {
106 buffy.writeBytes(bytearray, 0, lenght);
108 ch.writeAndFlush(buffy);
110 } catch (IOException e) {
111 LOGGER.error(e.getMessage(), e);
115 BufferedReader in = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
117 String line = in.readLine();
121 buffy = ch.alloc().buffer(128);
122 buffy.writeBytes(line.getBytes(Charset.defaultCharset()));
123 ch.writeAndFlush(buffy);
125 if ("bye".equals(line.toLowerCase())) {
131 } catch (Exception ex) {
132 LOGGER.error(ex.getMessage(), ex);
134 group.shutdownGracefully();
139 * @return close future
141 public Future<?> disconnect() {
142 return group.shutdownGracefully();
146 * @param securedClient
148 public void setSecuredClient(boolean securedClient) {
149 this.securedClient = securedClient;
153 * Sets up {@link SimpleClient} and fires run()
158 public static void main(String[] args) throws Exception {
159 // TODO - add using secure switch via parameter
163 if (args.length != 3) {
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 = SimpleClient.class.getResourceAsStream("/org/openflow/core/OFBinaryMessageInput.txt");
172 sc = new SimpleClient(host, port, filenamearg);
173 sc.setSecuredClient(true);
176 port = Integer.parseInt(args[1]);
177 String filenamearg = args[3];
178 sc = new SimpleClient(host, port, filenamearg);
179 sc.setSecuredClient(Boolean.parseBoolean(args[2]));
186 * @return the isOnlineFuture
188 public SettableFuture<Boolean> getIsOnlineFuture() {
189 return isOnlineFuture;