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