Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sdk / src / GroupSynchronization.h
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 /**
22  * @file
23  *
24  * This file contains the declaration of classes and its members related to
25  * GroupSynchronization.
26  */
27
28 #ifndef __OC_GROUPSYNCHRONIZATION__
29 #define __OC_GROUPSYNCHRONIZATION__
30
31 #include <string>
32 #include <vector>
33 #include <map>
34 #include <cstdlib>
35 #include "OCPlatform.h"
36 #include "OCApi.h"
37
38 using namespace OC;
39
40 namespace OIC
41 {
42 class GroupSynchronization
43 {
44 private:
45
46     std::map< std::string, OCResourceHandle > collectionResourceHandleList;
47     // collection resource handle list
48     std::map< OCResourceHandle, std::vector< OCResourceHandle > > childResourceHandleList;
49
50     std::map< std::string, OCResourceHandle > groupSyncResourceHandleList;
51     // group sync resource handle list
52     std::map< std::string, std::shared_ptr< OCResource > > groupSyncResourceList;
53     // remote group sync resource list
54
55     std::vector< OCResourceHandle > deviceResourceHandleList; // these cannot be removed.
56     OCResourceHandle deviceResourceHandle;
57
58     OCResourceHandle collectionResourceHandle; // collection handle
59     std::shared_ptr< OCResource > remoteCollectionResource;
60
61     FindCallback findCallback;
62
63     std::vector< std::shared_ptr< OCResource > > foundGroupResourceList;
64
65     std::mutex foundGroupMutex;
66 //    std::mutex groupSyncMutex;
67
68     std::shared_ptr< OCResourceRequest > resourceRequest; // this is used for slow response
69
70     static GroupSynchronization* groupSyncnstance;
71     static bool bIsFinding;
72
73     GroupSynchronization()
74     {
75         collectionResourceHandleList.clear();
76         childResourceHandleList.clear();
77         groupSyncResourceHandleList.clear();
78         groupSyncResourceList.clear();
79         deviceResourceHandleList.clear();
80
81         deviceResourceHandle = NULL;
82         collectionResourceHandle = NULL;
83         remoteCollectionResource = NULL;
84         findCallback = NULL;
85     }
86     ;
87
88     ~GroupSynchronization()
89     {
90         std::map< std::string, OCResourceHandle >::iterator handleIt;
91         for (handleIt = collectionResourceHandleList.begin();
92                 handleIt != collectionResourceHandleList.end(); ++handleIt)
93         {
94             deleteGroup(handleIt->first);
95         }
96     }
97     ;
98
99 public:
100
101     static GroupSynchronization* getInstance();
102     void deleteInstance();
103
104     OCStackResult findGroup(std::vector< std::string > collectionResourceTypes,
105             FindCallback callback);
106     OCStackResult createGroup(std::string collectionResourceType);
107     OCStackResult joinGroup(std::string collectionResourceTyps,
108             OCResourceHandle resourceHandle);
109     OCStackResult joinGroup(const std::shared_ptr< OCResource > resource,
110             OCResourceHandle resourceHandle);
111     OCStackResult leaveGroup(std::string collectionResourceType,
112             OCResourceHandle resourceHandle);
113     OCStackResult leaveGroup(const std::shared_ptr< OCResource > resource,
114                 std::string collectionResourceType,
115                 OCResourceHandle resourceHandle);
116     void deleteGroup(std::string collectionResourceType);
117
118     std::map< std::string, OCResourceHandle > getGroupList();
119
120 private:
121
122     OCEntityHandlerResult groupEntityHandler(
123             const std::shared_ptr< OCResourceRequest > request);
124
125     void onFindGroup(std::shared_ptr< OCResource > resource);
126     void onJoinGroup(const HeaderOptions& headerOptions, const OCRepresentation& rep,
127             const int eCode);
128     void onFindResource(std::shared_ptr< OCResource > resource);
129     void onGetJoinedRemoteChild(const HeaderOptions& headerOptions, const OCRepresentation& rep,
130             const int eCode);
131     void onLeaveGroup(const HeaderOptions& headerOptions, const OCRepresentation& rep,
132             const int eCode);
133 //    void onSubscribePresence (OCStackResult result,
134 //        const unsigned int nonce/*, std::string resourceType, std::string host*/);
135
136     void checkFindGroup(void);
137     bool IsSameGroup(std::shared_ptr< OCResource > resource);
138     void saveGroup(std::shared_ptr< OCResource > resource);
139
140     void debugGroupSync(void);
141
142 };
143 }
144 #endif    // __OC_GROUPSYNCHRONIZATION__