2 * Copyright (c) 2015 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
8 package org.opendaylight.openflowjava.protocol.impl.clients;
10 import com.google.common.util.concurrent.SettableFuture;
11 import io.netty.bootstrap.ServerBootstrap;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import io.netty.channel.socket.nio.NioServerSocketChannel;
16 import io.netty.util.concurrent.Future;
17 import java.net.InetSocketAddress;
18 import java.util.concurrent.ExecutionException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Listening client for testing purposes.
25 * @author martin.uhlir
27 public class ListeningSimpleClient implements OFClient {
29 private static final Logger LOG = LoggerFactory.getLogger(ListeningSimpleClient.class);
31 private boolean securedClient = false;
32 private EventLoopGroup workerGroup;
33 private SettableFuture<Boolean> isOnlineFuture;
34 private SettableFuture<Boolean> scenarioDone;
35 private ScenarioHandler scenarioHandler;
38 * Constructor of the class.
40 * @param port host listening port
42 public ListeningSimpleClient(int port) {
48 isOnlineFuture = SettableFuture.create();
49 scenarioDone = SettableFuture.create();
53 * Starting class of {@link ListeningSimpleClient}.
57 EventLoopGroup bossGroup = new NioEventLoopGroup(1);
58 workerGroup = new NioEventLoopGroup();
59 SimpleClientInitializer clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
60 clientInitializer.setScenario(scenarioHandler);
62 ServerBootstrap bootstrap = new ServerBootstrap();
63 bootstrap.group(bossGroup, workerGroup)
64 .channel(NioServerSocketChannel.class)
65 .childHandler(clientInitializer);
67 ChannelFuture future = bootstrap.bind(port).sync();
68 // Update port, as it may have been specified as 0
69 this.port = ((InetSocketAddress) future.channel().localAddress()).getPort();
70 isOnlineFuture.set(true);
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("listening client shutting down");
83 workerGroup.shutdownGracefully().get();
84 bossGroup.shutdownGracefully().get();
85 LOG.debug("listening client shutdown succesful");
86 } catch (InterruptedException | ExecutionException e) {
87 LOG.error("Error {}", e);
90 scenarioDone.set(true);
96 * @return close future
98 public Future<?> disconnect() {
99 LOG.debug("disconnecting client");
100 return workerGroup.shutdownGracefully();
104 public void setSecuredClient(boolean securedClient) {
105 this.securedClient = securedClient;
109 public SettableFuture<Boolean> getIsOnlineFuture() {
110 return isOnlineFuture;
114 public SettableFuture<Boolean> getScenarioDone() {
119 public void setScenarioHandler(ScenarioHandler scenario) {
120 this.scenarioHandler = scenario;
124 * Returns the actual port number.
126 public int getPort() {