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