/*
 * MIT License
 * Author: Mark Allyn
 *
 * traffic_cop.h — Thread 2: polls the Simulator and any RPi receivers,
 * translates results into TargetState records, and writes them to
 * TargetBuffer (Mutex B) and SharedRenderState (Mutex A) as needed.
 *
 * Run on a dedicated std::thread started in main().
 * Signal shutdown by calling stop() then joining the thread.
 */

#pragma once
#include <thread>
#include <atomic>
#include "target_buffer.h"
#include "shared_render_state.h"
#include "simulator.h"

class TrafficCop {
public:
    TrafficCop(TargetBuffer& tb, SharedRenderState& srs)
        : tb_(tb), srs_(srs) {}

    // Launch the polling thread.
    void start();

    // Signal the thread to stop and block until it exits.
    void stop();

private:
    void run();

    TargetBuffer&      tb_;
    SharedRenderState& srs_;
    Simulator          sim_;

    std::thread        thread_;
    std::atomic<bool>  running_{false};

    static constexpr int POLL_INTERVAL_MS = 250;  // 4 Hz
};
