2024-07-22 16:48:02 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2024 Symless Ltd.
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2024-07-22 16:48:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "ExitTimeout.h"
|
|
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string_view>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
namespace deskflow::test {
|
2024-07-22 16:48:02 +00:00
|
|
|
|
|
|
|
|
const auto checkMilliseconds = std::chrono::milliseconds(100);
|
|
|
|
|
|
|
|
|
|
using std::chrono::duration_cast;
|
|
|
|
|
using std::chrono::steady_clock;
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool timeoutReached(const steady_clock::time_point &start, const int minutes)
|
|
|
|
|
{
|
2024-07-22 16:48:02 +00:00
|
|
|
auto now = steady_clock::now();
|
|
|
|
|
auto duration = duration_cast<std::chrono::minutes>(now - start);
|
|
|
|
|
return duration.count() >= minutes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExitTimeout::ExitTimeout(const int minutes, const std::string_view &name)
|
|
|
|
|
: m_minutes(minutes),
|
|
|
|
|
m_name(name),
|
2024-10-17 18:04:35 +00:00
|
|
|
m_thread(std::make_unique<std::thread>([this]() { run(); }))
|
|
|
|
|
{
|
|
|
|
|
}
|
2024-07-22 16:48:02 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
ExitTimeout::~ExitTimeout()
|
|
|
|
|
{
|
2024-07-22 16:48:02 +00:00
|
|
|
m_running = false;
|
|
|
|
|
m_thread->join();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ExitTimeout::run() const
|
|
|
|
|
{
|
2024-07-22 16:48:02 +00:00
|
|
|
auto start = steady_clock::now();
|
|
|
|
|
while (m_running) {
|
|
|
|
|
std::this_thread::sleep_for(checkMilliseconds);
|
|
|
|
|
if (timeoutReached(start, m_minutes)) {
|
2024-10-17 18:04:35 +00:00
|
|
|
std::cerr << m_name << " timed out after " << m_minutes << " minute(s)" << std::endl;
|
2024-07-22 16:48:02 +00:00
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
} // namespace deskflow::test
|