Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sdk / src / GroupSynchronization.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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 "GroupSynchronization.h"
22 #include "logger.h"
23
24 using namespace OC;
25 using namespace std;
26
27 #define TAG "GROUP_SYNC"
28
29 namespace OIC
30 {
31     GroupSynchronization* GroupSynchronization::groupSyncnstance = NULL;
32     bool GroupSynchronization::bIsFinding = false;
33
34     GroupSynchronization* GroupSynchronization::getInstance()
35     {
36         if (groupSyncnstance == NULL)
37         {
38             groupSyncnstance = new GroupSynchronization();
39         }
40         return groupSyncnstance;
41     }
42
43     void GroupSynchronization::deleteInstance()
44     {
45         if (groupSyncnstance)
46         {
47             delete groupSyncnstance;
48             groupSyncnstance = NULL;
49         }
50     }
51
52     OCStackResult GroupSynchronization::findGroup(
53             std::vector< std::string > collectionResourceTypes, FindCallback callback)
54     {
55         OC_LOG(DEBUG, TAG, "GroupSynchronization::findGroup");
56
57         if(bIsFinding)
58         {
59             OC_LOG(DEBUG, TAG, "It was searching already.");
60             return OC_STACK_ERROR;
61         }
62
63         std::lock_guard < std::mutex > guard(foundGroupMutex);
64         foundGroupResourceList.clear();
65         findCallback = callback;
66
67         if (findCallback == NULL)
68         {
69             OC_LOG(DEBUG, TAG, "Find callback is NULL.");
70             return OC_STACK_ERROR;
71         }
72
73         for (unsigned int i = 0; i < collectionResourceTypes.size(); ++i)
74         {
75
76             std::string query = OC_MULTICAST_DISCOVERY_URI;
77             query.append("?rt=");
78             query.append(collectionResourceTypes.at(i));
79
80             OCPlatform::findResource("", query,
81                     OC_ALL,
82                     std::bind(&GroupSynchronization::onFindGroup, this,
83                         std::placeholders::_1));
84         }
85
86         bIsFinding = true;
87
88         // thread to check if GroupSynchronization::onFoundGroup is called or not.
89         std::thread t(std::bind(&GroupSynchronization::checkFindGroup, this));
90         t.detach();
91
92         return OC_STACK_OK;
93     }
94
95     OCStackResult GroupSynchronization::createGroup(std::string collectionResourceType)
96     {
97         std::lock_guard < std::mutex > guard(foundGroupMutex);
98         foundGroupResourceList.clear();
99
100         OCResourceHandle collectionResHandle = NULL;
101         OCResourceHandle groupSyncResHandle = NULL;
102
103         if (0 == collectionResourceType.length())
104         {
105             OC_LOG(DEBUG, TAG,
106                 "GroupSynchronization::createGroup : Error! Input params are wrong.");
107             return OC_STACK_INVALID_PARAM;
108         }
109
110         OC_LOG(DEBUG, TAG, "GroupSynchronization::createGroup - The created group is added.");
111
112         OCStackResult result;
113
114         // creating master collection resource
115         std::string collectionUri = "/" + collectionResourceType;
116         std::size_t i;
117         while ((i = collectionUri.find(".")) != std::string::npos)
118         {
119             collectionUri.replace(i, 1, "/");
120         }
121
122         OC_LOG_V(DEBUG, TAG, "GroupSynchronization::createGroup : collection uri - %s, type - %s",
123             collectionUri.c_str(), collectionResourceType.c_str());
124
125         std::string resourceInterface = DEFAULT_INTERFACE;
126
127         result = OCPlatform::registerResource(collectionResHandle, collectionUri,
128                 collectionResourceType, resourceInterface, NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
129         if (result != OC_STACK_OK)
130         {
131             OC_LOG_V(DEBUG, TAG, "To register resource (%s) was unsuccessful. result - %d",
132                 collectionUri.c_str(), result);
133             goto Error;
134         }
135
136         {
137             OCPlatform::bindInterfaceToResource(collectionResHandle, GROUP_INTERFACE);
138             if (result != OC_STACK_OK)
139             {
140                 OC_LOG_V(DEBUG, TAG,
141                     "To bind Interface (collection) was unsuccessful. result - %d",
142                     result);
143             }
144
145             collectionResourceHandleList[collectionResourceType] = collectionResHandle;
146
147             // creating master group sync resource
148             std::string groupSyncUri = collectionUri + "/groupsync";
149             std::string groupSyncResType = collectionResourceType + ".groupsync";
150
151             result = OCPlatform::registerResource(groupSyncResHandle, groupSyncUri,
152                     groupSyncResType, resourceInterface,
153                     std::bind(&GroupSynchronization::groupEntityHandler, this,
154                             std::placeholders::_1), OC_DISCOVERABLE | OC_OBSERVABLE);
155             if (result != OC_STACK_OK)
156             {
157                 OC_LOG_V(DEBUG, TAG,
158                     "To register resource (groupsync) was unsuccessful. result - %d", result);
159                 goto Error;
160             }
161
162             groupSyncResourceHandleList[collectionResourceType] = groupSyncResHandle;
163
164             return OC_STACK_OK;
165         }
166         Error:
167
168         if (collectionResHandle)
169         {
170             OCPlatform::unregisterResource(collectionResHandle);
171             auto iterator = collectionResourceHandleList.find(collectionResourceType);
172             if (iterator != collectionResourceHandleList.end())
173             {
174                 collectionResourceHandleList.erase(iterator);
175             }
176         }
177
178         if (groupSyncResHandle)
179         {
180             OCPlatform::unregisterResource(groupSyncResHandle);
181             auto iterator = groupSyncResourceHandleList.find(collectionResourceType);
182             if (iterator != groupSyncResourceHandleList.end())
183             {
184                 groupSyncResourceHandleList.erase(iterator);
185             }
186         }
187
188         return OC_STACK_NO_RESOURCE;
189     }
190
191     OCStackResult GroupSynchronization::joinGroup(std::string collectionResourceType,
192             OCResourceHandle resourceHandle)
193     {
194         std::lock_guard < std::mutex > guard(foundGroupMutex);
195         if ((0 == collectionResourceType.length()) || (!resourceHandle))
196         {
197             OC_LOG(DEBUG, TAG,
198                 "GroupSynchronization::joinGroup : Error! input params are wrong.");
199             return OC_STACK_INVALID_PARAM;
200         }
201
202         auto resIt = collectionResourceHandleList.find(collectionResourceType);
203         if (resIt == collectionResourceHandleList.end())
204         {
205             OC_LOG(DEBUG, TAG,
206                 "GroupSynchronization::joinGroup : error! There is no collection to join");
207             return OC_STACK_INVALID_PARAM;
208         }
209
210         OCResourceHandle collectionResHandle = resIt->second;
211         try{
212             OCStackResult result = OCPlatform::bindResource(collectionResHandle, resourceHandle);
213             if (result != OC_STACK_OK)
214             {
215                 OC_LOG_V(DEBUG, TAG,
216                     "GroupSynchronization::joinGroup : To bind resource was unsuccessful." \
217                     " result - %d", result);
218                 return OC_STACK_ERROR;
219             }
220         } catch(OCException &e) {
221             return OC_STACK_INVALID_PARAM;
222         }
223
224         OC_LOG(DEBUG, TAG,
225             "GroupSynchronization::joinGroup : To bind collectionResHandle and resourceHandle");
226
227         std::vector< OCResourceHandle > childHandleList;
228
229         auto childIt = childResourceHandleList.find(collectionResHandle);
230         if (childIt != childResourceHandleList.end())
231         {
232             childHandleList = childIt->second;
233         }
234
235         childHandleList.push_back(resourceHandle);
236         childResourceHandleList[collectionResHandle] = childHandleList;
237
238         deviceResourceHandleList.push_back(resourceHandle);
239
240         debugGroupSync();
241
242         return OC_STACK_OK;
243     }
244
245     OCStackResult GroupSynchronization::joinGroup(const std::shared_ptr< OCResource > resource,
246             OCResourceHandle resourceHandle)
247     {
248         std::lock_guard < std::mutex > guard(foundGroupMutex);
249         if ((!resource) || (!resourceHandle))
250         {
251             OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup : Error! Input params are wrong.");
252             return OC_STACK_INVALID_PARAM;
253         }
254
255         OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup");
256
257         // making representation to join group
258         std::string method = "joinGroup";
259         std::vector< std::string > type = resource->getResourceTypes();
260         std::string resourceType;
261         resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
262
263         OCRepresentation rep;
264         rep.setValue("method", method);
265         rep.setValue("collectionResourceType", type[0]);
266         rep.setValue("resourceType", resourceType);
267
268         OC_LOG_V(DEBUG, TAG, "\tmethod - %s", method.c_str());
269         OC_LOG_V(DEBUG, TAG, "\tcollectionResourceType - %s", type[0].c_str());
270         OC_LOG_V(DEBUG, TAG, "\tresourceType - %s", resourceType.c_str());
271
272         // creating group sync resource with the received collection resource.
273         // entity handler of group sync is used to join group.
274         std::string host = resource->host();
275         std::string uri = resource->uri() + "/groupsync";
276
277         std::vector< std::string > resourceTypes;
278         std::string temp;
279         for (unsigned int i = 0; i < type.size(); ++i)
280         {
281             temp = type[0] + ".groupsync";
282             resourceTypes.push_back(temp);
283         }
284
285         std::vector< std::string > resourceInterface;
286         resourceInterface.push_back(DEFAULT_INTERFACE);
287
288         OCResource::Ptr groupSyncResource =
289                         OCPlatform::constructResourceObject(host, uri,
290
291                             OC_ALL, false,
292                             resourceTypes, resourceInterface);
293
294         // OCResource::Ptr groupSyncResource = OCPlatform::constructResourceObject(host, uri,
295         //         OC_WIFI, false, resourceTypes, resourceInterface);
296
297         groupSyncResourceList[type[0]] = groupSyncResource;
298
299         OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup : creating groupSyncResource.");
300
301         // Create QueryParameters Map and add query params (if any)
302         QueryParamsMap queryParamsMap;
303
304         // request to join group to the remote group sync resource
305         OCStackResult result = groupSyncResource->put(rep, queryParamsMap,
306                 std::bind(&GroupSynchronization::onJoinGroup, this, std::placeholders::_1,
307                         std::placeholders::_2, std::placeholders::_3));
308         if (OC_STACK_OK == result)
309         {
310             OC_LOG(DEBUG, TAG,
311                 "GroupSynchronization::joinGroup : groupSyncResource->put was successful.");
312         }
313         else
314         {
315             OC_LOG_V(DEBUG, TAG,
316                 "GroupSynchronization::joinGroup : groupSyncResource->put was unsuccessful." \
317                 "result - %d", result);
318         }
319
320         // saving the remote collection resource.
321         // It is used in onJoinGroup() and onGetJoinedRemoteChild().
322         remoteCollectionResource = resource;
323
324         // saving the resource handle to join. It is used in onGetJoinedRemoteChild()
325         deviceResourceHandle = resourceHandle;
326
327         return OC_STACK_OK;
328     }
329
330     OCStackResult GroupSynchronization::leaveGroup(std::string collectionResourceType,
331             OCResourceHandle resourceHandle)
332     {
333         if ((0 == collectionResourceType.length()) || (!resourceHandle))
334         {
335             OC_LOG(DEBUG, TAG,
336                 "GroupSynchronization::leaveGroup : Error! Input params are wrong.");
337             return OC_STACK_INVALID_PARAM;
338         }
339
340         OC_LOG_V(DEBUG, TAG,
341             "GroupSynchronization::leaveGroup : collectionResourceType - %s",
342             collectionResourceType.c_str());
343
344         OCResourceHandle collectionResHandle;
345         auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
346
347         // if groupSyncResourceHandleList has resourceType,
348         // this app created collection resource handle.
349         if (handleIt != groupSyncResourceHandleList.end())
350         {
351             handleIt = collectionResourceHandleList.find(collectionResourceType);
352             if (handleIt == collectionResourceHandleList.end())
353             {
354                 OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : Error!" \
355                     "There is no collection resource handle to leave.");
356                 return OC_STACK_INVALID_PARAM;
357             }
358
359             collectionResHandle = handleIt->second;
360             if(collectionResHandle == NULL)
361                 return OC_STACK_INVALID_PARAM;
362
363             OCStackResult result;
364             try
365             {
366                 result = OCPlatform::unbindResource(collectionResHandle, resourceHandle);
367                 if (OC_STACK_OK == result)
368                 {
369                     OC_LOG(DEBUG, TAG,
370                         "GroupSynchronization::leaveGroup : To unbind resource was successful.");
371                 }
372                 else
373                 {
374                     OC_LOG_V(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
375                             "To unbind resource was unsuccessful. result - %d", result);
376                     return result;
377                 }
378             } catch(OCException &e) {
379                 OC_LOG_V(DEBUG, TAG, "ERROR : %s", e.reason().c_str());
380                 return OC_STACK_NO_RESOURCE;
381             }
382
383             auto It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
384                     resourceHandle);
385             if (It == deviceResourceHandleList.end()) // there is no resource handle to find
386             {
387                 try
388                 {
389                     result = OCPlatform::unregisterResource(resourceHandle);
390                     if (OC_STACK_OK == result)
391                     {
392                         OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
393                                 "To unregister resource was successful.");
394                     }
395                     else
396                     {
397                         OC_LOG_V(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
398                             "To unregister resource was unsuccessful. result - %d", result);
399                         return result;
400                     }
401                 } catch(OCException &e)
402                 {
403                     OC_LOG_V(DEBUG, TAG, "ERROR : %s", e.reason().c_str());
404                     return OC_STACK_NO_RESOURCE;
405                 }
406             }
407             else
408             {
409                 OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
410                     "This resource cannot be unregistered.");
411                 deviceResourceHandleList.erase(It);
412             }
413
414             auto handleListIt = childResourceHandleList.find(collectionResHandle);
415             if (handleListIt == childResourceHandleList.end())
416             {
417                 OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
418                     "Error! There is no child resource list to delete.");
419                 return OC_STACK_INVALID_PARAM;
420             }
421
422             std::vector< OCResourceHandle > childList = handleListIt->second;
423             auto childIt = std::find(childList.begin(), childList.end(), resourceHandle);
424             if (childIt != childList.end())
425             {
426                 OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \
427                     "Found! The resource to leave is found.");
428                 childList.erase(childIt);
429             }
430
431             childResourceHandleList[collectionResHandle] = childList;
432
433             debugGroupSync();
434         }
435
436         return OC_STACK_OK;
437     }
438
439 OCStackResult GroupSynchronization::leaveGroup(
440         const std::shared_ptr<OCResource> resource,
441         std::string collectionResourceType, OCResourceHandle resourceHandle)
442     {
443         if ((!resource) || (!resourceHandle))
444         {
445             OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup : Error! Input params are wrong.");
446             return OC_STACK_INVALID_PARAM;
447         }
448
449         OC_LOG(DEBUG, TAG, "GroupSynchronization::joinGroup");
450
451         // making representation to join group
452         std::vector< std::string > type = resource->getResourceTypes();
453         std::string host = resource->host();
454         std::string uri = resource->uri() + "/groupsync";
455
456         std::vector< std::string > resourceTypes;
457         std::string temp;
458         for (unsigned int i = 0; i < type.size(); ++i)
459         {
460             temp = type[0] + ".groupsync";
461             resourceTypes.push_back(temp);
462         }
463
464         std::vector< std::string > resourceInterface;
465         resourceInterface.push_back(DEFAULT_INTERFACE);
466
467         OCResource::Ptr groupSyncResource;
468         groupSyncResource = OCPlatform::constructResourceObject(host, uri,
469                 OC_ALL, false,
470                 resourceTypes, resourceInterface);
471         // groupSyncResource = OCPlatform::constructResourceObject(host, uri,
472         //         OC_WIFI, false, resourceTypes, resourceInterface);
473
474         // making representation to leave group
475         std::string method = "leaveGroup";
476 //        std::string type = OCGetResourceTypeName(collectionResourceType, 0);
477         std::string resourceType;
478         resourceType.append(OCGetResourceTypeName(resourceHandle, 0));
479
480         OCRepresentation rep;
481         rep.setValue("method", method);
482         rep.setValue("collectionResourceType", collectionResourceType);
483         rep.setValue("resourceType", resourceType);
484
485         OC_LOG_V(DEBUG, TAG, "\tmethod - %s", method.c_str());
486         OC_LOG_V(DEBUG, TAG, "\tcollectionResourceType - %s", collectionResourceType.c_str());
487         OC_LOG_V(DEBUG, TAG, "\tresourceType - %s", resourceType.c_str());
488
489         QueryParamsMap queryParamsMap;
490
491         // request to leave group to the remote group sync resource
492         OCStackResult result = groupSyncResource->put(rep, queryParamsMap,
493                 std::bind(&GroupSynchronization::onLeaveGroup, this, std::placeholders::_1,
494                         std::placeholders::_2, std::placeholders::_3));
495         if (OC_STACK_OK == result)
496         {
497             OC_LOG(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
498                 "groupSyncResource->put was successful.");
499         }
500         else
501         {
502             OC_LOG_V(DEBUG, TAG, "GroupSynchronization::leaveGroup : " \
503                 "groupSyncResource->put was unsuccessful. result - %d", result);
504         }
505         return result;
506     }
507
508     void GroupSynchronization::deleteGroup(std::string collectionResourceType)
509     {
510         if (0 == collectionResourceType.length())
511         {
512             OC_LOG(DEBUG, TAG,
513                 "GroupSynchronization::deleteGroup : Error! Input params are wrong.");
514             return;
515         }
516
517         OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup");
518
519         OCStackResult result;
520         OCResourceHandle resourceHandle;
521
522         auto handleIt = groupSyncResourceHandleList.find(collectionResourceType);
523
524         // if groupSyncResourceHandleList has resourceType,
525         // group sync of this app created collection resource.
526         if (handleIt != groupSyncResourceHandleList.end())
527         {
528             resourceHandle = handleIt->second; // group sync resource handle
529             result = OCPlatform::unregisterResource(resourceHandle);
530             if (OC_STACK_OK == result)
531             {
532                 OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
533                     "To unregister group sync resource handle was successful.");
534             }
535             else
536             {
537                 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
538                         "To unregister group sync resource handle was unsuccessful. " \
539                         "result - %d", result);
540             }
541
542             groupSyncResourceHandleList.erase(handleIt);
543         }
544
545         auto resourceIt = groupSyncResourceList.find(collectionResourceType);
546         if (resourceIt != groupSyncResourceList.end())
547         {
548             groupSyncResourceList.erase(resourceIt);
549         }
550
551         handleIt = collectionResourceHandleList.find(collectionResourceType);
552         if (handleIt == collectionResourceHandleList.end())
553         {
554             OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
555                 "Error! There is no collection resource handle to delete.");
556             return;
557         }
558         OCResourceHandle collectionResHandle = handleIt->second;
559
560         collectionResourceHandleList.erase(handleIt);
561
562         auto handleListIt = childResourceHandleList.find(collectionResHandle);
563         if (handleListIt == childResourceHandleList.end())
564         {
565             OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
566                 "There is no child resource list to delete.");
567
568             result = OCPlatform::unregisterResource(collectionResHandle);
569             if (result == OC_STACK_OK)
570             {
571                 OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
572                      "To unregister collection resource handle was successful.");
573             }
574             else
575             {
576                 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
577                     " To unregister collection resource handle was unsuccessful. result - %d",
578                     result);
579             }
580
581             debugGroupSync();
582             return;
583         }
584
585         std::vector< OCResourceHandle > childList = handleListIt->second;
586
587         childResourceHandleList.erase(handleListIt);
588
589         result = OCPlatform::unbindResources(collectionResHandle, childList);
590         if (OC_STACK_OK == result)
591         {
592             OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
593                 "To unbind resources was successful.");
594         }
595         else
596         {
597             OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
598                 "To unbind resources was unsuccessful. result - %d", result);
599         }
600
601         result = OCPlatform::unregisterResource(collectionResHandle);
602         if (result == OC_STACK_OK)
603         {
604             OC_LOG(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
605                 "To unregister collection resource handle was successful.");
606         }
607         else
608         {
609             OC_LOG_V(DEBUG, TAG, "GroupSynchronization::deleteGroup : " \
610                 " To unregister collection resource handle was unsuccessful. result - %d",
611                 result);
612         }
613
614         std::vector< OCResourceHandle >::iterator It;
615
616         for (unsigned int i = 0; i < childList.size(); i++)
617         {
618             resourceHandle = childList.at(i);
619
620             It = std::find(deviceResourceHandleList.begin(), deviceResourceHandleList.end(),
621                     resourceHandle);
622             if (It != deviceResourceHandleList.end()) // find !!
623             {
624                 deviceResourceHandleList.erase(It);
625             }
626             else
627             {
628                 result = OCPlatform::unregisterResource(resourceHandle);
629                 if (OC_STACK_OK == result)
630                 {
631                     OC_LOG_V(DEBUG, TAG,
632                         "GroupSynchronization::deleteGroup : UnregisterResource(%d)" \
633                         " was successful.", i + 1);
634                 }
635                 else
636                 {
637                     OC_LOG_V(DEBUG, TAG,
638                         "GroupSynchronization::deleteGroup : UnregisterResource(%d)" \
639                         " was unsuccessful. result - ", i + 1, result);
640                 }
641             }
642         }
643
644         debugGroupSync();
645     }
646
647     std::map< std::string, OCResourceHandle > GroupSynchronization::getGroupList()
648     {
649         return collectionResourceHandleList;
650     }
651
652     OCEntityHandlerResult GroupSynchronization::groupEntityHandler(
653             const std::shared_ptr< OCResourceRequest > request)
654     {
655         OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler");
656
657         if (request)
658         {
659             // Get the request type and request flag
660             std::string requestType = request->getRequestType();
661             int requestFlag = request->getRequestHandlerFlag();
662
663             if (requestFlag == RequestHandlerFlag::RequestFlag)
664             {
665                 OC_LOG(DEBUG, TAG, "\trequestFlag : Request");
666
667                 // If the request type is GET
668                 if (requestType == "GET")
669                 {
670                     OC_LOG(DEBUG, TAG, "\t\trequestType : GET");
671                 }
672                 else if (requestType == "PUT")
673                 {
674                     OC_LOG(DEBUG, TAG, "\t\trequestType : PUT");
675
676                     //get method name, group resource type and resource type to join group
677                     OCRepresentation rp = request->getResourceRepresentation();
678                     std::string methodType = rp.getValue< std::string >("method");
679                     std::string collectionResourceType = rp.getValue< std::string >(
680                             "collectionResourceType");
681                     std::string resourceType = rp.getValue< std::string >("resourceType");
682
683                     OC_LOG_V(DEBUG, TAG, "\t\t\tmethod : %s", methodType.c_str());
684                     OC_LOG_V(DEBUG, TAG, "\t\t\tcollection resourceType : %s",
685                         collectionResourceType.c_str());
686                     OC_LOG_V(DEBUG, TAG, "\t\t\tresourceType : %s", resourceType.c_str());
687
688                     auto handleIt = collectionResourceHandleList.find(collectionResourceType);
689                     if (handleIt == collectionResourceHandleList.end())
690                     {
691                         OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \
692                             "Error! There is no collection resource handle to delete.");
693                         return OC_EH_ERROR;
694                     }
695                     collectionResourceHandle = handleIt->second;
696                     // in case of join group it is used in onFindResource()
697
698                     if (methodType == "joinGroup")
699                     {
700                         std::string resourceName = OC_MULTICAST_DISCOVERY_URI;
701                         resourceName.append("?rt=");
702                         resourceName.append(resourceType);
703                         OC_LOG_V(DEBUG, TAG, "\t\t\tresourceName : %s", resourceName.c_str());
704
705                         resourceRequest = request;
706
707                         OCPlatform::findResource("", resourceName,
708                             OC_ALL,
709                             std::bind(&GroupSynchronization::onFindResource, this,
710                                 std::placeholders::_1));
711
712                         return OC_EH_SLOW;
713                     }
714                     else if (methodType == "leaveGroup")
715                     {
716                         auto it = childResourceHandleList.find(collectionResourceHandle);
717                         if (it == childResourceHandleList.end())
718                         {
719                             OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \
720                                 "Error! There is no child resource list.");
721                             return OC_EH_ERROR;
722                         }
723
724                         std::vector< OCResourceHandle > childList = it->second;
725                         OCResourceHandle resourceHandle;
726                         for (auto childIt = childList.begin(); childIt != childList.end();)
727                         {
728                             resourceHandle = (*childIt);
729                             char* type = (char*) OCGetResourceTypeName(resourceHandle, 0);
730
731                             if (0 == resourceType.compare(type))
732                             {
733                                 OC_LOG_V(DEBUG, TAG,
734                                     "GroupSynchronization::groupEntityHandler : " \
735                                     "Found! The resource to leave is found. - %s", type);
736
737                                 childIt = childList.erase(childIt++);
738
739                                 OCStackResult result = OCPlatform::unbindResource(
740                                         collectionResourceHandle, resourceHandle);
741                                 if (OC_STACK_OK == result)
742                                 {
743                                     OC_LOG(DEBUG, TAG,
744                                         "GroupSynchronization::groupEntityHandler : " \
745                                         "To unbind resource was successful.");
746                                 }
747                                 else
748                                 {
749                                     OC_LOG_V(DEBUG, TAG,
750                                         "GroupSynchronization::groupEntityHandler : " \
751                                         "To unbind resource was unsuccessful. result - %d",
752                                         result);
753                                 }
754
755                                 result = OCPlatform::unregisterResource(resourceHandle);
756                                 if (OC_STACK_OK == result)
757                                 {
758                                     OC_LOG(DEBUG, TAG,
759                                         "GroupSynchronization::groupEntityHandler : " \
760                                         "To unregister resource was successful.");
761                                 }
762                                 else
763                                 {
764                                     OC_LOG_V(DEBUG, TAG,
765                                         "GroupSynchronization::groupEntityHandler : "
766                                         "To unregister resource was unsuccessful. result - %d",
767                                         result);
768                                 }
769                             }
770                             else
771                             {
772                                 ++childIt;
773                             }
774
775                         }
776
777                         childResourceHandleList[collectionResourceHandle] = childList;
778
779                         debugGroupSync();
780
781                         auto pResponse = std::make_shared< OC::OCResourceResponse >();
782                         pResponse->setRequestHandle(request->getRequestHandle());
783                         pResponse->setResourceHandle(request->getResourceHandle());
784                         pResponse->setErrorCode(200);
785                         pResponse->setResponseResult(OC_EH_OK);
786
787                         OCRepresentation rep = request->getResourceRepresentation();
788                         pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
789                         if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
790                         {
791                             OC_LOG(DEBUG, TAG, "GroupSynchronization::groupEntityHandler : " \
792                                 "sendResponse is successful.");
793                         }
794                     }
795
796                     if (methodType != "") //TODO: Check groupmethodtype NULL
797                     {
798                     }
799                 }
800                 else if (requestType == "POST")
801                 {
802                     // POST request operations
803                 }
804                 else if (requestType == "DELETE")
805                 {
806                     // DELETE request operations
807                 }
808             }
809             else if (requestFlag == RequestHandlerFlag::ObserverFlag)
810             {
811                 OC_LOG(DEBUG, TAG, "\trequestFlag : Observer");
812             }
813         }
814         else
815         {
816             OC_LOG(DEBUG, TAG, "Request invalid");
817         }
818
819         return OC_EH_OK;
820     }
821
822     void GroupSynchronization::onFindGroup(std::shared_ptr< OCResource > resource)
823     {
824         OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindGroup");
825
826         try
827         {
828             if (resource)
829             {
830                 // Debugging
831                 std::string resourceURI;
832                 std::string hostAddress;
833
834                 // Get the resource URI
835                 resourceURI = resource->uri();
836                 OC_LOG_V(DEBUG, TAG, "\tURI of the resource: %s", resourceURI.c_str());
837
838                 // Get the resource host address
839                 hostAddress = resource->host();
840                 OC_LOG_V(DEBUG, TAG, "\tHost address of the resource: %s", hostAddress.c_str());
841
842                 hostAddress.append(resourceURI);
843
844 #ifndef NDEBUG
845                 // Get the resource types
846                 OC_LOG(DEBUG, TAG, "\tList of resource types: ");
847                 for (auto &resourceTypes : resource->getResourceTypes())
848                 {
849                     OC_LOG_V(DEBUG, TAG, "\t\t", resourceTypes.c_str());
850                 }
851
852                 // Get the resource interfaces
853                 OC_LOG(DEBUG, TAG, "\tList of resource interfaces: ");
854                 for (auto &resourceInterfaces : resource->getResourceInterfaces())
855                 {
856                     OC_LOG_V(DEBUG, TAG, "\t\t", resourceInterfaces.c_str());
857                 }
858 #endif
859
860                 if (false == IsSameGroup(resource))
861                 {
862                     saveGroup(resource);
863                     findCallback(resource);
864                 }
865             }
866             else
867             {
868                 // Resource is invalid
869                 OC_LOG(DEBUG, TAG, "Resource is invalid");
870                 findCallback(NULL);
871             }
872
873             bIsFinding = false;
874         }
875         catch (std::exception& e)
876         {
877             //log(e.what());
878         }
879     }
880
881     void GroupSynchronization::checkFindGroup(void)
882     {
883         OC_LOG(DEBUG, TAG, "GroupSynchronization::checkFindGroup");
884
885         for (int i = 0; i < 15; i++)
886         {
887             std::chrono::milliseconds workTime(300);
888             std::this_thread::sleep_for(workTime);
889
890             std::lock_guard < std::mutex > guard(foundGroupMutex);
891
892             if (false == foundGroupResourceList.empty())
893             {
894                 OC_LOG(DEBUG, TAG, "GroupSynchronization::checkFoundGroup : " \
895                     "Some group is received.");
896                 return;
897             }
898         }
899
900         OC_LOG(DEBUG, TAG, "GroupSynchronization::checkFoundGroup : " \
901             "It is failed to find resource within 3s.");
902
903         onFindGroup(NULL);
904         return;
905     }
906
907     bool GroupSynchronization::IsSameGroup(std::shared_ptr< OCResource > resource)
908     {
909         std::lock_guard < std::mutex > guard(foundGroupMutex);
910
911         if (true == foundGroupResourceList.empty())
912         {
913             OC_LOG(DEBUG, TAG, "GroupSynchronization::IsSameGroup : There is no found group.");
914             return false;
915         }
916
917         std::string foundHostAddress, savedHostAddress;
918         std::string foundHostUri, savedHostUri;
919         foundHostAddress = resource->host();
920         foundHostUri = resource->uri();
921
922         for (unsigned int i = 0; i < foundGroupResourceList.size(); ++i)
923         {
924             savedHostAddress = (foundGroupResourceList.at(i))->host();
925             savedHostUri = (foundGroupResourceList.at(i))->uri();
926
927             if (0 == foundHostAddress.compare(savedHostAddress.c_str()))
928             {
929                 if( 0 == foundHostUri.compare(savedHostAddress) )
930                 {
931                     OC_LOG(DEBUG, TAG,
932                         "GroupSynchronization::IsSameGroup : Found! The same group is found.");
933                     return true;
934                 }
935             }
936         }
937
938         OC_LOG(DEBUG, TAG, "GroupSynchronization::IsSameGroup :  There is no same group.");
939         return false;
940     }
941
942     void GroupSynchronization::saveGroup(std::shared_ptr< OCResource > resource)
943     {
944         OC_LOG(DEBUG, TAG, "GroupSynchronization::saveGroup");
945
946         std::lock_guard < std::mutex > guard(foundGroupMutex);
947
948         foundGroupResourceList.push_back(resource);
949     }
950
951     void GroupSynchronization::onJoinGroup(const HeaderOptions& headerOptions,
952             const OCRepresentation& rep, const int eCode)
953     {
954         if (eCode != OC_STACK_OK)
955         {
956             OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onJoinGroup : error - %d", eCode);
957             return;
958         }
959     }
960
961     void GroupSynchronization::onFindResource(std::shared_ptr< OCResource > resource)
962     {
963         OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindResource");
964
965         if (resource)
966         {
967             if(NULL == collectionResourceHandle)
968                 return;
969
970             // start of debugging
971             std::string resourceURI;
972             std::string hostAddress;
973
974             // Get the resource URI
975             resourceURI = resource->uri();
976             OC_LOG_V(DEBUG, TAG, "\tURI of the resource: %s", resourceURI.c_str());
977
978             // Get the resource host address
979             hostAddress = resource->host();
980             OC_LOG_V(DEBUG, TAG, "\tHost address of the resource: %s", hostAddress.c_str());
981
982             hostAddress.append(resourceURI);
983
984 #ifndef NDEBUG
985             // Get the resource types
986             OC_LOG(DEBUG, TAG, "\tList of resource types: ");
987             for (auto &resourceTypes : resource->getResourceTypes())
988             {
989                 OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceTypes.c_str());
990             }
991
992             // Get the resource interfaces
993             OC_LOG(DEBUG, TAG, "\tList of resource interfaces: ");
994             for (auto &resourceInterfaces : resource->getResourceInterfaces())
995             {
996                 OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceInterfaces.c_str());
997             }
998             // end of debugging
999 #endif
1000
1001             OCResourceHandle resourceHandle;
1002             OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
1003             if (result != OC_STACK_OK)
1004             {
1005                 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::" \
1006                     "onFindResource - Resource to join creation was unsuccessful. result - %d",
1007                     result);
1008                 return;
1009             }
1010
1011             result = OCPlatform::bindResource(collectionResourceHandle, resourceHandle);
1012             if (result != OC_STACK_OK)
1013             {
1014                 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onFindResource : " \
1015                     "To bind resource was unsuccessful. result - %d", result);
1016                 return;
1017             }
1018
1019             OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindResource : " \
1020                     "To bind joinGroupHandle and resourceHandle was successful.");
1021
1022             auto it = childResourceHandleList.find(collectionResourceHandle);
1023             std::vector< OCResourceHandle > childHandleList;
1024             if (it != childResourceHandleList.end())
1025             {
1026                 childHandleList = it->second;
1027             }
1028
1029             childHandleList.push_back(resourceHandle);
1030             childResourceHandleList[collectionResourceHandle] = childHandleList;
1031
1032             auto pResponse = std::make_shared< OC::OCResourceResponse >();
1033             pResponse->setRequestHandle(resourceRequest->getRequestHandle());
1034             pResponse->setResourceHandle(resourceRequest->getResourceHandle());
1035             pResponse->setErrorCode(200);
1036             pResponse->setResponseResult(OC_EH_OK);
1037
1038             OCRepresentation rep = resourceRequest->getResourceRepresentation();
1039             pResponse->setResourceRepresentation(rep);
1040             try{
1041                 if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
1042                 {
1043                     OC_LOG(DEBUG, TAG,
1044                         "GroupSynchronization::onFindResource : sendResponse is successful.");
1045                 }
1046             }
1047             catch( OCException &e )
1048             {
1049                 // OC_LOG(DEBUG, TAG, e.what);
1050                 return;
1051             }
1052         }
1053         else
1054         {
1055             OC_LOG(DEBUG, TAG, "GroupSynchronization::onFindResource : " \
1056                 "Resource is invalid. So a new Group Resource has to be created.");
1057         }
1058
1059         debugGroupSync();
1060     }
1061
1062     void GroupSynchronization::onGetJoinedRemoteChild(const HeaderOptions& headerOptions,
1063             const OCRepresentation& rep, const int eCode)
1064     {
1065         if (eCode != OC_STACK_OK)
1066         {
1067             OC_LOG_V(DEBUG, TAG,
1068                 "GroupSynchronization::onGetJoinedRemoteChild : error - %d", eCode);
1069             return;
1070         }
1071
1072         std::string resourceURI;
1073         std::vector< OCRepresentation > childList;
1074         OCRepresentation child;
1075
1076         OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild");
1077 #ifndef NDEBUG
1078         // debugging
1079         // Get the resource URI
1080         resourceURI = rep.getUri();
1081         OC_LOG_V(DEBUG, TAG, "\tURI of the resource: %s", resourceURI.c_str());
1082
1083         // Get the resource types
1084         OC_LOG(DEBUG, TAG, "\tList of resource types: ");
1085
1086         for (auto &resourceTypes : rep.getResourceTypes())
1087         {
1088             OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceTypes.c_str());
1089         }
1090
1091         // Get the resource interfaces
1092         OC_LOG(DEBUG, TAG, "\tList of resource interfaces: ");
1093         for (auto &resourceInterfaces : rep.getResourceInterfaces())
1094         {
1095             OC_LOG_V(DEBUG, TAG, "\t\t%s", resourceInterfaces.c_str());
1096         }
1097
1098         childList = rep.getChildren();
1099         for (unsigned int i = 0; i < childList.size(); ++i)
1100         {
1101             OC_LOG_V(DEBUG, TAG, "\tchild resource - %d", i + 1);
1102
1103             child = childList.at(i);
1104             resourceURI = child.getUri();
1105             OC_LOG_V(DEBUG, TAG, "\t\tURI of the resource: %s", resourceURI.c_str());
1106
1107             OC_LOG_V(DEBUG, TAG, "\t\tList of resource types: ");
1108             for (auto &types : child.getResourceTypes())
1109             {
1110                 OC_LOG_V(DEBUG, TAG, "\t\t\t%s", types.c_str());
1111             }
1112
1113             OC_LOG(DEBUG, TAG, "\tList of resource interfaces: ");
1114             for (auto &interfaces : child.getResourceInterfaces())
1115             {
1116                 OC_LOG_V(DEBUG, TAG, "\t\t\t%s", interfaces.c_str());
1117             }
1118         }
1119 #endif
1120
1121         // creating remote collection resource handle
1122         OCResourceHandle remoteCollectionResourceHandle;
1123         resourceURI = remoteCollectionResource->uri();
1124         std::vector< std::string > types = remoteCollectionResource->getResourceTypes();
1125         std::vector< std::string > interfaces = remoteCollectionResource->getResourceInterfaces();
1126
1127         OCStackResult result = OCPlatform::registerResource(remoteCollectionResourceHandle,
1128                 resourceURI, types[0], interfaces[0], NULL, OC_OBSERVABLE);
1129         if (result != OC_STACK_OK)
1130         {
1131             OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \
1132                 "To register remoteCollectionResourceHandle" \
1133                 " was unsuccessful. result - %d", result);
1134             return;
1135         }
1136         OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : " \
1137                 "To register remoteCollectionResourceHandle was successful.");
1138
1139         // binding remote collection resource handle and resource handle to join
1140         collectionResourceHandleList[types[0]] = remoteCollectionResourceHandle;
1141
1142         result = OCPlatform::bindResource(remoteCollectionResourceHandle, deviceResourceHandle);
1143         if (OC_STACK_OK == result)
1144         {
1145             OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : " \
1146                 "binding remoteCollectionResourceHandle and deviceResourceHandle");
1147         }
1148         else
1149         {
1150             OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \
1151                 "To bind remoteCollectionResourceHandle and deviceResourceHandle " \
1152                 "was unsuccessful. result - %d", result);
1153         }
1154
1155         std::vector< OCResourceHandle > childHandleList;
1156         childHandleList.push_back(deviceResourceHandle);
1157         deviceResourceHandleList.push_back(deviceResourceHandle);
1158
1159         // binding copied remote collection resource handle and copied remote resource
1160         OCResourceHandle resourceHandle;
1161         for (unsigned int i = 0; i < childList.size(); ++i)
1162         {
1163             OC_LOG_V(DEBUG, TAG, "\tremote resource - %d", i + 1);
1164
1165             child = childList.at(i);
1166             resourceURI = child.getUri();
1167             types = child.getResourceTypes();
1168             interfaces = child.getResourceInterfaces();
1169
1170             if (0 == types[0].compare(OCGetResourceTypeName(deviceResourceHandle, 0)))
1171             {
1172                 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : %s" \
1173                     " is bind already.", types[0].c_str());
1174                 continue;
1175             }
1176
1177             result = OCPlatform::registerResource(resourceHandle, resourceURI, types[0],
1178                     interfaces[0], NULL, OC_OBSERVABLE);
1179             if (OC_STACK_OK == result)
1180             {
1181                 result = OCPlatform::bindResource(remoteCollectionResourceHandle, resourceHandle);
1182                 if (result != OC_STACK_OK)
1183                 {
1184                     OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \
1185                         "binding remoteCollectionResourceHandle and resourceHandle " \
1186                         "was unsuccessful. result - %d", result);
1187                     OCPlatform::unregisterResource(resourceHandle);
1188                 }
1189
1190                 childHandleList.push_back(resourceHandle);
1191                 OC_LOG(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild : " \
1192                     "binding remoteCollectionResourceHandle and resourceHandle");
1193             }
1194             else
1195             {
1196                 OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onGetJoinedRemoteChild - " \
1197                     "To register remoteCollectionResourceHandle was unsuccessful." \
1198                     " result - %d", result);
1199             }
1200         }
1201
1202         childResourceHandleList[remoteCollectionResourceHandle] = childHandleList;
1203         // this handle list is used to leave group
1204
1205         debugGroupSync();
1206     }
1207
1208     void GroupSynchronization::onLeaveGroup(const HeaderOptions& headerOptions,
1209             const OCRepresentation& rep, const int eCode)
1210     {
1211         if (eCode != OC_STACK_OK)
1212         {
1213             OC_LOG_V(DEBUG, TAG, "GroupSynchronization::onLeaveGroup : error - %d", eCode);
1214             return;
1215         }
1216
1217         OC_LOG(DEBUG, TAG, "GroupSynchronization::onLeaveGroup");
1218         debugGroupSync();
1219     }
1220
1221     void GroupSynchronization::debugGroupSync(void)
1222     {
1223 #ifndef NDEBUG
1224         OC_LOG(DEBUG, TAG, "GroupSynchronization::debugGroupSync");
1225
1226         unsigned int i;
1227         std::map< std::string, OCResourceHandle >::iterator handleIt;
1228         std::map< OCResourceHandle, std::vector< OCResourceHandle > >::iterator childIt;
1229         std::string type;
1230         OCResourceHandle resourceHandle;
1231         std::vector< OCResourceHandle > handleList;
1232         std::shared_ptr< OCResource > resource;
1233
1234         OC_LOG(DEBUG, TAG, "Resource Handle Created by App");
1235         for (i = 0; i < deviceResourceHandleList.size(); i++)
1236         {
1237             resourceHandle = deviceResourceHandleList.at(i);
1238
1239             OC_LOG_V(DEBUG, TAG, "%d. details", i + 1);
1240             OC_LOG_V(DEBUG, TAG, "\turi - %s", OCGetResourceUri(resourceHandle));
1241             OC_LOG_V(DEBUG, TAG, "\tresource type - %s",
1242                 OCGetResourceTypeName(resourceHandle, 0));
1243             OC_LOG_V(DEBUG, TAG, "\tresource interface - %s",
1244                 OCGetResourceInterfaceName(resourceHandle, 0));
1245         }
1246
1247         OC_LOG_V(DEBUG, TAG, "Group Sync Resource Handle List. The number is %d",
1248             groupSyncResourceHandleList.size());
1249
1250         i = 1;
1251         for (handleIt = groupSyncResourceHandleList.begin();
1252                 handleIt != groupSyncResourceHandleList.end(); ++handleIt)
1253         {
1254             type = handleIt->first;
1255             OC_LOG_V(DEBUG, TAG, "\t%d. group sync resource type - %s", i, type.c_str());
1256             OC_LOG(DEBUG, TAG, "\tdetails");
1257
1258             resourceHandle = handleIt->second;
1259             OC_LOG_V(DEBUG, TAG, "\turi - %s",
1260                 OCGetResourceUri(resourceHandle));
1261             OC_LOG_V(DEBUG, TAG, "\tresource type - %s",
1262                 OCGetResourceTypeName(resourceHandle, 0));
1263             OC_LOG_V(DEBUG, TAG, "\tresource interface - %s",
1264                 OCGetResourceInterfaceName(resourceHandle, 0));
1265
1266             i++;
1267         }
1268
1269         OC_LOG_V(DEBUG, TAG, "Copied Remote Group Sync Resource List. The number is %d",
1270             groupSyncResourceList.size());
1271         std::vector< std::string > list;
1272         i = 1;
1273         for (auto resourceIt = groupSyncResourceList.begin();
1274                 resourceIt != groupSyncResourceList.end(); ++resourceIt)
1275         {
1276             type = resourceIt->first;
1277             OC_LOG_V(DEBUG, TAG, "\t%d. group sync resource type - %s", i, type.c_str());
1278             OC_LOG(DEBUG, TAG, "\tdetails");
1279
1280             resource = resourceIt->second;
1281             OC_LOG_V(DEBUG, TAG, "\thost - %s", resource->host().c_str());
1282             OC_LOG_V(DEBUG, TAG, "\turi - %s", resource->uri().c_str());
1283             list = resource->getResourceTypes();
1284             OC_LOG_V(DEBUG, TAG, "\tresource type - %s", list[0].c_str());
1285             list = resource->getResourceInterfaces();
1286             OC_LOG_V(DEBUG, TAG, "\tresource interface - %s", list[0].c_str());
1287             i++;
1288         }
1289
1290         OC_LOG(DEBUG, TAG, "Collection Resource Handle List");
1291         i = 1;
1292         for (handleIt = collectionResourceHandleList.begin();
1293                 handleIt != collectionResourceHandleList.end(); ++handleIt)
1294         {
1295             type = handleIt->first;
1296             OC_LOG_V(DEBUG, TAG, "\t%d. collection resource type - %s", i, type.c_str());
1297             OC_LOG(DEBUG, TAG, "\tdetails");
1298
1299             resourceHandle = handleIt->second;
1300             OC_LOG_V(DEBUG, TAG, "\turi - %s", OCGetResourceUri(resourceHandle));
1301             OC_LOG_V(DEBUG, TAG, "\tresource type - %s",
1302                 OCGetResourceTypeName(resourceHandle, 0));
1303             OC_LOG_V(DEBUG, TAG, "\tresource interface - %s",
1304                 OCGetResourceInterfaceName(resourceHandle, 0));
1305
1306             childIt = childResourceHandleList.find(resourceHandle);
1307             if (childIt != childResourceHandleList.end())
1308             {
1309                 handleList = childIt->second;
1310                 for (unsigned int j = 0; j < handleList.size(); j++)
1311                 {
1312
1313                     OC_LOG_V(DEBUG, TAG, "\t\t%d. child resource details", j + 1);
1314
1315                     resourceHandle = handleList.at(j);
1316                     OC_LOG_V(DEBUG, TAG, "\t\turi - %s",
1317                         OCGetResourceUri(resourceHandle));
1318                     OC_LOG_V(DEBUG, TAG, "\t\tresource type - %s",
1319                         OCGetResourceTypeName(resourceHandle, 0));
1320                     OC_LOG_V(DEBUG, TAG, "\t\tresource interface - %s",
1321                         OCGetResourceInterfaceName(resourceHandle, 0));
1322                 }
1323             }
1324             i++;
1325         }
1326 #endif
1327     }
1328 }