[Resource Encapsulation] Updated Discover API of Resource Client for RCSAddress
[platform/upstream/iotivity.git] / service / resource-encapsulation / examples / linux / SampleResourceClient.cpp
1 #include<iostream>
2
3 #include "ResourceClient.h"
4 #include "RCSResourceAttributes.h"
5 #include "OCPlatform.h"
6 #include "RCSAddress.h"
7
8 using namespace std;
9 using namespace OC;
10 using namespace OIC::Service;
11
12 std::shared_ptr<RemoteResourceObject>  resource;
13 RCSResourceAttributes resourceAttributes;
14 bool startCachingFlag;
15 bool isReady;
16
17 //callback function for discoverResource()
18 void OnResourceDiscovered(std::shared_ptr<RemoteResourceObject> foundResource)
19 {
20
21     cout << "\nOnResourceDiscovered callback" << std::endl;
22
23     std::string resourceURI = foundResource->getUri();
24     std::string hostAddress = foundResource->getAddress();
25
26     cout << "\tResource URI : " << resourceURI << std::endl;
27     cout << "\tResource Host : " << hostAddress << std::endl;
28
29     resource = foundResource;
30     isReady = true;
31 }
32
33 //callback for StartMonitoring()
34 void OnResourceStateChanged(ResourceState resourceState)
35 {
36
37     cout << "\nOnResourceStateChanged callback" << std::endl;
38
39     if (resourceState == ResourceState::NOT_MONITORING)
40         cout << "State changed to : NOT_MONITORING" << std::endl;
41     else if (resourceState == ResourceState::ALIVE)
42         cout << "State changed to : ALIVE" << std::endl;
43     else if (resourceState == ResourceState::REQUESTED)
44         cout << "State changed to : REQUESTED" << std::endl;
45     else if (resourceState == ResourceState::LOST_SIGNAL)
46         cout << "State changed to : LOST_SIGNAL" << std::endl;
47     else if (resourceState == ResourceState::DESTROYED)
48         cout << "State changed to : DESTROYED" << std::endl;
49 }
50
51 //callback for startCaching() [uptodate]
52 void OnCacheUpdated(const RCSResourceAttributes atttribute )
53 {
54     cout << "\nOnCacheUpdated callback" << std::endl;
55     if (atttribute.empty())
56     {
57         std::cout << "Attribute is Empty" << std::endl;
58     }
59     else
60     {
61         RCSResourceAttributes::const_iterator iter = atttribute.begin();
62         for (unsigned int i = 0; i < atttribute.size(); ++i)
63         {
64             std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
65             ++iter;
66         }
67     }
68
69 }
70 //callback for getRemoteAttributes()
71 void OnRemoteAttributesReceivedCallback(const RCSResourceAttributes &atttribute)
72 {
73
74     std::cout << "\nOnRemoteAttributesReceivedCallback callback" << std::endl;
75     if (atttribute.empty())
76     {
77         std::cout << "Got empty attribute " << std::endl;
78     }
79     else
80     {
81         resourceAttributes = atttribute;
82         RCSResourceAttributes::const_iterator iter = atttribute.begin();
83         for (unsigned int i = 0; i < atttribute.size(); ++i)
84         {
85             std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
86             ++iter;
87         }
88     }
89 }
90
91 //callback for setRemoteAttributes()
92 void OnRemoteAttributesSetCallback(const RCSResourceAttributes &atttribute)
93 {
94
95     std::cout << "\nOnRemoteAttributesSetCallback callback" << std::endl;
96     if (atttribute.empty())
97     {
98         std::cout << "Got empty attribute " << std::endl;
99     }
100     else
101     {
102         resourceAttributes = atttribute;
103         RCSResourceAttributes::const_iterator iter = atttribute.begin();
104         for (unsigned int i = 0; i < atttribute.size(); ++i)
105         {
106             std::cout << "key : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
107             ++iter;
108         }
109     }
110 }
111
112 int main()
113 {
114
115     DiscoveryManager *discoveryManagerInstance =  DiscoveryManager::getInstance();
116     bool cachingFlag = false;
117
118     //configuring the platform
119     PlatformConfig config
120     {
121         OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
122     };
123     OCPlatform::Configure(config);
124
125     std::cout << "\nPlatform configured successfully" << std::endl;
126     std::string uri = "";
127     std::string address = "";
128     std::string rt = "core.TemperatureSensor";
129
130     try
131     {
132
133         uri = OC_RSRVD_WELL_KNOWN_URI + uri + "?rt=" + rt;
134
135         //getting the object of RCSAddress for multicast discovery
136         RCSAddress rcsAddress = RCSAddress::multicast();
137
138         //discover the resource in the network
139         discoveryManagerInstance->discoverResource(rcsAddress, uri , &OnResourceDiscovered);
140     }
141     catch (InvalidParameterException e)
142     {
143         cout << "Exeception in discoverResource" << e.what() << std::endl;
144     }
145
146     bool isRun = true;
147     int userInput;
148     while (isRun)
149     {
150         while (isReady)
151         {
152             cout << endl;
153             cout << "1 :: Start Hosting" << endl;
154             cout << "2 :: Stop Hosting" << endl;
155             cout << "3 :: Get Attribute" << endl;
156             cout << "4 :: Set Attribute" << endl;
157             cout << "5 :: Start caching (No update to Application)" << endl;
158             cout <<  "6 :: Start caching (Update the application when data change)" <<
159                  endl; //look for the datachange on server
160             cout << "7 :: Get Resource cache State" << endl;
161             cout << "8 :: Get Cached Attributes" << endl;
162             cout << "9 :: Get Cached Attribute"  << endl;
163             cout << "10 :: Stop caching" << endl;
164             cout << "11 :: QUIT" << endl;
165
166             cin >> userInput;
167
168             if (userInput == 1)
169             {
170                 try
171                 {
172                     resource->startMonitoring(&OnResourceStateChanged);
173                     cout << "\n\n**********  Hosting Started ***********" << std::endl;
174                 }
175                 catch (InvalidParameterException e)
176                 {
177                     cout << "Exeception in startMonitoring :: " << e.what() << std::endl;
178                 }
179             }
180             else if (userInput == 2)
181             {
182                 resource->stopMonitoring();
183                 cout << "\n\n******  Hosting stopped******" << std::endl;
184             }
185             else if (userInput == 3)
186             {
187                 resource->getRemoteAttributes(&OnRemoteAttributesReceivedCallback);
188             }
189             else if (userInput == 4)
190             {
191                 int temperatureValue;
192                 if (0 == resourceAttributes.size())
193                 {
194                     cout << "\n***First Get the Attributes from Remote Device : press 3 to get attributes***" <<
195                          std::endl;
196                 }
197                 else
198                 {
199                     RCSResourceAttributes::const_iterator iter = resourceAttributes.begin();
200                     for (unsigned int i = 0; i < resourceAttributes.size(); ++i)
201                     {
202                         if ( iter->key() == "Temperature")
203                         {
204                             cout << "Enter the value you want to set :";
205                             cin >> temperatureValue;
206                             resourceAttributes["Temperature"]  = temperatureValue;
207                             resource->setRemoteAttributes(resourceAttributes, &OnRemoteAttributesSetCallback);
208                         }
209                         ++iter;
210                     }
211                 }
212             }
213             else if (userInput == 5)
214             {
215                 if (false == cachingFlag)
216                 {
217                     resource->startCaching();
218                     cout << "**********  caching Started ***********" << std::endl;
219                     cachingFlag = true;
220                 }
221                 else
222                 {
223                     cout << "***  Already Started... To start it again first stop it : press 10 ***" << std::endl;
224                 }
225             }
226             else if (userInput == 6)
227             {
228                 try
229                 {
230                     if (false == cachingFlag)
231                     {
232                         resource->startCaching(&OnCacheUpdated);
233                         cout << "**********  caching Started ***********" << std::endl;
234                     }
235                     else
236                     {
237                         cout << "***  Already Started... To start it again first stop it : press 10 ***" << std::endl;
238                     }
239                 }
240                 catch (InvalidParameterException e)
241                 {
242                     cout << "Exeception in startCaching :: " << e.what() << std::endl;
243                 }
244             }
245             else if (userInput == 7)
246             {
247
248                 CacheState state = resource->getResourceCacheState();
249                 if (state == CacheState ::READY)
250                     cout << "Current Cache State : " << "CACHE_STATE ::READY" << std::endl;
251                 else if (state == CacheState ::READY_YET)
252                     cout << "Current Cache State : " << "CACHE_STATE ::READY_YET" << std::endl;
253                 else if (state == CacheState ::LOST_SIGNAL)
254                     cout << "Current Cache State : " << "CACHE_STATE ::LOST_SIGNAL" << std::endl;
255                 else if (state == CacheState ::DESTROYED)
256                     cout << "Current Cache State : " << "CACHE_STATE ::DESTROYED" << std::endl;
257                 else if (state == CacheState ::UPDATING)
258                     cout << "Current Cache State : " << "CACHE_STATE ::UPDATING" << std::endl;
259                 else if (state == CacheState ::NONE)
260                     cout << "Current Cache State : " << "CACHE_STATE ::NONE" << std::endl;
261             }
262             else if (userInput == 8)
263             {
264                 try
265                 {
266                     RCSResourceAttributes atttribute = resource->getCachedAttributes();
267                     if (atttribute.empty())
268                     {
269                         cout << "Received cached attribute is empty" << std::endl;
270                     }
271                     else
272                     {
273                         RCSResourceAttributes::const_iterator iter = atttribute.begin();
274                         for (unsigned int i = 0; i < atttribute.size(); ++i)
275                         {
276                             std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
277                             ++iter;
278                         }
279                     }
280                 }
281                 catch (BadRequestException e)
282                 {
283                     cout << "getCachedAttributes exception : " << e.what() << std::endl;
284                 }
285             }
286             else if (userInput == 9)
287             {
288                 std::string key = "Temperature";
289                 try
290                 {
291                     RCSResourceAttributes::Value valueObj = resource->getCachedAttribute(key);
292                     int value = valueObj.get< int >();
293                     cout << "\nkey : " << key << "\nValue : " << value << std::endl;
294                 }
295                 catch (BadRequestException e)
296                 {
297                     cout << "getCachedAttribute exception : " << e.what() << std::endl;
298                 }
299                 catch (BadGetException e)
300                 {
301                     cout << "Exeception in getCachedAttribute  BadGetException:: " << e.what() << std::endl;
302                 }
303             }
304             else if (userInput == 10)
305             {
306                 resource->stopCaching();
307                 cachingFlag = false;
308                 cout << "****** Caching stopped ******" << std::endl;
309             }
310             else if (userInput == 11)
311             {
312                 isReady = false;
313                 isRun = false;
314             }
315             else
316             {
317                 cout << "***   Please enter the number between 1-11  ***" << std::endl;
318             }
319         }
320     }
321     return 0;
322 }
323