Add CoAP over Websocket interface in cloud
[platform/upstream/iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / server / Server.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.server;
23
24 import java.io.File;
25 import java.net.InetSocketAddress;
26 import java.security.cert.CertificateException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import javax.net.ssl.SSLException;
31
32 import org.iotivity.cloud.base.OICConstants;
33 import org.iotivity.cloud.util.Log;
34
35 import io.netty.bootstrap.ServerBootstrap;
36 import io.netty.channel.ChannelHandler;
37 import io.netty.channel.ChannelInitializer;
38 import io.netty.channel.ChannelPipeline;
39 import io.netty.channel.EventLoopGroup;
40 import io.netty.channel.nio.NioEventLoopGroup;
41 import io.netty.channel.socket.SocketChannel;
42 import io.netty.channel.socket.nio.NioServerSocketChannel;
43 import io.netty.handler.logging.LogLevel;
44 import io.netty.handler.logging.LoggingHandler;
45 import io.netty.handler.ssl.SslContext;
46 import io.netty.handler.ssl.SslContextBuilder;
47
48 public abstract class Server {
49
50     EventLoopGroup    acceptorGroup      = new NioEventLoopGroup(1);
51
52     EventLoopGroup    workerGroup        = new NioEventLoopGroup();
53
54     ServerInitializer mServerInitializer = new ServerInitializer();
55
56     InetSocketAddress mInetSocketAddress = null;
57
58     SslContext        mSslContext        = null;
59
60     private class ServerInitializer extends ChannelInitializer<SocketChannel> {
61         private List<ChannelHandler> additionalHandlers = new ArrayList<>();
62
63         public ServerInitializer() {
64         }
65
66         public void addHandler(ChannelHandler handler) {
67             additionalHandlers.add(handler);
68         }
69
70         @Override
71         public void initChannel(SocketChannel ch) throws Exception {
72             ChannelPipeline p = ch.pipeline();
73
74             if (mSslContext != null) {
75                 p.addLast(mSslContext.newHandler(ch.alloc()));
76             }
77
78             p.addLast(onQueryDefaultHandler());
79
80             for (ChannelHandler handler : additionalHandlers) {
81                 p.addLast(handler);
82             }
83         }
84     }
85
86     public Server(InetSocketAddress inetSocketAddress) {
87         mInetSocketAddress = inetSocketAddress;
88     }
89
90     public void startServer(boolean tlsMode)
91             throws CertificateException, SSLException, InterruptedException {
92
93         try {
94             if (tlsMode)
95                 Log.i("Server starts with TLS!");
96
97             if (tlsMode == true) {
98
99                 File serverCert = new File(OICConstants.CLOUD_CERT_FILE);
100
101                 File serverKey = new File(OICConstants.CLOUD_KEY_FILE);
102
103                 mSslContext = SslContextBuilder.forServer(serverCert, serverKey)
104                         .build();
105             }
106
107             ServerBootstrap b = new ServerBootstrap();
108             b.group(acceptorGroup, workerGroup);
109             b.channel(NioServerSocketChannel.class);
110             b.handler(new LoggingHandler(LogLevel.INFO));
111
112             b.childHandler(mServerInitializer);
113
114             b.bind(mInetSocketAddress).sync();
115         } catch (Exception e) {
116             e.printStackTrace();
117             throw e;
118         } finally {
119         }
120     }
121
122     public void stopServer() throws Exception {
123         acceptorGroup.shutdownGracefully().await();
124         workerGroup.shutdownGracefully().await();
125     }
126
127     public void addHandler(ChannelHandler handler) {
128         mServerInitializer.addHandler(handler);
129     }
130
131     abstract protected ChannelHandler[] onQueryDefaultHandler();
132 }