]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3207] Add a wrapper for boost rand generator
authorSlawek Figiel <slawek@isc.org>
Fri, 9 Feb 2024 11:41:35 +0000 (12:41 +0100)
committerSlawek Figiel <slawek@isc.org>
Tue, 20 Feb 2024 08:33:35 +0000 (09:33 +0100)
src/bin/perfdhcp/test_control.cc
src/bin/perfdhcp/test_control.h

index fa4c9423e69185969262a1a583f9199608108a50..fe14ddb65b7e5acb5c1cbc71b6ab8925b468e077 100644 (file)
@@ -361,7 +361,7 @@ TestControl::generateMacAddress(uint8_t& randomized) {
     const CommandOptions::MacAddrsVector& macs = options_.getMacsFromFile();
     // if we are using the -M option return a random one from the list...
     if (macs.size() > 0) {
-      uint16_t r = number_generator_();
+      uint16_t r = random_generator_->generate();
       if (r >= macs.size()) {
         r = 0;
       }
@@ -417,7 +417,7 @@ TestControl::generateDuid(uint8_t& randomized) {
     const CommandOptions::MacAddrsVector& macs = options_.getMacsFromFile();
     // pick a random mac address if we are using option -M..
     if (macs.size() > 0) {
-      uint16_t r = number_generator_();
+      uint16_t r = random_generator_->generate();
       if (r >= macs.size()) {
         r = 0;
       }
@@ -1106,10 +1106,12 @@ TestControl::reset() {
 
 TestControl::TestControl(CommandOptions& options, BasePerfSocket &socket) :
     exit_time_(not_a_date_time),
-    number_generator_(0, options.getMacsFromFile().size()),
     socket_(socket),
     receiver_(socket, options.isSingleThreaded(), options.getIpVersion()),
     stats_mgr_(options),
+    random_generator_(NumberGeneratorPtr(
+        new RandomGenerator(0, options.getMacsFromFile().size())
+    )),
     options_(options)
 {
     // Reset singleton state before test starts.
index eda953f8aa88a111cbb09d165c9418b2d0cc6daa..650d43eb754c47a4c508461ff5b6d8da426fc848 100644 (file)
@@ -13,7 +13,6 @@
 #include <perfdhcp/receiver.h>
 #include <perfdhcp/command_options.h>
 #include <perfdhcp/perf_socket.h>
-#include <perfdhcp/random_number_generator.h>
 
 #include <dhcp/iface_mgr.h>
 #include <dhcp/dhcp4.h>
@@ -24,6 +23,8 @@
 #include <boost/noncopyable.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/uniform_int_distribution.hpp>
 
 #include <string>
 #include <vector>
@@ -183,6 +184,33 @@ public:
         uint32_t range_; ///< Number of unique numbers generated.
     };
 
+    /// \brief Random numbers generator class. The generated numbers
+    /// are uniformly distributed in the range of [min, max].
+    class RandomGenerator : public NumberGenerator {
+    public:
+        /// \brief Constructor.
+        ///
+        /// \param min minimum number generated.
+        /// \param max maximum number generated.
+        RandomGenerator(uint32_t min, uint32_t max) :
+            NumberGenerator(),
+            distribution(min, max) {
+            // Initialize the randomness source with the current time.
+            randomnessGenerator.seed(time(NULL));
+        }
+
+        /// \brief Generate number in range of [min, max].
+        ///
+        /// \return generated number.
+        virtual uint32_t generate() {
+            return distribution(randomnessGenerator);
+        }
+
+    private:                  
+        boost::random::uniform_int_distribution<> distribution;         
+        boost::random::mt19937 randomnessGenerator;         
+    };
+
     /// \brief Length of the Ethernet HW address (MAC) in bytes.
     ///
     /// \todo Make this variable length as there are cases when HW
@@ -347,9 +375,6 @@ public:
     // solution is to make this class friend of test class but this is not
     // what's followed in other classes.
 protected:
-    /// Generate uniformly distributed integers in range of [min, max]
-    UniformRandomIntegerGenerator number_generator_;
-
     /// \brief Creates DHCPREQUEST from a DHCPACK message.
     ///
     /// @param msg_type the message type to be created (DHCPREQUEST or DHCPRELEASE)
@@ -1078,6 +1103,10 @@ protected:
     /// \brief Storage for reply messages.
     PacketStorage<dhcp::Pkt6> reply_storage_;
 
+    /// \brief Generate uniformly distributed integers in range of
+    /// [min, max].
+    NumberGeneratorPtr random_generator_;
+
     /// \brief Transaction id generator.
     NumberGeneratorPtr transid_gen_;