Apply to Resource Hosting to RE resource server added feature.
[platform/upstream/iotivity.git] / service / resource-hosting / src / RequestObject.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "RequestObject.h"
22
23 #include "RCSResourceObject.h"
24 #include "RCSSeparateResponse.h"
25
26 namespace OIC
27 {
28     namespace Service
29     {
30         void RequestObject::invokeRequest(RCSRemoteResourceObject::Ptr remoteObject,
31                 const RCSRequest & request, RequestMethod method,
32                 const RCSResourceAttributes & resourceAttibutes, SetRequestCallback setCB)
33         {
34             RequestObject::Ptr createdRequestObject = std::make_shared<RequestObject>();
35
36             RCSRequest req(request.getResourceObject().lock(), request.getOCRequest());
37             switch (method)
38             {
39             case RequestMethod::Set:
40             {
41                 if (!setCB)
42                 {
43                     remoteObject->setRemoteAttributes(resourceAttibutes,
44                             std::bind(&RequestObject::setRequestCB, createdRequestObject,
45                                     std::placeholders::_1, std::placeholders::_2,
46                                     req, createdRequestObject));
47                 }
48                 else
49                 {
50                     remoteObject->setRemoteAttributes(resourceAttibutes,
51                             std::bind(std::move(setCB),
52                                     std::placeholders::_1, std::placeholders::_2,
53                                     req, createdRequestObject));
54                 }
55             }
56                 break;
57             case RequestMethod::Get:
58             case RequestMethod::Delete:
59             default:
60                 // unknown type of method.
61                 break;
62             }
63         }
64
65         void RequestObject::setRequestCB(
66                 const RCSResourceAttributes & returnedAttributes, int /*eCode*/,
67                 const RCSRequest & request, RequestObject::Ptr /*this_ptr*/)
68         {
69             auto server = request.getResourceObject().lock();
70             if (!server) return;
71
72             RCSResourceObject::LockGuard guard(server);
73             server->getAttributes() = RCSResourceAttributes(returnedAttributes);
74
75             // TODO need to set error code.
76             RCSSeparateResponse(request).set();
77         }
78
79     } /* namespace Service */
80 } /* namespace OIC */