Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / groupsyncaction / group.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 <string>
22 #include <cstdlib>
23 #include <pthread.h>
24 #include "OCPlatform.h"
25 #include "OCApi.h"
26 #include "ThingsManager.h"
27
28 using namespace OC;
29 using namespace OIC;
30
31 static ThingsManager* gThingManager = NULL;
32
33 static std::vector< OCResourceHandle > gResourceHandleList;
34
35 static std::string collectionResourceType = "core.group";
36
37 void onFindResource(std::shared_ptr< OCResource > resource)
38 {
39     cout << "onFindResource" << endl;
40
41     try
42     {
43         if (resource)
44         {
45             OCResourceHandle resourceHandle;
46             OCStackResult result = OCPlatform::registerResource(resourceHandle, resource);
47             if (OC_STACK_OK == result)
48             {
49                 cout << "onFindResource : Resource creation was successful\n";
50             }
51             else
52             {
53                 cout << "onFindResource : Resource creation was unsuccessful\n";
54                 return;
55             }
56
57             result = gThingManager->joinGroup(collectionResourceType, resourceHandle);
58             if (OC_STACK_OK == result)
59             {
60                 cout << "onFindResource : Joining group was successful\n";
61             }
62             else
63             {
64                 cout << "onFindResource : Joining group was unsuccessful\n";
65
66                 OCPlatform::unregisterResource(resourceHandle);
67                 return;
68             }
69
70             gResourceHandleList.push_back(resourceHandle);
71         }
72         else
73         {
74             cout << "onFindResource : There is no found resource." << endl;
75         }
76     }catch (std::exception& e)
77     {
78         std::cout << "Exception: " << e.what() << std::endl;
79     }
80 }
81
82 int main(int argc, char* argv[])
83 {
84     // Create PlatformConfig object
85     PlatformConfig cfg
86     { OC::ServiceType::InProc, OC::ModeType::Both/*OC::ModeType::Server*/, "0.0.0.0", 0,
87             OC::QualityOfService::LowQos };
88
89     OCPlatform::Configure(cfg);
90     gThingManager = new ThingsManager();
91
92     int selectedMenu;
93     OCStackResult result;
94
95     try
96     {
97         while (true)
98         {
99             // some operations
100             cout << "(1) CREATE GROUP " << endl;
101             cout << "(11) FIND MUSIC PLAYER & JOIN GROUP | (12) FIND SPEAKER & JOIN GROUP" << endl;
102             cout << "(21) LEAVE GROUP - MUSIC PLAYER  | (22) LEAVE GROUP - SPEAKER" << endl;
103             cout << "(31) DELETE GROUP " << endl;
104
105             std::cin >> selectedMenu;
106
107             if (selectedMenu == 1)
108             {
109                 result = gThingManager->createGroup(collectionResourceType);
110                 if (OC_STACK_OK == result)
111                 {
112                     cout << "Group creation was successful\n";
113                 }
114                 else
115                 {
116                     cout << "Group creation was unsuccessful\n";
117                 }
118             }
119             else if (selectedMenu == 11)
120             {
121                 ostringstream query;
122                 query << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.musicplayer";
123
124                 cout << query.str() << endl;
125                 result = OCPlatform::findResource("",
126                             query.str(),
127                             CT_DEFAULT,
128                             onFindResource);
129
130                 if (OC_STACK_OK == result)
131                 {
132                     cout << "Finding music player was successful\n";
133                 }
134                 else
135                 {
136                     cout << "Finding music player was unsuccessful\n";
137                 }
138             }
139             else if (selectedMenu == 12)
140             {
141                 ostringstream query;
142                 query << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.speaker";
143                 result = OCPlatform::findResource("",
144                             query.str(),
145                             CT_DEFAULT,
146                             onFindResource);
147
148                 if (OC_STACK_OK == result)
149                 {
150                     cout << "Finding speaker was successful\n";
151                 }
152                 else
153                 {
154                     cout << "Finding speaker was unsuccessful\n";
155                 }
156             }
157             else if (selectedMenu == 21)
158             {
159                 std::vector< OCResourceHandle >::iterator It;
160                 OCResourceHandle resourceHandle;
161                 for (It = gResourceHandleList.begin(); It != gResourceHandleList.end(); ++It)
162                 {
163                     resourceHandle = (*It);
164                     std::string mpType = "core.musicplayer";
165                     std::string type = OCGetResourceTypeName(resourceHandle, 0);
166                     if (0 == mpType.compare(type))
167                     {
168                         result = gThingManager->leaveGroup(collectionResourceType, resourceHandle);
169                         if (OC_STACK_OK == result)
170                         {
171                             cout << "Leaving group of music player was successful\n";
172                         }
173                         else
174                         {
175                             cout << "Leaving group of music player was unsuccessful\n";
176                         }
177                         break;
178                     }
179                 }
180
181                 if(It == gResourceHandleList.end())
182                     continue;
183
184                 gResourceHandleList.erase(It);
185                 result = OCPlatform::unregisterResource(resourceHandle);
186                 if (OC_STACK_OK == result)
187                 {
188                     cout << "Unregistering music player was successful\n";
189                 }
190                 else
191                 {
192                     cout << "Unregistering music player was unsuccessful\n";
193                 }
194             }
195             else if (selectedMenu == 22)
196             {
197                 std::vector< OCResourceHandle >::iterator It;
198                 OCResourceHandle resourceHandle;
199                 for (It = gResourceHandleList.begin(); It != gResourceHandleList.end(); ++It)
200                 {
201                     resourceHandle = (*It);
202                     std::string mpType = "core.speaker";
203                     std::string type = OCGetResourceTypeName(resourceHandle, 0);
204                     if (0 == mpType.compare(type))
205                     {
206                         result = gThingManager->leaveGroup(collectionResourceType, resourceHandle);
207                         if (OC_STACK_OK == result)
208                         {
209                             cout << "Leaving group of speaker was successful\n";
210                         }
211                         else
212                         {
213                             cout << "Leaving group of speaker was unsuccessful\n";
214                         }
215                         break;
216                     }
217                 }
218
219                 if(It == gResourceHandleList.end())
220                     continue;
221
222                 gResourceHandleList.erase(It);
223                 result = OCPlatform::unregisterResource(resourceHandle);
224                 if (OC_STACK_OK == result)
225                 {
226                     cout << "Unregistering speaker was successful\n";
227                 }
228                 else
229                 {
230                     cout << "Unregistering speaker was unsuccessful\n";
231                 }
232             }
233             else if (selectedMenu == 31)
234             {
235                 gThingManager->deleteGroup(collectionResourceType);
236             }
237         }
238     }
239     catch (OCException& e)
240     {
241         std::cout << "Exception: " << e.what() << std::endl;
242     }
243
244     return 0;
245 }
246