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.util.ArrayList;
26 import java.util.List;
28 import org.iotivity.cloud.base.protocols.coap.CoapDecoder;
29 import org.iotivity.cloud.base.protocols.coap.CoapEncoder;
30 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
32 import io.netty.bootstrap.Bootstrap;
33 import io.netty.channel.ChannelFuture;
34 import io.netty.channel.ChannelHandler;
35 import io.netty.channel.ChannelInitializer;
36 import io.netty.channel.ChannelOption;
37 import io.netty.channel.ChannelPipeline;
38 import io.netty.channel.EventLoopGroup;
39 import io.netty.channel.nio.NioEventLoopGroup;
40 import io.netty.channel.socket.SocketChannel;
41 import io.netty.channel.socket.nio.NioSocketChannel;
42 import io.netty.util.concurrent.GenericFutureListener;
44 public class CoapClient {
46 private static class CoAPClientInitializer
47 extends ChannelInitializer<SocketChannel> {
49 private List<ChannelHandler> additionalHandlers = new ArrayList<ChannelHandler>();
51 public CoAPClientInitializer() {
54 public void addHandler(ChannelHandler handler) {
55 additionalHandlers.add(handler);
59 public void initChannel(SocketChannel ch) {
60 ChannelPipeline p = ch.pipeline();
62 * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
64 p.addLast(new CoapDecoder());
65 p.addLast(new CoapEncoder());
66 for (ChannelHandler handler : additionalHandlers) {
72 private ChannelFuture channelFuture;
74 CoAPClientInitializer initializer = new CoAPClientInitializer();
76 public void addHandler(ChannelHandler handler) {
77 initializer.addHandler(handler);
80 public void startClient(final InetSocketAddress inetSocketAddress)
81 throws InterruptedException {
84 EventLoopGroup bossGroup = new NioEventLoopGroup();
86 // EpollEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2);
89 Bootstrap b = new Bootstrap();
91 b.channel(NioSocketChannel.class);
92 b.option(ChannelOption.TCP_NODELAY, true);
93 b.option(ChannelOption.SO_KEEPALIVE, true);
94 b.option(ChannelOption.SO_REUSEADDR, true);
96 b.handler(initializer);
98 channelFuture = b.connect(inetSocketAddress).sync();
101 .addListener(new GenericFutureListener<ChannelFuture>() {
103 public void operationComplete(ChannelFuture future)
106 "Connection status of TCP CoAP CLIENT : "
107 + future.isSuccess());
114 public ChannelFuture getChannelFuture() {
115 return channelFuture;
118 public void sendRequest(CoapRequest request) {
119 channelFuture.channel().writeAndFlush(request);
125 public void stopClient() {
128 if (channelFuture != null) {
129 channelFuture.channel().disconnect().sync().addListener(
130 new GenericFutureListener<ChannelFuture>() {
132 public void operationComplete(ChannelFuture future)
135 "DisConnection status of TCP CoAP CLIENT : "
136 + future.isSuccess());
140 } catch (InterruptedException e1) {
141 e1.printStackTrace();