2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.openflowjava.protocol.impl.core;
\r
11 import io.netty.bootstrap.Bootstrap;
\r
12 import io.netty.channel.ChannelFuture;
\r
13 import io.netty.channel.ChannelOption;
\r
14 import io.netty.channel.EventLoopGroup;
\r
15 import io.netty.channel.nio.NioEventLoopGroup;
\r
16 import io.netty.channel.socket.nio.NioDatagramChannel;
\r
17 import io.netty.util.concurrent.GenericFutureListener;
\r
19 import java.net.InetAddress;
\r
20 import java.net.InetSocketAddress;
\r
22 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
\r
23 import org.opendaylight.openflowjava.protocol.impl.connection.ServerFacade;
\r
24 import org.slf4j.Logger;
\r
25 import org.slf4j.LoggerFactory;
\r
27 import com.google.common.util.concurrent.ListenableFuture;
\r
28 import com.google.common.util.concurrent.SettableFuture;
\r
31 * Class implementing server over UDP for handling incoming connections.
\r
33 * @author michal.polkorab
\r
35 public final class UdpHandler implements ServerFacade {
\r
37 private static final Logger LOGGER = LoggerFactory
\r
38 .getLogger(UdpHandler.class);
\r
40 private String address;
\r
41 private EventLoopGroup group;
\r
42 private final InetAddress startupAddress;
\r
43 private final SettableFuture<Boolean> isOnlineFuture;
\r
44 private UdpChannelInitializer channelInitializer;
\r
45 private ThreadConfiguration threadConfig;
\r
48 * Constructor of UdpHandler that listens on selected port.
\r
50 * @param port listening port of UdpHandler server
\r
52 public UdpHandler(final int port) {
\r
57 * Constructor of UdpHandler that listens on selected address and port.
\r
58 * @param address listening address of UdpHandler server
\r
59 * @param port listening port of UdpHandler server
\r
61 public UdpHandler(final InetAddress address, final int port) {
\r
63 this.startupAddress = address;
\r
64 isOnlineFuture = SettableFuture.create();
\r
69 if (threadConfig != null) {
\r
70 group = new NioEventLoopGroup(threadConfig.getWorkerThreadCount());
\r
72 group = new NioEventLoopGroup();
\r
74 final ChannelFuture f;
\r
76 Bootstrap b = new Bootstrap();
\r
78 .channel(NioDatagramChannel.class)
\r
79 .option(ChannelOption.SO_BROADCAST, false)
\r
80 .handler(channelInitializer);
\r
82 if (startupAddress != null) {
\r
83 f = b.bind(startupAddress.getHostAddress(), port).sync();
\r
85 f = b.bind(port).sync();
\r
87 } catch (InterruptedException e) {
\r
88 LOGGER.error("Interrupted while binding port {}", port, e);
\r
93 InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
\r
94 this.address = isa.getHostString();
\r
96 // Update port, as it may have been specified as 0
\r
97 this.port = isa.getPort();
\r
99 LOGGER.debug("Address from udpHandler: {}", address);
\r
100 isOnlineFuture.set(true);
\r
101 LOGGER.info("Switch listener started and ready to accept incoming udp connections on port: {}", port);
\r
102 f.channel().closeFuture().sync();
\r
103 } catch (InterruptedException e) {
\r
104 LOGGER.error("Interrupted while waiting for port {} shutdown", port, e);
\r
111 public ListenableFuture<Boolean> shutdown() {
\r
112 final SettableFuture<Boolean> result = SettableFuture.create();
\r
113 group.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
\r
116 public void operationComplete(
\r
117 final io.netty.util.concurrent.Future<Object> downResult) throws Exception {
\r
118 result.set(downResult.isSuccess());
\r
119 if (downResult.cause() != null) {
\r
120 result.setException(downResult.cause());
\r
129 public ListenableFuture<Boolean> getIsOnlineFuture() {
\r
130 return isOnlineFuture;
\r
136 public int getPort() {
\r
141 * @param channelInitializer
\r
143 public void setChannelInitializer(UdpChannelInitializer channelInitializer) {
\r
144 this.channelInitializer = channelInitializer;
\r
148 public void setThreadConfig(ThreadConfiguration threadConfig) {
\r
149 this.threadConfig = threadConfig;
\r