2 * //****************************************************************** // //
3 * Copyright 2016 Samsung Electronics All Rights Reserved. //
4 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // //
5 * Licensed under the Apache License, Version 2.0 (the "License"); // you may
6 * not use this file except in compliance with the License. // You may obtain a
7 * copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // //
8 * Unless required by applicable law or agreed to in writing, software //
9 * distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT
10 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the
11 * License for the specific language governing permissions and // limitations
12 * under the License. //
13 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
15 // package org.iotivity.cloud.base;
17 // import java.net.InetSocketAddress;
18 // import java.security.cert.CertificateException;
19 // import java.util.ArrayList;
20 // import java.util.List;
22 // import javax.net.ssl.SSLException;
24 // import io.netty.bootstrap.ServerBootstrap;
25 // import io.netty.channel.ChannelFuture;
26 // import io.netty.channel.ChannelHandler;
27 // import io.netty.channel.ChannelInitializer;
28 // import io.netty.channel.ChannelPipeline;
29 // import io.netty.channel.EventLoopGroup;
30 // import io.netty.channel.nio.NioEventLoopGroup;
31 // import io.netty.channel.socket.SocketChannel;
32 // import io.netty.channel.socket.nio.NioServerSocketChannel;
33 // import io.netty.handler.codec.http.HttpRequestDecoder;
34 // import io.netty.handler.codec.http.HttpResponseEncoder;
35 // import io.netty.handler.logging.LogLevel;
36 // import io.netty.handler.logging.LoggingHandler;
37 // import io.netty.util.concurrent.GenericFutureListener;
39 // public class HttpServer {
41 // private static class HttpServerInitializer
42 // extends ChannelInitializer<SocketChannel> {
44 // private List<ChannelHandler> additionalHandlers = new
45 // ArrayList<ChannelHandler>();
47 // public HttpServerInitializer() {
50 // public void addHandler(ChannelHandler handler) {
51 // additionalHandlers.add(handler);
55 // public void initChannel(SocketChannel ch) {
56 // ChannelPipeline p = ch.pipeline();
58 // * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
60 // p.addLast(new HttpRequestDecoder());
61 // // Uncomment the following line if you don't want to handle
63 // // p.addLast(new HttpObjectAggregator(1048576));
64 // p.addLast(new HttpResponseEncoder());
65 // // Remove the following line if you don't want automatic content
67 // // p.addLast(new HttpContentCompressor());
68 // for (ChannelHandler handler : additionalHandlers) {
69 // p.addLast(handler);
75 // EventLoopGroup bossGroup = new NioEventLoopGroup(1);
77 // EventLoopGroup workerGroup = new NioEventLoopGroup();
79 // HttpServerInitializer initializer = new HttpServerInitializer();
81 // public void addHandler(ChannelHandler handler) {
82 // initializer.addHandler(handler);
85 // public void startServer(InetSocketAddress inetSocketAddress)
86 // throws CertificateException, SSLException, InterruptedException {
89 // ServerBootstrap b = new ServerBootstrap();
90 // b.group(bossGroup, workerGroup);
91 // b.channel(NioServerSocketChannel.class);
92 // b.handler(new LoggingHandler(LogLevel.INFO));
94 // b.childHandler(initializer);
96 // ChannelFuture ch = b.bind(inetSocketAddress).sync();
97 // ch.addListener(new GenericFutureListener<ChannelFuture>() {
100 // public void operationComplete(ChannelFuture future)
101 // throws Exception {
102 // // TODO Auto-generated method stub
104 // .println("Connection status of TCP Http SERVER : "
105 // + future.isSuccess());
114 // public void stopServer() {
115 // // shut down all event loops
116 // if (bossGroup != null) {
117 // bossGroup.shutdownGracefully();
120 // bossGroup.terminationFuture().sync();
121 // } catch (InterruptedException e) {
122 // // TODO Auto-generated catch block
123 // e.printStackTrace();
127 // if (workerGroup != null) {
128 // workerGroup.shutdownGracefully();
131 // workerGroup.terminationFuture().sync();
132 // } catch (InterruptedException e) {
133 // // TODO Auto-generated catch block
134 // e.printStackTrace();