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 SimpleClientInitializer clientInitializer;
40 private ScenarioHandler scenarioHandler;
43 * Constructor of class
45 * @param host address of host
46 * @param port host listening port
48 public SimpleClient(String host, int port) {
55 isOnlineFuture = SettableFuture.create();
56 scenarioDone = SettableFuture.create();
60 * Starting class of {@link SimpleClient}
64 group = new NioEventLoopGroup();
65 clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
66 clientInitializer.setScenario(scenarioHandler);
68 Bootstrap b = new Bootstrap();
70 .channel(NioSocketChannel.class)
71 .handler(clientInitializer);
73 b.connect(host, port).sync();
75 synchronized (scenarioHandler) {
76 LOGGER.debug("WAITING FOR SCENARIO");
77 scenarioHandler.wait();
79 } catch (Exception ex) {
80 LOGGER.error(ex.getMessage(), ex);
82 LOGGER.debug("shutting down");
84 group.shutdownGracefully().get();
85 LOGGER.debug("shutdown succesful");
86 } catch (InterruptedException | ExecutionException e) {
87 LOGGER.error(e.getMessage(), e);
90 scenarioDone.set(true);
94 * @return close future
96 public Future<?> disconnect() {
97 LOGGER.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()
112 public static void main(String[] args) throws Exception {
116 if (args.length != 3) {
117 LOGGER.error("Usage: " + SimpleClient.class.getSimpleName()
118 + " <host> <port> <secured>");
119 LOGGER.error("Trying to use default setting.");
120 InetAddress ia = InetAddress.getLocalHost();
121 InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
122 host = all[0].getHostAddress();
124 sc = new SimpleClient(host, port);
125 sc.setSecuredClient(true);
128 port = Integer.parseInt(args[1]);
129 sc = new SimpleClient(host, port);
130 sc.setSecuredClient(Boolean.parseBoolean(args[2]));
136 public SettableFuture<Boolean> getIsOnlineFuture() {
137 return isOnlineFuture;
141 public SettableFuture<Boolean> getScenarioDone() {
146 public void setScenarioHandler(ScenarioHandler scenario) {
147 this.scenarioHandler = scenario;