Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceClient / RCSDiscoveryManager.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 "RCSDiscoveryManager.h"
22
23 #include "RCSRemoteResourceObject.h"
24 #include "PrimitiveResource.h"
25
26 #include "ScopeLogger.h"
27
28 #define TAG PCF("RCSDiscoveryManager")
29
30 using namespace OIC::Service;
31
32 namespace
33 {
34     void findCallback(std::shared_ptr< PrimitiveResource > primitiveResource,
35             RCSDiscoveryManager::ResourceDiscoveredCallback cb)
36     {
37         SCOPE_LOG_F(DEBUG, TAG);
38
39         if (!primitiveResource)
40         {
41             OC_LOG(ERROR, TAG, "findCallback : primitiveResource is null.");
42             return;
43         }
44
45         cb(std::make_shared< RCSRemoteResourceObject >(primitiveResource));
46     }
47 }
48
49 namespace OIC
50 {
51     namespace Service
52     {
53         RCSDiscoveryManager* RCSDiscoveryManager::getInstance()
54         {
55             static RCSDiscoveryManager instance;
56             return &instance;
57         }
58
59         void RCSDiscoveryManager::discoverResource(const RCSAddress& address,
60                 const std::string& resourceURI, ResourceDiscoveredCallback cb)
61         {
62             SCOPE_LOG_F(DEBUG, TAG);
63
64             if (!cb)
65             {
66                 OC_LOG(ERROR, TAG, "discoverResource NULL Callback");
67                 throw InvalidParameterException{ "discoverResource NULL Callback'" };
68             }
69
70             OIC::Service::discoverResource(address, resourceURI,
71                     std::bind(findCallback, std::placeholders::_1, std::move(cb)));
72         }
73     }
74 }