2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.clients;
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;
18 import java.net.InetAddress;
19 import java.util.concurrent.ExecutionException;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.common.util.concurrent.SettableFuture;
27 * Simple client for testing purposes
29 * @author michal.polkorab
31 public class UdpSimpleClient implements OFClient {
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;
42 * Constructor of class
44 * @param host address of host
45 * @param port host listening port
47 public UdpSimpleClient(String host, int port) {
54 isOnlineFuture = SettableFuture.create();
55 scenarioDone = SettableFuture.create();
59 * Starting class of {@link UdpSimpleClient}
63 group = new NioEventLoopGroup();
64 UdpSimpleClientInitializer clientInitializer = new UdpSimpleClientInitializer(isOnlineFuture);
65 clientInitializer.setScenario(scenarioHandler);
67 Bootstrap b = new Bootstrap();
69 .channel(NioDatagramChannel.class)
70 .option(ChannelOption.SO_BROADCAST, false)
71 .handler(clientInitializer);
73 b.connect(host, port).sync();
75 synchronized (scenarioHandler) {
76 LOGGER.debug("WAITING FOR SCENARIO");
77 while (! scenarioHandler.isScenarioFinished()) {
78 scenarioHandler.wait();
81 } catch (Exception ex) {
82 LOGGER.error(ex.getMessage(), ex);
84 LOGGER.debug("shutting down");
86 group.shutdownGracefully().get();
87 LOGGER.debug("shutdown succesful");
88 } catch (InterruptedException | ExecutionException e) {
89 LOGGER.error(e.getMessage(), e);
92 scenarioDone.set(true);
96 * @return close future
98 public Future<?> disconnect() {
99 LOGGER.debug("disconnecting client");
100 return group.shutdownGracefully();
104 * Sets up {@link UdpSimpleClient} and fires run()
109 public static void main(String[] args) throws Exception {
113 if (args.length != 2) {
114 LOGGER.error("Usage: " + UdpSimpleClient.class.getSimpleName()
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();
121 sc = new UdpSimpleClient(host, port);
124 port = Integer.parseInt(args[1]);
125 sc = new UdpSimpleClient(host, port);
132 public SettableFuture<Boolean> getIsOnlineFuture() {
133 return isOnlineFuture;
137 public SettableFuture<Boolean> getScenarioDone() {
142 public void setScenarioHandler(ScenarioHandler scenario) {
143 this.scenarioHandler = scenario;
147 public void setSecuredClient(boolean securedClient) {
148 // TODO: Finish implementation when DTLS is supported