artdaq  v3_11_02
TableReceiver.hh
1 #ifndef ARTDAQ_DAQRATE_DETAIL_TABLERECEIVER_HH
2 #define ARTDAQ_DAQRATE_DETAIL_TABLERECEIVER_HH
3 
4 #include <netinet/in.h>
5 #include <map>
6 #include <memory>
7 #include <set>
8 
9 #include "fhiclcpp/fwd.h"
10 
11 #include "artdaq-core/Data/Fragment.hh"
12 #include "artdaq-utilities/Plugins/MetricManager.hh"
13 #include "artdaq/DAQrate/detail/FragCounter.hh"
14 #include "artdaq/DAQrate/detail/RoutingPacket.hh"
15 #include "artdaq/TransferPlugins/TransferInterface.hh"
16 #include "fhiclcpp/types/Atom.h"
17 #include "fhiclcpp/types/OptionalTable.h"
18 #include "fhiclcpp/types/TableFragment.h"
19 
20 namespace artdaq {
21 class TableReceiver;
22 }
23 
29 {
30 public:
36  struct Config
37  {
39  fhicl::Atom<bool> use_routing_manager{fhicl::Name{"use_routing_manager"}, fhicl::Comment{"True if using the Routing Manager"}, false};
41  fhicl::Atom<bool> route_on_request_mode{fhicl::Name{"route_on_request_mode"}, fhicl::Comment{"True if a request for routing information should be sent to the RoutingManager (versus RoutingManager pushing table updates)."}, false};
43  fhicl::Atom<bool> use_routing_table_thread{fhicl::Name{"use_routing_table_thread"}, fhicl::Comment{"True if a thread should be run to receive routing updates. Required if route_on_request_mode is false."}, true};
45  fhicl::Atom<int> table_port{fhicl::Name{"table_update_port"}, fhicl::Comment{"Port to connect to for receiving table updates"}, 35556};
47  fhicl::Atom<std::string> routing_manager_hostname{fhicl::Name{"routing_manager_hostname"}, fhicl::Comment{"outingManager hostname for Table connection"}, "localhost"};
49  fhicl::Atom<int> routing_timeout_ms{fhicl::Name{"routing_timeout_ms"}, fhicl::Comment{"Time to wait (in ms) for a routing table update"}, 1000};
51  fhicl::Atom<size_t> routing_table_max_size{fhicl::Name{"routing_table_max_size"}, fhicl::Comment{"Maximum number of entries in the routing table"}, 1000};
52  };
54  using Parameters = fhicl::WrappedTable<Config>;
55 
56  using RoutingTable = std::map<artdaq::Fragment::sequence_id_t, int>;
57 
58  static constexpr int ROUTING_FAILED = -1111;
59 
64  explicit TableReceiver(const fhicl::ParameterSet& ps);
65 
69  virtual ~TableReceiver();
70 
75 
80 
86  int GetRoutingTableEntry(artdaq::Fragment::sequence_id_t seqID);
87 
92  size_t GetRoutingTableEntryCount() const;
93 
98  size_t GetRemainingRoutingTableEntries() const;
99 
103  void StopTableReceiver() { should_stop_ = true; }
104 
109  void RemoveRoutingTableEntry(Fragment::sequence_id_t seq);
110 
114  void SendMetrics() const;
115 
119  bool RoutingManagerEnabled() const { return use_routing_manager_; }
120 
121 private:
122  TableReceiver(TableReceiver const&) = delete;
123  TableReceiver(TableReceiver&&) = delete;
124  TableReceiver& operator=(TableReceiver const&) = delete;
125  TableReceiver& operator=(TableReceiver&&) = delete;
126 
127  void connectToRoutingManager_();
128  void disconnectFromRoutingManager_();
129 
130  void startTableReceiverThread_();
131 
132  bool receiveTableUpdate_();
133  void receiveTableUpdatesLoop_();
134 
135  void sendTableUpdateRequest_(Fragment::sequence_id_t seq);
136 
137 private:
138  bool use_routing_manager_;
139  std::atomic<bool> should_stop_;
140  int table_port_;
141  std::string table_address_;
142  int table_socket_;
143  RoutingTable routing_table_;
144  Fragment::sequence_id_t routing_table_last_;
145  size_t routing_table_max_size_;
146  mutable std::mutex routing_mutex_;
147  std::unique_ptr<boost::thread> routing_thread_;
148  mutable std::atomic<size_t> routing_wait_time_;
149  mutable std::atomic<size_t> routing_wait_time_count_;
150  mutable std::condition_variable routing_cv_;
151 
152  size_t routing_timeout_ms_;
153 
154  mutable std::atomic<uint64_t> highest_sequence_id_routed_;
155 };
156 
157 #endif //ARTDAQ_DAQRATE_DETAIL_TABLERECEIVER_HH
TableReceiver(const fhicl::ParameterSet &ps)
TableReceiver Constructor.
fhicl::Atom< bool > route_on_request_mode
&quot;route_on_request_mode&quot; (Default: false): True if a request for routing information should be sent to...
fhicl::Atom< bool > use_routing_manager
&quot;use_routing_manager&quot; (Default: false): True if using the Routing Manager
fhicl::Atom< int > routing_timeout_ms
&quot;routing_timeout_ms&quot; (Default: 1000): Time to wait for a routing table update
static constexpr int ROUTING_FAILED
Value used to indicate that a route was not properly generated.
RoutingTable GetRoutingTable() const
Get a copy of the current RoutingTable.
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation (if desired)
Configuration for Routing table reception
virtual ~TableReceiver()
TableReceiver Destructor.
fhicl::Atom< bool > use_routing_table_thread
&quot;use_routing_table_thread&quot; (Default: true): True if a thread should be run to receive routing updates...
void SendMetrics() const
Report metrics to MetricManager.
void RemoveRoutingTableEntry(Fragment::sequence_id_t seq)
Remove the given sequence ID from the routing table and sent_count lists.
fhicl::Atom< int > table_port
&quot;table_update_port&quot; (Default: 35556): Port to connect to for receiving table updates ...
void StopTableReceiver()
Stop the TableReceiver.
fhicl::Atom< size_t > routing_table_max_size
&quot;routing_table_max_size&quot; (Default: 1000): Maximum number of entries in the routing table ...
int GetRoutingTableEntry(artdaq::Fragment::sequence_id_t seqID)
Get the destination rank for the given sequence ID.
RoutingTable GetAndClearRoutingTable()
Get the current RoutingTable and remove all entries.
size_t GetRemainingRoutingTableEntries() const
Gets the number of sends remaining in the routing table, in case other parts of the system want to us...
bool RoutingManagerEnabled() const
Whether the TableReceiver will receive tables from the RoutingManager.
size_t GetRoutingTableEntryCount() const
Gets the current size of the Routing Table, in case other parts of the system want to use this inform...
Sends Fragment objects using TransferInterface plugins. Uses Routing Tables if confgiured, otherwise will Round-Robin Fragments to the destinations.
fhicl::Atom< std::string > routing_manager_hostname
&quot;routing_manager_hostname&quot; (Default: &quot;localhost&quot;): RoutingManager hostname for Table connection ...
std::map< artdaq::Fragment::sequence_id_t, int > RoutingTable
Internal representation of a routing table, relating a sequence ID to a destination rank...