DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
QueueReceiverModel.hxx
Go to the documentation of this file.
1
5
6#include "logging/Logging.hpp"
7
8#include <atomic>
9#include <memory>
10#include <optional>
11#include <string>
12#include <thread>
13#include <typeinfo>
14#include <utility>
15
16namespace dunedaq::iomanager {
17
18template<typename Datatype>
20 : ReceiverConcept<Datatype>(request)
21{
22 TLOG() << "QueueReceiverModel created with DT! Addr: " << this;
23 // get queue ref from queueregistry based on conn_id
24 // std::string sink_name = conn_id to sink_name;
25 // m_source = std::make_unique<appfwk::DAQSource<Datatype>>(sink_name);
26 m_queue = QueueRegistry::get().get_queue<Datatype>(request.uid);
27 TLOG() << "QueueReceiverModel m_queue=" << static_cast<void*>(m_queue.get());
28}
29
30template<typename Datatype>
32 : ReceiverConcept<Datatype>(other.m_conn.uid)
33 , m_with_callback(other.m_with_callback.load())
34 , m_callback(std::move(other.m_callback))
35 , m_event_loop_runner(std::move(other.m_event_loop_runner))
36 , m_queue(std::move(other.m_queue))
37{
38}
39
40template<typename Datatype>
41inline Datatype
43{
44 if (m_with_callback) {
45 TLOG() << "QueueReceiver model is equipped with callback! Ignoring receive call.";
46 throw ReceiveCallbackConflict(ERS_HERE, this->id().uid);
47 }
48 if (m_queue == nullptr) {
49 throw ConnectionInstanceNotFound(ERS_HERE, this->id().uid);
50 }
51 // TLOG() << "Hand off data...";
52 Datatype dt;
53 try {
54 m_queue->pop(dt, timeout);
55 } catch (QueueTimeoutExpired& ex) {
56 throw TimeoutExpired(ERS_HERE, this->id().uid, "pop", timeout.count(), ex);
57 }
58 return dt;
59 // if (m_queue->write(
60}
61
62template<typename Datatype>
63inline std::optional<Datatype>
65{
66 if (m_with_callback) {
67 TLOG() << "QueueReceiver model is equipped with callback! Ignoring receive call.";
68 ers::error(ReceiveCallbackConflict(ERS_HERE, this->id().uid));
69 return std::nullopt;
70 }
71 if (m_queue == nullptr) {
72 ers::error(ConnectionInstanceNotFound(ERS_HERE, this->id().uid));
73 return std::nullopt;
74 }
75 // TLOG() << "Hand off data...";
76 Datatype dt;
77 auto ret = m_queue->try_pop(dt, timeout);
78 if (ret) {
79 return std::make_optional(std::move(dt));
80 }
81 return std::nullopt;
82 // if (m_queue->write(
83}
84
85template<typename Datatype>
86inline void
87QueueReceiverModel<Datatype>::add_callback(std::function<void(Datatype&)> callback)
88{
89 remove_callback();
90 TLOG() << "Registering callback.";
91 m_callback = callback;
92 m_with_callback = true;
93 // start event loop (thread that calls when receive happens)
94 m_event_loop_runner = std::make_unique<std::thread>([&]() {
95 Datatype dt;
96 bool ret = true;
97 while (m_with_callback.load() || ret) {
98 // TLOG() << "Take data from q then invoke callback...";
99 ret = m_queue->try_pop(dt, m_with_callback.load() ? std::chrono::milliseconds(1) : std::chrono::milliseconds(0));
100 if (ret) {
101 m_callback(dt);
102 }
103 }
104 });
105}
106
107template<typename Datatype>
108inline void
110{
111 m_with_callback = false;
112 if (m_event_loop_runner != nullptr && m_event_loop_runner->joinable()) {
113 m_event_loop_runner->join();
114 m_event_loop_runner.reset(nullptr);
115 } else if (m_event_loop_runner != nullptr) {
116 TLOG() << "Event loop can't be closed!";
117 }
118 // remove function.
119}
120
121} // namespace dunedaq::iomanager
#define ERS_HERE
QueueReceiverModel(ConnectionId const &request)
std::shared_ptr< Queue< Datatype > > m_queue
void add_callback(std::function< void(Datatype &)> callback) override
std::optional< Datatype > try_receive(Receiver::timeout_t timeout) override
Datatype receive(Receiver::timeout_t timeout) override
static QueueRegistry & get()
Get a handle to the QueueRegistry.
std::shared_ptr< Queue< T > > get_queue(const std::string &name)
Get a handle to a Queue.
std::chrono::milliseconds timeout_t
Definition Receiver.hpp:27
#define TLOG(...)
Definition macro.hpp:22
void error(const Issue &issue)
Definition ers.hpp:81