artdaq  v3_09_00
RequestSender.hh
1 #ifndef artdaq_DAQrate_RequestSender_hh
2 #define artdaq_DAQrate_RequestSender_hh
3 
4 #include "artdaq-core/Data/RawEvent.hh"
5 #include "artdaq-utilities/Plugins/MetricManager.hh"
6 #include "artdaq/DAQdata/Globals.hh" // Before trace.h gets included in ConcurrentQueue (from GlobalQueue)
7 #include "artdaq/DAQrate/detail/RequestMessage.hh"
8 #include "fhiclcpp/ParameterSet.h"
9 #include "fhiclcpp/types/Table.h"
10 
11 #include <arpa/inet.h>
12 #include <netinet/in.h>
13 #include <sys/socket.h>
14 #include <sys/types.h>
15 #include <chrono>
16 #include <cstdint>
17 #include <future>
18 #include <map>
19 #include <memory>
20 
21 namespace artdaq {
22 
27 {
28 public:
35  {
37  fhicl::Atom<bool> use_routing_manager{fhicl::Name{"use_routing_manager"}, fhicl::Comment{"True if using the Routing Manager"}, false};
39  fhicl::Atom<int> routing_token_port{fhicl::Name{"routing_token_port"}, fhicl::Comment{"Port to send tokens on"}, 35555};
41  fhicl::Atom<std::string> routing_token_host{fhicl::Name{"routing_manager_hostname"}, fhicl::Comment{"Hostname or IP of RoutingManager"}, "localhost"};
42  };
43 
47  struct Config
48  {
50  fhicl::Atom<bool> send_requests{fhicl::Name{"send_requests"}, fhicl::Comment{"Enable sending Data Request messages"}, false};
52  fhicl::Atom<int> request_port{fhicl::Name{"request_port"}, fhicl::Comment{"Port to send DataRequests on"}, 3001};
54  fhicl::Atom<size_t> request_delay_ms{fhicl::Name{"request_delay_ms"}, fhicl::Comment{"How long to wait before sending new DataRequests"}, 10};
56  fhicl::Atom<size_t> request_shutdown_timeout_us{fhicl::Name{"request_shutdown_timeout_us"}, fhicl::Comment{"How long to wait for pending requests to be sent at shutdown"}, 100000};
58  fhicl::Atom<std::string> output_address{fhicl::Name{"multicast_interface_ip"}, fhicl::Comment{"Use this hostname for multicast output(to assign to the proper NIC)"}, "0.0.0.0"};
60  fhicl::Atom<std::string> request_address{fhicl::Name{"request_address"}, fhicl::Comment{"Multicast address to send DataRequests to"}, "227.128.12.26"};
61  fhicl::Table<RoutingTokenConfig> routing_token_config{fhicl::Name{"routing_token_config"}, fhicl::Comment{"FHiCL table containing RoutingToken configuration"}};
62  };
64  using Parameters = fhicl::WrappedTable<Config>;
65 
69  RequestSender() = delete;
70 
74  RequestSender(RequestSender const&) = delete;
75 
80  RequestSender& operator=(RequestSender const&) = delete;
81 
82  RequestSender(RequestSender&&) = delete;
84 
89  explicit RequestSender(const fhicl::ParameterSet& pset);
93  virtual ~RequestSender();
94 
100 
105  detail::RequestMessageMode GetRequestMode() const { return request_mode_; }
106 
111  void SendRequest(bool endOfRunOnly = false);
112 
118  void AddRequest(Fragment::sequence_id_t seqID, Fragment::timestamp_t timestamp);
119 
124  void RemoveRequest(Fragment::sequence_id_t seqID);
125 
131  void SendRoutingToken(int nSlots, int run_number);
132 
137  size_t GetSentTokenCount() const { return tokens_sent_.load(); }
138 
143  void SetRunNumber(uint32_t run) { run_number_ = run; }
144 
149  bool RoutingTokenSendsEnabled() { return send_routing_tokens_; }
150 
157  bool RequestsInFlight() { return request_sending_.load() != 0; }
158 
159 private:
160 private:
161  // Request stuff
162  bool send_requests_;
163  std::atomic<bool> initialized_;
164  mutable std::mutex request_mutex_;
165  mutable std::mutex request_send_mutex_;
166  std::map<Fragment::sequence_id_t, Fragment::timestamp_t> active_requests_;
167  std::string request_address_;
168  int request_port_;
169  size_t request_delay_;
170  size_t request_shutdown_timeout_us_;
171  int request_socket_;
172  struct sockaddr_in request_addr_;
173  std::string multicast_out_addr_;
174  detail::RequestMessageMode request_mode_;
175 
176  bool send_routing_tokens_;
177  int token_port_;
178  int token_socket_;
179  std::string token_address_;
180  std::atomic<int> request_sending_;
181  std::atomic<size_t> tokens_sent_;
182  uint32_t run_number_;
183 
184 private:
185  void setup_requests_();
186 
187  void do_send_request_();
188 
189  void setup_tokens_();
190 
191  void send_routing_token_(int nSlots, int run_number);
192 };
193 } // namespace artdaq
194 #endif /* artdaq_DAQrate_RequestSender_hh */
void RemoveRequest(Fragment::sequence_id_t seqID)
Remove a request from the request list.
size_t GetSentTokenCount() const
Get the count of number of tokens sent.
detail::RequestMessageMode GetRequestMode() const
Get the mode for RequestMessages.
fhicl::Atom< int > request_port
&quot;request_port&quot; (Default: 3001): Port to send DataRequests on
The RequestSender contains methods used to send data requests and Routing tokens. ...
void SendRoutingToken(int nSlots, int run_number)
Send a RoutingToken message indicating that slots are available.
virtual ~RequestSender()
RequestSender Destructor.
void SendRequest(bool endOfRunOnly=false)
Send a request message containing all current requests.
fhicl::Atom< bool > send_requests
&quot;send_requests&quot; (Default: false): Whether to send DataRequests when new sequence IDs are seen ...
Configuration for Routing token sending
RequestSender()=delete
Default Constructor is deleted.
void SetRunNumber(uint32_t run)
Set the run number to be used in request messages.
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation (if desired)
fhicl::Atom< std::string > output_address
&quot;multicast_interface_ip&quot; (Default: &quot;0.0.0.0&quot;): Use this hostname for multicast output (to assign to t...
void SetRequestMode(detail::RequestMessageMode mode)
Set the mode for RequestMessages. Used to indicate when RequestSender should enter &quot;EndOfRun&quot; mode...
RequestMessageMode
Mode used to indicate current run conditions to the request receiver.
fhicl::Atom< size_t > request_shutdown_timeout_us
&quot;request_shutdown_timeout_us&quot; (Default: 100000 us): How long to wait for pending requests to be sent ...
fhicl::Table< RoutingTokenConfig > routing_token_config
Configuration for sending RoutingTokens. See artdaq::RequestSender::RoutingTokenConfig.
bool RoutingTokenSendsEnabled()
Determine if routing token sends are enabled.
fhicl::Atom< std::string > routing_token_host
&quot;routing_manager_hostname&quot; (Default: &quot;localhost&quot;) : Hostname or IP of RoutingManager ...
RequestSender & operator=(RequestSender const &)=delete
Copy Assignment operator is deleted.
fhicl::Atom< size_t > request_delay_ms
&quot;request_delay_ms&quot; (Default: 10): How long to wait before sending new DataRequests ...
bool RequestsInFlight()
Determine if the RequestSender is currently sending any requests.
fhicl::Atom< bool > use_routing_manager
&quot;use_routing_manager&quot; (Default: false) : Whether to send tokens to a RoutingManager ...
fhicl::Atom< int > routing_token_port
&quot;routing_token_port&quot; (Default: 35555) : Port to send tokens on
fhicl::Atom< std::string > request_address
&quot;request_address&quot; (Default: &quot;227.128.12.26&quot;): Multicast address to send DataRequests to ...
void AddRequest(Fragment::sequence_id_t seqID, Fragment::timestamp_t timestamp)
Add a request to the request list.
Configuration of the RequestSender. May be used for parameter validation