Fix mq sample client crash
[platform/upstream/iotivity.git] / cloud / samples / client / messagequeue / mq_publisher.cpp
index dbb558f..743b794 100644 (file)
@@ -52,32 +52,66 @@ void printRepresentation(OCRepresentation rep)
     for (auto itr = rep.begin(); itr != rep.end(); ++itr)
     {
         cout << "\t" << itr->attrname() << ":\t" << itr->getValueToString() << endl;
+        if (itr->type() == AttributeType::Vector)
+        {
+            switch (itr->base_type())
+            {
+                case AttributeType::OCRepresentation:
+                    for (auto itr2 : (*itr).getValue<vector<OCRepresentation> >())
+                    {
+                        printRepresentation(itr2);
+                    }
+                    break;
+
+                case AttributeType::Integer:
+                    for (auto itr2 : (*itr).getValue<vector<int> >())
+                    {
+                        cout << "\t\t" << itr2 << endl;
+                    }
+                    break;
+
+                case AttributeType::String:
+                    for (auto itr2 : (*itr).getValue<vector<string> >())
+                    {
+                        cout << "\t\t" << itr2 << endl;
+                    }
+                    break;
+
+                default:
+                    cout << "Unhandled base type " << itr->base_type() << endl;
+                    break;
+            }
+        }
+        else if (itr->type() == AttributeType::OCRepresentation)
+        {
+            printRepresentation((*itr).getValue<OCRepresentation>());
+        }
     }
 }
 
 ////////////////////////////////////////Publisher Sample
 
-void createTopicCB(const int ecode, const std::string &originUri,
-                   std::shared_ptr<OC::OCResource> topic)
+void createTopicCB(const int ecode, const string &originUri,
+                   shared_ptr<OC::OCResource> topic)
 {
-    std::cout << "Create topic response received, code: " << ecode << std::endl;
+    cout << "Create topic response received, code: " << ecode << endl;
 
     if (ecode == OCStackResult::OC_STACK_RESOURCE_CREATED)
     {
-        std::cout << "Created topic : " << topic->uri() << std::endl;
+        cout << "Created topic : " << topic->uri() << endl;
     }
     else
     {
-        std::cout << "Topic creation failed : " << originUri << std::endl;
+        cout << "Topic creation failed : " << originUri << endl;
     }
 }
 
 void publishMessageCB(const HeaderOptions &, const OCRepresentation &, const int eCode)
 {
-    std::cout << "Publish message response received, code: " << eCode << std::endl;
+    cout << "Publish message response received, code: " << eCode << endl;
 }
 
-void discoverTopicCB(const int ecode, const std::string &, std::shared_ptr<OC::OCResource> topic)
+void discoverTopicCB(const int ecode, const string &, shared_ptr<OC::OCResource> topic)
 {
     cout << "Topic discovered code: " << ecode << endl;
     gTopicList.push_back(topic);
@@ -85,9 +119,9 @@ void discoverTopicCB(const int ecode, const std::string &, std::shared_ptr<OC::O
 }
 ////////////////////////////////////////End of Publisher Sample
 
-std::condition_variable g_callbackLock;
-std::string             g_uid;
-std::string             g_accesstoken;
+condition_variable g_callbackLock;
+string             g_uid;
+string             g_accesstoken;
 
 void handleLoginoutCB(const HeaderOptions &,
                       const OCRepresentation &rep, const int ecode)
@@ -148,7 +182,7 @@ int main(int argc, char *argv[])
                                        CT_ADAPTER_TCP);
 
     mutex blocker;
-    unique_lock<std::mutex> lock(blocker);
+    unique_lock<mutex> lock(blocker);
 
     if (argc == 5)
     {
@@ -182,66 +216,89 @@ int main(int argc, char *argv[])
     {
         cin >> cmd;
 
-        QueryParamsMap query;
-        OCRepresentation rep;
-        string      topicType;
-
-        switch (cmd[0])
+        try
         {
-            case '0':
-                gTopicList.clear();
-                cout << "Discovering topics" << endl;
-                result = g_mqBrokerResource->discoveryMQTopics(query, &discoverTopicCB, QualityOfService::LowQos);
-                break;
-
-            case '1':
-                gTopicList.clear();
-                cout << "Put topic type to discover: ";
-                cin >> cmd;
-                query["rt"] = cmd;
-                result = g_mqBrokerResource->discoveryMQTopics(query, &discoverTopicCB, QualityOfService::LowQos);
-                break;
-
-            case '2':
-                cout << "Put discovered topic index to select: ";
-                cin >> cmd;
-                g_mqSelectedTopicResource = gTopicList[atoi(cmd.c_str())];
-                cout << g_mqSelectedTopicResource->uri() << " selected" << endl;
-                break;
-
-            case '3':
-                cout << "Put message to selected topic: ";
-                cin >> cmd;
-                rep["message"] = cmd;
-                result = g_mqSelectedTopicResource->publishMQTopic(rep, query, &publishMessageCB,
-                         QualityOfService::LowQos);
-                break;
-
-            case '4':
-                cout << "Put topic uri to create: ";
-                cin >> cmd;
-                result = g_mqBrokerResource->createMQTopic(rep, cmd, query, &createTopicCB,
-                         QualityOfService::LowQos);
-                break;
-
-            case '5':
-                cout << "Put topic uri to create: ";
-                cin >> cmd;
-                cout << "Put topic type: ";
-                cin >> topicType;
-                query["rt"] = topicType;
-                result = g_mqBrokerResource->createMQTopic(rep, cmd, query, &createTopicCB,
-                         QualityOfService::LowQos);
-                break;
-
-            case 'q':
-                goto exit;
-                break;
-        }
 
-        if (result != OC_STACK_OK)
+            QueryParamsMap query;
+            OCRepresentation rep;
+            string      topicType;
+
+            switch (cmd[0])
+            {
+                case '0':
+                    gTopicList.clear();
+                    cout << "Discovering topics" << endl;
+                    result = g_mqBrokerResource->discoveryMQTopics(query, &discoverTopicCB, QualityOfService::LowQos);
+                    break;
+
+                case '1':
+                    gTopicList.clear();
+                    cout << "Put topic type to discover: ";
+                    cin >> cmd;
+                    query["rt"] = cmd;
+                    result = g_mqBrokerResource->discoveryMQTopics(query, &discoverTopicCB, QualityOfService::LowQos);
+                    break;
+
+                case '2':
+                    cout << "Put discovered topic index to select: ";
+                    cin >> cmd;
+                    {
+                        int index = atoi(cmd.c_str());
+                        if (index < 0 || (unsigned int) index >= gTopicList.size())
+                        {
+                            cout << "invalid topic index selected" << endl;
+                            continue;
+                        }
+
+                        g_mqSelectedTopicResource = gTopicList[index];
+                        cout << g_mqSelectedTopicResource->uri() << " selected" << endl;
+                    }
+                    break;
+
+                case '3':
+                    if (g_mqSelectedTopicResource == nullptr)
+                    {
+                        cout << "Topic is not selected." << endl;
+                        continue;
+                    }
+
+                    cout << "Put message to selected topic: ";
+                    cin >> cmd;
+                    rep["message"] = cmd;
+                    result = g_mqSelectedTopicResource->publishMQTopic(rep, query, &publishMessageCB,
+                             QualityOfService::LowQos);
+                    break;
+
+                case '4':
+                    cout << "Put topic uri to create: ";
+                    cin >> cmd;
+                    result = g_mqBrokerResource->createMQTopic(rep, cmd, query, &createTopicCB,
+                             QualityOfService::LowQos);
+                    break;
+
+                case '5':
+                    cout << "Put topic uri to create: ";
+                    cin >> cmd;
+                    cout << "Put topic type: ";
+                    cin >> topicType;
+                    query["rt"] = topicType;
+                    result = g_mqBrokerResource->createMQTopic(rep, cmd, query, &createTopicCB,
+                             QualityOfService::LowQos);
+                    break;
+
+                case 'q':
+                    goto exit;
+                    break;
+            }
+
+            if (result != OC_STACK_OK)
+            {
+                cout << "Error, return code: " << result << endl;
+            }
+        }
+        catch (const exception &e)
         {
-            cout << "Error, return code: " << result << endl;
+            cout << "Precondition failed: " << e.what() << endl;
         }
     }