[svn] commit: r881 - in /branches/jelte-configuration/src: bin/auth/auth_srv.cc bin/auth/main.cc lib/cc/cpp/data.h lib/config/cpp/ccsession.cc lib/config/cpp/ccsession.h

BIND 10 source code commits bind10-changes at lists.isc.org
Fri Feb 19 12:54:14 UTC 2010


Author: jelte
Date: Fri Feb 19 12:54:14 2010
New Revision: 881

Log:
add parseAnswer helper function
moved two sets of common code into one private handleConfigUpdate function

Modified:
    branches/jelte-configuration/src/bin/auth/auth_srv.cc
    branches/jelte-configuration/src/bin/auth/main.cc
    branches/jelte-configuration/src/lib/cc/cpp/data.h
    branches/jelte-configuration/src/lib/config/cpp/ccsession.cc
    branches/jelte-configuration/src/lib/config/cpp/ccsession.h

Modified: branches/jelte-configuration/src/bin/auth/auth_srv.cc
==============================================================================
--- branches/jelte-configuration/src/bin/auth/auth_srv.cc (original)
+++ branches/jelte-configuration/src/bin/auth/auth_srv.cc Fri Feb 19 12:54:14 2010
@@ -120,5 +120,7 @@
         // todo: what to do with port change. restart automatically?
         // ignore atm
     //}
-    return isc::data::Element::createFromString("{ \"result\": [0] }");
+    std::cout << "[XX] auth: new config " << config << std::endl;
+    
+    return isc::config::createAnswer(0);
 }

Modified: branches/jelte-configuration/src/bin/auth/main.cc
==============================================================================
--- branches/jelte-configuration/src/bin/auth/main.cc (original)
+++ branches/jelte-configuration/src/bin/auth/main.cc Fri Feb 19 12:54:14 2010
@@ -64,18 +64,15 @@
 
 isc::data::ElementPtr
 my_command_handler(isc::data::ElementPtr command) {
-    isc::data::ElementPtr answer = isc::data::Element::createFromString("{ \"result\": [0] }");
+    isc::data::ElementPtr answer = isc::config::createAnswer(0);
 
     cout << "[XX] Handle command: " << endl << command->str() << endl;
     if (command->get(0)->stringValue() == "print_message") 
     {
         cout << command->get(1)->get("message") << endl;
         /* let's add that message to our answer as well */
-        cout << "[XX] answer was: " << answer->str() << endl;
         answer->get("result")->add(command->get(1));
-        cout << "[XX] answer now: " << answer->str() << endl;
     }
-
     return answer;
 }
 

Modified: branches/jelte-configuration/src/lib/cc/cpp/data.h
==============================================================================
--- branches/jelte-configuration/src/lib/cc/cpp/data.h (original)
+++ branches/jelte-configuration/src/lib/cc/cpp/data.h Fri Feb 19 12:54:14 2010
@@ -35,7 +35,6 @@
 /// is called for an Element that has a wrong type (e.g. int_value on a
 /// ListElement)
 ///
-// todo: include types and called function in the exception
 class TypeError : public isc::Exception {
 public:
     TypeError(const char* file, size_t line, const char* what) :

Modified: branches/jelte-configuration/src/lib/config/cpp/ccsession.cc
==============================================================================
--- branches/jelte-configuration/src/lib/config/cpp/ccsession.cc (original)
+++ branches/jelte-configuration/src/lib/config/cpp/ccsession.cc Fri Feb 19 12:54:14 2010
@@ -36,6 +36,7 @@
 #include <cc/data.h>
 #include <module_spec.h>
 #include <cc/session.h>
+#include <exceptions/exceptions.h>
 
 //#include "common.h"
 #include "ccsession.h"
@@ -50,8 +51,9 @@
 namespace isc {
 namespace config {
 
-ElementPtr
-create_answer(const int rcode, const ElementPtr arg)
+/// Creates a standard config/command protocol answer message
+ElementPtr
+createAnswer(const int rcode, const ElementPtr arg)
 {
     ElementPtr answer = Element::createFromString("{\"result\": []");
     ElementPtr answer_content = answer->get("result");
@@ -61,13 +63,35 @@
 }
 
 ElementPtr
-create_answer(const int rcode, const std::string& arg)
+createAnswer(const int rcode, const std::string& arg)
 {
     ElementPtr answer = Element::createFromString("{\"result\": []");
     ElementPtr answer_content = answer->get("result");
     answer_content->add(Element::create(rcode));
     answer_content->add(Element::create(arg));
     return answer;
+}
+
+ElementPtr
+parseAnswer(int &rcode, const ElementPtr msg)
+{
+    if (!msg->contains("result")) {
+        // TODO: raise CCSessionError exception
+        dns_throw(CCSessionError, "No result in answer message");
+    } else {
+        ElementPtr result = msg->get("result");
+        if (result->get(0)->getType() != Element::integer) {
+            dns_throw(CCSessionError, "First element of result is not an rcode in answer message");
+        } else if (result->get(0)->intValue() != 0 && result->get(1)->getType() != Element::string) {
+            dns_throw(CCSessionError, "Rcode in answer message is non-zero, but other argument is not a StringElement");
+        }
+        rcode = result->get(0)->intValue();
+        if (result->size() > 1) {
+            return result->get(1);
+        } else {
+            return ElementPtr();
+        }
+    }
 }
 
 void
@@ -126,14 +150,33 @@
         session_.group_sendmsg(cmd, "ConfigManager");
         session_.group_recvmsg(env, answer, false);
         cout << "[XX] got config: " << endl << answer->str() << endl;
-        if (answer->contains("result") &&
-            answer->get("result")->get(0)->intValue() == 0 &&
-            answer->get("result")->size() > 1) {
-            config_handler(answer->get("result")->get(1));
-        } else {
-            cout << "[XX] no result in answer" << endl;
-        }
-    }
+        int rcode;
+        ElementPtr new_config = parseAnswer(rcode, answer);
+        handleConfigUpdate(new_config);
+    }
+}
+
+/// Validates the new config values, if they are correct,
+/// call the config handler
+/// If that results in success, store the new config
+ElementPtr
+ModuleCCSession::handleConfigUpdate(ElementPtr new_config)
+{
+    ElementPtr answer;
+    if (!config_handler_) {
+        answer = createAnswer(1, module_name_ + " does not have a config handler");
+    } else if (!module_specification_.validate_config(new_config)) {
+        answer = createAnswer(2, "Error in config validation");
+    } else {
+        // handle config update
+        answer = config_handler_(new_config);
+        int rcode;
+        parseAnswer(rcode, answer);
+        if (rcode == 0) {
+            config_ = new_config;
+        }
+    }
+    return answer;
 }
 
 int
@@ -157,14 +200,7 @@
         ElementPtr answer;
         if (data->contains("config_update")) {
             ElementPtr new_config = data->get("config_update");
-            if (!config_handler_) {
-                answer = create_answer(1, module_name_ + " does not have a config handler");
-            } else if (!module_specification_.validate_config(new_config)) {
-                answer = create_answer(2, "Error in config validation");
-            } else {
-                // handle config update
-                answer = config_handler_(new_config);
-            }
+            answer = handleConfigUpdate(new_config);
         }
         if (data->contains("command")) {
             if (command_handler_) {

Modified: branches/jelte-configuration/src/lib/config/cpp/ccsession.h
==============================================================================
--- branches/jelte-configuration/src/lib/config/cpp/ccsession.h (original)
+++ branches/jelte-configuration/src/lib/config/cpp/ccsession.h Fri Feb 19 12:54:14 2010
@@ -25,6 +25,18 @@
 
 namespace isc {
 namespace config {
+
+///
+/// \brief A standard cc session exception that is thrown if a function
+/// is there is a problem with one of the messages
+///
+// todo: include types and called function in the exception
+class CCSessionError : public isc::Exception {
+public:
+    CCSessionError(const char* file, size_t line, const char* what) :
+        isc::Exception(file, line, what) {}
+};
+
 
 class ModuleCCSession {
 public:
@@ -72,7 +84,7 @@
      * This protocol is very likely to change.
      */
     void set_command_handler(isc::data::ElementPtr(*command_handler)(isc::data::ElementPtr command)) { command_handler_ = command_handler; };
-    
+
 private:
     void read_module_specification(const std::string& filename);
     
@@ -80,13 +92,14 @@
     isc::cc::Session session_;
     ModuleSpec module_specification_;
     isc::data::ElementPtr config_;
+    ElementPtr handleConfigUpdate(ElementPtr new_config);
 
     isc::data::ElementPtr(*config_handler_)(isc::data::ElementPtr new_config);
     isc::data::ElementPtr(*command_handler_)(isc::data::ElementPtr command);
 };
 
 ElementPtr createAnswer(const int rcode, const ElementPtr arg);
-ElementPtr createAnswer(const int rcode, const std::string arg);
+ElementPtr createAnswer(const int rcode, const std::string& arg);
 
 }
 }




More information about the bind10-changes mailing list