Modifying version number for building on tizen 3.0
[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
79     // Create PlatformConfig object
80     PlatformConfig cfg
81     { OC::ServiceType::InProc, OC::ModeType::Both/*OC::ModeType::Server*/, "0.0.0.0", 0,
82             OC::QualityOfService::LowQos };
83
84     OCPlatform::Configure(cfg);
85     gThingManager = new ThingsManager();
86
87     int selectedMenu;
88     OCStackResult result;
89
90     try
91     {
92         while (true)
93         {
94             // some operations
95             cout << "(1) CREATE GROUP " << endl;
96             cout << "(11) FIND MUSIC PLAYER & JOIN GROUP | (12) FIND SPEAKER & JOIN GROUP" << endl;
97             cout << "(21) LEAVE GROUP - MUSIC PLAYER  | (22) LEAVE GROUP - SPEAKER" << endl;
98             cout << "(31) DELETE GROUP " << endl;
99
100             std::cin >> selectedMenu;
101
102             if (selectedMenu == 1)
103             {
104                 result = gThingManager->createGroup(collectionResourceType);
105                 if (OC_STACK_OK == result)
106                 {
107                     cout << "Group creation was successful\n";
108                 }
109                 else
110                 {
111                     cout << "Group creation was unsuccessful\n";
112                 }
113             }
114             else if (selectedMenu == 11)
115             {
116                 result = OCPlatform::findResource("",
117                         "coap://224.0.1.187/oc/core?rt=core.musicplayer", onFindResource);
118                 if (OC_STACK_OK == result)
119                 {
120                     cout << "Finding music player was successful\n";
121                 }
122                 else
123                 {
124                     cout << "Finding music player was unsuccessful\n";
125                 }
126             }
127             else if (selectedMenu == 12)
128             {
129                 result = OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.speaker",
130                         onFindResource);
131                 if (OC_STACK_OK == result)
132                 {
133                     cout << "Finding speaker was successful\n";
134                 }
135                 else
136                 {
137                     cout << "Finding speaker was unsuccessful\n";
138                 }
139             }
140             else if (selectedMenu == 21)
141             {
142                 std::vector< OCResourceHandle >::iterator It;
143                 OCResourceHandle resourceHandle;
144                 for (It = gResourceHandleList.begin(); It != gResourceHandleList.end(); ++It)
145                 {
146                     resourceHandle = (*It);
147                     std::string mpType = "core.musicplayer";
148                     std::string type = OCGetResourceTypeName(resourceHandle, 0);
149                     if (0 == mpType.compare(type))
150                     {
151                         result = gThingManager->leaveGroup(collectionResourceType, resourceHandle);
152                         if (OC_STACK_OK == result)
153                         {
154                             cout << "Leaving group of music player was successful\n";
155                         }
156                         else
157                         {
158                             cout << "Leaving group of music player was unsuccessful\n";
159                         }
160                         break;
161                     }
162                 }
163
164                 gResourceHandleList.erase(It);
165                 result = OCPlatform::unregisterResource(resourceHandle);
166                 if (OC_STACK_OK == result)
167                 {
168                     cout << "Unregistering music player was successful\n";
169                 }
170                 else
171                 {
172                     cout << "Unregistering music player was unsuccessful\n";
173                 }
174             }
175             else if (selectedMenu == 22)
176             {
177                 std::vector< OCResourceHandle >::iterator It;
178                 OCResourceHandle resourceHandle;
179                 for (It = gResourceHandleList.begin(); It != gResourceHandleList.end(); ++It)
180                 {
181                     resourceHandle = (*It);
182                     std::string mpType = "core.speaker";
183                     std::string type = OCGetResourceTypeName(resourceHandle, 0);
184                     if (0 == mpType.compare(type))
185                     {
186                         result = gThingManager->leaveGroup(collectionResourceType, resourceHandle);
187                         if (OC_STACK_OK == result)
188                         {
189                             cout << "Leaving group of speaker was successful\n";
190                         }
191                         else
192                         {
193                             cout << "Leaving group of speaker was unsuccessful\n";
194                         }
195                         break;
196                     }
197                 }
198
199                 gResourceHandleList.erase(It);
200                 result = OCPlatform::unregisterResource(resourceHandle);
201                 if (OC_STACK_OK == result)
202                 {
203                     cout << "Unregistering speaker was successful\n";
204                 }
205                 else
206                 {
207                     cout << "Unregistering speaker was unsuccessful\n";
208                 }
209             }
210             else if (selectedMenu == 31)
211             {
212                 gThingManager->deleteGroup(collectionResourceType);
213             }
214         }
215     }
216     catch (OCException& e)
217     {
218         //log(e.what());
219     }
220
221     return 0;
222 }
223