Imported Upstream version 0.9.1
[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 void foundResource(std::shared_ptr< OCResource > resource)
37 {
38
39     std::string resourceURI;
40     std::string hostAddress;
41
42     try
43     {
44         cout << "FOUND RESOURCE" << endl;
45
46         if (resource)
47         {
48             resourceURI = resource->uri();
49             hostAddress = resource->host();
50
51             cout << "\tResource URI : " << resourceURI << endl;
52             cout << "\tResource Host : " << hostAddress << endl;
53             cout << "\tResource Interfaces : " << resource->getResourceInterfaces().front() << endl;
54             cout << "\tResource Type : " << resource->getResourceTypes().front() << endl;
55             if (resourceURI == "/a/light" || resourceURI == "/a/fan")
56             {
57                 OCResourceHandle foundResourceHandle;
58                 OCStackResult result = OCPlatform::registerResource(foundResourceHandle, resource);
59                 cout << "\tresource registed!" << endl;
60                 if (result == OC_STACK_OK)
61                 {
62                     OCPlatform::bindResource(resourceHandle, foundResourceHandle);
63                     resourceHandleVector.push_back(foundResourceHandle);
64                 }
65                 else
66                 {
67                     cout << "\tresource Error!" << endl;
68                 }
69             }
70         }
71     }
72     catch (std::exception& e)
73     {
74         std::cout << "Exception in foundResource:"<< e.what() << std::endl;
75     }
76
77 }
78
79 int main(int argc, char* argv[])
80 {
81     ostringstream requestURI;
82
83     PlatformConfig config
84     { OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
85
86     try
87     {
88         string resourceURI = "/core/a/collection";
89         string resourceTypeName = "a.collection";
90         string resourceInterface = BATCH_INTERFACE;
91         OCPlatform::Configure(config);
92
93         // EntityHandler cb = std::bind(, PH::_1, PH::_2);
94
95         OCPlatform::registerResource(resourceHandle, resourceURI, resourceTypeName,
96                 resourceInterface,
97                 NULL,
98                 //&entityHandler, // entityHandler
99                 OC_DISCOVERABLE);
100
101         cout << "registerResource is called." << endl;
102
103         requestURI << OC_MULTICAST_DISCOVERY_URI << "?rt=core.light";
104
105         OCPlatform::findResource("", requestURI.str(),
106                                  OC_ALL, &foundResource);
107
108         OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
109         OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
110
111         int selectedMenu;
112         bool isRun = true;
113         while (isRun)
114         {
115             cout << endl << "0 :: Quit 1 :: UNREGISTER RESOURCES\n" << endl;
116             std::cin >> selectedMenu;
117
118             switch(selectedMenu)
119             {
120             case 0:
121                 isRun = false;
122                 break;
123             case 1:
124                 std::cout << "Unregistering resources" << std::endl;
125                 for (unsigned int i = 0; i < resourceHandleVector.size(); ++i)
126                 {
127                     OCPlatform::unregisterResource(resourceHandleVector.at(i));
128                 }
129                 break;
130             default:
131                 cout << "Invalid option" << endl;
132             }
133
134         }
135     }
136     catch (OCException& e)
137     {
138         oclog() << "Exception in main: "<< e.what();
139     }
140
141     return 0;
142 }
143