Browse Source

sync cpp part with python part (similar functionality and function/class names)

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/jelte-configuration@869 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 15 years ago
parent
commit
b5eff6e5bd

+ 1 - 1
src/bin/auth/main.cc

@@ -106,7 +106,7 @@ main(int argc, char* argv[]) {
         } else {
             specfile = std::string(AUTH_SPECFILE_LOCATION);
         }
-        CommandSession cs = CommandSession(specfile,
+        ModuleCCSession cs = ModuleCCSession(specfile,
                                            my_config_handler,
                                            my_command_handler);
     

+ 1 - 1
src/bin/parkinglot/main.cc

@@ -110,7 +110,7 @@ main(int argc, char* argv[]) {
         } else {
             specfile = std::string(PARKINGLOT_SPECFILE_LOCATION);
         }
-        CommandSession cs = CommandSession(specfile, my_config_handler, my_command_handler);
+        ModuleCCSession cs = ModuleCCSession(specfile, my_config_handler, my_command_handler);
     
         // main server loop
         fd_set fds;

+ 10 - 10
src/lib/config/cpp/ccsession.cc

@@ -50,7 +50,7 @@ using isc::data::ParseError;
 using isc::data::ModuleSpecError;
 
 void
-CommandSession::read_data_definition(const std::string& filename) {
+ModuleCCSession::read_module_specification(const std::string& filename) {
     std::ifstream file;
 
     // this file should be declared in a @something@ directive
@@ -61,27 +61,27 @@ CommandSession::read_data_definition(const std::string& filename) {
     }
 
     try {
-        data_definition_ = ModuleSpec(file, true);
+        module_specification_ = ModuleSpec(file, true);
     } catch (ParseError pe) {
-        cout << "Error parsing definition file: " << pe.what() << endl;
+        cout << "Error parsing module specification file: " << pe.what() << endl;
         exit(1);
     } catch (ModuleSpecError dde) {
-        cout << "Error reading definition file: " << dde.what() << endl;
+        cout << "Error reading module specification file: " << dde.what() << endl;
         exit(1);
     }
     file.close();
 }
 
-CommandSession::CommandSession(std::string spec_file_name,
+ModuleCCSession::ModuleCCSession(std::string spec_file_name,
                                isc::data::ElementPtr(*config_handler)(isc::data::ElementPtr new_config),
                                isc::data::ElementPtr(*command_handler)(isc::data::ElementPtr command)
                               ) throw (isc::cc::SessionError):
     session_(isc::cc::Session())
 {
-    read_data_definition(spec_file_name);
+    read_module_specification(spec_file_name);
     sleep(1);
 
-    module_name_ = data_definition_.getDefinition()->get("module_spec")->get("module_name")->stringValue();
+    module_name_ = module_specification_.getFullSpec()->get("module_spec")->get("module_name")->stringValue();
     config_handler_ = config_handler;
     command_handler_ = command_handler;
 
@@ -96,7 +96,7 @@ CommandSession::CommandSession(std::string spec_file_name,
     //session_.subscribe("Boss", "*");
     //session_.subscribe("statistics", "*");
     // send the data specification
-    session_.group_sendmsg(data_definition_.getDefinition(), "ConfigManager");
+    session_.group_sendmsg(module_specification_.getFullSpec(), "ConfigManager");
     session_.group_recvmsg(env, answer, false);
     
     // get any stored configuration from the manager
@@ -116,13 +116,13 @@ CommandSession::CommandSession(std::string spec_file_name,
 }
 
 int
-CommandSession::getSocket()
+ModuleCCSession::getSocket()
 {
     return (session_.getSocket());
 }
 
 int
-CommandSession::check_command()
+ModuleCCSession::check_command()
 {
     cout << "[XX] check for command" << endl;
     ElementPtr cmd, routing, data;

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

@@ -23,20 +23,20 @@
 #include <cc/session.h>
 #include <cc/data.h>
 
-class CommandSession {
+class ModuleCCSession {
 public:
     /**
      * Initialize a config/command session
      * @param module_name: The name of this module. This is not a
      *                     reference because we expect static strings
      *                     to be passed here.
-     * @param spec_file_name: The name of the file containing the data
-     *                        definition.
+     * @param spec_file_name: The name of the file containing the
+     *                        module specification.
      */
-    CommandSession(std::string spec_file_name,
-                   isc::data::ElementPtr(*config_handler)(isc::data::ElementPtr new_config) = NULL,
-                   isc::data::ElementPtr(*command_handler)(isc::data::ElementPtr command) = NULL
-                  ) throw (isc::cc::SessionError);
+    ModuleCCSession(std::string spec_file_name,
+                    isc::data::ElementPtr(*config_handler)(isc::data::ElementPtr new_config) = NULL,
+                    isc::data::ElementPtr(*command_handler)(isc::data::ElementPtr command) = NULL
+                    ) throw (isc::cc::SessionError);
     int getSocket();
 
     /**
@@ -71,11 +71,11 @@ public:
     void set_command_handler(isc::data::ElementPtr(*command_handler)(isc::data::ElementPtr command)) { command_handler_ = command_handler; };
     
 private:
-    void read_data_definition(const std::string& filename);
+    void read_module_specification(const std::string& filename);
     
     std::string module_name_;
     isc::cc::Session session_;
-    isc::data::ModuleSpec data_definition_;
+    isc::data::ModuleSpec module_specification_;
     isc::data::ElementPtr config_;
 
     isc::data::ElementPtr(*config_handler_)(isc::data::ElementPtr new_config);

+ 54 - 16
src/lib/config/cpp/data_def.cc

@@ -26,6 +26,10 @@
 
 using namespace isc::data;
 
+//
+// static functions
+//
+
 // todo: is there a direct way to get a std::string from an enum label?
 static std::string
 getType_string(Element::types type)
@@ -98,7 +102,7 @@ check_config_item(const ElementPtr& spec) {
                     !spec->get("item_optional")->boolValue()
                    );
 
-    // if list, check the list definition
+    // if list, check the list specification
     if (getType_value(spec->get("item_type")->stringValue()) == Element::list) {
         check_leaf_item(spec, "list_item_spec", Element::map, true);
         check_config_item(spec->get("list_item_spec"));
@@ -150,10 +154,10 @@ check_data_specification(const ElementPtr& spec) {
     }
 }
 
-// checks whether the given element is a valid data definition
+// checks whether the given element is a valid module specification
 // throws a ModuleSpecError if the specification is bad
 static void
-check_definition(const ElementPtr& def)
+check_module_specification(const ElementPtr& def)
 {
     if (!def->contains("module_spec")) {
         throw ModuleSpecError("Data specification does not contain module_spec element");
@@ -162,6 +166,10 @@ check_definition(const ElementPtr& def)
     }
 }
 
+//
+// Public functions
+//
+
 ModuleSpec::ModuleSpec(const std::string& file_name,
                                const bool check)
                                throw(ParseError, ModuleSpecError) {
@@ -174,21 +182,60 @@ ModuleSpec::ModuleSpec(const std::string& file_name,
         throw ModuleSpecError(errs.str());
     }
 
-    definition = Element::createFromString(file, file_name);
+    module_specification = Element::createFromString(file, file_name);
     if (check) {
-        check_definition(definition);
+        check_module_specification(module_specification);
     }
 }
 
+
 ModuleSpec::ModuleSpec(std::istream& in, const bool check)
                                throw(ParseError, ModuleSpecError) {
-    definition = Element::createFromString(in);
+    module_specification = Element::createFromString(in);
     // make sure the whole structure is complete and valid
     if (check) {
-        check_definition(definition);
+        check_module_specification(module_specification);
+    }
+}
+
+const ElementPtr
+ModuleSpec::getCommandsSpec()
+{
+    if (module_specification->contains("commands")) {
+        return module_specification->get("commands");
+    } else {
+        return ElementPtr();
+    }
+}
+
+const ElementPtr
+ModuleSpec::getConfigSpec()
+{
+    if (module_specification->contains("config_data")) {
+        return module_specification->get("config_data");
+    } else {
+        return ElementPtr();
     }
 }
 
+const std::string
+ModuleSpec::getModuleName()
+{
+    return module_specification->get("module_name")->stringValue();
+}
+
+bool
+ModuleSpec::validate(const ElementPtr data)
+{
+    ElementPtr spec = module_specification->find("module_spec/config_data");
+    return validate_spec_list(spec, data);
+}
+
+
+//
+// private functions
+//
+
 //
 // helper functions for validation
 //
@@ -285,12 +332,3 @@ ModuleSpec::validate_spec_list(const ElementPtr spec, const ElementPtr data) {
     return true;
 }
 
-// TODO
-// this function does *not* check if the specification is in correct
-// form, we should do that in the constructor
-bool
-ModuleSpec::validate(const ElementPtr data) {
-    ElementPtr spec = definition->find("module_spec/config_data");
-    return validate_spec_list(spec, data);
-}
-

+ 32 - 18
src/lib/config/cpp/data_def.h

@@ -30,7 +30,7 @@ namespace isc { namespace data {
     /// what() there
     class ModuleSpecError : public std::exception {
     public:
-        ModuleSpecError(std::string m = "Data definition is invalid") : msg(m) {}
+        ModuleSpecError(std::string m = "Module specification is invalid") : msg(m) {}
         ~ModuleSpecError() throw() {}
         const char* what() const throw() { return msg.c_str(); }
     private:
@@ -53,38 +53,52 @@ namespace isc { namespace data {
         /// Create a \c ModuleSpec instance with the given data as
         /// the specification
         /// \param e The Element containing the data specification
-        explicit ModuleSpec(ElementPtr e) : definition(e) {};
+        explicit ModuleSpec(ElementPtr e) : module_specification(e) {};
 
         /// Creates a \c ModuleSpec instance from the contents
         /// of the file given by file_name.
-        /// If check is true, and the definition is not of the correct
-        /// form, a ModuleSpecError is thrown. If the file could
-        /// not be parse, a ParseError is thrown.
+        /// If check is true, and the module specification is not of
+        /// the correct form, a ModuleSpecError is thrown. If the file
+        /// could not be parse, a ParseError is thrown.
         /// \param file_name The file to be opened and parsed
-        /// \param check If true, the data definition in the file is
-        /// checked to be of the correct form
+        /// \param check If true, the module specification in the file
+        /// is checked to be of the correct form
         ModuleSpec(const std::string& file_name, const bool check = true)
                        throw(ParseError, ModuleSpecError);
 
-        // todo: make check default false, or leave out option completely and always check?
         /// Creates a \c ModuleSpec instance from the given input
         /// stream that contains the contents of a .spec file.
-        /// If check is true, and the definition is not of
+        /// If check is true, and the module specification is not of
         /// the correct form, a ModuleSpecError is thrown. If the
         /// file could not be parsed, a ParseError is thrown.
         /// \param in The std::istream containing the .spec file data
-        /// \param check If true, the data definition is checked to be
-        /// of the correct form
+        /// \param check If true, the module specification is checked
+        /// to be of the correct form
         explicit ModuleSpec(std::istream& in, const bool check = true)
                                 throw(ParseError, ModuleSpecError);
 
-        /// Returns the base \c element of the data definition contained
-        /// by this instance
-        /// \return ElementPtr Shared pointer to the data definition
-        const ElementPtr getDefinition() { return definition; };
+        /// Returns the commands part of the specification as an
+        /// ElementPtr, returns an empty ElementPtr if there is none
+        /// \return ElementPtr Shared pointer to the commands
+        ///                    part of the specification
+        const ElementPtr getCommandsSpec();
+
+        /// Returns the configuration part of the specification as an
+        /// ElementPtr
+        /// \return ElementPtr Shared pointer to the configuration
+        ///                    part of the specification
+        const ElementPtr getConfigSpec();
+
+        /// Returns the full module specification as an ElementPtr
+        /// \return ElementPtr Shared pointer to the specification
+        const ElementPtr getFullSpec() { return module_specification; };
+
+        /// Returns the module name as specified by the specification
+        const std::string getModuleName();
+        
         // returns true if the given element conforms to this data
-        // definition scheme
-        /// Validates the given data for this specification.
+        // configuration specification
+        /// Validates the given configuration data for this specification.
         /// \param data The base \c Element of the data to check
         /// \return true if the data conforms to the specification,
         /// false otherwise.
@@ -95,7 +109,7 @@ namespace isc { namespace data {
         bool validate_spec(const ElementPtr spec, const ElementPtr data);
         bool validate_spec_list(const ElementPtr spec, const ElementPtr data);
 
-        ElementPtr definition;
+        ElementPtr module_specification;
     };
 
 } }

+ 3 - 3
src/lib/config/cpp/data_def_unittests.cc

@@ -47,11 +47,11 @@ TEST(ModuleSpec, ReadingSpecfiles) {
     // Tests whether we can open specfiles and if we get the
     // right parse errors
     ModuleSpec dd = ModuleSpec(specfile("spec1.spec"));
-    EXPECT_EQ(dd.getDefinition()->get("module_spec")
+    EXPECT_EQ(dd.getFullSpec()->get("module_spec")
                                 ->get("module_name")
                                 ->stringValue(), "Spec1");
     dd = ModuleSpec(specfile("spec2.spec"));
-    EXPECT_EQ(dd.getDefinition()->get("module_spec")
+    EXPECT_EQ(dd.getFullSpec()->get("module_spec")
                                 ->get("config_data")->size(), 6);
     data_def_error("doesnotexist",
                    "Error opening ",
@@ -61,7 +61,7 @@ TEST(ModuleSpec, ReadingSpecfiles) {
     std::ifstream file;
     file.open(specfile("spec1.spec").c_str());
     dd = ModuleSpec(file);
-    EXPECT_EQ(dd.getDefinition()->get("module_spec")
+    EXPECT_EQ(dd.getFullSpec()->get("module_spec")
                                 ->get("module_name")
                                 ->stringValue(), "Spec1");
 }