1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include "UnitTestHelper.h"
27 #include "RCSDiscoveryManager.h"
28 #include "RCSRemoteResourceObject.h"
29 #include "RCSResourceAttributes.h"
30 #include "RCSAddress.h"
32 using namespace testing;
33 using namespace OIC::Service;
35 class ResourceContainerTestSimulator
36 : public std::enable_shared_from_this<ResourceContainerTestSimulator>
39 typedef std::shared_ptr<ResourceContainerTestSimulator> Ptr;
41 RCSResourceObject::Ptr server;
42 RCSRemoteResourceObject::Ptr remoteResource;
45 std::mutex mutexForDiscovery;
47 std::string MULTICASTURI;
48 std::string RESOURCEURI;
49 std::string RESOURCETYPE;
50 std::string RESOURCEINTERFACE;
53 std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
56 ResourceContainerTestSimulator()
57 : server(nullptr), remoteResource(nullptr),
59 MULTICASTURI("/oic/res"),
60 RESOURCEURI("/a/TempHumSensor/Container"),
61 RESOURCETYPE("resource.container"),
62 RESOURCEINTERFACE("oic.if.baseline"),
63 ATTR_KEY("Temperature"),
67 ~ResourceContainerTestSimulator()
71 if (remoteResource != nullptr && remoteResource->isCaching())
73 remoteResource->stopCaching();
75 if (remoteResource != nullptr && remoteResource->isMonitoring())
77 remoteResource->stopMonitoring();
80 catch (RCSException &e)
82 std::cout << "exception : " << e.what() << std::endl;
87 void onDiscoveryResource_Impl(RCSRemoteResourceObject::Ptr resourceObject)
89 if (remoteResource != nullptr)
94 if (RESOURCEURI.compare(resourceObject->getUri()) != 0)
99 remoteResource = resourceObject;
100 mutexForDiscovery.unlock();
103 static void onDiscoveryResource(RCSRemoteResourceObject::Ptr resourceObject,
104 std::weak_ptr<ResourceContainerTestSimulator> rPtr)
106 std::shared_ptr<ResourceContainerTestSimulator> ptr = rPtr.lock();
109 ptr->onDiscoveryResource_Impl(resourceObject);
113 std::cout << "Aleady delete simulator\n";
116 void waitForDiscovery()
118 std::chrono::milliseconds interval(100);
121 if (mutexForDiscovery.try_lock())
123 mutexForDiscovery.unlock();
126 std::this_thread::sleep_for(interval);
131 void defaultRunSimulator()
138 void createResource()
140 server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE)
141 .setDiscoverable(true).setObservable(true).build();
142 server->setAttribute(ATTR_KEY, ATTR_VALUE);
145 void discoveryResource()
147 discoveryResource(RESOURCETYPE);
150 void discoveryResource(std::string &resourceType)
154 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
155 RCSAddress::multicast(), MULTICASTURI, resourceType,
156 std::bind(onDiscoveryResource, std::placeholders::_1,
157 std::weak_ptr<ResourceContainerTestSimulator>(shared_from_this())));
158 mutexForDiscovery.lock();
160 catch (RCSInvalidParameterException &e)
162 std::cout << "exception : " << e.what() << std::endl;
166 std::string getServerUri() const
171 RCSResourceObject::Ptr getResourceServer() const
176 RCSRemoteResourceObject::Ptr getRemoteResource() const
178 return remoteResource;
181 void ChangeAttributeValue()
183 std::chrono::milliseconds interval(100);
184 if (server != nullptr)
185 server->setAttribute(ATTR_KEY, ATTR_VALUE + 10);
186 std::this_thread::sleep_for(interval);
189 void ChangeResourceState()
191 std::chrono::milliseconds interval(400);
192 if (server != nullptr)
194 std::this_thread::sleep_for(interval);