Merge branch '1.1-rel'
[platform/upstream/iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / CoapClient.java
1 /*
2  *******************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.base;
23
24 import java.net.InetSocketAddress;
25 import java.util.ArrayList;
26 import java.util.List;
27
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;
32
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;
44
45 public class CoapClient {
46
47     private static class CoAPClientInitializer
48             extends ChannelInitializer<SocketChannel> {
49
50         private List<ChannelHandler> additionalHandlers = new ArrayList<ChannelHandler>();
51
52         public CoAPClientInitializer() {
53         }
54
55         public void addHandler(ChannelHandler handler) {
56             additionalHandlers.add(handler);
57         }
58
59         @Override
60         public void initChannel(SocketChannel ch) {
61             ChannelPipeline p = ch.pipeline();
62             /*
63              * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
64              */
65             p.addLast(new CoapDecoder());
66             p.addLast(new CoapEncoder());
67             for (ChannelHandler handler : additionalHandlers) {
68                 p.addLast(handler);
69             }
70         }
71     }
72
73     ChannelFuture channelFuture;
74
75     EventLoopGroup connectorGroup = new NioEventLoopGroup();
76
77     CoAPClientInitializer initializer = new CoAPClientInitializer();
78
79     public void addHandler(ChannelHandler handler) {
80         initializer.addHandler(handler);
81     }
82
83     public void startClient(final InetSocketAddress inetSocketAddress)
84             throws InterruptedException {
85                 
86         try {
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);
93
94             b.handler(initializer);
95
96             channelFuture = b.connect(inetSocketAddress).sync();
97
98             channelFuture
99                     .addListener(new GenericFutureListener<ChannelFuture>() {
100                         @Override
101                         public void operationComplete(ChannelFuture future)
102                                 throws Exception {
103                             Logger.d(
104                                     "Connection status of TCP CoAP CLIENT  : "
105                                             + future.isSuccess());
106                         }
107                     });
108         } finally {
109         }
110     }
111
112     public ChannelFuture getChannelFuture() {
113         return channelFuture;
114     }
115
116     public void sendRequest(CoapRequest request) {
117         channelFuture.channel().writeAndFlush(request);
118     }
119
120     public void stopClient() throws Exception {
121         connectorGroup.shutdownGracefully().await();
122     }
123 }