Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-hosting / unittest / ResourceEncapsulationTestSimulator.h
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 <memory>
22 #include <mutex>
23 #include <atomic>
24 #include <condition_variable>
25
26 #include "UnitTestHelper.h"
27
28 #include "OCPlatform.h"
29 #include "RCSDiscoveryManager.h"
30 #include "RCSRemoteResourceObject.h"
31 #include "RCSResourceObject.h"
32 #include "RCSResourceAttributes.h"
33 #include "RCSAddress.h"
34
35 #include "RequestObject.h"
36
37 using namespace testing;
38 using namespace OIC::Service;
39
40 class ResourceEncapsulationTestSimulator
41 {
42 public:
43     RCSResourceObject::Ptr server;
44     RCSRemoteResourceObject::Ptr remoteResource;
45
46 private:
47     std::mutex mutexForDiscovery;
48     std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
49
50     std::string MULTICASTURI;
51     std::string HOSTED_RESOURCEURI;
52     std::string RESOURCEURI;
53     std::string RESOURCETYPE;
54     std::string RESOURCEINTERFACE;
55     std::string ATTR_KEY;
56     int ATTR_VALUE;
57
58 public:
59     ResourceEncapsulationTestSimulator()
60     :server(nullptr), remoteResource(nullptr),
61      mutexForDiscovery(),
62      MULTICASTURI("/oic/res"),
63      HOSTED_RESOURCEURI("/a/TempHumSensor"),
64      RESOURCEURI("/a/TempHumSensor/hosting"),
65      RESOURCETYPE("oic.r.resourcehosting"),
66      RESOURCEINTERFACE("oic.if.baseline"),
67      ATTR_KEY("Temperature"),
68      ATTR_VALUE(0)
69     { }
70     ~ResourceEncapsulationTestSimulator()
71     { }
72
73 private:
74     void onDiscoveryResource(RCSRemoteResourceObject::Ptr resourceObject)
75     {
76         if (remoteResource != nullptr)
77         {
78             return;
79         }
80
81         if (RESOURCEURI.compare(resourceObject->getUri()) != 0)
82         {
83             return;
84         }
85
86         remoteResource = resourceObject;
87         discoveryTask->cancel();
88         mutexForDiscovery.unlock();
89     }
90
91     void waitForDiscovery()
92     {
93         std::chrono::milliseconds interval(100);
94         while(true)
95         {
96             if(mutexForDiscovery.try_lock())
97             {
98                 mutexForDiscovery.unlock();
99                 return;
100             }
101             std::this_thread::sleep_for(interval);
102         }
103     }
104     void WaitForPtrBeingUnique()
105     {
106         while((remoteResource && !remoteResource.unique()) || (server && !server.unique()))
107         {
108             std::this_thread::sleep_for(std::chrono::milliseconds{ 1000 });
109         }
110     }
111
112 public:
113     void destroy()
114     {
115         if(server.use_count()) server.reset();
116         if(remoteResource.use_count()) remoteResource.reset();
117         WaitForPtrBeingUnique();
118     }
119     void defaultRunSimulator()
120     {
121         createResource();
122         discoveryResource();
123         waitForDiscovery();
124     }
125
126     void createResource(std::string postUri = "")
127     {
128         HOSTED_RESOURCEURI = HOSTED_RESOURCEURI + postUri;
129         server = RCSResourceObject::Builder(HOSTED_RESOURCEURI +  "/hosting",
130                 RESOURCETYPE, RESOURCEINTERFACE).
131                 setDiscoverable(true).setObservable(true).build();
132         server->setAttribute(ATTR_KEY, RCSResourceAttributes::Value(ATTR_VALUE));
133     }
134
135     void discoveryResource()
136     {
137         discoveryResource(RESOURCETYPE);
138     }
139
140     void discoveryResource(std::string & resourceType)
141     {
142         try
143         {
144             discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
145                     RCSAddress::multicast(), MULTICASTURI, resourceType,
146                     std::bind(
147                             &ResourceEncapsulationTestSimulator::onDiscoveryResource,
148                             this, std::placeholders::_1));
149             mutexForDiscovery.lock();
150         }
151         catch(std::exception & e)
152         {
153             std::cout << "exception : " << e.what() << std::endl;
154         }
155     }
156
157     std::string getServerUri() const
158     {
159         return RESOURCEURI;
160     }
161
162     std::string getHostedServerUri() const
163     {
164         return HOSTED_RESOURCEURI;
165     }
166
167     RCSResourceObject::Ptr getResourceServer() const
168     {
169         return server;
170     }
171  
172     RCSRemoteResourceObject::Ptr getRemoteResource() const
173     {
174         return remoteResource;
175     }
176 };