Browse Source

[1954] Make CommandOptions class singleton.

Marcin Siodelski 13 years ago
parent
commit
d18d92e4e6
2 changed files with 69 additions and 17 deletions
  1. 48 9
      tests/tools/perfdhcp/command_options.cc
  2. 21 8
      tests/tools/perfdhcp/command_options.h

+ 48 - 9
tests/tools/perfdhcp/command_options.cc

@@ -35,6 +35,28 @@ using namespace isc;
 namespace isc {
 namespace perfdhcp {
 
+/// IfaceMgr is a singleton implementation
+CommandOptions* CommandOptions::instance_ = 0;
+
+void
+CommandOptions::instanceCreate() {
+    if (instance_) {
+        // no need to do anything. Instance is already created.
+        // Who called it again anyway? Uh oh. Had to be us, as
+        // this is private method.
+        return;
+    }
+    instance_ = new CommandOptions();
+}
+
+CommandOptions&
+CommandOptions::instance() {
+    if (instance_ == 0) {
+        instanceCreate();
+    }
+    return (*instance_);
+}
+
 // Reset stored values to the defaults.
 void
 CommandOptions::reset() {
@@ -288,17 +310,34 @@ CommandOptions::initialize(int argc, char** const argv) {
     }
     }
 
-	if (ipversion_ == 0)
-		ipversion_ = 4;
-	if (template_file_.size() > 1) {
-		if (xid_offset_.size() == 1)
+    if (ipversion_ == 0)
+        ipversion_ = 4;
+    if (template_file_.size() > 1) {
+        if (xid_offset_.size() == 1)
             xid_offset_.push_back(xid_offset_[0]);
-		if (rnd_offset_.size() == 1)
-			rnd_offset_.push_back(rnd_offset_[0]);
-	}
+        if (rnd_offset_.size() == 1)
+            rnd_offset_.push_back(rnd_offset_[0]);
+    }
 
-    // TODO HADNLE SERVER ARG
+    /* get server argument */
+    check(optind < argc -1, "extra arguments?");
+    if (optind == argc - 1) {
+        server_name_ = argv[optind];
+        /* decode special cases */
+        if ((ipversion_ == 4) &&
+            (server_name_.compare("all") == 0)) {
+            broadcast_ = 1;
+            server_name_ = "255.255.255.255";
+        } else if ((ipversion_ == 6) &&
+                   (server_name_.compare("all") == 0)) {
+            server_name_ = "FF02::1:2";
+        } else if ((ipversion_ == 6) &&
+                   (server_name_.compare("servers") == 0)) {
+            server_name_ = "FF05::1:3";
+        }
+    }
 
+    // TODO USE NON EXISTING IFACE MANAGER TO HANDLE -l option
 }
 
 void
@@ -414,7 +453,7 @@ CommandOptions::check(bool condition, const std::string errmsg) const {
 void
 CommandOptions::usage(void)
 {
-	fprintf(stderr, "%s",
+	fprintf(stdout, "%s",
 "perfdhcp [-hv] [-4|-6] [-r<rate>] [-t<report>] [-R<range>] [-b<base>]\n"
 "    [-n<num-request>] [-p<test-period>] [-d<drop-time>] [-D<max-drop>]\n"
 "    [-l<local-addr|interface>] [-P<preload>] [-a<aggressivity>]\n"

+ 21 - 8
tests/tools/perfdhcp/command_options.h

@@ -18,6 +18,8 @@
 #include <string>
 #include <vector>
 
+#include <boost/noncopyable.hpp>
+
 namespace isc {
 namespace perfdhcp {
 
@@ -26,19 +28,18 @@ namespace perfdhcp {
 /// This class is responsible for parsing the command-line and storing the
 /// specified options.
 ///
-class CommandOptions {
+class CommandOptions : public boost::noncopyable {
 public:
     enum ExchangeMode {
         DO_SA,
         DORR_SARR
     };
 
-    /// \brief Default Constructor
+    /// CommandOptions is a singleton class. This method returns reference
+    /// to its sole instance.
     ///
-    /// Set values to defaults.
-    CommandOptions() {
-        reset();
-    }
+    /// \return the only existing instance of command options
+    static CommandOptions& instance();
 
     /// \brief Reset to defaults
     ///
@@ -220,7 +221,19 @@ public:
     /// Prints perfdhcp usage
     void usage(void);
 
+protected:
+
+    /// \brief Default Constructor
+    ///
+    /// Protected constructor as this is a singleton class. 
+    /// Use CommandOptions::instance() to get instance of it.
+    CommandOptions() {
+        reset();
+    }
+
 private:
+    /// \brief Create instance of the singleton class
+    static void instanceCreate();
 
     /// \brief Initializes class members based command line
     ///
@@ -261,6 +274,8 @@ private:
     /// \throw InvalidParameter if base is invalid
     void decodeDuid(const std::string& base);
 
+    static CommandOptions * instance_;       ///< A pointer to sole instance of this class
+
     uint8_t ipversion_;                      ///< IP version
     ExchangeMode exchange_mode_  ;           ///< Packet exchange mode (e.g. DORR/SARR)
     int rate_;                               ///< rate in exchange per second
@@ -296,8 +311,6 @@ private:
     std::string diags_;                      ///< diagnostic selectors
     std::string wrapped_;                    ///< wrapped command
     std::string server_name_;                ///< server
-
-
 };
 
 } // namespace perfdhcp