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.server;
25 import java.net.InetSocketAddress;
26 import java.security.cert.CertificateException;
27 import java.util.ArrayList;
28 import java.util.List;
30 import javax.net.ssl.SSLException;
32 import org.iotivity.cloud.base.OICConstants;
33 import org.iotivity.cloud.util.Log;
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;
48 public abstract class Server {
50 EventLoopGroup acceptorGroup = new NioEventLoopGroup(1);
52 EventLoopGroup workerGroup = new NioEventLoopGroup();
54 ServerInitializer mServerInitializer = new ServerInitializer();
56 InetSocketAddress mInetSocketAddress = null;
58 SslContext mSslContext = null;
60 private class ServerInitializer extends ChannelInitializer<SocketChannel> {
61 private List<ChannelHandler> additionalHandlers = new ArrayList<>();
63 public ServerInitializer() {
66 public void addHandler(ChannelHandler handler) {
67 additionalHandlers.add(handler);
71 public void initChannel(SocketChannel ch) throws Exception {
72 ChannelPipeline p = ch.pipeline();
74 if (mSslContext != null) {
75 p.addLast(mSslContext.newHandler(ch.alloc()));
78 p.addLast(onQueryDefaultHandler());
80 for (ChannelHandler handler : additionalHandlers) {
86 public Server(InetSocketAddress inetSocketAddress) {
87 mInetSocketAddress = inetSocketAddress;
90 public void startServer(boolean tlsMode)
91 throws CertificateException, SSLException, InterruptedException {
95 Log.i("Server starts with TLS!");
97 if (tlsMode == true) {
99 File serverCert = new File(OICConstants.CLOUD_CERT_FILE);
101 File serverKey = new File(OICConstants.CLOUD_KEY_FILE);
103 mSslContext = SslContextBuilder.forServer(serverCert, serverKey)
107 ServerBootstrap b = new ServerBootstrap();
108 b.group(acceptorGroup, workerGroup);
109 b.channel(NioServerSocketChannel.class);
110 b.handler(new LoggingHandler(LogLevel.INFO));
112 b.childHandler(mServerInitializer);
114 b.bind(mInetSocketAddress).sync();
115 } catch (Exception e) {
122 public void stopServer() throws Exception {
123 acceptorGroup.shutdownGracefully().await();
124 workerGroup.shutdownGracefully().await();
127 public void addHandler(ChannelHandler handler) {
128 mServerInitializer.addHandler(handler);
131 abstract protected ChannelHandler[] onQueryDefaultHandler();