artdaq  v3_07_02
TokenReceiver.hh
1 #ifndef ARTDAQ_DAQRATE_TOKEN_RECEIVER_HH
2 #define ARTDAQ_DAQRATE_TOKEN_RECEIVER_HH
3 
4 #include "artdaq/DAQrate/StatisticsHelper.hh"
5 #include "artdaq/RoutingPolicies/RoutingMasterPolicy.hh"
6 #include "fhiclcpp/ParameterSet.h"
7 #include "fhiclcpp/types/Atom.h"
8 #include "fhiclcpp/types/ConfigurationTable.h"
9 
10 #include <sys/epoll.h>
11 #include <boost/thread.hpp>
12 #include <map>
13 
14 namespace artdaq {
19 {
20 public:
24  struct Config
25  {
27  fhicl::Atom<int> routing_token_port{fhicl::Name{"routing_token_port"}, fhicl::Comment{"Port to listen for routing tokens on"}, 355555};
28  };
30  using Parameters = fhicl::WrappedTable<Config>;
31 
40  explicit TokenReceiver(const fhicl::ParameterSet& ps, std::shared_ptr<RoutingMasterPolicy> policy,
41  detail::RoutingMasterMode routing_mode, size_t number_of_senders, size_t update_interval_msec);
42 
46  virtual ~TokenReceiver();
47 
51  void startTokenReception();
52 
56  void pauseTokenReception() { reception_is_paused_ = true; }
57 
61  void resumeTokenReception() { reception_is_paused_ = false; }
62 
67  void stopTokenReception(bool force = false);
68 
74  void setStatsHelper(std::shared_ptr<StatisticsHelper> helper, std::string const& stat_key)
75  {
76  statsHelperPtr_ = helper;
77  tokens_received_stat_key_ = stat_key;
78  }
79 
84  void setRunNumber(uint32_t run) { run_number_ = run; }
85 
90  size_t getReceivedTokenCount() const { return received_token_count_; }
91 
92 private:
93  void receiveTokensLoop_();
94 
95  int token_port_;
96  std::shared_ptr<RoutingMasterPolicy> policy_;
97  detail::RoutingMasterMode routing_mode_;
98  size_t number_of_senders_;
99  size_t update_interval_msec_;
100 
101  int token_socket_;
102  std::vector<epoll_event> receive_token_events_;
103  std::unordered_map<int, std::string> receive_token_addrs_;
104  int token_epoll_fd_;
105 
106  boost::thread token_thread_;
107  std::atomic<bool> thread_is_running_;
108  std::atomic<bool> reception_is_paused_;
109  std::atomic<bool> shutdown_requested_;
110  std::atomic<uint32_t> run_number_;
111 
112  std::atomic<size_t> received_token_count_;
113  std::unordered_map<int, size_t> received_token_counter_;
114  std::shared_ptr<StatisticsHelper> statsHelperPtr_;
115  std::string tokens_received_stat_key_;
116 };
117 } // namespace artdaq
118 
119 #endif //ARTDAQ_DAQRATE_TOKEN_RECEIVER_HH
Configuration of the TokenReceiver. May be used for parameter validation.
RoutingMasterMode
Mode indicating whether the RoutingMaster is routing events by Sequence ID or by Send Count...
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation (if desired)
Receives event builder &quot;free buffer&quot; tokens and adds them to a specified RoutingPolicy.
size_t getReceivedTokenCount() const
Returns the number of tokens that have been received.
void stopTokenReception(bool force=false)
Stops the reception of event builder tokens.
void setRunNumber(uint32_t run)
Sets the current run number.
TokenReceiver(const fhicl::ParameterSet &ps, std::shared_ptr< RoutingMasterPolicy > policy, detail::RoutingMasterMode routing_mode, size_t number_of_senders, size_t update_interval_msec)
TokenReceiver Constructor.
Definition: TokenReceiver.cc:8
fhicl::Atom< int > routing_token_port
&quot;routing_token_port&quot; (Default: 35555) : Port on which routing tokens will be received ...
void startTokenReception()
Starts the reception of event builder tokens.
void setStatsHelper(std::shared_ptr< StatisticsHelper > helper, std::string const &stat_key)
Specifies a StatisticsHelper instance to use when gathering statistics.
virtual ~TokenReceiver()
TokenReceiver Destructor.
void pauseTokenReception()
Temporarily suspends the reception of event builder tokens.
void resumeTokenReception()
Resumes the reception of event builder tokens after a suspension.