Merge "Quickfix - removed join in SimpleClient"
[openflowjava.git] / simple-client / src / main / 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.channel.EventLoopGroup;
6 import io.netty.channel.nio.NioEventLoopGroup;
7 import io.netty.channel.socket.nio.NioSocketChannel;
8 import io.netty.util.concurrent.Future;
9
10 import java.net.InetAddress;
11 import java.util.concurrent.ExecutionException;
12
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import com.google.common.util.concurrent.SettableFuture;
17
18 /**
19  * Simple client for testing purposes
20  *
21  * @author michal.polkorab
22  */
23 public class SimpleClient extends Thread {
24
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;
34     
35     /**
36      * Constructor of class
37      *
38      * @param host address of host
39      * @param port host listening port
40      */
41     public SimpleClient(String host, int port) {
42         this.host = host;
43         this.port = port;
44         init();
45     }
46
47     private void init() {
48         isOnlineFuture = SettableFuture.create();
49         scenarioDone = SettableFuture.create();
50     }
51     
52     /**
53      * Starting class of {@link SimpleClient}
54      */
55     @Override
56     public void run() {
57         group = new NioEventLoopGroup();
58         clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
59         clientInitializer.setScenario(scenarioHandler);
60         try {
61             Bootstrap b = new Bootstrap();
62             b.group(group)
63                 .channel(NioSocketChannel.class)
64                 .handler(clientInitializer);
65
66             b.connect(host, port).sync();
67
68             synchronized (scenarioHandler) {
69                 LOGGER.debug("WAITING FOR SCENARIO");
70                 scenarioHandler.wait();
71             }
72         } catch (Exception ex) {
73             LOGGER.error(ex.getMessage(), ex);
74         } finally {
75             LOGGER.info("shutting down");
76             try {
77                 group.shutdownGracefully().get();
78                 LOGGER.info("shutdown succesful");
79             } catch (InterruptedException | ExecutionException e) {
80                 LOGGER.error(e.getMessage(), e);
81             }
82         }
83         scenarioDone.set(true);
84     }
85
86     /**
87      * @return close future
88      */
89     public Future<?> disconnect() {
90         LOGGER.debug("disconnecting client");
91         return group.shutdownGracefully();
92     }
93
94     /**
95      * @param securedClient
96      */
97     public void setSecuredClient(boolean securedClient) {
98         this.securedClient = securedClient;
99     }
100
101     /**
102      * Sets up {@link SimpleClient} and fires run()
103      *
104      * @param args
105      * @throws Exception
106      */
107     public static void main(String[] args) throws Exception {
108         String host;
109         int port;
110         SimpleClient sc;
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();
118             port = 6633;
119             sc = new SimpleClient(host, port);
120             sc.setSecuredClient(true);
121         } else {
122             host = args[0];
123             port = Integer.parseInt(args[1]);
124             sc = new SimpleClient(host, port);
125             sc.setSecuredClient(Boolean.parseBoolean(args[2]));
126         }
127         sc.start();
128         
129     }
130     
131     /**
132      * @return the isOnlineFuture
133      */
134     public SettableFuture<Boolean> getIsOnlineFuture() {
135         return isOnlineFuture;
136     }
137     
138     /**
139      * @return the scenarioDone
140      */
141     public SettableFuture<Boolean> getScenarioDone() {
142         return scenarioDone;
143     }
144     
145     /**
146      * @param scenario list of wanted actions
147      */
148     public void setScenarioHandler(ScenarioHandler scenario) {
149         this.scenarioHandler = scenario;
150     }
151 }