Implement reconnection and ping-pong between cloud services. Make
[iotivity.git] / cloud / interface / src / main / java / org / iotivity / cloud / ciserver / resources / proxy / rd / DevicePresence.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.ciserver.resources.proxy.rd;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28
29 import org.iotivity.cloud.base.connector.ConnectorPool;
30 import org.iotivity.cloud.base.device.Device;
31 import org.iotivity.cloud.base.device.IRequestChannel;
32 import org.iotivity.cloud.base.device.IResponseEventHandler;
33 import org.iotivity.cloud.base.exception.ClientException;
34 import org.iotivity.cloud.base.exception.ServerException;
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.enums.RequestMethod;
39 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
40 import org.iotivity.cloud.base.resource.Resource;
41 import org.iotivity.cloud.ciserver.Constants;
42 import org.iotivity.cloud.util.Cbor;
43
44 /**
45  *
46  * This class provides a set of APIs to send requests about device presence to
47  * resource directory
48  *
49  */
50
51 public class DevicePresence extends Resource {
52     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
53
54     public DevicePresence() {
55         super(Arrays.asList(Constants.PREFIX_OIC,
56                 Constants.DEVICE_PRESENCE_URI));
57     }
58
59     class AccountReceiveHandler implements IResponseEventHandler {
60
61         private Device   mSrcDevice;
62         private IRequest mRequest;
63
64         public AccountReceiveHandler(IRequest request, Device srcDevice) {
65             mSrcDevice = srcDevice;
66             mRequest = request;
67         }
68
69         @Override
70         public void onResponseReceived(IResponse response)
71                 throws ClientException {
72             switch (response.getStatus()) {
73                 case CONTENT:
74                     HashMap<String, Object> payloadData = mCbor
75                             .parsePayloadFromCbor(response.getPayload(),
76                                     HashMap.class);
77
78                     ArrayList<String> devices = (ArrayList<String>) getResponseDeviceList(
79                             payloadData);
80
81                     if (mRequest.getUriQuery() != null
82                             && mRequest.getUriQueryMap()
83                                     .containsKey(Constants.REQ_DEVICE_ID)) {
84                         if (!devices.containsAll(mRequest.getUriQueryMap()
85                                 .get(Constants.REQ_DEVICE_ID))) {
86                             mSrcDevice.sendResponse(
87                                     MessageBuilder.createResponse(mRequest,
88                                             ResponseStatus.BAD_REQUEST));
89                         }
90                     } else {
91                         String additionalQuery = makeAdditionalQuery(
92                                 payloadData, mSrcDevice.getDeviceId());
93                         if (additionalQuery != null) {
94                             String uriQuery = additionalQuery.toString()
95                                     + (mRequest.getUriQuery() != null
96                                             ? (";" + mRequest.getUriQuery())
97                                             : "");
98                             mRequest = MessageBuilder.modifyRequest(mRequest,
99                                     null, uriQuery, null, null);
100                         }
101                     }
102
103                     ConnectorPool.getConnection("rd").sendRequest(mRequest, mSrcDevice);
104                     break;
105
106                 default:
107                     mSrcDevice.sendResponse(response);
108             }
109         }
110
111         private String makeAdditionalQuery(HashMap<String, Object> payloadData,
112                 String did) {
113
114             StringBuilder additionalQuery = new StringBuilder();
115
116             List<String> deviceList = getResponseDeviceList(payloadData);
117
118             if (deviceList.isEmpty()) {
119                 return null;
120             }
121
122             int index = deviceList.size();
123             for (String device : deviceList) {
124                 additionalQuery.append(Constants.REQ_DEVICE_ID + "=" + device);
125                 if (--index > 0) {
126                     additionalQuery.append(";");
127                 }
128             }
129             return additionalQuery.toString();
130         }
131
132         @SuppressWarnings("unchecked")
133         private List<String> getResponseDeviceList(
134                 HashMap<String, Object> payloadData) {
135             List<String> deviceList = (List<String>) payloadData
136                     .get(Constants.REQ_DEVICE_LIST);
137
138             return deviceList;
139         }
140     }
141
142     @Override
143     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
144             throws ServerException {
145         StringBuffer additionalQuery = new StringBuffer();
146         additionalQuery.append(Constants.USER_ID + "=" + srcDevice.getUserId());
147         additionalQuery.append(";");
148         additionalQuery.append(
149                 Constants.REQ_MEMBER_LIST + "=" + srcDevice.getUserId());
150         String uriQuery = additionalQuery.toString()
151                 + (request.getUriQuery() != null ? (";" + request.getUriQuery())
152                         : "");
153
154         StringBuffer uriPath = new StringBuffer();
155         uriPath.append(Constants.PREFIX_OIC + "/");
156         uriPath.append(Constants.ACL_URI + "/");
157         uriPath.append(Constants.GROUP_URI + "/");
158         uriPath.append(srcDevice.getUserId());
159
160         IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.GET,
161                 uriPath.toString(), uriQuery);
162
163         ConnectorPool.getConnection("account").sendRequest(requestToAS,
164                 new AccountReceiveHandler(request, srcDevice));
165     }
166 }