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 com.google.common.util.concurrent.SettableFuture;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.channel.ChannelOption;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.nio.NioEventLoopGroup;
16 import io.netty.channel.socket.nio.NioDatagramChannel;
17 import io.netty.util.concurrent.Future;
18 import java.net.InetAddress;
19 import java.util.concurrent.ExecutionException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Simple client for testing purposes.
26 * @author michal.polkorab
28 public class UdpSimpleClient implements OFClient {
30 private static final Logger LOG = LoggerFactory.getLogger(UdpSimpleClient.class);
31 private final String host;
32 private final int port;
33 private EventLoopGroup group;
34 private SettableFuture<Boolean> isOnlineFuture;
35 private SettableFuture<Boolean> scenarioDone;
36 private ScenarioHandler scenarioHandler;
39 * Constructor of class.
41 * @param host address of host
42 * @param port host listening port
44 public UdpSimpleClient(String host, int port) {
51 isOnlineFuture = SettableFuture.create();
52 scenarioDone = SettableFuture.create();
56 * Starting class of {@link UdpSimpleClient}.
60 group = new NioEventLoopGroup();
61 UdpSimpleClientInitializer clientInitializer = new UdpSimpleClientInitializer(isOnlineFuture);
62 clientInitializer.setScenario(scenarioHandler);
64 Bootstrap bootstrap = new Bootstrap();
65 bootstrap.group(group)
66 .channel(NioDatagramChannel.class)
67 .option(ChannelOption.SO_BROADCAST, false)
68 .handler(clientInitializer);
70 bootstrap.connect(host, port).sync();
72 synchronized (scenarioHandler) {
73 LOG.debug("WAITING FOR SCENARIO");
74 while (! scenarioHandler.isScenarioFinished()) {
75 scenarioHandler.wait();
78 } catch (InterruptedException ex) {
79 LOG.error("Error {}", ex);
81 LOG.debug("shutting down");
83 group.shutdownGracefully().get();
84 LOG.debug("shutdown succesful");
85 } catch (InterruptedException | ExecutionException e) {
86 LOG.error("Error {}", e);
89 scenarioDone.set(true);
95 * @return close future
97 public Future<?> disconnect() {
98 LOG.debug("disconnecting client");
99 return group.shutdownGracefully();
103 * Sets up {@link UdpSimpleClient} and fires run().
105 public static void main(String[] args) throws Exception {
109 if (args.length != 2) {
110 LOG.error("Usage: {} <host> <port>", UdpSimpleClient.class.getSimpleName());
111 LOG.error("Trying to use default setting.");
112 InetAddress ia = InetAddress.getLocalHost();
113 InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
114 host = all[0].getHostAddress();
116 sc = new UdpSimpleClient(host, port);
119 port = Integer.parseInt(args[1]);
120 sc = new UdpSimpleClient(host, port);
127 public SettableFuture<Boolean> getIsOnlineFuture() {
128 return isOnlineFuture;
132 public SettableFuture<Boolean> getScenarioDone() {
137 public void setScenarioHandler(ScenarioHandler scenario) {
138 this.scenarioHandler = scenario;
142 public void setSecuredClient(boolean securedClient) {
143 // TODO: Finish implementation when DTLS is supported