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