2 * Copyright (c) 2013 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.EventLoopGroup;
13 import io.netty.channel.nio.NioEventLoopGroup;
14 import io.netty.channel.socket.nio.NioSocketChannel;
15 import io.netty.util.concurrent.Future;
17 import java.net.InetAddress;
18 import java.util.concurrent.ExecutionException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import com.google.common.util.concurrent.SettableFuture;
26 * Simple client for testing purposes
28 * @author michal.polkorab
30 public class SimpleClient implements OFClient {
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 ScenarioHandler scenarioHandler;
42 * Constructor of class
44 * @param host address of host
45 * @param port host listening port
47 public SimpleClient(String host, int port) {
54 isOnlineFuture = SettableFuture.create();
55 scenarioDone = SettableFuture.create();
59 * Starting class of {@link SimpleClient}
63 group = new NioEventLoopGroup();
64 SimpleClientInitializer clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
65 clientInitializer.setScenario(scenarioHandler);
67 Bootstrap b = new Bootstrap();
69 .channel(NioSocketChannel.class)
70 .handler(clientInitializer);
72 b.connect(host, port).sync();
74 synchronized (scenarioHandler) {
75 LOGGER.debug("WAITING FOR SCENARIO");
76 while (! scenarioHandler.isScenarioFinished()) {
77 scenarioHandler.wait();
80 } catch (Exception ex) {
81 LOGGER.error(ex.getMessage(), ex);
83 LOGGER.debug("shutting down");
85 group.shutdownGracefully().get();
86 LOGGER.debug("shutdown succesful");
87 } catch (InterruptedException | ExecutionException e) {
88 LOGGER.error(e.getMessage(), e);
91 scenarioDone.set(true);
95 * @return close future
97 public Future<?> disconnect() {
98 LOGGER.debug("disconnecting client");
99 return group.shutdownGracefully();
103 public void setSecuredClient(boolean securedClient) {
104 this.securedClient = securedClient;
108 * Sets up {@link SimpleClient} and fires run()
113 public static void main(String[] args) throws Exception {
117 if (args.length != 3) {
118 LOGGER.error("Usage: " + SimpleClient.class.getSimpleName()
119 + " <host> <port> <secured>");
120 LOGGER.error("Trying to use default setting.");
121 InetAddress ia = InetAddress.getLocalHost();
122 InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
123 host = all[0].getHostAddress();
125 sc = new SimpleClient(host, port);
126 sc.setSecuredClient(true);
129 port = Integer.parseInt(args[1]);
130 sc = new SimpleClient(host, port);
131 sc.setSecuredClient(Boolean.parseBoolean(args[2]));
137 public SettableFuture<Boolean> getIsOnlineFuture() {
138 return isOnlineFuture;
142 public SettableFuture<Boolean> getScenarioDone() {
147 public void setScenarioHandler(ScenarioHandler scenario) {
148 this.scenarioHandler = scenario;