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;
34 import io.netty.bootstrap.ServerBootstrap;
35 import io.netty.channel.ChannelFuture;
36 import io.netty.channel.ChannelHandler;
37 import io.netty.channel.ChannelInitializer;
38 import io.netty.channel.ChannelOption;
39 import io.netty.channel.ChannelPipeline;
40 import io.netty.channel.EventLoopGroup;
41 import io.netty.channel.nio.NioEventLoopGroup;
42 import io.netty.channel.socket.SocketChannel;
43 import io.netty.channel.socket.nio.NioServerSocketChannel;
44 import io.netty.handler.logging.LogLevel;
45 import io.netty.handler.logging.LoggingHandler;
46 import io.netty.util.concurrent.GenericFutureListener;
48 public class CoapServer {
50 private static class CoAPServerInitializer
51 extends ChannelInitializer<SocketChannel> {
53 private List<ChannelHandler> additionalHandlers = new ArrayList<ChannelHandler>();
55 public CoAPServerInitializer() {
58 public void addHandler(ChannelHandler handler) {
59 additionalHandlers.add(handler);
63 public void initChannel(SocketChannel ch) {
64 ChannelPipeline p = ch.pipeline();
66 * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
68 p.addLast(new CoapDecoder());
69 p.addLast(new CoapEncoder());
70 for (ChannelHandler handler : additionalHandlers) {
76 EventLoopGroup bossGroup = new NioEventLoopGroup(1);
78 EventLoopGroup workerGroup = new NioEventLoopGroup();
80 CoAPServerInitializer initializer = new CoAPServerInitializer();
82 public void addHandler(ChannelHandler handler) {
83 initializer.addHandler(handler);
86 public void startServer(InetSocketAddress inetSocketAddress)
87 throws CertificateException, SSLException, InterruptedException {
90 ServerBootstrap b = new ServerBootstrap();
91 b.group(bossGroup, workerGroup);
92 b.channel(NioServerSocketChannel.class);
93 b.option(ChannelOption.TCP_NODELAY, true);
94 b.option(ChannelOption.SO_KEEPALIVE, true);
95 b.handler(new LoggingHandler(LogLevel.INFO));
97 b.childHandler(initializer);
99 ChannelFuture ch = b.bind(inetSocketAddress).sync();
100 ch.addListener(new GenericFutureListener<ChannelFuture>() {
103 public void operationComplete(ChannelFuture future)
105 // TODO Auto-generated method stub
107 .println("Connection status of TCP CoAP SERVER : "
108 + future.isSuccess());
117 public void stopServer() {
118 // shut down all event loops
119 if (bossGroup != null) {
120 bossGroup.shutdownGracefully();
123 bossGroup.terminationFuture().sync();
124 } catch (InterruptedException e) {
125 // TODO Auto-generated catch block
130 if (workerGroup != null) {
131 workerGroup.shutdownGracefully();
134 workerGroup.terminationFuture().sync();
135 } catch (InterruptedException e) {
136 // TODO Auto-generated catch block