1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 /// This sample provides the way to create cloud sample
27 #include <condition_variable>
35 #include "ocpayload.h"
38 #include <OCPlatform.h>
43 #define DEFAULT_MQ_BROKER_URI "/oic/ps"
44 #define maxSequenceNumber 0xFFFFFF
46 OC::OCResource::Ptr g_mqBrokerResource = nullptr;
47 OC::OCResource::Ptr g_mqSelectedTopicResource = nullptr;
49 vector<shared_ptr<OC::OCResource> > gTopicList;
51 void printRepresentation(OCRepresentation rep)
53 for (auto itr = rep.begin(); itr != rep.end(); ++itr)
55 cout << "\t" << itr->attrname() << ":\t" << itr->getValueToString() << endl;
56 if (itr->type() == AttributeType::Vector)
58 switch (itr->base_type())
60 case AttributeType::OCRepresentation:
61 for (auto itr2 : (*itr).getValue<vector<OCRepresentation> >())
63 printRepresentation(itr2);
67 case AttributeType::Integer:
68 for (auto itr2 : (*itr).getValue<vector<int> >())
70 cout << "\t\t" << itr2 << endl;
74 case AttributeType::String:
75 for (auto itr2 : (*itr).getValue<vector<string> >())
77 cout << "\t\t" << itr2 << endl;
82 cout << "Unhandled base type " << itr->base_type() << endl;
86 else if (itr->type() == AttributeType::OCRepresentation)
88 printRepresentation((*itr).getValue<OCRepresentation>());
93 ////////////////////////////////////////Subscriber Sample
94 void subscribeCB(const HeaderOptions &,
95 const OCRepresentation &rep, const int eCode, const int sequenceNumber)
99 if (eCode == OC_STACK_OK && sequenceNumber != maxSequenceNumber + 1)
101 if (sequenceNumber == OC_OBSERVE_REGISTER)
103 cout << "Observe registration action is successful" << endl;
106 cout << "OBSERVE RESULT:" << endl;
107 printRepresentation(rep);
111 if (eCode == OC_STACK_OK)
113 cout << "Observe registration failed or de-registration action failed/succeeded" << endl;
117 cout << "onObserve Response error: " << eCode << endl;
124 cout << "Exception: " << e.what() << " in onObserve" << endl;
128 void discoverTopicCB(const int ecode, const string &brokerUri,
129 shared_ptr<OC::OCResource> topic)
131 cout << "Topic discovered from " << brokerUri << " code: " << ecode << endl;
132 gTopicList.push_back(topic);
133 cout << "Topic [" << gTopicList.size() - 1 << "] " << topic->uri() << " discovered" << endl;
135 ////////////////////////////////////////Subscriber Sample
137 condition_variable g_callbackLock;
139 string g_accesstoken;
141 void handleLoginoutCB(const HeaderOptions &,
142 const OCRepresentation &rep, const int ecode)
144 cout << "Auth response received code: " << ecode << endl;
146 if (rep.getPayload() != NULL)
148 printRepresentation(rep);
153 g_accesstoken = rep.getValueToString("accesstoken");
155 g_uid = rep.getValueToString("uid");
158 g_callbackLock.notify_all();
161 static FILE *client_open(const char * /*path*/, const char *mode)
163 return fopen("./mq_subscriber.dat", mode);
166 int main(int argc, char *argv[])
168 if (argc != 4 && argc != 5)
170 cout << "Put \"[host-ipaddress:port] [authprovider] [authcode]\" for sign-up and sign-in"
172 cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in" <<
177 OCPersistentStorage ps{ client_open, fread, fwrite, fclose, unlink };
183 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
184 0, // Uses randomly available port
185 QualityOfService::LowQos,
189 OCPlatform::Configure(cfg);
191 OCStackResult result = OC_STACK_ERROR;
193 string host = "coap+tcp://";
196 OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(host,
200 unique_lock<mutex> lock(blocker);
204 accountMgr->signIn(argv[2], argv[3], &handleLoginoutCB);
205 g_callbackLock.wait(lock);
209 accountMgr->signUp(argv[2], argv[3], &handleLoginoutCB);
210 g_callbackLock.wait(lock);
211 accountMgr->signIn(g_uid, g_accesstoken, &handleLoginoutCB);
212 g_callbackLock.wait(lock);
215 // MQ broker resource
216 g_mqBrokerResource = OCPlatform::constructResourceObject(host, DEFAULT_MQ_BROKER_URI,
217 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4), false,
218 { string("oic.wk.ps") }, { string(DEFAULT_INTERFACE) });
220 cout << "===Message Queue subscriber sample===" << endl;
221 cout << "PUT 0 to discover all topics" << endl;
222 cout << "PUT 1 to discover type based topics" << endl;
223 cout << "PUT 2 to select topic for subscribing or unsubscribing" << endl;
224 cout << "PUT 3 to subscribe to selected topic" << endl;
225 cout << "PUT 4 to unsubscribe to selected topic" << endl;
235 QueryParamsMap query;
236 OCRepresentation rep;
242 cout << "Discovering topics" << endl;
243 result = g_mqBrokerResource->discoveryMQTopics(query, &discoverTopicCB, QualityOfService::LowQos);
248 cout << "Put topic type to discover: ";
251 result = g_mqBrokerResource->discoveryMQTopics(query, &discoverTopicCB, QualityOfService::LowQos);
255 cout << "Put discovered topic index to select: ";
258 int index = atoi(cmd.c_str());
259 if(index < 0 || (unsigned int) index >= gTopicList.size())
261 cout << "invalid topic index selected" << endl;
265 g_mqSelectedTopicResource = gTopicList[index];
266 cout << g_mqSelectedTopicResource->uri() << " selected" << endl;
271 if(g_mqSelectedTopicResource == nullptr)
273 cout << "Topic is not selected." << endl;
277 cout << "Subscribe to selected topic" << endl;
278 result = g_mqSelectedTopicResource->subscribeMQTopic(ObserveType::Observe, query, &subscribeCB,
279 QualityOfService::LowQos);
283 cout << "Unsubscribe to selected topic" << endl;
284 result = g_mqSelectedTopicResource->unsubscribeMQTopic(QualityOfService::LowQos);
292 if (result != OC_STACK_OK)
294 cout << "Error, return code: " << result << endl;
299 cout << "Precondition failed." << endl;