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 com.google.common.util.concurrent.SettableFuture;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import io.netty.channel.socket.nio.NioSocketChannel;
16 import io.netty.util.concurrent.Future;
17 import java.net.InetAddress;
18 import java.util.concurrent.ExecutionException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Simple client for testing purposes.
25 * @author michal.polkorab
27 public class SimpleClient implements OFClient {
29 private static final Logger LOG = LoggerFactory.getLogger(SimpleClient.class);
30 private final String host;
31 private final int port;
32 private boolean securedClient = false;
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 SimpleClient(String host, int port) {
51 isOnlineFuture = SettableFuture.create();
52 scenarioDone = SettableFuture.create();
56 * Starting class of {@link SimpleClient}.
60 group = new NioEventLoopGroup();
61 SimpleClientInitializer clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
62 clientInitializer.setScenario(scenarioHandler);
64 Bootstrap bootstrap = new Bootstrap();
65 bootstrap.group(group)
66 .channel(NioSocketChannel.class)
67 .handler(clientInitializer);
69 bootstrap.connect(host, port).sync();
71 synchronized (scenarioHandler) {
72 LOG.debug("WAITING FOR SCENARIO");
73 while (! scenarioHandler.isScenarioFinished()) {
74 scenarioHandler.wait();
77 } catch (InterruptedException ex) {
78 LOG.error("Error {}", ex);
80 LOG.debug("shutting down");
82 group.shutdownGracefully().get();
83 LOG.debug("shutdown succesful");
84 } catch (InterruptedException | ExecutionException e) {
85 LOG.error("Error {}", e);
88 scenarioDone.set(true);
94 * @return close future
96 public Future<?> disconnect() {
97 LOG.debug("disconnecting client");
98 return group.shutdownGracefully();
102 public void setSecuredClient(boolean securedClient) {
103 this.securedClient = securedClient;
107 * Sets up {@link SimpleClient} and fires run().
109 public static void main(String[] args) throws Exception {
113 if (args.length != 3) {
114 LOG.error("Usage: {} <host> <port> <secured>", SimpleClient.class.getSimpleName());
115 LOG.error("Trying to use default setting.");
116 InetAddress ia = InetAddress.getLocalHost();
117 InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
118 host = all[0].getHostAddress();
120 sc = new SimpleClient(host, port);
121 sc.setSecuredClient(true);
124 port = Integer.parseInt(args[1]);
125 sc = new SimpleClient(host, port);
126 sc.setSecuredClient(Boolean.parseBoolean(args[2]));
132 public SettableFuture<Boolean> getIsOnlineFuture() {
133 return isOnlineFuture;
137 public SettableFuture<Boolean> getScenarioDone() {
142 public void setScenarioHandler(ScenarioHandler scenario) {
143 this.scenarioHandler = scenario;