Bring aboard "version", "daemon" and "config-file" configuration option classes.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Thu, 6 Oct 2011 10:58:32 +0000 (07:58 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Thu, 6 Oct 2011 10:58:32 +0000 (07:58 -0300)
src/CMakeLists.txt
src/config/option/configfileoption.cpp [new file with mode: 0644]
src/config/option/configfileoption.h [new file with mode: 0644]
src/config/option/daemonoption.cpp [new file with mode: 0644]
src/config/option/daemonoption.h [new file with mode: 0644]
src/config/option/versionoption.cpp [new file with mode: 0644]
src/config/option/versionoption.h [new file with mode: 0644]

index 5f7562a..fae4cf2 100644 (file)
@@ -38,6 +38,8 @@ set(SOURCES
     config/configurationoptions.cpp
     config/configurationreader.cpp
     config/host.cpp
+    config/option/daemonoption.cpp
+    config/option/configfileoption.cpp
     config/option/configurationoption.cpp
     config/option/hostconfigurationoption.cpp
     config/option/hostpingintervaloption.cpp
@@ -51,6 +53,7 @@ set(SOURCES
     config/option/pingfaillimitoption.cpp
     config/option/sourcenetworkinterfaceoption.cpp
     config/option/statusnotifiercmdoption.cpp
+    config/option/versionoption.cpp
     dns/dnsresolver.cpp
     dns/hostaddress.cpp
     dns/timetolive.cpp
diff --git a/src/config/option/configfileoption.cpp b/src/config/option/configfileoption.cpp
new file mode 100644 (file)
index 0000000..2a0eea1
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#include "config/option/configfileoption.h"
+
+#include <iostream>
+
+#include <logfunc.hpp>
+
+using namespace std;
+using boost::program_options::value;
+using boost::program_options::variables_map;
+using I2n::Logger::GlobalLogger;
+
+//-----------------------------------------------------------------------------
+// ConfigFileOption
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Default constructor.
+ */
+ConfigFileOption::ConfigFileOption() :
+    ConfigurationOption(
+        "config-file",
+        value<string>()->default_value( "/etc/pingcheck.conf" ),
+        "Name of the configuration file."
+    )
+{
+}
+
+/**
+ * @brief Destructor.
+ */
+ConfigFileOption::~ConfigFileOption()
+{
+}
+
+bool ConfigFileOption::validate( const variables_map & ) const
+{
+    return true;
+}
+
+bool ConfigFileOption::parse(
+        const variables_map& vm,
+        Configuration *configuration
+)
+{
+    // config-file
+    if ( 1 <= vm.count( get_command_string() ) )
+    {
+        string config_file_name = vm[ get_command_string() ].as<string> ();
+        configuration->set_config_file_name( config_file_name );
+
+        GlobalLogger.info() << get_command_string() << "=" << config_file_name << endl;
+        return true;
+    }
+
+    return false;
+}
diff --git a/src/config/option/configfileoption.h b/src/config/option/configfileoption.h
new file mode 100644 (file)
index 0000000..6900b43
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef CONFIG_FILE_OPTION_H
+#define CONFIG_FILE_OPTION_H
+
+#include <boost/program_options.hpp>
+
+#include "config/option/configurationoption.h"
+
+//-----------------------------------------------------------------------------
+// ConfigFileOption
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Configuration file name option.
+ */
+class ConfigFileOption : public ConfigurationOption
+{
+public:
+    ConfigFileOption();
+    virtual ~ConfigFileOption();
+
+    virtual bool validate(
+            const boost::program_options::variables_map &vm
+    ) const;
+
+    virtual bool parse(
+            const boost::program_options::variables_map &vm,
+            Configuration *configuration
+    );
+
+};
+
+#endif // CONFIG_FILE_OPTION_H
diff --git a/src/config/option/daemonoption.cpp b/src/config/option/daemonoption.cpp
new file mode 100644 (file)
index 0000000..655e7d2
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#include "config/option/daemonoption.h"
+
+#include <iostream>
+
+#include <logfunc.hpp>
+
+using namespace std;
+using boost::program_options::variables_map;
+using I2n::Logger::GlobalLogger;
+
+//-----------------------------------------------------------------------------
+// DaemonOption
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Default constructor.
+ */
+DaemonOption::DaemonOption() :
+    ConfigurationOption( "daemon", "Run the evil in background." )
+{
+}
+
+/**
+ * @brief Destructor.
+ */
+DaemonOption::~DaemonOption()
+{
+}
+
+bool DaemonOption::validate( const variables_map & ) const
+{
+    return true;
+}
+
+bool DaemonOption::parse(
+        const variables_map& vm,
+        Configuration *configuration
+)
+{
+    // daemon
+    bool have_daemon = ( 1<= vm.count( get_command_string() ) );
+    configuration->set_daemon( have_daemon );
+    GlobalLogger.info() << get_command_string() << "=" << have_daemon << endl;
+
+    return false;
+}
diff --git a/src/config/option/daemonoption.h b/src/config/option/daemonoption.h
new file mode 100644 (file)
index 0000000..9254207
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef DAEMON_OPTION_H
+#define DAEMON_OPTION_H
+
+#include <boost/program_options.hpp>
+
+#include "config/option/configurationoption.h"
+
+//-----------------------------------------------------------------------------
+// DaemonOption
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Daemon option.
+ */
+class DaemonOption : public ConfigurationOption
+{
+public:
+    DaemonOption();
+    virtual ~DaemonOption();
+
+    virtual bool validate(
+            const boost::program_options::variables_map &vm
+    ) const;
+
+    virtual bool parse(
+            const boost::program_options::variables_map &vm,
+            Configuration *configuration
+    );
+
+};
+
+#endif // DAEMON_OPTION_H
diff --git a/src/config/option/versionoption.cpp b/src/config/option/versionoption.cpp
new file mode 100644 (file)
index 0000000..7523baa
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#include "config/option/versionoption.h"
+
+#include <iostream>
+
+#include <logfunc.hpp>
+
+using namespace std;
+using boost::program_options::variables_map;
+using I2n::Logger::GlobalLogger;
+
+//-----------------------------------------------------------------------------
+// VersionOption
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Default constructor.
+ */
+VersionOption::VersionOption() :
+    ConfigurationOption( "version", "Print the version string and exit." )
+{
+}
+
+/**
+ * @brief Destructor.
+ */
+VersionOption::~VersionOption()
+{
+}
+
+bool VersionOption::validate( const variables_map & ) const
+{
+    return true;
+}
+
+bool VersionOption::parse(
+        const variables_map& vm,
+        Configuration *
+)
+{
+    // version
+    if ( 1 <= vm.count( get_command_string() ) )
+    {
+        // TODO GlobalOutput::print( version )
+        cout << PROJECT_NAME << " version " << VERSION_STRING << endl;
+        return true;
+    }
+
+    return false;
+}
diff --git a/src/config/option/versionoption.h b/src/config/option/versionoption.h
new file mode 100644 (file)
index 0000000..2868d3b
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef VERSION_OPTION_H
+#define VERSION_OPTION_H
+
+#include <boost/program_options.hpp>
+
+#include "config/option/configurationoption.h"
+
+//-----------------------------------------------------------------------------
+// VersionOption
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Version option.
+ */
+class VersionOption : public ConfigurationOption
+{
+public:
+    VersionOption();
+    virtual ~VersionOption();
+
+    virtual bool validate(
+            const boost::program_options::variables_map &vm
+    ) const;
+
+    virtual bool parse(
+            const boost::program_options::variables_map &vm,
+            Configuration *configuration
+    );
+
+};
+
+#endif // VERSION_OPTION_H