1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "simulator_client.h"
22 #include "simulator_remote_resource_impl.h"
23 #include "simulator_logger.h"
24 #include "simulator_utils.h"
27 #define TAG "SIMULATOR_CLIENT"
29 SimulatorClient *SimulatorClient::getInstance()
31 static SimulatorClient s_instance;
35 void SimulatorClient::findResources(ResourceFindCallback callback)
38 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
40 typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
41 OCConnectivityType, OC::FindCallback);
43 invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "",
44 OC_MULTICAST_DISCOVERY_URI,
46 static_cast<OC::FindCallback>(std::bind(&SimulatorClient::onResourceFound, this,
47 std::placeholders::_1, callback)));
50 void SimulatorClient::findResources(const std::string &resourceType,
51 ResourceFindCallback callback)
53 if (resourceType.empty())
54 throw InvalidArgsException(SIMULATOR_INVALID_TYPE, "resource type is empty!");
57 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
59 std::ostringstream query;
60 query << OC_MULTICAST_DISCOVERY_URI << "?rt=" << resourceType;
62 typedef OCStackResult (*FindResource)(const std::string &, const std::string &,
63 OCConnectivityType, OC::FindCallback);
65 invokeocplatform(static_cast<FindResource>(OC::OCPlatform::findResource), "", query.str(),
67 static_cast<OC::FindCallback>(std::bind(&SimulatorClient::onResourceFound,
68 this, std::placeholders::_1, callback)));
71 void SimulatorClient::onResourceFound(std::shared_ptr<OC::OCResource> ocResource,
72 ResourceFindCallback callback)
76 OC_LOG(ERROR, TAG, "Invalid OCResource !");
80 // Construct SimulatorRemoteResource
81 SimulatorRemoteResourceSP simulatorResource =
82 std::make_shared<SimulatorRemoteResourceImpl>(ocResource);
83 if (!simulatorResource)
85 OC_LOG(ERROR, TAG, "Failed to create simulator remote resource !");
89 OC_LOG(DEBUG, TAG, "Invoking resource found client callback !");
90 callback(simulatorResource);