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