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 io.netty.bootstrap.ServerBootstrap;
11 import io.netty.channel.ChannelFuture;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.nio.NioEventLoopGroup;
14 import io.netty.channel.socket.nio.NioServerSocketChannel;
15 import io.netty.util.concurrent.Future;
17 import java.net.InetSocketAddress;
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 * Listening client for testing purposes
27 * @author martin.uhlir
30 public class ListeningSimpleClient implements OFClient {
32 private static final Logger LOG = LoggerFactory.getLogger(ListeningSimpleClient.class);
34 private boolean securedClient = false;
35 private EventLoopGroup workerGroup;
36 private SettableFuture<Boolean> isOnlineFuture;
37 private SettableFuture<Boolean> scenarioDone;
38 private ScenarioHandler scenarioHandler;
41 * Constructor of the class
43 * @param port host listening port
45 public ListeningSimpleClient(int port) {
51 isOnlineFuture = SettableFuture.create();
52 scenarioDone = SettableFuture.create();
56 * Starting class of {@link ListeningSimpleClient}
60 EventLoopGroup bossGroup = new NioEventLoopGroup(1);
61 workerGroup = new NioEventLoopGroup();
62 SimpleClientInitializer clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
63 clientInitializer.setScenario(scenarioHandler);
65 ServerBootstrap b = new ServerBootstrap();
66 b.group(bossGroup, workerGroup)
67 .channel(NioServerSocketChannel.class)
68 .childHandler(clientInitializer);
70 ChannelFuture f = b.bind(port).sync();
71 // Update port, as it may have been specified as 0
72 this.port = ((InetSocketAddress) f.channel().localAddress()).getPort();
73 isOnlineFuture.set(true);
75 synchronized (scenarioHandler) {
76 LOG.debug("WAITING FOR SCENARIO");
77 while (! scenarioHandler.isScenarioFinished()) {
78 scenarioHandler.wait();
81 } catch (Exception ex) {
82 LOG.error(ex.getMessage(), ex);
84 LOG.debug("listening client shutting down");
86 workerGroup.shutdownGracefully().get();
87 bossGroup.shutdownGracefully().get();
88 LOG.debug("listening client shutdown succesful");
89 } catch (InterruptedException | ExecutionException e) {
90 LOG.error(e.getMessage(), e);
93 scenarioDone.set(true);
97 * @return close future
99 public Future<?> disconnect() {
100 LOG.debug("disconnecting client");
101 return workerGroup.shutdownGracefully();
105 public void setSecuredClient(boolean securedClient) {
106 this.securedClient = securedClient;
110 public SettableFuture<Boolean> getIsOnlineFuture() {
111 return isOnlineFuture;
115 public SettableFuture<Boolean> getScenarioDone() {
120 public void setScenarioHandler(ScenarioHandler scenario) {
121 this.scenarioHandler = scenario;
125 * @return actual port number
127 public int getPort() {