Files
salmanoff/smocore/deviceManager/deviceReattacher.cpp
T

90 lines
2.4 KiB
C++
Raw Normal View History

#include <config.h>
#include <iostream>
#include <functional>
#include <componentThread.h>
2026-06-09 11:19:42 -04:00
#include <adapters/boostAsio/deadlineTimerAReq.h>
#include <deviceManager/deviceReattacher.h>
#include <deviceManager/deviceManager.h>
#include <spinscale/co/nonViralCompletion.h>
namespace smo {
namespace device {
DeviceReattacher::DeviceReattacher(
2025-12-27 16:21:22 -04:00
DeviceManager& parent, std::shared_ptr<sscl::ComponentThread> ioThread)
2026-06-09 11:19:42 -04:00
: parent(parent),
ioThread(ioThread), daemonTimer(ioThread->getIoContext())
{
/** EXPLANATION:
2026-06-09 11:19:42 -04:00
* deviceReattacherCDaemon is a dynamic posting non-viral coroutine: start()
* passes ExplicitPostTarget{ioThread->getIoContext()} so the daemon body
* always runs on ioThread. daemonTimer is reused each loop iteration.
*/
}
2026-06-09 11:19:42 -04:00
sscl::co::DynamicNonViralPostingInvoker
DeviceReattacher::deviceReattacherCDaemon(
[[maybe_unused]] sscl::co::ExplicitPostTarget postTarget,
[[maybe_unused]] std::exception_ptr &exceptionPtr,
2026-06-09 11:19:42 -04:00
[[maybe_unused]] std::function<void()> callback,
sscl::SyncCancelerForAsyncWork &canceler)
{
2026-06-09 11:19:42 -04:00
boost::asio::io_context &timerIoContext =
sscl::ComponentThread::getSelf()->getIoContext();
const auto periodMs = boost::posix_time::milliseconds(
CONFIG_MRNTT_DEVMGR_REATTACHER_PERIOD_MS);
2026-06-09 11:19:42 -04:00
while (!canceler.isCancellationRequested())
2025-11-27 22:52:09 -04:00
{
2026-06-09 11:19:42 -04:00
const bool expiredNormally = co_await
adapters::boostAsio::getDeadlineTimerAReqAwaiter(
timerIoContext, daemonTimer, periodMs);
2026-06-09 11:19:42 -04:00
if (!expiredNormally) {
break;
}
2026-06-09 11:19:42 -04:00
co_await parent.attachAllUnattachedDevicesFromKnownListCReq();
2025-09-28 23:35:20 -04:00
}
2026-06-09 11:19:42 -04:00
co_return;
}
2026-06-09 11:19:42 -04:00
void DeviceReattacher::start()
{
2026-06-09 11:19:42 -04:00
taskNursery.openAdmission();
taskNursery.launch(
[this](sscl::co::NonViralTaskNursery::Slot::Lease &lease)
{
return deviceReattacherCDaemon(
sscl::co::ExplicitPostTarget{ioThread->getIoContext()},
lease.getExceptionStorage(),
lease.getCallerLambda(),
lease.getSyncCanceler());
},
[](std::exception_ptr &exceptionPtr)
{
2026-06-09 11:19:42 -04:00
sscl::co::NonViralCompletion nvc(exceptionPtr);
if (nvc.hasException())
{
try {
nvc.checkAndRethrowException();
} catch (const std::exception &e) {
2026-06-09 11:19:42 -04:00
std::cerr << "DeviceReattacher: "
<< e.what() << std::endl;
}
}
2026-06-09 11:19:42 -04:00
});
}
2026-06-09 11:19:42 -04:00
void DeviceReattacher::stop()
{
2026-06-09 11:19:42 -04:00
daemonTimer.cancel();
taskNursery.requestCancelOnAll();
taskNursery.closeAdmission();
taskNursery.syncAwaitAllSettlements(ioThread->getIoContext());
}
} // namespace device
} // namespace smo