[Simulator] Minor UI changes fixing the IOT-1087.
[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
34 import io.netty.bootstrap.ServerBootstrap;
35 import io.netty.channel.ChannelFuture;
36 import io.netty.channel.ChannelHandler;
37 import io.netty.channel.ChannelInitializer;
38 import io.netty.channel.ChannelOption;
39 import io.netty.channel.ChannelPipeline;
40 import io.netty.channel.EventLoopGroup;
41 import io.netty.channel.nio.NioEventLoopGroup;
42 import io.netty.channel.socket.SocketChannel;
43 import io.netty.channel.socket.nio.NioServerSocketChannel;
44 import io.netty.handler.logging.LogLevel;
45 import io.netty.handler.logging.LoggingHandler;
46 import io.netty.util.concurrent.GenericFutureListener;
47
48 public class CoapServer {
49
50     private static class CoAPServerInitializer
51             extends ChannelInitializer<SocketChannel> {
52
53         private List<ChannelHandler> additionalHandlers = new ArrayList<ChannelHandler>();
54
55         public CoAPServerInitializer() {
56         }
57
58         public void addHandler(ChannelHandler handler) {
59             additionalHandlers.add(handler);
60         }
61
62         @Override
63         public void initChannel(SocketChannel ch) {
64             ChannelPipeline p = ch.pipeline();
65             /*
66              * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
67              */
68             p.addLast(new CoapDecoder());
69             p.addLast(new CoapEncoder());
70             for (ChannelHandler handler : additionalHandlers) {
71                 p.addLast(handler);
72             }
73         }
74     }
75
76     EventLoopGroup bossGroup = new NioEventLoopGroup(1);
77
78     EventLoopGroup workerGroup = new NioEventLoopGroup();
79
80     CoAPServerInitializer initializer = new CoAPServerInitializer();
81
82     public void addHandler(ChannelHandler handler) {
83         initializer.addHandler(handler);
84     }
85
86     public void startServer(InetSocketAddress inetSocketAddress)
87             throws CertificateException, SSLException, InterruptedException {
88
89         try {
90             ServerBootstrap b = new ServerBootstrap();
91             b.group(bossGroup, workerGroup);
92             b.channel(NioServerSocketChannel.class);
93             b.option(ChannelOption.TCP_NODELAY, true);
94             b.option(ChannelOption.SO_KEEPALIVE, true);
95             b.handler(new LoggingHandler(LogLevel.INFO));
96
97             b.childHandler(initializer);
98
99             ChannelFuture ch = b.bind(inetSocketAddress).sync();
100             ch.addListener(new GenericFutureListener<ChannelFuture>() {
101
102                 @Override
103                 public void operationComplete(ChannelFuture future)
104                         throws Exception {
105                     // TODO Auto-generated method stub
106                     System.out
107                             .println("Connection status of TCP CoAP SERVER  : "
108                                     + future.isSuccess());
109                 }
110
111             });
112         } finally {
113         }
114
115     }
116
117     public void stopServer() {
118         // shut down all event loops
119         if (bossGroup != null) {
120             bossGroup.shutdownGracefully();
121
122             try {
123                 bossGroup.terminationFuture().sync();
124             } catch (InterruptedException e) {
125                 // TODO Auto-generated catch block
126                 e.printStackTrace();
127             }
128         }
129
130         if (workerGroup != null) {
131             workerGroup.shutdownGracefully();
132
133             try {
134                 workerGroup.terminationFuture().sync();
135             } catch (InterruptedException e) {
136                 // TODO Auto-generated catch block
137                 e.printStackTrace();
138             }
139         }
140     }
141
142 }