SenseApiMgr: Use new bridge and loop classes
This commit is contained in:
@@ -5,6 +5,8 @@
|
|||||||
#include <senseApis/senseApiManager.h>
|
#include <senseApis/senseApiManager.h>
|
||||||
#include <senseApis/senseApiLib.h>
|
#include <senseApis/senseApiLib.h>
|
||||||
#include <opts.h>
|
#include <opts.h>
|
||||||
|
#include <asynchronousBridge.h>
|
||||||
|
#include <asynchronousLoop.h>
|
||||||
#include <user/senseApiDesc.h>
|
#include <user/senseApiDesc.h>
|
||||||
#include <deviceManager/deviceManager.h>
|
#include <deviceManager/deviceManager.h>
|
||||||
|
|
||||||
@@ -313,136 +315,118 @@ void SenseApiManager::detachSenseDeviceReq(
|
|||||||
|
|
||||||
void SenseApiManager::attachAllSenseDevicesFromSpecs(void)
|
void SenseApiManager::attachAllSenseDevicesFromSpecs(void)
|
||||||
{
|
{
|
||||||
std::atomic<int> nTotal = device::DeviceManager::deviceAttachmentSpecs
|
|
||||||
.size();
|
|
||||||
std::atomic<int> nSucceeded = 0, nFailed = 0;
|
|
||||||
|
|
||||||
auto self = ComponentThread::getSelf();
|
auto self = ComponentThread::getSelf();
|
||||||
|
AsynchronousBridge bridge(self->getIoService());
|
||||||
|
AsynchronousLoop loop(device::DeviceManager::deviceAttachmentSpecs.size());
|
||||||
|
|
||||||
for (const auto& spec : device::DeviceManager::deviceAttachmentSpecs)
|
for (const auto& spec : device::DeviceManager::deviceAttachmentSpecs)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
attachSenseDeviceReq(spec,
|
attachSenseDeviceReq(spec,
|
||||||
[spec, &nTotal, &nSucceeded, &nFailed, caller = self](bool success) -> void
|
[spec, &loop, &bridge](bool success) -> void
|
||||||
{
|
{
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
++nFailed;
|
++loop.nFailed;
|
||||||
std::cerr << __func__ << ": Failed to attach device: "
|
std::cerr << __func__ << ": Failed to attach device: "
|
||||||
<< spec->deviceIdentifier << "\n";
|
<< spec->deviceIdentifier << "\n";
|
||||||
|
|
||||||
caller->getIoService().post([]{});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
++nSucceeded;
|
++loop.nSucceeded;
|
||||||
if (nSucceeded.load() + nFailed.load() != nTotal.load()) {
|
if (!loop.isComplete()) { return; }
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << __func__ << ": " << nSucceeded.load()
|
std::cout << __func__ << ": " << loop.nSucceeded.load()
|
||||||
<< " devices attached, "
|
<< " devices attached, "
|
||||||
<< nFailed.load() << " devices failed\n";
|
<< loop.nFailed.load() << " devices failed\n";
|
||||||
caller->getIoService().post([]{});
|
|
||||||
|
bridge.setAsyncOperationComplete();
|
||||||
});
|
});
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::cerr << __func__ << ": Exception: " << e.what() << "\n";
|
std::cerr << __func__ << ": Exception: " << e.what() << "\n";
|
||||||
++nFailed;
|
++loop.nFailed;
|
||||||
|
if (loop.isComplete())
|
||||||
|
{ bridge.setAsyncOperationComplete(); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bridge the async op here. */
|
/* Bridge the async op here. */
|
||||||
for (;;)
|
bridge.waitForAsyncOperationCompleteOrIoServiceStopped();
|
||||||
{
|
if (bridge.exitedBecauseIoServiceStopped())
|
||||||
self->getIoService().run_one();
|
|
||||||
if ((nSucceeded.load() + nFailed.load() == nTotal.load())
|
|
||||||
|| self->getIoService().stopped())
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self->getIoService().stopped())
|
|
||||||
{
|
{
|
||||||
/* Return early because the io_service is stopped. */
|
/* Return early because the io_service is stopped. */
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nTotal.load() != nSucceeded.load() + nFailed.load())
|
if (loop.nTotal.load() != loop.nSucceeded.load() + loop.nFailed.load())
|
||||||
{
|
{
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
std::string(__func__) + ": Failed to get through all devices");
|
std::string(__func__) + ": Failed to get through all devices");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << __func__ << ": " << nSucceeded.load() << "/" << nTotal.load()
|
std::cout << __func__ << ": " << loop.nSucceeded.load() << "/"
|
||||||
<< " devices attached, "
|
<< loop.nTotal.load() << " devices attached, "
|
||||||
<< nFailed.load() << "/" << nTotal.load() << " devices failed\n";
|
<< loop.nFailed.load() << "/" << loop.nTotal.load()
|
||||||
|
<< " devices failed\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void SenseApiManager::detachAllSenseDevicesReq(void)
|
void SenseApiManager::detachAllSenseDevicesReq(void)
|
||||||
{
|
{
|
||||||
std::atomic<int> nTotal = device::DeviceManager::deviceAttachmentSpecs
|
|
||||||
.size();
|
|
||||||
std::atomic<int> nSucceeded = 0, nFailed = 0;
|
|
||||||
|
|
||||||
auto self = ComponentThread::getSelf();
|
auto self = ComponentThread::getSelf();
|
||||||
|
AsynchronousBridge bridge(self->getIoService());
|
||||||
|
AsynchronousLoop loop(device::DeviceManager::deviceAttachmentSpecs.size());
|
||||||
|
|
||||||
for (const auto& spec : device::DeviceManager::deviceAttachmentSpecs)
|
for (const auto& spec : device::DeviceManager::deviceAttachmentSpecs)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
detachSenseDeviceReq(spec,
|
detachSenseDeviceReq(spec,
|
||||||
[spec, &nTotal, &nSucceeded, &nFailed, caller = self](bool success) -> void
|
[spec, &loop, &bridge](bool success) -> void
|
||||||
{
|
{
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
++nFailed;
|
++loop.nFailed;
|
||||||
std::cerr << __func__ << ": Failed to detach device: "
|
std::cerr << __func__ << ": Failed to detach device: "
|
||||||
<< spec->deviceIdentifier << "\n";
|
<< spec->deviceIdentifier << "\n";
|
||||||
|
|
||||||
caller->getIoService().post([]{});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
++nSucceeded;
|
++loop.nSucceeded;
|
||||||
if (nSucceeded.load() + nFailed.load() != nTotal.load()) {
|
if (!loop.isComplete()) { return; }
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << __func__ << ": " << nSucceeded.load()
|
std::cout << __func__ << ": " << loop.nSucceeded.load()
|
||||||
<< " devices detached, "
|
<< " devices detached, "
|
||||||
<< nFailed.load() << " devices failed\n";
|
<< loop.nFailed.load() << " devices failed\n";
|
||||||
caller->getIoService().post([]{});
|
|
||||||
|
bridge.setAsyncOperationComplete();
|
||||||
});
|
});
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::cerr << __func__ << ": Exception: " << e.what() << "\n";
|
std::cerr << __func__ << ": Exception: " << e.what() << "\n";
|
||||||
++nFailed;
|
++loop.nFailed;
|
||||||
|
if (loop.isComplete())
|
||||||
|
{ bridge.setAsyncOperationComplete(); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bridge the async op here. */
|
/* Bridge the async op here. */
|
||||||
for (;;)
|
bridge.waitForAsyncOperationCompleteOrIoServiceStopped();
|
||||||
{
|
if (bridge.exitedBecauseIoServiceStopped())
|
||||||
self->getIoService().run_one();
|
|
||||||
if ((nSucceeded.load() + nFailed.load() == nTotal.load())
|
|
||||||
|| self->getIoService().stopped())
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self->getIoService().stopped())
|
|
||||||
{
|
{
|
||||||
/* Return early because the io_service is stopped. */
|
/* Return early because the io_service is stopped. */
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nTotal.load() != nSucceeded.load() + nFailed.load())
|
if (loop.nTotal.load() != loop.nSucceeded.load() + loop.nFailed.load())
|
||||||
{
|
{
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
std::string(__func__) + ": Failed to get through all devices");
|
std::string(__func__) + ": Failed to get through all devices");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << __func__ << ": " << nSucceeded.load() << "/" << nTotal.load()
|
std::cout << __func__ << ": " << loop.nSucceeded.load() << "/"
|
||||||
<< " devices detached, "
|
<< loop.nTotal.load() << " devices detached, "
|
||||||
<< nFailed.load() << "/" << nTotal.load() << " devices failed\n";
|
<< loop.nFailed.load() << "/" << loop.nTotal.load()
|
||||||
|
<< " devices failed\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sense_api
|
} // namespace sense_api
|
||||||
|
|||||||
Reference in New Issue
Block a user