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