[IOT-1604] Fix cloud routing code
[iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / connector / 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.connector;
23
24 import java.util.HashMap;
25
26 import org.iotivity.cloud.base.OICConstants;
27 import org.iotivity.cloud.base.device.IRequestChannel;
28 import org.iotivity.cloud.base.device.IResponseEventHandler;
29 import org.iotivity.cloud.base.exception.ClientException;
30 import org.iotivity.cloud.base.exception.ClientException.RequesterGoneException;
31 import org.iotivity.cloud.base.protocols.IRequest;
32 import org.iotivity.cloud.base.protocols.IResponse;
33 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
34 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
35 import org.iotivity.cloud.base.protocols.enums.Observe;
36 import org.iotivity.cloud.util.Bytes;
37 import org.iotivity.cloud.util.Log;
38
39 import io.netty.channel.Channel;
40
41 public class CoapClient implements IRequestChannel, IResponseEventHandler {
42
43     private class RequestInfo {
44         private byte[]                originToken     = null;
45         private IRequest              originRequest   = null;
46         private IResponseEventHandler responseHandler = null;
47         private Observe               observe         = Observe.NOTHING;
48
49         public RequestInfo(byte[] originToken, IRequest originRequest,
50                 IResponseEventHandler responseHandler, Observe observe) {
51             this.originToken = originToken;
52             this.originRequest = originRequest;
53             this.responseHandler = responseHandler;
54             this.observe = observe;
55         }
56     }
57
58     private HashMap<Long, RequestInfo> mTokenExchanger = new HashMap<>();
59     private Long                       mToken          = 0L;
60     private Channel                    mChannel        = null;
61
62     private HashMap<Long, Long>        mSubscription   = new HashMap<>();
63
64     public CoapClient(Channel channel) {
65         mChannel = channel;
66     }
67
68     @Override
69     public void sendRequest(IRequest request,
70             IResponseEventHandler responseEvent) {
71         // Exchange request token to internal token and
72         // add token with responseHandler to map
73         try {
74             byte[] token = null;
75             long newToken;
76             synchronized (mToken) {
77                 newToken = mToken;
78             }
79
80             CoapRequest coapRequest = (CoapRequest) request;
81
82             token = coapRequest.getToken();
83
84             Observe observe = request.getObserve();
85
86             switch (request.getObserve()) {
87                 case UNSUBSCRIBE:
88                     newToken = removeObserve(Bytes.bytesToLong(token));
89                     break;
90
91                 case SUBSCRIBE:
92                     addObserve(Bytes.bytesToLong(token), newToken);
93                 default:
94                     // We create temp token
95                     // TODO: temporal handling
96                     if (request.getUriPath()
97                             .equals(OICConstants.RESOURCE_PRESENCE_FULL_URI)) {
98                         addObserve(Bytes.bytesToLong(token), newToken);
99                         observe = Observe.SUBSCRIBE;
100                     }
101                     synchronized (mToken) {
102                         newToken = mToken++;
103                     }
104                     break;
105             }
106
107             coapRequest.setToken(Bytes.longTo8Bytes(newToken));
108             mTokenExchanger.put(newToken,
109                     new RequestInfo(token, request, responseEvent, observe));
110
111             mChannel.writeAndFlush(request);
112
113         } catch (Exception e) {
114             Log.f(mChannel, e);
115         }
116     }
117
118     @Override
119     public void onResponseReceived(IResponse response) throws ClientException {
120         // Response received from this device.
121         // Exchange internal token to request token
122         // And call actual requester device
123
124         // Response is always CoapResponse
125         CoapResponse coapResponse = (CoapResponse) response;
126
127         RequestInfo reqInfo = mTokenExchanger
128                 .get(Bytes.bytesToLong(coapResponse.getToken()));
129
130         if (reqInfo == null) {
131             throw new RequesterGoneException("Unable to find "
132                     + Bytes.bytesToLong(coapResponse.getToken()));
133         }
134
135         ((CoapRequest) reqInfo.originRequest).setToken(reqInfo.originToken);
136
137         // Subscription response should stored
138         if (reqInfo.observe != Observe.SUBSCRIBE) {
139             mTokenExchanger.remove(Bytes.bytesToLong(coapResponse.getToken()));
140             if (mSubscription
141                     .containsKey(Bytes.bytesToLong(reqInfo.originToken))) {
142                 mSubscription.remove(Bytes.bytesToLong(reqInfo.originToken));
143             }
144         }
145
146         if (reqInfo.responseHandler != null) {
147             coapResponse.setToken(reqInfo.originToken);
148             reqInfo.responseHandler.onResponseReceived(coapResponse);
149         }
150     }
151
152     public void addObserve(long token, long newtoken) {
153
154         mSubscription.put(token, newtoken);
155     }
156
157     public Long removeObserve(long token) {
158
159         Long getToken = mSubscription.remove(token);
160         return getToken;
161     }
162
163     public Long isObserveRequest(Long token) {
164         Long getToken = null;
165         getToken = mSubscription.get(token);
166
167         return getToken;
168     }
169 }