ifw-daq  3.0.0-pre2
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_
Simple class that allows you to keep track of how many replies are pending.
static std::shared_ptr< PendingReplies > Create()
Create instance.
void Release(uint64_t token)
ReplyToken Acquire(std::string source_id, std::string request)
Acquire token.
bool HasPendingReplies() const
std::shared_ptr< ReplyToken > AcquireShared(std::string source_id, std::string request)
PendingReplies()=default
std::vector< PendingReply > GetPendingReplies() const
Token representing a not-yet-received reply.
ReplyToken & operator=(ReplyToken &&)=default
void Release()
Release token explicitly.
ReplyToken(ReplyToken &&)=default