--- /dev/null
+/*\r
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.openflowjava.protocol.impl.core;\r
+\r
+import io.netty.bootstrap.Bootstrap;\r
+import io.netty.channel.ChannelFuture;\r
+import io.netty.channel.ChannelOption;\r
+import io.netty.channel.EventLoopGroup;\r
+import io.netty.channel.nio.NioEventLoopGroup;\r
+import io.netty.channel.socket.nio.NioDatagramChannel;\r
+import io.netty.util.concurrent.GenericFutureListener;\r
+\r
+import java.net.InetAddress;\r
+import java.net.InetSocketAddress;\r
+\r
+import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;\r
+import org.opendaylight.openflowjava.protocol.impl.connection.ServerFacade;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import com.google.common.util.concurrent.ListenableFuture;\r
+import com.google.common.util.concurrent.SettableFuture;\r
+\r
+/**\r
+ * Class implementing server over UDP for handling incoming connections.\r
+ * \r
+ * @author michal.polkorab\r
+ */\r
+public final class UdpHandler implements ServerFacade {\r
+\r
+ private static final Logger LOGGER = LoggerFactory\r
+ .getLogger(UdpHandler.class);\r
+ private int port;\r
+ private String address;\r
+ private EventLoopGroup group;\r
+ private final InetAddress startupAddress;\r
+ private final SettableFuture<Boolean> isOnlineFuture;\r
+ private UdpChannelInitializer channelInitializer;\r
+ private ThreadConfiguration threadConfig;\r
+\r
+ /**\r
+ * Constructor of UdpHandler that listens on selected port.\r
+ *\r
+ * @param port listening port of UdpHandler server\r
+ */\r
+ public UdpHandler(final int port) {\r
+ this(null, port);\r
+ }\r
+\r
+ /**\r
+ * Constructor of UdpHandler that listens on selected address and port.\r
+ * @param address listening address of UdpHandler server\r
+ * @param port listening port of UdpHandler server\r
+ */\r
+ public UdpHandler(final InetAddress address, final int port) {\r
+ this.port = port;\r
+ this.startupAddress = address;\r
+ isOnlineFuture = SettableFuture.create();\r
+ }\r
+\r
+ @Override\r
+ public void run() {\r
+ if (threadConfig != null) {\r
+ group = new NioEventLoopGroup(threadConfig.getWorkerThreadCount());\r
+ } else {\r
+ group = new NioEventLoopGroup();\r
+ }\r
+ final ChannelFuture f;\r
+ try {\r
+ Bootstrap b = new Bootstrap();\r
+ b.group(group)\r
+ .channel(NioDatagramChannel.class)\r
+ .option(ChannelOption.SO_BROADCAST, false)\r
+ .handler(channelInitializer);\r
+\r
+ if (startupAddress != null) {\r
+ f = b.bind(startupAddress.getHostAddress(), port).sync();\r
+ } else {\r
+ f = b.bind(port).sync();\r
+ }\r
+ } catch (InterruptedException e) {\r
+ LOGGER.error("Interrupted while binding port {}", port, e);\r
+ return;\r
+ }\r
+\r
+ try {\r
+ InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();\r
+ this.address = isa.getHostString();\r
+\r
+ // Update port, as it may have been specified as 0\r
+ this.port = isa.getPort();\r
+\r
+ LOGGER.debug("Address from udpHandler: {}", address);\r
+ isOnlineFuture.set(true);\r
+ LOGGER.info("Switch listener started and ready to accept incoming udp connections on port: {}", port);\r
+ f.channel().closeFuture().sync();\r
+ } catch (InterruptedException e) {\r
+ LOGGER.error("Interrupted while waiting for port {} shutdown", port, e);\r
+ } finally {\r
+ shutdown();\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public ListenableFuture<Boolean> shutdown() {\r
+ final SettableFuture<Boolean> result = SettableFuture.create();\r
+ group.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {\r
+\r
+ @Override\r
+ public void operationComplete(\r
+ final io.netty.util.concurrent.Future<Object> downResult) throws Exception {\r
+ result.set(downResult.isSuccess());\r
+ if (downResult.cause() != null) {\r
+ result.setException(downResult.cause());\r
+ }\r
+ }\r
+\r
+ });\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public ListenableFuture<Boolean> getIsOnlineFuture() {\r
+ return isOnlineFuture;\r
+ }\r
+\r
+ /**\r
+ * @return the port\r
+ */\r
+ public int getPort() {\r
+ return port;\r
+ }\r
+\r
+ /**\r
+ * @param channelInitializer\r
+ */\r
+ public void setChannelInitializer(UdpChannelInitializer channelInitializer) {\r
+ this.channelInitializer = channelInitializer;\r
+ }\r
+\r
+ @Override\r
+ public void setThreadConfig(ThreadConfiguration threadConfig) {\r
+ this.threadConfig = threadConfig;\r
+ }\r
+}
\ No newline at end of file