Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / CoapServer.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.base;
23
24 import java.net.InetSocketAddress;
25 import java.security.cert.CertificateException;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import javax.net.ssl.SSLException;
30
31 import org.iotivity.cloud.base.protocols.coap.CoapDecoder;
32 import org.iotivity.cloud.base.protocols.coap.CoapEncoder;
33 import org.iotivity.cloud.util.Logger;
34
35 import io.netty.bootstrap.ServerBootstrap;
36 import io.netty.channel.ChannelFuture;
37 import io.netty.channel.ChannelHandler;
38 import io.netty.channel.ChannelInitializer;
39 import io.netty.channel.ChannelOption;
40 import io.netty.channel.ChannelPipeline;
41 import io.netty.channel.EventLoopGroup;
42 import io.netty.channel.nio.NioEventLoopGroup;
43 import io.netty.channel.socket.SocketChannel;
44 import io.netty.channel.socket.nio.NioServerSocketChannel;
45 import io.netty.handler.logging.LogLevel;
46 import io.netty.handler.logging.LoggingHandler;
47 import io.netty.util.concurrent.GenericFutureListener;
48
49 public class CoapServer {
50
51     private static class CoAPServerInitializer
52             extends ChannelInitializer<SocketChannel> {
53
54         private List<ChannelHandler> additionalHandlers = new ArrayList<ChannelHandler>();
55
56         public CoAPServerInitializer() {
57         }
58
59         public void addHandler(ChannelHandler handler) {
60             additionalHandlers.add(handler);
61         }
62
63         @Override
64         public void initChannel(SocketChannel ch) {
65             ChannelPipeline p = ch.pipeline();
66             /*
67              * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
68              */
69             p.addLast(new CoapDecoder());
70             p.addLast(new CoapEncoder());
71             for (ChannelHandler handler : additionalHandlers) {
72                 p.addLast(handler);
73             }
74         }
75     }
76
77     EventLoopGroup acceptorGroup = new NioEventLoopGroup(1);
78
79     EventLoopGroup workerGroup = new NioEventLoopGroup();
80
81     CoAPServerInitializer initializer = new CoAPServerInitializer();
82     
83     public void addHandler(ChannelHandler handler) {
84         initializer.addHandler(handler);
85     }
86
87     public void startServer(InetSocketAddress inetSocketAddress)
88             throws CertificateException, SSLException, InterruptedException {
89
90         try {
91             ServerBootstrap b = new ServerBootstrap();
92             b.group(acceptorGroup, workerGroup);
93             b.channel(NioServerSocketChannel.class);
94             b.option(ChannelOption.TCP_NODELAY, true);
95             b.option(ChannelOption.SO_KEEPALIVE, true);
96             b.handler(new LoggingHandler(LogLevel.INFO));
97
98             b.childHandler(initializer);
99
100             ChannelFuture channelFuture = b.bind(inetSocketAddress).sync();
101
102             channelFuture.addListener(new GenericFutureListener<ChannelFuture>() {
103                 @Override
104                 public void operationComplete(ChannelFuture future)
105                         throws Exception {
106                     // TODO Auto-generated method stub
107                     Logger.d("Connection status of TCP CoAP SERVER  : "
108                                     + future.isSuccess());
109                 }
110             });
111         } finally {
112         }
113     }
114
115     public void stopServer() throws Exception {
116         acceptorGroup.shutdownGracefully().await();
117         workerGroup.shutdownGracefully().await();
118     }
119 }