Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / UdpHandler.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.core;
10
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;
18
19 import java.net.InetAddress;
20 import java.net.InetSocketAddress;
21
22 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.google.common.util.concurrent.SettableFuture;
28
29 /**
30  * Class implementing server over UDP for handling incoming connections.
31  * 
32  * @author michal.polkorab
33  */
34 public final class UdpHandler implements ServerFacade {
35
36     private static final Logger LOGGER = LoggerFactory
37             .getLogger(UdpHandler.class);
38     private int port;
39     private EventLoopGroup group;
40     private final InetAddress startupAddress;
41     private final SettableFuture<Boolean> isOnlineFuture;
42     private UdpChannelInitializer channelInitializer;
43     private ThreadConfiguration threadConfig;
44
45     /**
46      * Constructor of UdpHandler that listens on selected port.
47      *
48      * @param port listening port of UdpHandler server
49      */
50     public UdpHandler(final int port) {
51         this(null, port);
52     }
53
54     /**
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
58      */
59     public UdpHandler(final InetAddress address, final int port) {
60         this.port = port;
61         this.startupAddress = address;
62         isOnlineFuture = SettableFuture.create();
63     }
64
65     @Override
66     public void run() {
67         if (threadConfig != null) {
68             group = new NioEventLoopGroup(threadConfig.getWorkerThreadCount());
69         } else {
70             group = new NioEventLoopGroup();
71         }
72         final ChannelFuture f;
73         try {
74             Bootstrap b = new Bootstrap();
75             b.group(group)
76              .channel(NioDatagramChannel.class)
77              .option(ChannelOption.SO_BROADCAST, false)
78              .handler(channelInitializer);
79
80             if (startupAddress != null) {
81                 f = b.bind(startupAddress.getHostAddress(), port).sync();
82             } else {
83                 f = b.bind(port).sync();
84             }
85         } catch (InterruptedException e) {
86             LOGGER.error("Interrupted while binding port {}", port, e);
87             return;
88         }
89
90         try {
91             InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
92             String address = isa.getHostString();
93
94             // Update port, as it may have been specified as 0
95             this.port = isa.getPort();
96
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);
103         } finally {
104             shutdown();
105         }
106     }
107
108     @Override
109     public ListenableFuture<Boolean> shutdown() {
110         final SettableFuture<Boolean> result = SettableFuture.create();
111         group.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
112
113             @Override
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());
119                 }
120             }
121
122         });
123         return result;
124     }
125
126     @Override
127     public ListenableFuture<Boolean> getIsOnlineFuture() {
128         return isOnlineFuture;
129     }
130
131     /**
132      * @return the port
133      */
134     public int getPort() {
135         return port;
136     }
137
138     /**
139      * @param channelInitializer
140      */
141     public void setChannelInitializer(UdpChannelInitializer channelInitializer) {
142         this.channelInitializer = channelInitializer;
143     }
144
145     @Override
146     public void setThreadConfig(ThreadConfiguration threadConfig) {
147         this.threadConfig = threadConfig;
148     }
149 }