diff options
Diffstat (limited to 'src/lib/cc/command_interpreter.cc')
-rw-r--r-- | src/lib/cc/command_interpreter.cc | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/src/lib/cc/command_interpreter.cc b/src/lib/cc/command_interpreter.cc index c17230b242..163cdc1da0 100644 --- a/src/lib/cc/command_interpreter.cc +++ b/src/lib/cc/command_interpreter.cc @@ -8,8 +8,9 @@ #include <exceptions/exceptions.h> #include <cc/command_interpreter.h> -#include <string> #include <cc/data.h> +#include <string> +#include <set> using namespace std; @@ -171,5 +172,56 @@ parseCommand(ConstElementPtr& arg, ConstElementPtr command) { return (cmd->stringValue()); } +ConstElementPtr +combineCommandsLists(const ConstElementPtr& response1, + const ConstElementPtr& response2) { + // Usually when this method is called there should be two non-null + // responses. If there is just a single response, return this + // response. + if (!response1 && response2) { + return (response2); + + } else if (response1 && !response2) { + return (response1); + + } else if (!response1 && !response2) { + return (ConstElementPtr()); + + } else { + // Both responses are non-null so we need to combine the lists + // of supported commands if the status codes are 0. + int status_code; + ConstElementPtr args1 = parseAnswer(status_code, response1); + if (status_code != 0) { + return (response1); + } + + ConstElementPtr args2 = parseAnswer(status_code, response2); + if (status_code != 0) { + return (response2); + } + + const std::vector<ElementPtr> vec1 = args1->listValue(); + const std::vector<ElementPtr> vec2 = args2->listValue(); + + // Storing command names in a set guarantees that the non-unique + // command names are aggregated. + std::set<std::string> combined_set; + for (auto v = vec1.cbegin(); v != vec1.cend(); ++v) { + combined_set.insert((*v)->stringValue()); + } + for (auto v = vec2.cbegin(); v != vec2.cend(); ++v) { + combined_set.insert((*v)->stringValue()); + } + + // Create a combined list of commands. + ElementPtr combined_list = Element::createList(); + for (auto s = combined_set.cbegin(); s != combined_set.cend(); ++s) { + combined_list->add(Element::create(*s)); + } + return (createAnswer(CONTROL_RESULT_SUCCESS, combined_list)); + } +} + } } |