[IOT-1337] Exception handling added in cloud client sample
authorMinji Park <minjii.park@samsung.com>
Mon, 26 Sep 2016 05:58:34 +0000 (14:58 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Mon, 26 Sep 2016 07:18:51 +0000 (07:18 +0000)
- Exception handling added in group_invite and MQ samples

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

index 22379ef..812e76e 100644 (file)
@@ -243,143 +243,150 @@ int main(int argc, char *argv[])
     {
         cin >> cmd;
 
-        QueryParamsMap query;
-        OCRepresentation rep;
-
-        switch (atoi(cmd.c_str()))
+        try
         {
-            case 1:
-                cout << "Put userUUID to search:" << endl;
-                cin >> cmd;
-                result = accountMgr->searchUser(cmd, &ocPost);
-                break;
-
-            case 2:
-                cout << "Put email to search:" << endl;
-                cin >> cmd;
-                query["email"] = cmd;
-                result = accountMgr->searchUser(query, &ocPost);
-                break;
-
-            case 3:
-                cout << "Put phone number to search:" << endl;
-                cin >> cmd;
-                query["phone"] = cmd;
-                result = accountMgr->searchUser(query, &ocPost);
-                break;
-
-            case 4:
-                cout << "PUT deviceID to delete:";
-                cin >> cmd;
-                result = accountMgr->deleteDevice(cmd, &onDelete);
-                break;
-
-            case 5:
-                result = accountMgr->createGroup(OC::AclGroupType::PUBLIC, &ocPost);
-                break;
-
-            case 6:
-                cout << "PUT groupId to observe:";
-                cin >> cmd;
-                result = accountMgr->observeGroup(cmd, &onObserve);
-                break;
-
-            case 7:
-                result = accountMgr->getGroupList(&ocPost);
-                break;
-
-            case 8:
-                cout << "PUT groupId to delete:";
-                cin >> cmd;
-                result = accountMgr->deleteGroup(cmd, &onDelete);
-                break;
-
-            case 9:
-                cout << "PUT groupId to join:";
-                cin >> cmd;
-                result = accountMgr->joinGroup(cmd, &ocPost);
-                break;
-
-            case 10:
-                cout << "PUT groupId to add device:";
-                cin >> cmd;
-                cout << "PUT deviceId to add to group:";
-                cin >> cmd2;
-                {
-                    vector<string> deviceIds;
-                    deviceIds.push_back(cmd2);
-                    result = accountMgr->addDeviceToGroup(cmd, deviceIds, &ocPost);
-                }
-                break;
-
-            case 11:
-                cout << "PUT groupId to get info:";
-                cin >> cmd;
-                result = accountMgr->getGroupInfo(cmd, &ocPost);
-                break;
-
-            case 12:
-                cout << "PUT groupId to leave:";
-                cin >> cmd;
-                result = accountMgr->leaveGroup(cmd, &onDelete);
-                break;
-
-            case 13:
-                cout << "PUT groupId to remove device:";
-                cin >> cmd;
-                cout << "PUT deviceId to remove from group:";
-                cin >> cmd2;
-                {
-                    vector<string> deviceIds;
-                    deviceIds.push_back(cmd2);
-                    result = accountMgr->deleteDeviceFromGroup(cmd, deviceIds, &onDelete);
-                }
-                break;
-
-            case 14:
-                result = accountMgr->observeInvitation(&onObserve);
-                break;
-
-            case 15:
-                cout << "PUT groupId to invite:";
-                cin >> cmd;
-                cout << "PUT userUUID to invite:";
-                cin >> cmd2;
-                result = accountMgr->sendInvitation(cmd, cmd2, &ocPost);
-                break;
-
-            case 16:
-                cout << "PUT groupId to cancel invitation:";
-                cin >> cmd;
-                cout << "PUT userUUID to cancel invitation:";
-                cin >> cmd2;
-                result = accountMgr->cancelInvitation(cmd, cmd2, &onDelete);
-                break;
-
-            case 17:
-                cout << "PUT groupId to delete invitation:";
-                cin >> cmd;
-                result = accountMgr->deleteInvitation(cmd, &onDelete);
-                break;
-
-            case 18:
-                cout << "PUT groupId to cancel observe:";
-                cin >> cmd;
-                result = accountMgr->cancelObserveGroup(cmd);
-                break;
-
-            case 19:
-                result = accountMgr->cancelObserveInvitation();
-                break;
-
-            case 20:
-                goto exit;
-                break;
-        }
+            QueryParamsMap query;
+            OCRepresentation rep;
+
+            switch (atoi(cmd.c_str()))
+            {
+                case 1:
+                    cout << "Put userUUID to search:" << endl;
+                    cin >> cmd;
+                    result = accountMgr->searchUser(cmd, &ocPost);
+                    break;
+
+                case 2:
+                    cout << "Put email to search:" << endl;
+                    cin >> cmd;
+                    query["email"] = cmd;
+                    result = accountMgr->searchUser(query, &ocPost);
+                    break;
+
+                case 3:
+                    cout << "Put phone number to search:" << endl;
+                    cin >> cmd;
+                    query["phone"] = cmd;
+                    result = accountMgr->searchUser(query, &ocPost);
+                    break;
+
+                case 4:
+                    cout << "PUT deviceID to delete:";
+                    cin >> cmd;
+                    result = accountMgr->deleteDevice(cmd, &onDelete);
+                    break;
+
+                case 5:
+                    result = accountMgr->createGroup(OC::AclGroupType::PUBLIC, &ocPost);
+                    break;
 
-        if (result != OC_STACK_OK)
+                case 6:
+                    cout << "PUT groupId to observe:";
+                    cin >> cmd;
+                    result = accountMgr->observeGroup(cmd, &onObserve);
+                    break;
+
+                case 7:
+                    result = accountMgr->getGroupList(&ocPost);
+                    break;
+
+                case 8:
+                    cout << "PUT groupId to delete:";
+                    cin >> cmd;
+                    result = accountMgr->deleteGroup(cmd, &onDelete);
+                    break;
+
+                case 9:
+                    cout << "PUT groupId to join:";
+                    cin >> cmd;
+                    result = accountMgr->joinGroup(cmd, &ocPost);
+                    break;
+
+                case 10:
+                    cout << "PUT groupId to add device:";
+                    cin >> cmd;
+                    cout << "PUT deviceId to add to group:";
+                    cin >> cmd2;
+                    {
+                        vector<string> deviceIds;
+                        deviceIds.push_back(cmd2);
+                        result = accountMgr->addDeviceToGroup(cmd, deviceIds, &ocPost);
+                    }
+                    break;
+
+                case 11:
+                    cout << "PUT groupId to get info:";
+                    cin >> cmd;
+                    result = accountMgr->getGroupInfo(cmd, &ocPost);
+                    break;
+
+                case 12:
+                    cout << "PUT groupId to leave:";
+                    cin >> cmd;
+                    result = accountMgr->leaveGroup(cmd, &onDelete);
+                    break;
+
+                case 13:
+                    cout << "PUT groupId to remove device:";
+                    cin >> cmd;
+                    cout << "PUT deviceId to remove from group:";
+                    cin >> cmd2;
+                    {
+                        vector<string> deviceIds;
+                        deviceIds.push_back(cmd2);
+                        result = accountMgr->deleteDeviceFromGroup(cmd, deviceIds, &onDelete);
+                    }
+                    break;
+
+                case 14:
+                    result = accountMgr->observeInvitation(&onObserve);
+                    break;
+
+                case 15:
+                    cout << "PUT groupId to invite:";
+                    cin >> cmd;
+                    cout << "PUT userUUID to invite:";
+                    cin >> cmd2;
+                    result = accountMgr->sendInvitation(cmd, cmd2, &ocPost);
+                    break;
+
+                case 16:
+                    cout << "PUT groupId to cancel invitation:";
+                    cin >> cmd;
+                    cout << "PUT userUUID to cancel invitation:";
+                    cin >> cmd2;
+                    result = accountMgr->cancelInvitation(cmd, cmd2, &onDelete);
+                    break;
+
+                case 17:
+                    cout << "PUT groupId to delete invitation:";
+                    cin >> cmd;
+                    result = accountMgr->deleteInvitation(cmd, &onDelete);
+                    break;
+
+                case 18:
+                    cout << "PUT groupId to cancel observe:";
+                    cin >> cmd;
+                    result = accountMgr->cancelObserveGroup(cmd);
+                    break;
+
+                case 19:
+                    result = accountMgr->cancelObserveInvitation();
+                    break;
+
+                case 20:
+                    goto exit;
+                    break;
+            }
+
+            if (result != OC_STACK_OK)
+            {
+                cout << "Error, return code: " << result << endl;
+            }
+        }
+        catch (exception e)
         {
-            cout << "Error, return code: " << result << endl;
+            cout << "Precondition failed." << endl;
         }
     }
 
index f04e037..0fac651 100644 (file)
@@ -239,11 +239,26 @@ int main(int argc, char *argv[])
             case '2':
                 cout << "Put discovered topic index to select: ";
                 cin >> cmd;
-                g_mqSelectedTopicResource = gTopicList[atoi(cmd.c_str())];
-                cout << g_mqSelectedTopicResource->uri() << " selected" << endl;
+                {
+                    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;
index efc4eac..5399afc 100644 (file)
@@ -230,51 +230,73 @@ int main(int argc, char *argv[])
     {
         cin >> cmd;
 
-        QueryParamsMap query;
-        OCRepresentation rep;
-
-        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 << "Subscribe to selected topic" << endl;
-                result = g_mqSelectedTopicResource->subscribeMQTopic(ObserveType::Observe, query, &subscribeCB,
-                         QualityOfService::LowQos);
-                break;
-
-            case '4':
-                cout << "Unsubscribe to selected topic" << endl;
-                result = g_mqSelectedTopicResource->unsubscribeMQTopic(QualityOfService::LowQos);
-                break;
-
-            case 'q':
-                goto exit;
-                break;
-        }
+            QueryParamsMap query;
+            OCRepresentation rep;
+
+            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 << "Subscribe to selected topic" << endl;
+                    result = g_mqSelectedTopicResource->subscribeMQTopic(ObserveType::Observe, query, &subscribeCB,
+                             QualityOfService::LowQos);
+                    break;
+
+                case '4':
+                    cout << "Unsubscribe to selected topic" << endl;
+                    result = g_mqSelectedTopicResource->unsubscribeMQTopic(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(exception e)
         {
-            cout << "Error, return code: " << result << endl;
+            cout << "Precondition failed." << endl;
         }
     }