2025-09-28 23:07:39 -04:00
|
|
|
#include <config.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <componentThread.h>
|
2026-06-09 11:19:42 -04:00
|
|
|
#include <adapters/boostAsio/deadlineTimerAReq.h>
|
2025-09-28 23:07:39 -04:00
|
|
|
#include <deviceManager/deviceReattacher.h>
|
|
|
|
|
#include <deviceManager/deviceManager.h>
|
2026-06-07 19:37:50 -04:00
|
|
|
#include <spinscale/co/nonViralCompletion.h>
|
2025-09-28 23:07:39 -04:00
|
|
|
|
|
|
|
|
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())
|
2025-09-28 23:07:39 -04:00
|
|
|
{
|
2026-06-07 19:37:50 -04:00
|
|
|
/** 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-07 19:37:50 -04:00
|
|
|
*/
|
2025-09-28 23:07:39 -04:00
|
|
|
}
|
|
|
|
|
|
2026-06-09 11:19:42 -04:00
|
|
|
sscl::co::DynamicNonViralPostingInvoker
|
|
|
|
|
DeviceReattacher::deviceReattacherCDaemon(
|
|
|
|
|
[[maybe_unused]] sscl::co::ExplicitPostTarget postTarget,
|
2026-05-24 16:12:29 -04:00
|
|
|
[[maybe_unused]] std::exception_ptr &exceptionPtr,
|
2026-06-09 11:19:42 -04:00
|
|
|
[[maybe_unused]] std::function<void()> callback,
|
|
|
|
|
sscl::SyncCancelerForAsyncWork &canceler)
|
2025-09-28 23:07:39 -04:00
|
|
|
{
|
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);
|
2025-09-28 23:07:39 -04:00
|
|
|
|
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);
|
2025-10-31 08:16:05 -04:00
|
|
|
|
2026-06-09 11:19:42 -04:00
|
|
|
if (!expiredNormally) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-09-28 23:07:39 -04:00
|
|
|
|
2026-06-09 11:19:42 -04:00
|
|
|
co_await parent.attachAllUnattachedDevicesFromKnownListCReq();
|
2025-09-28 23:35:20 -04:00
|
|
|
}
|
2025-09-28 23:07:39 -04:00
|
|
|
|
2026-06-09 11:19:42 -04:00
|
|
|
co_return;
|
2025-09-28 23:07:39 -04:00
|
|
|
}
|
|
|
|
|
|
2026-06-09 11:19:42 -04:00
|
|
|
void DeviceReattacher::start()
|
2026-05-24 16:12:29 -04:00
|
|
|
{
|
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-05-24 16:12:29 -04:00
|
|
|
{
|
2026-06-09 11:19:42 -04:00
|
|
|
sscl::co::NonViralCompletion nvc(exceptionPtr);
|
2026-06-07 19:37:50 -04:00
|
|
|
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-07 19:37:50 -04:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-09 11:19:42 -04:00
|
|
|
});
|
2026-05-24 16:12:29 -04:00
|
|
|
}
|
|
|
|
|
|
2026-06-09 11:19:42 -04:00
|
|
|
void DeviceReattacher::stop()
|
2025-09-28 23:07:39 -04:00
|
|
|
{
|
2026-06-09 11:19:42 -04:00
|
|
|
daemonTimer.cancel();
|
|
|
|
|
taskNursery.requestCancelOnAll();
|
|
|
|
|
taskNursery.closeAdmission();
|
|
|
|
|
taskNursery.syncAwaitAllSettlements(ioThread->getIoContext());
|
2025-09-28 23:07:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace device
|
|
|
|
|
} // namespace smo
|