2 *******************************************************************
4 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 // package org.iotivity.cloud.base;
24 // import java.net.InetSocketAddress;
25 // import java.security.cert.CertificateException;
26 // import java.util.ArrayList;
27 // import java.util.List;
29 // import javax.net.ssl.SSLException;
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;
46 // public class HttpServer {
48 // private static class HttpServerInitializer
49 // extends ChannelInitializer<SocketChannel> {
51 // private List<ChannelHandler> additionalHandlers = new
52 // ArrayList<ChannelHandler>();
54 // public HttpServerInitializer() {
57 // public void addHandler(ChannelHandler handler) {
58 // additionalHandlers.add(handler);
62 // public void initChannel(SocketChannel ch) {
63 // ChannelPipeline p = ch.pipeline();
65 // * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
67 // p.addLast(new HttpRequestDecoder());
68 // // Uncomment the following line if you don't want to handle
70 // // p.addLast(new HttpObjectAggregator(1048576));
71 // p.addLast(new HttpResponseEncoder());
72 // // Remove the following line if you don't want automatic content
74 // // p.addLast(new HttpContentCompressor());
75 // for (ChannelHandler handler : additionalHandlers) {
76 // p.addLast(handler);
82 // EventLoopGroup bossGroup = new NioEventLoopGroup(1);
84 // EventLoopGroup workerGroup = new NioEventLoopGroup();
86 // HttpServerInitializer initializer = new HttpServerInitializer();
88 // public void addHandler(ChannelHandler handler) {
89 // initializer.addHandler(handler);
92 // public void startServer(InetSocketAddress inetSocketAddress)
93 // throws CertificateException, SSLException, InterruptedException {
96 // ServerBootstrap b = new ServerBootstrap();
97 // b.group(bossGroup, workerGroup);
98 // b.channel(NioServerSocketChannel.class);
99 // b.handler(new LoggingHandler(LogLevel.INFO));
101 // b.childHandler(initializer);
103 // ChannelFuture ch = b.bind(inetSocketAddress).sync();
104 // ch.addListener(new GenericFutureListener<ChannelFuture>() {
107 // public void operationComplete(ChannelFuture future)
108 // throws Exception {
109 // // TODO Auto-generated method stub
111 // .println("Connection status of TCP Http SERVER : "
112 // + future.isSuccess());
121 // public void stopServer() {
122 // // shut down all event loops
123 // if (bossGroup != null) {
124 // bossGroup.shutdownGracefully();
127 // bossGroup.terminationFuture().sync();
128 // } catch (InterruptedException e) {
129 // // TODO Auto-generated catch block
130 // e.printStackTrace();
134 // if (workerGroup != null) {
135 // workerGroup.shutdownGracefully();
138 // workerGroup.terminationFuture().sync();
139 // } catch (InterruptedException e) {
140 // // TODO Auto-generated catch block
141 // e.printStackTrace();