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 scenarioHandler.wait();
78 } catch (Exception ex) {
79 LOGGER.error(ex.getMessage(), ex);
81 LOGGER.debug("shutting down");
83 group.shutdownGracefully().get();
84 LOGGER.debug("shutdown succesful");
85 } catch (InterruptedException | ExecutionException e) {
86 LOGGER.error(e.getMessage(), e);
89 scenarioDone.set(true);
93 * @return close future
95 public Future<?> disconnect() {
96 LOGGER.debug("disconnecting client");
97 return group.shutdownGracefully();
101 public void setSecuredClient(boolean securedClient) {
102 this.securedClient = securedClient;
106 * Sets up {@link SimpleClient} and fires run()
111 public static void main(String[] args) throws Exception {
115 if (args.length != 3) {
116 LOGGER.error("Usage: " + SimpleClient.class.getSimpleName()
117 + " <host> <port> <secured>");
118 LOGGER.error("Trying to use default setting.");
119 InetAddress ia = InetAddress.getLocalHost();
120 InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
121 host = all[0].getHostAddress();
123 sc = new SimpleClient(host, port);
124 sc.setSecuredClient(true);
127 port = Integer.parseInt(args[1]);
128 sc = new SimpleClient(host, port);
129 sc.setSecuredClient(Boolean.parseBoolean(args[2]));
135 public SettableFuture<Boolean> getIsOnlineFuture() {
136 return isOnlineFuture;
140 public SettableFuture<Boolean> getScenarioDone() {
145 public void setScenarioHandler(ScenarioHandler scenario) {
146 this.scenarioHandler = scenario;