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"
45 OC::OCResource::Ptr g_mqBrokerResource = nullptr;
46 OC::OCResource::Ptr g_mqSelectedTopicResource = nullptr;
48 vector<shared_ptr<OC::OCResource> > gTopicList;
50 void printRepresentation(OCRepresentation rep)
52 for (auto itr = rep.begin(); itr != rep.end(); ++itr)
54 cout << "\t" << itr->attrname() << ":\t" << itr->getValueToString() << endl;
55 if (itr->type() == AttributeType::Vector)
57 switch (itr->base_type())
59 case AttributeType::OCRepresentation:
60 for (auto itr2 : (*itr).getValue<vector<OCRepresentation> >())
62 printRepresentation(itr2);
66 case AttributeType::Integer:
67 for (auto itr2 : (*itr).getValue<vector<int> >())
69 cout << "\t\t" << itr2 << endl;
73 case AttributeType::String:
74 for (auto itr2 : (*itr).getValue<vector<string> >())
76 cout << "\t\t" << itr2 << endl;
81 cout << "Unhandled base type " << itr->base_type() << endl;
85 else if (itr->type() == AttributeType::OCRepresentation)
87 printRepresentation((*itr).getValue<OCRepresentation>());
92 ////////////////////////////////////////Publisher Sample
94 void createTopicCB(const int ecode, const string &originUri,
95 shared_ptr<OC::OCResource> topic)
97 cout << "Create topic response received, code: " << ecode << endl;
99 if (ecode == OCStackResult::OC_STACK_RESOURCE_CREATED)
101 cout << "Created topic : " << topic->uri() << endl;
105 cout << "Topic creation failed : " << originUri << endl;
109 void publishMessageCB(const HeaderOptions &, const OCRepresentation &, const int eCode)
111 cout << "Publish message response received, code: " << eCode << endl;
114 void discoverTopicCB(const int ecode, const string &, shared_ptr<OC::OCResource> topic)
116 cout << "Topic discovered code: " << ecode << endl;
117 gTopicList.push_back(topic);
118 cout << "Topic [" << gTopicList.size() - 1 << "] " << topic->uri() << " discovered" << endl;
120 ////////////////////////////////////////End of Publisher Sample
122 condition_variable g_callbackLock;
124 string g_accesstoken;
126 void handleLoginoutCB(const HeaderOptions &,
127 const OCRepresentation &rep, const int ecode)
129 cout << "Auth response received code: " << ecode << endl;
131 if (rep.getPayload() != NULL)
133 printRepresentation(rep);
138 g_accesstoken = rep.getValueToString("accesstoken");
140 g_uid = rep.getValueToString("uid");
143 g_callbackLock.notify_all();
146 static FILE *client_open(const char * /*path*/, const char *mode)
148 return fopen("./mq_publisher.dat", mode);
151 int main(int argc, char *argv[])
153 if (argc != 4 && argc != 5)
155 cout << "Put \"[host-ipaddress:port] [authprovider] [authcode]\" for sign-up and sign-in"
157 cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in" <<
162 OCPersistentStorage ps{ client_open, fread, fwrite, fclose, unlink };
168 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
169 0, // Uses randomly available port
170 QualityOfService::LowQos,
174 OCPlatform::Configure(cfg);
176 OCStackResult result = OC_STACK_ERROR;
178 string host = "coap+tcp://";
181 OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(host,
185 unique_lock<mutex> lock(blocker);
189 accountMgr->signIn(argv[2], argv[3], &handleLoginoutCB);
190 g_callbackLock.wait(lock);
194 accountMgr->signUp(argv[2], argv[3], &handleLoginoutCB);
195 g_callbackLock.wait(lock);
196 accountMgr->signIn(g_uid, g_accesstoken, &handleLoginoutCB);
197 g_callbackLock.wait(lock);
200 // MQ broker resource
201 g_mqBrokerResource = OCPlatform::constructResourceObject(host, DEFAULT_MQ_BROKER_URI,
202 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4), false,
203 { string("oic.wk.ps") }, { string(DEFAULT_INTERFACE) });
205 cout << "===Message Queue publisher sample===" << endl;
206 cout << "PUT 0 to discover all topics" << endl;
207 cout << "PUT 1 to discover type based topics" << endl;
208 cout << "PUT 2 to select topic index for publishing data" << endl;
209 cout << "PUT 3 to publish data to selected topic" << endl;
210 cout << "PUT 4 to create topic" << endl;
211 cout << "PUT 5 to create type based topic" << endl;
219 QueryParamsMap query;
220 OCRepresentation rep;
227 cout << "Discovering topics" << endl;
228 result = g_mqBrokerResource->discoveryMQTopics(query, &discoverTopicCB, QualityOfService::LowQos);
233 cout << "Put topic type to discover: ";
236 result = g_mqBrokerResource->discoveryMQTopics(query, &discoverTopicCB, QualityOfService::LowQos);
240 cout << "Put discovered topic index to select: ";
243 int index = atoi(cmd.c_str());
244 if(index < 0 || (unsigned int) index >= gTopicList.size())
246 cout << "invalid topic index selected" << endl;
250 g_mqSelectedTopicResource = gTopicList[index];
251 cout << g_mqSelectedTopicResource->uri() << " selected" << endl;
256 if(g_mqSelectedTopicResource == nullptr)
258 cout << "Topic is not selected." << endl;
262 cout << "Put message to selected topic: ";
264 rep["message"] = cmd;
265 result = g_mqSelectedTopicResource->publishMQTopic(rep, query, &publishMessageCB,
266 QualityOfService::LowQos);
270 cout << "Put topic uri to create: ";
272 result = g_mqBrokerResource->createMQTopic(rep, cmd, query, &createTopicCB,
273 QualityOfService::LowQos);
277 cout << "Put topic uri to create: ";
279 cout << "Put topic type: ";
281 query["rt"] = topicType;
282 result = g_mqBrokerResource->createMQTopic(rep, cmd, query, &createTopicCB,
283 QualityOfService::LowQos);
291 if (result != OC_STACK_OK)
293 cout << "Error, return code: " << result << endl;