Browse Source

refactor checkCommand

Jelte Jansen 14 years ago
parent
commit
f8d17c606d
2 changed files with 54 additions and 31 deletions
  1. 45 31
      src/lib/config/ccsession.cc
  2. 9 0
      src/lib/config/ccsession.h

+ 45 - 31
src/lib/config/ccsession.cc

@@ -284,6 +284,49 @@ ModuleCCSession::hasQueuedMsgs() const {
     return (session_.hasQueuedMsgs());
 }
 
+ConstElementPtr
+ModuleCCSession::checkConfigUpdateCommand(const std::string& target_module,
+                                          ConstElementPtr arg) {
+    if (target_module == module_name_) {
+        return handleConfigUpdate(arg);
+    } else {
+        // ok this update is not for us, if we have this module
+        // in our remote config list, update that
+        updateRemoteConfig(target_module, arg);
+        // we're not supposed to answer to this, so return
+        return ElementPtr();
+    }
+}
+
+ConstElementPtr
+ModuleCCSession::checkModuleCommand(const std::string& cmd_str,
+                                    const std::string& target_module,
+                                    ConstElementPtr arg) {
+    if (target_module == module_name_) {
+        if (command_handler_) {
+            ElementPtr errors = Element::createList();
+            if (module_specification_.validate_command(cmd_str,
+                                                       arg,
+                                                       errors)) {
+                return command_handler_(cmd_str, arg);
+            } else {
+                std::stringstream ss;
+                ss << "Error in command validation: ";
+                BOOST_FOREACH(ConstElementPtr error,
+                              errors->listValue()) {
+                    ss << error->stringValue();
+                }
+                return createAnswer(3, ss.str());
+            }
+        } else {
+            return createAnswer(1,
+                                "Command given but no "
+                                "command handler for module");
+        }
+    }
+    return ElementPtr();
+}
+
 int
 ModuleCCSession::checkCommand() {
     ConstElementPtr cmd, routing, data;
@@ -300,38 +343,9 @@ ModuleCCSession::checkCommand() {
             std::string cmd_str = parseCommand(arg, data);
             std::string target_module = routing->get("group")->stringValue();
             if (cmd_str == "config_update") {
-                if (target_module == module_name_) {
-                    answer = handleConfigUpdate(arg);
-                } else {
-                    // ok this update is not for us, if we have this module
-                    // in our remote config list, update that
-                    updateRemoteConfig(target_module, arg);
-                    // we're not supposed to answer to this, so return
-                    return (0);
-                }
+                answer = checkConfigUpdateCommand(target_module, arg);
             } else {
-                if (target_module == module_name_) {
-                    if (command_handler_) {
-                        ElementPtr errors = Element::createList();
-                        if (module_specification_.validate_command(cmd_str,
-                                                                   arg,
-                                                                   errors)) {
-                            answer = command_handler_(cmd_str, arg);
-                        } else {
-                            std::stringstream ss;
-                            ss << "Error in command validation: ";
-                            BOOST_FOREACH(ConstElementPtr error,
-                                          errors->listValue()) {
-                                ss << error->stringValue();
-                            }
-                            answer = createAnswer(3, ss.str());
-                        }
-                    } else {
-                        answer = createAnswer(1,
-                                              "Command given but no "
-                                              "command handler for module");
-                    }
-                }
+                answer = checkModuleCommand(cmd_str, target_module, arg);
             }
         } catch (const CCSessionError& re) {
             // TODO: Once we have logging and timeouts, we should not

+ 9 - 0
src/lib/config/ccsession.h

@@ -246,6 +246,15 @@ private:
     isc::data::ConstElementPtr handleConfigUpdate(
         isc::data::ConstElementPtr new_config);
 
+    isc::data::ConstElementPtr checkConfigUpdateCommand(
+        const std::string& target_module,
+        isc::data::ConstElementPtr arg);
+    
+    isc::data::ConstElementPtr checkModuleCommand(
+        const std::string& cmd_str,
+        const std::string& target_module,
+        isc::data::ConstElementPtr arg);
+
     isc::data::ConstElementPtr(*config_handler_)(
         isc::data::ConstElementPtr new_config);
     isc::data::ConstElementPtr(*command_handler_)(