Revert "Change build option - IP only"
[platform/upstream/iotivity.git] / service / simulator / src / client-controller / simulator_client.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 "simulator_client.h"
22 #include "simulator_remote_resource_impl.h"
23 #include "simulator_logger.h"
24 #include "simulator_utils.h"
25 #include "logger.h"
26
27 #define TAG "SIMULATOR_CLIENT"
28
29 SimulatorClient *SimulatorClient::getInstance()
30 {
31     static SimulatorClient s_instance;
32     return &s_instance;
33 }
34
35 void SimulatorClient::findResources(ResourceFindCallback callback)
36 {
37     if (!callback)
38         throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
39
40     typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
41                                           OCConnectivityType, OC::FindCallback);
42
43     invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "",
44                      OC_MULTICAST_DISCOVERY_URI,
45                      CT_DEFAULT,
46                      static_cast<OC::FindCallback>(std::bind(&SimulatorClient::onResourceFound, this,
47                              std::placeholders::_1, callback)));
48 }
49
50 void SimulatorClient::findResources(const std::string &resourceType,
51                                     ResourceFindCallback callback)
52 {
53     if (resourceType.empty())
54         throw InvalidArgsException(SIMULATOR_INVALID_TYPE, "resource type is empty!");
55
56     if (!callback)
57         throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
58
59     std::ostringstream query;
60     query << OC_MULTICAST_DISCOVERY_URI << "?rt=" << resourceType;
61
62     typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
63                                           OCConnectivityType, OC::FindCallback);
64
65     invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "", query.str(),
66                      CT_DEFAULT,
67                      static_cast<OC::FindCallback>(std::bind(&SimulatorClient::onResourceFound,
68                              this, std::placeholders::_1, callback)));
69 }
70
71 void SimulatorClient::onResourceFound(std::shared_ptr<OC::OCResource> ocResource,
72                                       ResourceFindCallback callback)
73 {
74     if (!ocResource)
75     {
76         OC_LOG(ERROR, TAG, "Invalid OCResource !");
77         return;
78     }
79
80     // Construct SimulatorRemoteResource
81     SimulatorRemoteResourceSP simulatorResource =
82         std::make_shared<SimulatorRemoteResourceImpl>(ocResource);
83     if (!simulatorResource)
84     {
85         OC_LOG(ERROR, TAG, "Failed to create simulator remote resource !");
86         return;
87     }
88
89     OC_LOG(DEBUG, TAG, "Invoking resource found client callback !");
90     callback(simulatorResource);
91 }
92
93