ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
pendingReplies.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_common_libdaq
4  * @copyright 2022 ESO - European Southern Observatory
5  *
6  * @brief Contains declaration for classes related to pending replies
7  */
8 #ifndef OCF_DAQ_PENDING_REPLIES_HPP_
9 #define OCF_DAQ_PENDING_REPLIES_HPP_
10 
11 #include <cstdint>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 #include <tuple>
16 
17 namespace daq {
18 
19 class PendingReplies;
20 
21 /**
22  * Token representing a not-yet-received reply.
23  *
24  * @ingroup daq_common_libdaq
25  */
26 class ReplyToken {
27  public:
28  ReplyToken(ReplyToken&&) = default;
30  ~ReplyToken();
31 
32  /**
33  * Release token explicitly.
34  */
35  void Release();
36 
37  protected:
39  explicit ReplyToken(uint64_t token, std::weak_ptr<PendingReplies> pending_replies) noexcept;
40 
41  private:
42  uint64_t m_token;
43  std::weak_ptr<PendingReplies> m_pending_replies;
44 };
45 
46 /**
47  * Simple class that allows you to keep track of how many replies are pending.
48  *
49  * Usage:
50  *
51  * For each request acquire a token with `acquire`.
52  * The token is movable but not copyable to guarantee uniqueness.
53  *
54  * Once reply is released release the token by deleting it.
55  *
56  * @ingroup daq_common_libdaq
57  */
58 class PendingReplies : public std::enable_shared_from_this<PendingReplies> {
59 public:
60  struct PendingReply {
61  std::string source_id;
62  std::string request;
63  };
64 
65  /**
66  * Create instance.
67  */
68  static std::shared_ptr<PendingReplies> Create();
69 
70  /**
71  * Acquire token.
72  *
73  * Keep the token alive until reply is received, then delete it.
74  */
75  ReplyToken Acquire(std::string source_id, std::string request);
76  std::shared_ptr<ReplyToken> AcquireShared(std::string source_id, std::string request);
77 
78  bool HasPendingReplies() const;
79 
80  std::vector<PendingReply> GetPendingReplies() const;
81 protected:
82  friend ReplyToken;
83  void Release(uint64_t token);
84  PendingReplies() = default;
85 private:
86  uint64_t m_next_token_id = 1;
87  std::vector<std::tuple<uint64_t, PendingReply>> m_pending;
88 };
89 
90 } // namespace daq
91 
92 #endif // #ifndef OCF_DAQ_PENDING_REPLIES_HPP_
daq::PendingReplies::Release
void Release(uint64_t token)
Definition: pendingReplies.cpp:53
daq::PendingReplies::GetPendingReplies
std::vector< PendingReply > GetPendingReplies() const
Definition: pendingReplies.cpp:30
daq::PendingReplies
Simple class that allows you to keep track of how many replies are pending.
Definition: pendingReplies.hpp:58
daq::PendingReplies::HasPendingReplies
bool HasPendingReplies() const
Definition: pendingReplies.cpp:26
daq::PendingReplies::AcquireShared
std::shared_ptr< ReplyToken > AcquireShared(std::string source_id, std::string request)
Definition: pendingReplies.cpp:45
daq::PendingReplies::ReplyToken
friend ReplyToken
Definition: pendingReplies.hpp:82
daq::ReplyToken
Token representing a not-yet-received reply.
Definition: pendingReplies.hpp:26
daq::ReplyToken::PendingReplies
friend PendingReplies
Definition: pendingReplies.hpp:38
daq::ReplyToken::operator=
ReplyToken & operator=(ReplyToken &&)=default
daq
Definition: asyncProcess.cpp:15
daq::PendingReplies::PendingReplies
PendingReplies()=default
daq::PendingReplies::Create
static std::shared_ptr< PendingReplies > Create()
Create instance.
Definition: pendingReplies.cpp:22
daq::ReplyToken::ReplyToken
ReplyToken(ReplyToken &&)=default
daq::ReplyToken::~ReplyToken
~ReplyToken()
Definition: pendingReplies.cpp:11
daq::PendingReplies::PendingReply::source_id
std::string source_id
Definition: pendingReplies.hpp:61
daq::PendingReplies::PendingReply::request
std::string request
Definition: pendingReplies.hpp:62
daq::PendingReplies::Acquire
ReplyToken Acquire(std::string source_id, std::string request)
Acquire token.
Definition: pendingReplies.cpp:38
daq::ReplyToken::Release
void Release()
Release token explicitly.
Definition: pendingReplies.cpp:16
daq::PendingReplies::PendingReply
Definition: pendingReplies.hpp:60