Merge "add basic lib - plugin communication"
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClient.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.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;
11
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;
20
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.common.util.concurrent.SettableFuture;
25
26 /**
27  * Simple client for testing purposes
28  *
29  * @author michal.polkorab
30  */
31 public class SimpleClient extends Thread {
32
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;
40     
41     /**
42      * Constructor of class
43      *
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
47      */
48     public SimpleClient(String host, int port, String filename) {
49         this.host = host;
50         this.port = port;
51         if (filename != null) {
52             try {
53                 fis = new FileInputStream(filename);
54             } catch (FileNotFoundException ex) {
55                 LOGGER.error(ex.getMessage(), ex);
56             }
57         }
58         init();
59     }
60
61     /**
62      * @param host
63      * @param port
64      * @param filename
65      */
66     public SimpleClient(String host, int port, InputStream filename) {
67         this.host = host;
68         this.port = port;
69         this.fis = filename;
70         init();
71     }
72
73     private void init() {
74         isOnlineFuture = SettableFuture.create();
75     }
76     
77     /**
78      * Starting class of {@link SimpleClient}
79      */
80     @Override
81     public void run() {
82         group = new NioEventLoopGroup();
83         try {
84             Bootstrap b = new Bootstrap();
85             if (securedClient) {
86                 b.group(group)
87                         .channel(NioSocketChannel.class)
88                         .handler(new SimpleClientInitializer(isOnlineFuture));
89             } else {
90                 b.group(group)
91                         .channel(NioSocketChannel.class)
92                         .handler(new SimpleClientHandler(isOnlineFuture));
93             }
94
95             Channel ch = b.connect(host, port).sync().channel();
96             
97             byte[] bytearray = new byte[64];
98             ByteBuf buffy = ch.alloc().buffer(128);
99
100             LOGGER.debug("Before fis != null " + fis);
101             if (fis != null) {
102                 try {
103                     LOGGER.debug("Size to read (in bytes) : " + fis.available());
104                     int lenght;
105                     while ((lenght = fis.read(bytearray)) != -1) {
106                         buffy.writeBytes(bytearray, 0, lenght);
107                     }
108                     ch.writeAndFlush(buffy);
109                     fis.close();
110                 } catch (IOException e) {
111                     LOGGER.error(e.getMessage(), e);
112                 }
113             }
114
115             BufferedReader in = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
116             for (;;) {
117                 String line = in.readLine();
118                 if (line == null) {
119                     break;
120                 }
121                 buffy = ch.alloc().buffer(128);
122                 buffy.writeBytes(line.getBytes(Charset.defaultCharset()));
123                 ch.writeAndFlush(buffy);
124
125                 if ("bye".equals(line.toLowerCase())) {
126                     LOGGER.info("Bye");
127                     in.close();
128                     break;
129                 }
130             }
131         } catch (Exception ex) {
132             LOGGER.error(ex.getMessage(), ex);
133         } finally {
134             group.shutdownGracefully();
135         }
136     }
137
138     /**
139      * @return close future
140      */
141     public Future<?> disconnect() {
142         return group.shutdownGracefully();
143     }
144
145     /**
146      * @param securedClient
147      */
148     public void setSecuredClient(boolean securedClient) {
149         this.securedClient = securedClient;
150     }
151
152     /**
153      * Sets up {@link SimpleClient} and fires run()
154      *
155      * @param args
156      * @throws Exception
157      */
158     public static void main(String[] args) throws Exception {
159         // TODO - add using secure switch via parameter
160         String host;
161         int port;
162         SimpleClient sc;
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();
170             port = 6633;
171             InputStream filenamearg = SimpleClient.class.getResourceAsStream("/org/openflow/core/OFBinaryMessageInput.txt");
172             sc = new SimpleClient(host, port, filenamearg);
173             sc.setSecuredClient(true);
174         } else {
175             host = args[0];
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]));
180         }
181         sc.start();
182         
183     }
184     
185     /**
186      * @return the isOnlineFuture
187      */
188     public SettableFuture<Boolean> getIsOnlineFuture() {
189         return isOnlineFuture;
190     }
191 }