Remove trailing whitespace
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / UdpSimpleClient.java
1 /*
2  * Copyright (c) 2014 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.ChannelOption;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import io.netty.channel.socket.nio.NioDatagramChannel;
16 import io.netty.util.concurrent.Future;
17
18 import java.net.InetAddress;
19 import java.util.concurrent.ExecutionException;
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 UdpSimpleClient implements OFClient {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(UdpSimpleClient.class);
34     private final String host;
35     private final int port;
36     private EventLoopGroup group;
37     private SettableFuture<Boolean> isOnlineFuture;
38     private SettableFuture<Boolean> scenarioDone;
39     private ScenarioHandler scenarioHandler;
40
41     /**
42      * Constructor of class
43      *
44      * @param host address of host
45      * @param port host listening port
46      */
47     public UdpSimpleClient(String host, int port) {
48         this.host = host;
49         this.port = port;
50         init();
51     }
52
53     private void init() {
54         isOnlineFuture = SettableFuture.create();
55         scenarioDone = SettableFuture.create();
56     }
57
58     /**
59      * Starting class of {@link UdpSimpleClient}
60      */
61     @Override
62     public void run() {
63         group = new NioEventLoopGroup();
64         UdpSimpleClientInitializer clientInitializer = new UdpSimpleClientInitializer(isOnlineFuture);
65         clientInitializer.setScenario(scenarioHandler);
66         try {
67             Bootstrap b = new Bootstrap();
68             b.group(group)
69                 .channel(NioDatagramChannel.class)
70                 .option(ChannelOption.SO_BROADCAST, false)
71                 .handler(clientInitializer);
72
73             b.connect(host, port).sync();
74
75             synchronized (scenarioHandler) {
76                 LOGGER.debug("WAITING FOR SCENARIO");
77                 while (! scenarioHandler.isScenarioFinished()) {
78                     scenarioHandler.wait();
79                 }
80             }
81         } catch (Exception ex) {
82             LOGGER.error(ex.getMessage(), ex);
83         } finally {
84             LOGGER.debug("shutting down");
85             try {
86                 group.shutdownGracefully().get();
87                 LOGGER.debug("shutdown succesful");
88             } catch (InterruptedException | ExecutionException e) {
89                 LOGGER.error(e.getMessage(), e);
90             }
91         }
92         scenarioDone.set(true);
93     }
94
95     /**
96      * @return close future
97      */
98     public Future<?> disconnect() {
99         LOGGER.debug("disconnecting client");
100         return group.shutdownGracefully();
101     }
102
103     /**
104      * Sets up {@link UdpSimpleClient} and fires run()
105      *
106      * @param args
107      * @throws Exception
108      */
109     public static void main(String[] args) throws Exception {
110         String host;
111         int port;
112         UdpSimpleClient sc;
113         if (args.length != 2) {
114             LOGGER.error("Usage: " + UdpSimpleClient.class.getSimpleName()
115                     + " <host> <port>");
116             LOGGER.error("Trying to use default setting.");
117             InetAddress ia = InetAddress.getLocalHost();
118             InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
119             host = all[0].getHostAddress();
120             port = 6633;
121             sc = new UdpSimpleClient(host, port);
122         } else {
123             host = args[0];
124             port = Integer.parseInt(args[1]);
125             sc = new UdpSimpleClient(host, port);
126         }
127         sc.run();
128
129     }
130
131     @Override
132     public SettableFuture<Boolean> getIsOnlineFuture() {
133         return isOnlineFuture;
134     }
135
136     @Override
137     public SettableFuture<Boolean> getScenarioDone() {
138         return scenarioDone;
139     }
140
141     @Override
142     public void setScenarioHandler(ScenarioHandler scenario) {
143         this.scenarioHandler = scenario;
144     }
145
146     @Override
147     public void setSecuredClient(boolean securedClient) {
148         // TODO: Finish implementation when DTLS is supported
149     }
150 }