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