|
| 1 | +#ifndef __SCHEDULER_H__ |
| 2 | +#define __SCHEDULER_H__ |
| 3 | + |
| 4 | +#include <chrono> |
| 5 | +#include <condition_variable> |
| 6 | +#include <functional> |
| 7 | +#include <future> |
| 8 | +#include <mutex> |
| 9 | +#include <unordered_map> |
| 10 | +#include <vector> |
| 11 | +#include "global_clock.h" |
| 12 | + |
| 13 | +// must be set |
| 14 | +#define MAX_ACK_TIMEOUT \ |
| 15 | + (20 * TICK_DURATION) ///< Maximum time to wait for ACK |
| 16 | + |
| 17 | +/** |
| 18 | + * @class Scheduler |
| 19 | + * @brief Manages retransmission timers and tracks acknowledgments for packets. |
| 20 | + * |
| 21 | + * This class provides functionality to manage retransmission timers for |
| 22 | + * packets, handle acknowledgments, and clean up packet data once |
| 23 | + * retransmissions are complete. |
| 24 | + */ |
| 25 | +class Scheduler |
| 26 | +{ |
| 27 | +public: |
| 28 | + using Callback = std::function<void(int)>; |
| 29 | + |
| 30 | + Scheduler(); |
| 31 | + ~Scheduler(); |
| 32 | + |
| 33 | + /** |
| 34 | + * @brief Stops all active timers and waits for their completion. |
| 35 | + * |
| 36 | + * This method ensures that all active timers (threads) complete their |
| 37 | + * execution before the program exits. |
| 38 | + */ |
| 39 | + void stopAllTimers(); |
| 40 | + |
| 41 | + /** |
| 42 | + * @brief Starts a retransmission timer for a given packet ID. |
| 43 | + * |
| 44 | + * The function initiates a timer to wait for an ACK (Acknowledgment) for the specified packet. |
| 45 | + * If the ACK is received within the MAX_ACK_TIMEOUT, it clears the packet data and sets the result |
| 46 | + * of the ackPromise to `true`, indicating success. If the timeout occurs and no ACK is received, |
| 47 | + * it triggers the provided callback function to retransmit the packet and increments the retry count. |
| 48 | + * |
| 49 | + * @param packetID The unique ID of the packet being transmitted. |
| 50 | + * @param callback The callback function to call when retransmitting the packet after a timeout. |
| 51 | + * @param ackPromise A shared promise that communicates whether the packet transmission was successful |
| 52 | + * (i.e., ACK was received). |
| 53 | + */ |
| 54 | + void startRetransmissionTimer(int packetID, Callback callback, std::shared_ptr<std::promise<bool>> ackPromise); |
| 55 | + |
| 56 | + /** |
| 57 | + * @brief Receives an acknowledgment for a packet. |
| 58 | + * |
| 59 | + * @param packetID The ID of the packet that has been acknowledged. |
| 60 | + */ |
| 61 | + void receiveACK(int packetID); |
| 62 | + |
| 63 | + /** |
| 64 | + * @brief Clears the data associated with a packet. |
| 65 | + * |
| 66 | + * @param packetID The ID of the packet whose data is to be cleared. |
| 67 | + */ |
| 68 | + void clearPacketData(int packetID); |
| 69 | + |
| 70 | +private: |
| 71 | + std::unordered_map<int, bool> |
| 72 | + ackReceived; ///< Map to track received acknowledgments |
| 73 | + std::unordered_map<int, int> |
| 74 | + retryCounts; ///< Map to track retry counts for packets |
| 75 | + std::mutex mutex; ///< Mutex for thread safety |
| 76 | + std::condition_variable cv; ///< Condition variable for synchronization |
| 77 | + std::vector<std::future<void>> |
| 78 | + futures; ///< Vector to store futures of active threads |
| 79 | +}; |
| 80 | + |
| 81 | +#endif // __SCHEDULER_H__ |
0 commit comments