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