[Simulator] Minor UI changes fixing the IOT-1087.
[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
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;
43
44 public class CoapClient {
45
46     private static class CoAPClientInitializer
47             extends ChannelInitializer<SocketChannel> {
48
49         private List<ChannelHandler> additionalHandlers = new ArrayList<ChannelHandler>();
50
51         public CoAPClientInitializer() {
52         }
53
54         public void addHandler(ChannelHandler handler) {
55             additionalHandlers.add(handler);
56         }
57
58         @Override
59         public void initChannel(SocketChannel ch) {
60             ChannelPipeline p = ch.pipeline();
61             /*
62              * if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); }
63              */
64             p.addLast(new CoapDecoder());
65             p.addLast(new CoapEncoder());
66             for (ChannelHandler handler : additionalHandlers) {
67                 p.addLast(handler);
68             }
69         }
70     }
71
72     private ChannelFuture channelFuture;
73
74     CoAPClientInitializer initializer = new CoAPClientInitializer();
75
76     public void addHandler(ChannelHandler handler) {
77         initializer.addHandler(handler);
78     }
79
80     public void startClient(final InetSocketAddress inetSocketAddress)
81             throws InterruptedException {
82         // Create bootstrap
83
84         EventLoopGroup bossGroup = new NioEventLoopGroup();
85         // bossGroup = new
86         // EpollEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2);
87
88         try {
89             Bootstrap b = new Bootstrap();
90             b.group(bossGroup);
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);
95
96             b.handler(initializer);
97
98             channelFuture = b.connect(inetSocketAddress).sync();
99
100             channelFuture
101                     .addListener(new GenericFutureListener<ChannelFuture>() {
102                         @Override
103                         public void operationComplete(ChannelFuture future)
104                                 throws Exception {
105                             System.out.println(
106                                     "Connection status of TCP CoAP CLIENT  : "
107                                             + future.isSuccess());
108                         }
109                     });
110         } finally {
111         }
112     }
113
114     public ChannelFuture getChannelFuture() {
115         return channelFuture;
116     }
117
118     public void sendRequest(CoapRequest request) {
119         channelFuture.channel().writeAndFlush(request);
120     }
121
122     /**
123      * stop connection
124      */
125     public void stopClient() {
126
127         try {
128             if (channelFuture != null) {
129                 channelFuture.channel().disconnect().sync().addListener(
130                         new GenericFutureListener<ChannelFuture>() {
131
132                             public void operationComplete(ChannelFuture future)
133                                     throws Exception {
134                                 System.out.println(
135                                         "DisConnection status of TCP CoAP CLIENT : "
136                                                 + future.isSuccess());
137                             }
138                         });
139             }
140         } catch (InterruptedException e1) {
141             e1.printStackTrace();
142         }
143     }
144 }