2 * Copyright (c) 2014 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.core;
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.ChannelOption;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.epoll.EpollDatagramChannel;
16 import io.netty.channel.epoll.EpollEventLoopGroup;
17 import io.netty.channel.nio.NioEventLoopGroup;
18 import io.netty.channel.socket.DatagramChannel;
19 import io.netty.channel.socket.nio.NioDatagramChannel;
20 import io.netty.util.concurrent.GenericFutureListener;
22 import java.net.InetAddress;
23 import java.net.InetSocketAddress;
25 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.google.common.util.concurrent.ListenableFuture;
30 import com.google.common.util.concurrent.SettableFuture;
33 * Class implementing server over UDP for handling incoming connections.
35 * @author michal.polkorab
37 public final class UdpHandler implements ServerFacade {
39 private static final Logger LOGGER = LoggerFactory
40 .getLogger(UdpHandler.class);
42 private EventLoopGroup group;
43 private final InetAddress startupAddress;
44 private final SettableFuture<Boolean> isOnlineFuture;
45 private UdpChannelInitializer channelInitializer;
46 private ThreadConfiguration threadConfig;
47 private Class<? extends DatagramChannel> datagramChannelClass;
50 * Constructor of UdpHandler that listens on selected port.
52 * @param port listening port of UdpHandler server
54 public UdpHandler(final int port) {
59 * Constructor of UdpHandler that listens on selected address and port.
60 * @param address listening address of UdpHandler server
61 * @param port listening port of UdpHandler server
63 public UdpHandler(final InetAddress address, final int port) {
65 this.startupAddress = address;
66 isOnlineFuture = SettableFuture.create();
71 final ChannelFuture f;
73 Bootstrap b = new Bootstrap();
75 .channel(datagramChannelClass)
76 .option(ChannelOption.SO_BROADCAST, false)
77 .handler(channelInitializer);
79 if (startupAddress != null) {
80 f = b.bind(startupAddress.getHostAddress(), port).sync();
82 f = b.bind(port).sync();
84 } catch (InterruptedException e) {
85 LOGGER.error("Interrupted while binding port {}", port, e);
90 InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
91 String address = isa.getHostString();
93 // Update port, as it may have been specified as 0
94 this.port = isa.getPort();
96 LOGGER.debug("Address from udpHandler: {}", address);
97 isOnlineFuture.set(true);
98 LOGGER.info("Switch listener started and ready to accept incoming udp connections on port: {}", port);
99 f.channel().closeFuture().sync();
100 } catch (InterruptedException e) {
101 LOGGER.error("Interrupted while waiting for port {} shutdown", port, e);
108 public ListenableFuture<Boolean> shutdown() {
109 final SettableFuture<Boolean> result = SettableFuture.create();
110 group.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
113 public void operationComplete(
114 final io.netty.util.concurrent.Future<Object> downResult) throws Exception {
115 result.set(downResult.isSuccess());
116 if (downResult.cause() != null) {
117 result.setException(downResult.cause());
126 public ListenableFuture<Boolean> getIsOnlineFuture() {
127 return isOnlineFuture;
133 public int getPort() {
138 * @param channelInitializer
140 public void setChannelInitializer(UdpChannelInitializer channelInitializer) {
141 this.channelInitializer = channelInitializer;
145 public void setThreadConfig(ThreadConfiguration threadConfig) {
146 this.threadConfig = threadConfig;
150 * Initiate event loop groups
151 * @param threadConfiguration number of threads to be created, if not specified in threadConfig
153 public void initiateEventLoopGroups(ThreadConfiguration threadConfiguration, boolean isEpollEnabled) {
156 initiateEpollEventLoopGroups(threadConfiguration);
158 initiateNioEventLoopGroups(threadConfiguration);
163 * Initiate Nio event loop groups
164 * @param threadConfiguration number of threads to be created, if not specified in threadConfig
166 public void initiateNioEventLoopGroups(ThreadConfiguration threadConfiguration) {
167 datagramChannelClass = NioDatagramChannel.class;
168 if (threadConfiguration != null) {
169 group = new NioEventLoopGroup(threadConfiguration.getWorkerThreadCount());
171 group = new NioEventLoopGroup();
176 * Initiate Epoll event loop groups with Nio as fall back
177 * @param threadConfiguration
179 protected void initiateEpollEventLoopGroups(ThreadConfiguration threadConfiguration) {
181 datagramChannelClass = EpollDatagramChannel.class;
182 if (threadConfiguration != null) {
183 group = new EpollEventLoopGroup(threadConfiguration.getWorkerThreadCount());
185 group = new EpollEventLoopGroup();
188 } catch (Throwable ex) {
189 LOGGER.debug("Epoll initiation failed");
193 initiateNioEventLoopGroups(threadConfiguration);