2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-31 05:55:28 +00:00

[#3207] Add a wrapper for boost rand generator

This commit is contained in:
Slawek Figiel
2024-02-09 12:41:35 +01:00
parent 1679c812d9
commit 47f77e7633
2 changed files with 38 additions and 7 deletions

View 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.

View 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_;