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.nio.NioEventLoopGroup;
16 import io.netty.channel.socket.nio.NioDatagramChannel;
17 import io.netty.util.concurrent.GenericFutureListener;
19 import java.net.InetAddress;
20 import java.net.InetSocketAddress;
22 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.google.common.util.concurrent.SettableFuture;
30 * Class implementing server over UDP for handling incoming connections.
32 * @author michal.polkorab
34 public final class UdpHandler implements ServerFacade {
36 private static final Logger LOGGER = LoggerFactory
37 .getLogger(UdpHandler.class);
39 private EventLoopGroup group;
40 private final InetAddress startupAddress;
41 private final SettableFuture<Boolean> isOnlineFuture;
42 private UdpChannelInitializer channelInitializer;
43 private ThreadConfiguration threadConfig;
46 * Constructor of UdpHandler that listens on selected port.
48 * @param port listening port of UdpHandler server
50 public UdpHandler(final int port) {
55 * Constructor of UdpHandler that listens on selected address and port.
56 * @param address listening address of UdpHandler server
57 * @param port listening port of UdpHandler server
59 public UdpHandler(final InetAddress address, final int port) {
61 this.startupAddress = address;
62 isOnlineFuture = SettableFuture.create();
67 if (threadConfig != null) {
68 group = new NioEventLoopGroup(threadConfig.getWorkerThreadCount());
70 group = new NioEventLoopGroup();
72 final ChannelFuture f;
74 Bootstrap b = new Bootstrap();
76 .channel(NioDatagramChannel.class)
77 .option(ChannelOption.SO_BROADCAST, false)
78 .handler(channelInitializer);
80 if (startupAddress != null) {
81 f = b.bind(startupAddress.getHostAddress(), port).sync();
83 f = b.bind(port).sync();
85 } catch (InterruptedException e) {
86 LOGGER.error("Interrupted while binding port {}", port, e);
91 InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
92 String address = isa.getHostString();
94 // Update port, as it may have been specified as 0
95 this.port = isa.getPort();
97 LOGGER.debug("Address from udpHandler: {}", address);
98 isOnlineFuture.set(true);
99 LOGGER.info("Switch listener started and ready to accept incoming udp connections on port: {}", port);
100 f.channel().closeFuture().sync();
101 } catch (InterruptedException e) {
102 LOGGER.error("Interrupted while waiting for port {} shutdown", port, e);
109 public ListenableFuture<Boolean> shutdown() {
110 final SettableFuture<Boolean> result = SettableFuture.create();
111 group.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
114 public void operationComplete(
115 final io.netty.util.concurrent.Future<Object> downResult) throws Exception {
116 result.set(downResult.isSuccess());
117 if (downResult.cause() != null) {
118 result.setException(downResult.cause());
127 public ListenableFuture<Boolean> getIsOnlineFuture() {
128 return isOnlineFuture;
134 public int getPort() {
139 * @param channelInitializer
141 public void setChannelInitializer(UdpChannelInitializer channelInitializer) {
142 this.channelInitializer = channelInitializer;
146 public void setThreadConfig(ThreadConfiguration threadConfig) {
147 this.threadConfig = threadConfig;