Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / examples / groupserver.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 "OCPlatform.h"
22 #include "OCApi.h"
23
24 #include <functional>
25 #include <pthread.h>
26 #include <iostream>
27
28 using namespace std;
29 using namespace OC;
30
31 namespace PH = std::placeholders;
32
33 OCResourceHandle resourceHandle;
34 std::vector< OCResourceHandle > resourceHandleVector;
35
36 static void printUsage()
37 {
38     std::cout<<"Usage: groupclient <0|1>\n";
39     std::cout<<"ConnectivityType: Default \n";
40     std::cout<<"ConnectivityType 0: IP\n";
41 }
42 void foundResource(std::shared_ptr< OCResource > resource)
43 {
44
45     std::string resourceURI;
46     std::string hostAddress;
47
48     try
49     {
50         cout << "FOUND RESOURCE" << endl;
51
52         if (resource)
53         {
54             resourceURI = resource->uri();
55             hostAddress = resource->host();
56
57             cout << "\tResource URI : " << resourceURI << endl;
58             cout << "\tResource Host : " << hostAddress << endl;
59             cout << "\tResource Interfaces : " << resource->getResourceInterfaces().front() << endl;
60             cout << "\tResource Type : " << resource->getResourceTypes().front() << endl;
61             if (resourceURI == "/a/light" || resourceURI == "/a/fan")
62             {
63                 OCResourceHandle foundResourceHandle;
64                 OCStackResult result = OCPlatform::registerResource(foundResourceHandle, resource);
65                 cout << "\tresource registed!" << endl;
66                 if (result == OC_STACK_OK)
67                 {
68                     OCPlatform::bindResource(resourceHandle, foundResourceHandle);
69                     resourceHandleVector.push_back(foundResourceHandle);
70                 }
71                 else
72                 {
73                     cout << "\tresource Error!" << endl;
74                 }
75             }
76         }
77     }
78     catch (std::exception& e)
79     {
80         std::cout << "Exception in foundResource:"<< e.what() << std::endl;
81     }
82
83 }
84
85 int main(int argc, char* argv[])
86 {
87     ostringstream requestURI;
88
89     OCConnectivityType connectivityType = CT_ADAPTER_IP;
90
91     if(argc == 2)
92     {
93         try
94         {
95             std::size_t inputValLen;
96             int optionSelected = stoi(argv[1], &inputValLen);
97
98             if(inputValLen == strlen(argv[1]))
99             {
100                 if(optionSelected == 0)
101                 {
102                     std::cout << "Using IP."<< std::endl;
103                     connectivityType = CT_ADAPTER_IP;
104                 }
105                 else
106                 {
107                     std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
108                 }
109             }
110             else
111             {
112                 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
113             }
114         }
115         catch(exception& e)
116         {
117             std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
118         }
119     }
120     else
121     {
122         printUsage();
123
124     }
125
126     PlatformConfig config
127     { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
128
129     try
130     {
131         string resourceURI = "/core/a/collection";
132         string resourceTypeName = "a.collection";
133         string resourceInterface = BATCH_INTERFACE;
134         OCPlatform::Configure(config);
135
136         // EntityHandler cb = std::bind(, PH::_1, PH::_2);
137
138         OCPlatform::registerResource(resourceHandle, resourceURI, resourceTypeName,
139                 resourceInterface,
140                 NULL,
141                 //&entityHandler, // entityHandler
142                 OC_DISCOVERABLE);
143
144         cout << "registerResource is called." << endl;
145
146         requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.light";
147
148         OCPlatform::findResource("", requestURI.str(),
149                                  connectivityType, &foundResource);
150
151         OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
152         OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
153
154         int selectedMenu;
155         bool isRun = true;
156         while (isRun)
157         {
158             cout << endl << "0 :: Quit 1 :: UNREGISTER RESOURCES\n" << endl;
159             std::cin >> selectedMenu;
160
161             switch(selectedMenu)
162             {
163             case 0:
164                 isRun = false;
165                 break;
166             case 1:
167                 std::cout << "Unregistering resources" << std::endl;
168                 for (unsigned int i = 0; i < resourceHandleVector.size(); ++i)
169                 {
170                     OCPlatform::unregisterResource(resourceHandleVector.at(i));
171                 }
172                 break;
173             default:
174                 cout << "Invalid option" << endl;
175             }
176
177         }
178     }
179     catch (OCException& e)
180     {
181         oclog() << "Exception in main: "<< e.what();
182     }
183
184     return 0;
185 }
186