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;
31 import org.iotivity.cloud.util.Logger;
33 import io.netty.bootstrap.Bootstrap;
34 import io.netty.channel.ChannelFuture;
35 import io.netty.channel.ChannelHandler;
36 import io.netty.channel.ChannelInitializer;
37 import io.netty.channel.ChannelOption;
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.NioSocketChannel;
43 import io.netty.util.concurrent.GenericFutureListener;
45 public class CoapClient {
47 private static class CoAPClientInitializer
48 extends ChannelInitializer<SocketChannel> {
50 private List<ChannelHandler> additionalHandlers = new ArrayList<ChannelHandler>();
52 public CoAPClientInitializer() {
55 public void addHandler(ChannelHandler handler) {
56 additionalHandlers.add(handler);
60 public void initChannel(SocketChannel ch) {
61 ChannelPipeline p = ch.pipeline();
63 * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
65 p.addLast(new CoapDecoder());
66 p.addLast(new CoapEncoder());
67 for (ChannelHandler handler : additionalHandlers) {
73 ChannelFuture channelFuture;
75 EventLoopGroup connectorGroup = new NioEventLoopGroup();
77 CoAPClientInitializer initializer = new CoAPClientInitializer();
79 public void addHandler(ChannelHandler handler) {
80 initializer.addHandler(handler);
83 public void startClient(final InetSocketAddress inetSocketAddress)
84 throws InterruptedException {
87 Bootstrap b = new Bootstrap();
88 b.group(connectorGroup);
89 b.channel(NioSocketChannel.class);
90 b.option(ChannelOption.TCP_NODELAY, true);
91 b.option(ChannelOption.SO_KEEPALIVE, true);
92 b.option(ChannelOption.SO_REUSEADDR, true);
94 b.handler(initializer);
96 channelFuture = b.connect(inetSocketAddress).sync();
99 .addListener(new GenericFutureListener<ChannelFuture>() {
101 public void operationComplete(ChannelFuture future)
104 "Connection status of TCP CoAP CLIENT : "
105 + future.isSuccess());
112 public ChannelFuture getChannelFuture() {
113 return channelFuture;
116 public void sendRequest(CoapRequest request) {
117 channelFuture.channel().writeAndFlush(request);
120 public void stopClient() throws Exception {
121 connectorGroup.shutdownGracefully().await();