14 #include <boost/asio/io_context.hpp> 
   15 #include <boost/throw_exception.hpp> 
   17 #include <fmt/format.h> 
   18 #include <log4cplus/loggingmacros.h> 
   29     : m_executor(executor)
 
   31     , m_workspace(workspace)
 
   32     , m_scheduler(scheduler)
 
   33     , m_work_guard(m_executor.get_io_context().get_executor())
 
   34     , m_sigset(executor.get_io_context(), SIGINT, SIGTERM)
 
   35     , m_logger(log4cplus::Logger::getInstance(
"dpm.service")) {
 
   36     InitiateSignalHandler();
 
   44         std::shared_ptr<daqif::StorageStatus> rep = m_mal.createDataEntity<daqif::StorageStatus>();
 
   45         rep->setCapacity(status.capacity);
 
   46         rep->setFree(status.free);
 
   47         rep->setAvailable(status.available);
 
   49         return boost::make_ready_future<std::shared_ptr<daqif::StorageStatus>>(rep);
 
   52         return boost::make_exceptional_future<std::shared_ptr<daqif::StorageStatus>>(
 
   53             daqif::Exception(r.
Str()));
 
   62     LOG4CPLUS_INFO(m_logger, 
"Application termination requested with Exit()");
 
   63     return boost::async(m_executor, [
this]() 
mutable -> std::string {
 
   70     return boost::make_ready_future(std::string(VERSION));
 
   73 boost::future<std::shared_ptr<::daqif::DaqReply>>
 
   75     LOG4CPLUS_DEBUG(m_logger, fmt::format(
"QueueDaq() Queuing new DAQ"));
 
   76     using V = ::daqif::DaqReply;
 
   77     using R = std::shared_ptr<V>;
 
   78     return boost::async(m_executor, [specification, 
this]() 
mutable -> R {
 
   80             auto id = m_scheduler.
QueueDaq(specification);
 
   82                 auto rep = m_mal.createDataEntity<V>();
 
   88                 BOOST_THROW_EXCEPTION(daqif::DaqException(
id, r.
Str()));
 
   90         } 
catch (daqif::DaqException 
const& e) {
 
   92             LOG4CPLUS_ERROR(m_logger, 
"QueueDaq() failed with ICD error:\n" << r.
Str());
 
   96             LOG4CPLUS_ERROR(m_logger, 
"QueueDaq() failed with following error(s):\n" << r);
 
   97             BOOST_THROW_EXCEPTION(daqif::DaqException(
"", r.
Str()));
 
  103     LOG4CPLUS_INFO(m_logger, fmt::format(
"AbortDaq({}): Handling request", 
id));
 
  104     using V = ::daqif::DaqReply;
 
  105     using R = std::shared_ptr<V>;
 
  106     return boost::async(m_executor, [
id, 
this]() 
mutable -> R {
 
  110                 auto rep = m_mal.createDataEntity<V>();
 
  112                 rep->setError(
false);
 
  116                 throw daqif::DaqException(
id, r.
Str());
 
  118         } 
catch (daqif::DaqException 
const& e) {
 
  120             LOG4CPLUS_ERROR(m_logger, 
"AbortDaq() failed with following error(s):\n" << r);
 
  124             LOG4CPLUS_ERROR(m_logger, 
"AbortDaq() failed with following error(s):\n" << r);
 
  125             BOOST_THROW_EXCEPTION(daqif::DaqException(
"", r.
Str()));
 
  131     LOG4CPLUS_INFO(m_logger, fmt::format(
"GetDaqStatus({}): Handling request", 
id));
 
  132     using V = ::daqif::DaqStatus;
 
  133     using R = std::shared_ptr<V>;
 
  134     return boost::async(m_executor, [
id, 
this]() 
mutable -> R {
 
  136             return GetDaqStatusSync(
id);
 
  137         } 
catch (daqif::DaqException 
const& e) {
 
  139             LOG4CPLUS_ERROR(m_logger, 
"GetDaqStatus() failed with following error(s):\n" << r);
 
  143             LOG4CPLUS_ERROR(m_logger, 
"GetDaqStatus() failed with following error(s):\n" << r);
 
  144             BOOST_THROW_EXCEPTION(daqif::DaqException(
id, r.
Str()));
 
  150     LOG4CPLUS_INFO(m_logger, 
"GetActiveDaqs(): Handling request");
 
  151     using V = std::shared_ptr<::daqif::DaqStatus>;
 
  152     using R = std::vector<V>;
 
  153     return boost::async(m_executor, [
this]() 
mutable -> R {
 
  157             for (
auto const& 
id : ids) {
 
  158                 statuses.emplace_back(GetDaqStatusSync(
id));
 
  161         } 
catch (daqif::DaqException 
const& e) {
 
  163             LOG4CPLUS_ERROR(m_logger, 
"GetActiveDaqs() failed with following error(s):\n" << r);
 
  167             LOG4CPLUS_ERROR(m_logger, 
"GetActiveDaqs() failed with following error(s):\n" << r);
 
  168             BOOST_THROW_EXCEPTION(daqif::DaqException(
"", r.
Str()));
 
  173 void DpmService::InitiateSignalHandler() {
 
  174     m_sigset.async_wait([&](boost::system::error_code 
const& ec, 
int sig_num) {
 
  178         LOG4CPLUS_INFO(m_logger,
 
  179                        fmt::format(
"Application termination requested with signal {}", sig_num));
 
  184 void DpmService::HandleExit() {
 
  185     LOG4CPLUS_TRACE(m_logger, 
"HandleExit(): Application termination has been requested");
 
  187     if (m_work_guard.owns_work()) {
 
  189         m_work_guard.reset();
 
  194         LOG4CPLUS_INFO(m_logger,
 
  195                        "Application termination already requested before. Stopping immediately!");
 
  200 std::shared_ptr<daqif::DaqStatus> DpmService::GetDaqStatusSync(
const std::string& 
id) {
 
  201     LOG4CPLUS_INFO(m_logger, fmt::format(
"GetDaqStatusSync({}): Handling request", 
id));
 
  202     using V = ::daqif::DaqStatus;
 
  205         auto rep = m_mal.createDataEntity<V>();
 
  209         error::NestedExceptionReporter r(std::current_exception());
 
  210         BOOST_THROW_EXCEPTION(daqif::DaqException(
id, r.Str()));