remove prefix .well-known uri & change from 'ocf' to 'oic' uri
[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 IResponseEventHandler responseHandler = null;
46         private Observe               observe         = Observe.NOTHING;
47
48         public RequestInfo(byte[] originToken,
49                 IResponseEventHandler responseHandler, Observe observe) {
50             this.originToken = originToken;
51             this.responseHandler = responseHandler;
52             this.observe = observe;
53         }
54     }
55
56     private HashMap<Long, RequestInfo> mTokenExchanger = new HashMap<>();
57     private Long                       mToken          = 0L;
58     private Channel                    mChannel        = null;
59
60     private HashMap<Long, Long>        mSubscription   = new HashMap<>();
61
62     public CoapClient(Channel channel) {
63         mChannel = channel;
64     }
65
66     @Override
67     public void sendRequest(IRequest request,
68             IResponseEventHandler responseEvent) {
69         // Exchange request token to internal token and
70         // add token with responseHandler to map
71         try {
72             byte[] token = null;
73             long newToken;
74             synchronized (mToken) {
75                 newToken = mToken;
76             }
77
78             CoapRequest coapRequest = (CoapRequest) request;
79
80             token = coapRequest.getToken();
81
82             Observe observe = request.getObserve();
83
84             switch (request.getObserve()) {
85                 case UNSUBSCRIBE:
86                     newToken = mSubscription.remove(Bytes.bytesToLong(token));
87                     break;
88
89                 case SUBSCRIBE:
90                     mSubscription.put(Bytes.bytesToLong(token), newToken);
91                 default:
92                     // We create temp token
93                     // TODO: temporal handling
94                     if (request.getUriPath()
95                             .equals(OICConstants.RESOURCE_PRESENCE_FULL_URI)) {
96                         mSubscription.put(Bytes.bytesToLong(token), newToken);
97                         observe = Observe.SUBSCRIBE;
98                     }
99                     synchronized (mToken) {
100                         newToken = mToken++;
101                     }
102                     break;
103             }
104
105             coapRequest.setToken(Bytes.longTo8Bytes(newToken));
106             mTokenExchanger.put(newToken,
107                     new RequestInfo(token, responseEvent, observe));
108
109             mChannel.writeAndFlush(request);
110
111         } catch (Exception e) {
112             Log.f(mChannel, e);
113         }
114     }
115
116     @Override
117     public void onResponseReceived(IResponse response) throws ClientException {
118         // Response received from this device.
119         // Exchange internal token to request token
120         // And call actual requester device
121
122         // Response is always CoapResponse
123         CoapResponse coapResponse = (CoapResponse) response;
124
125         RequestInfo reqInfo = mTokenExchanger
126                 .get(Bytes.bytesToLong(coapResponse.getToken()));
127
128         if (reqInfo == null) {
129             throw new RequesterGoneException("Unable to find "
130                     + Bytes.bytesToLong(coapResponse.getToken()));
131         }
132
133         // Subscription response should stored
134         if (reqInfo.observe != Observe.SUBSCRIBE) {
135             mTokenExchanger.remove(Bytes.bytesToLong(coapResponse.getToken()));
136         }
137
138         coapResponse.setToken(reqInfo.originToken);
139         reqInfo.responseHandler.onResponseReceived(response);
140     }
141 }