UDP support implementation
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / SimpleClient.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.clients;
10
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.nio.NioEventLoopGroup;
14 import io.netty.channel.socket.nio.NioSocketChannel;
15 import io.netty.util.concurrent.Future;
16
17 import java.net.InetAddress;
18 import java.util.concurrent.ExecutionException;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.util.concurrent.SettableFuture;
24
25 /**
26  * Simple client for testing purposes
27  *
28  * @author michal.polkorab
29  */
30 public class SimpleClient implements OFClient {
31
32     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleClient.class);
33     private final String host;
34     private final int port;
35     private boolean securedClient = false;
36     private EventLoopGroup group;
37     private SettableFuture<Boolean> isOnlineFuture;
38     private SettableFuture<Boolean> scenarioDone;
39     private SimpleClientInitializer clientInitializer;
40     private ScenarioHandler scenarioHandler;
41     
42     /**
43      * Constructor of class
44      *
45      * @param host address of host
46      * @param port host listening port
47      */
48     public SimpleClient(String host, int port) {
49         this.host = host;
50         this.port = port;
51         init();
52     }
53
54     private void init() {
55         isOnlineFuture = SettableFuture.create();
56         scenarioDone = SettableFuture.create();
57     }
58     
59     /**
60      * Starting class of {@link SimpleClient}
61      */
62     @Override
63     public void run() {
64         group = new NioEventLoopGroup();
65         clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
66         clientInitializer.setScenario(scenarioHandler);
67         try {
68             Bootstrap b = new Bootstrap();
69             b.group(group)
70                 .channel(NioSocketChannel.class)
71                 .handler(clientInitializer);
72
73             b.connect(host, port).sync();
74
75             synchronized (scenarioHandler) {
76                 LOGGER.debug("WAITING FOR SCENARIO");
77                 scenarioHandler.wait();
78             }
79         } catch (Exception ex) {
80             LOGGER.error(ex.getMessage(), ex);
81         } finally {
82             LOGGER.debug("shutting down");
83             try {
84                 group.shutdownGracefully().get();
85                 LOGGER.debug("shutdown succesful");
86             } catch (InterruptedException | ExecutionException e) {
87                 LOGGER.error(e.getMessage(), e);
88             }
89         }
90         scenarioDone.set(true);
91     }
92
93     /**
94      * @return close future
95      */
96     public Future<?> disconnect() {
97         LOGGER.debug("disconnecting client");
98         return group.shutdownGracefully();
99     }
100
101     @Override
102     public void setSecuredClient(boolean securedClient) {
103         this.securedClient = securedClient;
104     }
105
106     /**
107      * Sets up {@link SimpleClient} and fires run()
108      *
109      * @param args
110      * @throws Exception
111      */
112     public static void main(String[] args) throws Exception {
113         String host;
114         int port;
115         SimpleClient sc;
116         if (args.length != 3) {
117             LOGGER.error("Usage: " + SimpleClient.class.getSimpleName()
118                     + " <host> <port> <secured>");
119             LOGGER.error("Trying to use default setting.");
120             InetAddress ia = InetAddress.getLocalHost();
121             InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
122             host = all[0].getHostAddress();
123             port = 6633;
124             sc = new SimpleClient(host, port);
125             sc.setSecuredClient(true);
126         } else {
127             host = args[0];
128             port = Integer.parseInt(args[1]);
129             sc = new SimpleClient(host, port);
130             sc.setSecuredClient(Boolean.parseBoolean(args[2]));
131         }
132         sc.run();
133     }
134
135     @Override
136     public SettableFuture<Boolean> getIsOnlineFuture() {
137         return isOnlineFuture;
138     }
139
140     @Override
141     public SettableFuture<Boolean> getScenarioDone() {
142         return scenarioDone;
143     }
144
145     @Override
146     public void setScenarioHandler(ScenarioHandler scenario) {
147         this.scenarioHandler = scenario;
148     }
149 }