Fix mq sample client crash
authorJee Hyeok Kim <jihyeok13.kim@samsung.com>
Mon, 10 Oct 2016 01:19:28 +0000 (10:19 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Mon, 10 Oct 2016 10:19:35 +0000 (10:19 +0000)
1. Fix mq_publisher crash (IOT-1432)
1. Fix mq_subscriber crash (IOT-1433)

Change-Id: I499f2e1ef95cfde69e29618c8580271009b0a61e
Signed-off-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/12989
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Minji Park <minjii.park@samsung.com>
cloud/samples/client/messagequeue/mq_publisher.cpp
cloud/samples/client/messagequeue/mq_subscriber.cpp

index 0fac651..743b794 100644 (file)
@@ -216,81 +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;
-                {
-                    int index = atoi(cmd.c_str());
-                    if(index < 0 || (unsigned int) index >= gTopicList.size())
+
+            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 << "invalid topic index selected" << endl;
+                        cout << "Topic is not 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;
-        }
+                    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;
 
-        if (result != OC_STACK_OK)
+                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;
         }
     }
 
index 5399afc..4263760 100644 (file)
@@ -256,7 +256,7 @@ int main(int argc, char *argv[])
                     cin >> cmd;
                     {
                         int index = atoi(cmd.c_str());
-                        if(index < 0 || (unsigned int) index >= gTopicList.size())
+                        if (index < 0 || (unsigned int) index >= gTopicList.size())
                         {
                             cout << "invalid topic index selected" << endl;
                             continue;
@@ -268,7 +268,7 @@ int main(int argc, char *argv[])
                     break;
 
                 case '3':
-                    if(g_mqSelectedTopicResource == nullptr)
+                    if (g_mqSelectedTopicResource == nullptr)
                     {
                         cout << "Topic is not selected." << endl;
                         continue;
@@ -280,6 +280,11 @@ int main(int argc, char *argv[])
                     break;
 
                 case '4':
+                    if (g_mqSelectedTopicResource == nullptr)
+                    {
+                        cout << "Topic is not selected." << endl;
+                        continue;
+                    }
                     cout << "Unsubscribe to selected topic" << endl;
                     result = g_mqSelectedTopicResource->unsubscribeMQTopic(QualityOfService::LowQos);
                     break;
@@ -294,9 +299,9 @@ int main(int argc, char *argv[])
                 cout << "Error, return code: " << result << endl;
             }
         }
-        catch(exception e)
+        catch (const exception &e)
         {
-            cout << "Precondition failed." << endl;
+            cout << "Precondition failed: " << e.what() << endl;
         }
     }