Refactor logging to use slf4j
[iotivity.git] / cloud / stack / src / main / java / org / iotivity / cloud / base / device / CoapDevice.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.device;
23
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.Iterator;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.iotivity.cloud.base.connector.CoapClient;
33 import org.iotivity.cloud.base.connector.ConnectorPool;
34 import org.iotivity.cloud.base.exception.ClientException;
35 import org.iotivity.cloud.base.protocols.IRequest;
36 import org.iotivity.cloud.base.protocols.IResponse;
37 import org.iotivity.cloud.base.protocols.MessageBuilder;
38 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
39 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
40 import org.iotivity.cloud.base.protocols.enums.Observe;
41 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
42 import org.iotivity.cloud.util.Bytes;
43
44 import io.netty.channel.ChannelHandlerContext;
45
46 public class CoapDevice extends Device {
47     private final static Logger        Log                 = LoggerFactory.getLogger(CoapDevice.class);
48     private CoapClient                 mCoapClient         = null;
49     private String                     mUserId             = null;
50     private String                     mDeviceId           = null;
51     private String                     mAccessToken        = null;
52     private Date                       mIssuedTime         = null;
53     private int                        mExpiredPolicy      = 0;
54     private ArrayList<IRequestChannel> mObserveChannelList = new ArrayList<>();
55     private ConcurrentMap<Long, IRequest>    mObserveRequestList = new ConcurrentHashMap<>();
56
57     private static final int           INFINITE_TIME       = -1;
58
59     public CoapDevice(ChannelHandlerContext ctx) {
60         super(ctx);
61     }
62
63     public void updateDevice(String did, String uid, String accesstoken) {
64         mDeviceId = did;
65         mUserId = uid;
66         mAccessToken = accesstoken;
67     }
68
69     @Override
70     public String getDeviceId() {
71         return mDeviceId;
72     }
73
74     @Override
75     public String getUserId() {
76         return mUserId;
77     }
78
79     @Override
80     public String getAccessToken() {
81         return mAccessToken;
82     }
83
84     public Date getIssuedTime() {
85         return mIssuedTime;
86     }
87
88     public int getExpiredPolicy() {
89         return mExpiredPolicy;
90     }
91
92     public void setExpiredPolicy(int expiredPolicy) {
93         mIssuedTime = new Date();
94         this.mExpiredPolicy = expiredPolicy;
95     }
96
97     public void setUerId(String userId) {
98         this.mUserId = userId;
99     }
100
101     public void setAccessToken(String accessToken) {
102         this.mAccessToken = accessToken;
103     }
104
105     public void addObserveChannel(IRequestChannel channel) {
106         mObserveChannelList.add(channel);
107     }
108
109     public void removeObserveChannel(IRequestChannel channel)
110             throws ClientException {
111         if (mObserveChannelList.contains(channel)) {
112
113             Iterator<Long> iterator = mObserveRequestList.keySet().iterator();
114             while (iterator.hasNext()) {
115                 Long token = iterator.next();
116                 CoapClient coapClient = (CoapClient) channel;
117                 if (coapClient.isObserveRequest(token) != null) {
118                     coapClient.removeObserve(token);
119                     CoapRequest coapRequest = (CoapRequest) mObserveRequestList
120                             .get(token);
121                     coapRequest.setObserve(Observe.UNSUBSCRIBE);
122                     IResponse response = MessageBuilder.createResponse(
123                             coapRequest, ResponseStatus.CONTENT, null, null);
124                     sendResponse(response);
125                 }
126             }
127             mObserveChannelList.remove(channel);
128         }
129     }
130
131     public void addObserveRequest(Long token, IRequest request) {
132
133         mObserveRequestList.put(token, request);
134     }
135
136     public void removeObserveRequest(Long token) {
137
138         mObserveRequestList.remove(token);
139     }
140
141     // This is called by cloud resource model
142     @Override
143     public void sendResponse(IResponse response) {
144         // This message must converted to CoapResponse
145         CoapResponse coapResponse = (CoapResponse) response;
146
147         Iterator<Long> iterator = mObserveRequestList.keySet().iterator();
148         while (iterator.hasNext()) {
149             Long token = iterator.next();
150             Long respToken = Bytes.bytesToLong(coapResponse.getToken());
151             if (respToken.equals(token)
152                     && coapResponse.getObserve() == Observe.NOTHING) {
153                 iterator.remove();
154             }
155         }
156         ctx.channel().writeAndFlush(response);
157     }
158
159     public IRequestChannel getRequestChannel() {
160         if (mCoapClient == null) {
161             mCoapClient = new CoapClient(ctx.channel());
162         }
163
164         return mCoapClient;
165     }
166
167     public boolean isExpiredTime() {
168
169         if (mExpiredPolicy == INFINITE_TIME) {
170             return false;
171         }
172
173         Date currentTime = new Date();
174         long difference = currentTime.getTime() - mIssuedTime.getTime();
175         long remainTime = mExpiredPolicy - difference / 1000;
176
177         if (remainTime < 0) {
178
179             Log.warn("accessToken is expired..");
180             return true;
181         }
182
183         return false;
184     }
185
186     @Override
187     public void onConnected() {
188         mObserveChannelList.addAll(ConnectorPool.getConnectionList());
189     }
190
191     @Override
192     public void onDisconnected() {
193         Iterator<Long> iterator = mObserveRequestList.keySet().iterator();
194         while (iterator.hasNext()) {
195             Long token = iterator.next();
196             for (IRequestChannel serverChannel : mObserveChannelList) {
197                 CoapClient coapClient = (CoapClient) serverChannel;
198                 if (coapClient != null
199                         && coapClient.isObserveRequest(token) != null) {
200                     CoapRequest coapRequest = (CoapRequest) mObserveRequestList
201                             .get(token);
202                     coapRequest.setObserve(Observe.UNSUBSCRIBE);
203                     coapRequest.setToken(Bytes.longTo8Bytes(token));
204                     serverChannel.sendRequest(MessageBuilder.modifyRequest(
205                             coapRequest, null, null, null, null), this);
206                 }
207             }
208         }
209     }
210 }