117 lines
3.3 KiB
C++
117 lines
3.3 KiB
C++
/*
|
|
controller.cpp
|
|
|
|
Copyright (c) 2022 Kevin Matz (kevin.matz@gmail.com)
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
#include "controller.h"
|
|
|
|
namespace ARTNET {
|
|
|
|
Controller::Controller()
|
|
: minimum_polling_interval(2500)
|
|
, _periodic_polling_enabled(true)
|
|
, _pollster(std::thread(&Controller::_periodic_polling, this))
|
|
, _talktome_request()
|
|
, _priority_request(DpCritial)
|
|
{
|
|
}
|
|
|
|
Controller::~Controller()
|
|
{
|
|
if (_pollster.joinable())
|
|
{
|
|
_periodic_polling_enabled = false; // disable
|
|
{
|
|
std::shared_lock lk_ctl(_mtx_poll_control);
|
|
_poll_request.notify_all(); // wake-up
|
|
}
|
|
_pollster.join();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Controller::setTalkToMeRequest
|
|
* @param talktome
|
|
*/
|
|
void Controller::setTalkToMeRequest(TalkToMe talktome)
|
|
{
|
|
std::unique_lock lk_ctl(_mtx_poll_control);
|
|
_talktome_request = talktome;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Controller::setPriorityRequest
|
|
* @param priority
|
|
*/
|
|
void Controller::setPriorityRequest(Priority priority)
|
|
{
|
|
std::unique_lock lk_ctl(_mtx_poll_control);
|
|
_priority_request = priority;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Controller::txArtPoll
|
|
* @param talkToMe
|
|
* @param priority
|
|
*/
|
|
void Controller::txArtPoll(TalkToMe talkToMe, Priority priority) const
|
|
{
|
|
auto data = std::make_shared<poll_data>();
|
|
data->talk_to_me = talkToMe;
|
|
data->diagnostic_level = priority;
|
|
auto packet = std::make_shared<ArtPoll>(data);
|
|
send(packet, broadcastIp());
|
|
}
|
|
|
|
|
|
void Controller::_periodic_polling()
|
|
{
|
|
std::chrono::system_clock::time_point last_send = std::chrono::system_clock::from_time_t(0);
|
|
std::chrono::nanoseconds elapsed;
|
|
std::unique_lock lk_thread(_mtx_poll_thread);
|
|
TalkToMe talktome;
|
|
Priority priority;
|
|
|
|
while (_periodic_polling_enabled) {
|
|
// enforce strict minimum update times
|
|
elapsed = std::chrono::system_clock::now() - last_send;
|
|
if (elapsed < minimum_polling_interval)
|
|
std::this_thread::sleep_for(minimum_polling_interval - elapsed);
|
|
|
|
{
|
|
std::unique_lock lk_ctl(_mtx_poll_control);
|
|
talktome = _talktome_request;
|
|
priority = _priority_request;
|
|
}
|
|
txArtPoll(talktome, priority);
|
|
last_send = std::chrono::system_clock::now();
|
|
|
|
// sleep before the next cycle
|
|
_poll_request.wait_for(lk_thread, minimum_polling_interval);
|
|
}
|
|
}
|
|
|
|
} // namespace ARTNET
|