Added support for multicast presence
[platform/upstream/iotivity.git] / resource / examples / presenceclient.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 // PresenceClient.cpp : A client example for presence notification
22 //
23 #include <string>
24 #include <cstdlib>
25 #include <pthread.h>
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28
29 using namespace OC;
30
31 std::shared_ptr<OCResource> curResource;
32
33 // Callback to presence
34 void presenceHandler(OCStackResult result, const unsigned int nonce)
35 {
36     switch(result)
37     {
38         case OC_STACK_OK:
39             std::cout << "Nonce# " << nonce << std::endl;
40             break;
41         case OC_STACK_PRESENCE_STOPPED:
42             std::cout << "Presence Stopped\n";
43             break;
44         case OC_STACK_PRESENCE_DO_NOT_HANDLE:
45             std::cout << "Presence do not handle\n";
46             break;
47         default:
48             std::cout << "Error\n";
49             break;
50     }
51 }
52
53 // Callback to found resources
54 void foundResource(std::shared_ptr<OCResource> resource)
55 {
56     if(curResource)
57     {
58         std::cout << "Found another resource, ignoring"<<std::endl;
59     }
60
61     std::string resourceURI;
62     std::string hostAddress;
63     try
64     {
65         // Do some operations with resource object.
66         if(resource)
67         {
68             std::cout<<"DISCOVERED Resource:"<<std::endl;
69             // Get the resource URI
70             resourceURI = resource->uri();
71             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
72
73             // Get the resource host address
74             hostAddress = resource->host();
75             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
76
77             // Get the resource types
78             std::cout << "\tList of resource types: " << std::endl;
79             for(auto &resourceTypes : resource->getResourceTypes())
80             {
81                 std::cout << "\t\t" << resourceTypes << std::endl;
82             }
83
84             // Get the resource interfaces
85             std::cout << "\tList of resource interfaces: " << std::endl;
86             for(auto &resourceInterfaces : resource->getResourceInterfaces())
87             {
88                 std::cout << "\t\t" << resourceInterfaces << std::endl;
89             }
90
91             if(resourceURI == "/a/light")
92             {
93                 curResource = resource;
94                 OCPlatform::OCPresenceHandle presenceHandle;
95                 OCPlatform::subscribePresence(presenceHandle, hostAddress, &presenceHandler);
96                 std::cout<< "subscribing for unicast presence... " <<std::endl;
97             }
98         }
99         else
100         {
101             // Resource is invalid
102             std::cout << "Resource is invalid" << std::endl;
103         }
104
105     }
106     catch(std::exception& e)
107     {
108         //log(e.what());
109     }
110 }
111
112 void PrintUsage()
113 {
114     std::cout << std::endl;
115     std::cout << "Usage : presenceclient <isMulticastAddress>\n";
116     std::cout << "   0 - unicast subcribe presence\n";
117     std::cout << "   1 - multicast subscribe presence\n\n";
118 }
119
120 // 0 - unicast subcribe presence
121 // 1 - multicast subscribe presence
122 int isMulticastAddress = 0;
123
124 int main(int argc, char* argv[1])
125 {
126     PrintUsage();
127
128     if (argc == 1)
129     {
130         isMulticastAddress = 0;
131     }
132     else if (argc == 2)
133     {
134         int value = atoi(argv[1]);
135         if (value == 1)
136             isMulticastAddress = 1;
137         else
138             isMulticastAddress = 0;
139     }
140     else
141     {
142         return -1;
143     }
144
145     // Create PlatformConfig object
146     PlatformConfig cfg {
147         OC::ServiceType::InProc,
148         OC::ModeType::Client,
149         "0.0.0.0",
150         0,
151         OC::QualityOfService::LowQos
152     };
153
154     try
155     {
156         OCPlatform::Configure(cfg);
157
158         if(isMulticastAddress)
159         {
160             OCPlatform::OCPresenceHandle presenceHandle;
161             OCPlatform::subscribePresence(presenceHandle, OC_MULTICAST_IP, presenceHandler);
162             std::cout<< "subscribing for multicast presence... " <<std::endl;
163         }
164         else
165         {
166             // Find all resources
167             OCPlatform::findResource("", "coap://224.0.1.187/oc/core", &foundResource);
168             std::cout<< "Finding Resource... " <<std::endl;
169         }
170
171         while(true)
172         {
173             // some operations
174         }
175
176     }catch(OCException& e)
177     {
178         //log(e.what());
179     }
180
181     return 0;
182 }
183