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 org.iotivity.cloud.base.protocols.coap.CoapDecoder;
32 import org.iotivity.cloud.base.protocols.coap.CoapEncoder;
33 import org.iotivity.cloud.util.Logger;
35 import io.netty.bootstrap.ServerBootstrap;
36 import io.netty.channel.ChannelFuture;
37 import io.netty.channel.ChannelHandler;
38 import io.netty.channel.ChannelInitializer;
39 import io.netty.channel.ChannelOption;
40 import io.netty.channel.ChannelPipeline;
41 import io.netty.channel.EventLoopGroup;
42 import io.netty.channel.nio.NioEventLoopGroup;
43 import io.netty.channel.socket.SocketChannel;
44 import io.netty.channel.socket.nio.NioServerSocketChannel;
45 import io.netty.handler.logging.LogLevel;
46 import io.netty.handler.logging.LoggingHandler;
47 import io.netty.util.concurrent.GenericFutureListener;
49 public class CoapServer {
51 private static class CoAPServerInitializer
52 extends ChannelInitializer<SocketChannel> {
54 private List<ChannelHandler> additionalHandlers = new ArrayList<ChannelHandler>();
56 public CoAPServerInitializer() {
59 public void addHandler(ChannelHandler handler) {
60 additionalHandlers.add(handler);
64 public void initChannel(SocketChannel ch) {
65 ChannelPipeline p = ch.pipeline();
67 * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
69 p.addLast(new CoapDecoder());
70 p.addLast(new CoapEncoder());
71 for (ChannelHandler handler : additionalHandlers) {
77 EventLoopGroup acceptorGroup = new NioEventLoopGroup(1);
79 EventLoopGroup workerGroup = new NioEventLoopGroup();
81 CoAPServerInitializer initializer = new CoAPServerInitializer();
83 public void addHandler(ChannelHandler handler) {
84 initializer.addHandler(handler);
87 public void startServer(InetSocketAddress inetSocketAddress)
88 throws CertificateException, SSLException, InterruptedException {
91 ServerBootstrap b = new ServerBootstrap();
92 b.group(acceptorGroup, workerGroup);
93 b.channel(NioServerSocketChannel.class);
94 b.option(ChannelOption.TCP_NODELAY, true);
95 b.option(ChannelOption.SO_KEEPALIVE, true);
96 b.handler(new LoggingHandler(LogLevel.INFO));
98 b.childHandler(initializer);
100 ChannelFuture channelFuture = b.bind(inetSocketAddress).sync();
102 channelFuture.addListener(new GenericFutureListener<ChannelFuture>() {
104 public void operationComplete(ChannelFuture future)
106 // TODO Auto-generated method stub
107 Logger.d("Connection status of TCP CoAP SERVER : "
108 + future.isSuccess());
115 public void stopServer() throws Exception {
116 acceptorGroup.shutdownGracefully().await();
117 workerGroup.shutdownGracefully().await();