// Copyright 2020-2024 Jeisson Hidalgo-Cespedes. ECCI-UCR. CC BY 4.0 #include "ProducerTest.hpp" #include "Log.hpp" #include "Util.hpp" ProducerTest::ProducerTest(size_t packageCount, int productorDelay , size_t consumerCount) : packageCount(packageCount) , productorDelay(productorDelay) , consumerCount(consumerCount) { } int ProducerTest::run() { // Produce each asked message for (size_t index = 0; index < this->packageCount; ++index) { this->produce(this->createMessage(index)); } // Produce an empty message to communicate we finished this->produce(NetworkMessage()); // Report production is done Log::append(Log::INFO, "Producer", std::to_string(this->packageCount) + " messages sent"); return EXIT_SUCCESS; } NetworkMessage ProducerTest::createMessage(size_t index) const { // Source is always 1: this producer const uint16_t source = 1; // Target is selected by random const uint16_t target = 1 + Util::random(0 , static_cast(this->consumerCount)); // IMPORTANT: This simulation uses sleep() to mimics the process of // producing a message. However, you must NEVER use sleep() for real projects Util::sleepFor(this->productorDelay); // Create and return a copy of the network message return NetworkMessage(target, source, index); }