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.channel.EventLoopGroup;
6 import io.netty.channel.nio.NioEventLoopGroup;
7 import io.netty.channel.socket.nio.NioSocketChannel;
8 import io.netty.util.concurrent.Future;
10 import java.net.InetAddress;
11 import java.util.concurrent.ExecutionException;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import com.google.common.util.concurrent.SettableFuture;
19 * Simple client for testing purposes
21 * @author michal.polkorab
23 public class SimpleClient extends Thread {
25 private static final Logger LOGGER = LoggerFactory.getLogger(SimpleClient.class);
26 private final String host;
27 private final int port;
28 private boolean securedClient = false;
29 private EventLoopGroup group;
30 private SettableFuture<Boolean> isOnlineFuture;
31 private SettableFuture<Boolean> scenarioDone;
32 private SimpleClientInitializer clientInitializer;
33 private ScenarioHandler scenarioHandler;
36 * Constructor of class
38 * @param host address of host
39 * @param port host listening port
41 public SimpleClient(String host, int port) {
48 isOnlineFuture = SettableFuture.create();
49 scenarioDone = SettableFuture.create();
53 * Starting class of {@link SimpleClient}
57 group = new NioEventLoopGroup();
58 clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
59 clientInitializer.setScenario(scenarioHandler);
61 Bootstrap b = new Bootstrap();
63 .channel(NioSocketChannel.class)
64 .handler(clientInitializer);
66 b.connect(host, port).sync();
68 scenarioHandler.join();
69 synchronized (scenarioHandler) {
70 scenarioHandler.wait();
72 } catch (Exception ex) {
73 LOGGER.error(ex.getMessage(), ex);
75 LOGGER.info("shutting down");
77 group.shutdownGracefully().get();
78 LOGGER.info("shutdown succesful");
79 } catch (InterruptedException | ExecutionException e) {
80 LOGGER.error(e.getMessage(), e);
83 scenarioDone.set(true);
87 * @return close future
89 public Future<?> disconnect() {
90 LOGGER.debug("disconnecting client");
91 return group.shutdownGracefully();
95 * @param securedClient
97 public void setSecuredClient(boolean securedClient) {
98 this.securedClient = securedClient;
102 * Sets up {@link SimpleClient} and fires run()
107 public static void main(String[] args) throws Exception {
111 if (args.length != 3) {
112 LOGGER.error("Usage: " + SimpleClient.class.getSimpleName()
113 + " <host> <port> <secured>");
114 LOGGER.error("Trying to use default setting.");
115 InetAddress ia = InetAddress.getLocalHost();
116 InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
117 host = all[0].getHostAddress();
119 sc = new SimpleClient(host, port);
120 sc.setSecuredClient(true);
123 port = Integer.parseInt(args[1]);
124 sc = new SimpleClient(host, port);
125 sc.setSecuredClient(Boolean.parseBoolean(args[2]));
132 * @return the isOnlineFuture
134 public SettableFuture<Boolean> getIsOnlineFuture() {
135 return isOnlineFuture;
139 * @return the scenarioDone
141 public SettableFuture<Boolean> getScenarioDone() {
146 * @param scenario list of wanted actions
148 public void setScenarioHandler(ScenarioHandler scenario) {
149 this.scenarioHandler = scenario;