artdaq  v3_02_00
DataSenderManager.hh
1 #ifndef ARTDAQ_DAQRATE_DATASENDERMANAGER_HH
2 #define ARTDAQ_DAQRATE_DATASENDERMANAGER_HH
3 
4 #include <map>
5 #include <set>
6 #include <memory>
7 #include <netinet/in.h>
8 
9 #include "fhiclcpp/fwd.h"
10 
11 #include "artdaq-core/Data/Fragment.hh"
12 #include "artdaq/TransferPlugins/TransferInterface.hh"
13 #include "artdaq/DAQrate/detail/FragCounter.hh"
14 #include "artdaq-utilities/Plugins/MetricManager.hh"
15 #include "artdaq/DAQrate/detail/RoutingPacket.hh"
16 #include "artdaq/TransferPlugins/detail/HostMap.hh"
17 #include "fhiclcpp/types/Atom.h"
18 #include "fhiclcpp/types/OptionalTable.h"
19 #include "fhiclcpp/types/TableFragment.h"
20 
21 namespace artdaq
22 {
23  class DataSenderManager;
24 }
25 
31 {
32 public:
33 
35  {
36  fhicl::Atom<bool> use_routing_master{ fhicl::Name{ "use_routing_master"}, fhicl::Comment{ "True if using the Routing Master"}, false };
37  fhicl::Atom<int> table_port{ fhicl::Name{ "table_update_port"}, fhicl::Comment{ "Port that table updates should arrive on" },35556 };
38  fhicl::Atom<std::string> table_address{ fhicl::Name{ "table_update_address"}, fhicl::Comment{ "Address that table updates should arrive on" }, "227.128.12.28" };
39  fhicl::Atom<int> ack_port{ fhicl::Name{ "table_acknowledge_port" },fhicl::Comment{ "Port that acknowledgements should be sent to" },35557 };
40  fhicl::Atom<std::string> ack_address{ fhicl::Name{ "routing_master_hostname"}, fhicl::Comment{ "Host that acknowledgements should be sent to" },"localhost" };
41  fhicl::Atom<int> routing_timeout_ms{ fhicl::Name{"routing_timeout_ms"}, fhicl::Comment{"Time to wait (in ms) for a routing table update if the table is exhausted"}, 1000 };
42  fhicl::Atom<int> routing_retry_count{ fhicl::Name{"routing_retry_count"}, fhicl::Comment{"Number of times to retry getting destination from routing table"}, 5 };
43  };
44 
46  {
47  fhicl::OptionalTable<artdaq::TransferInterface::Config> dest{ fhicl::Name{"d1"}, fhicl::Comment{"Configuration for transfer to destination"} };
48  };
49 
50  struct Config
51  {
52  fhicl::Atom<bool> broadcast_sends{ fhicl::Name{"broadcast_sends"}, fhicl::Comment{"Send all Fragments to all destinations"}, false };
53  fhicl::Atom<bool> nonblocking_sends{ fhicl::Name{"nonblocking_sends"}, fhicl::Comment{"Whether sends should block. Used for DL->DISP connection."}, false };
54  fhicl::Atom<size_t> send_timeout_us{ fhicl::Name{"send_timeout_usec"}, fhicl::Comment{"Timeout for sends in non-reliable modes (broadcast and nonblocking)"},5000000 };
55  fhicl::Atom<size_t> send_retry_count{ fhicl::Name{"send_retry_count"}, fhicl::Comment{"Number of times to retry a send in non-reliable mode"}, 2 };
56  fhicl::OptionalTable<RoutingTableConfig> routing_table_config{ fhicl::Name{"routing_table_config"} };
57  fhicl::OptionalTable<DestinationsConfig> destinations{ fhicl::Name{"destinations"} };
58  fhicl::TableFragment<artdaq::HostMap::Config> host_map;
59  fhicl::Sequence<size_t> enabled_destinations{ fhicl::Name{"enabled_destinations"}, fhicl::Comment{"List of destiantion ranks to activate (must be defined in destinations block)"}, std::vector<size_t>() };
60  };
61 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
62  using Parameters = fhicl::WrappedTable<Config>;
63 #endif
64 
89  explicit DataSenderManager(const fhicl::ParameterSet& ps);
90 
94  virtual ~DataSenderManager();
95 
101  std::pair<int, TransferInterface::CopyStatus> sendFragment(Fragment&& frag);
102 
107  size_t count() const;
108 
114  size_t slotCount(size_t rank) const;
115 
120  size_t destinationCount() const { return destinations_.size(); }
121 
126  std::set<int> enabled_destinations() const { return enabled_destinations_; }
127 
132  size_t GetRoutingTableEntryCount() const;
133 private:
134 
135  // Calculate where the fragment with this sequenceID should go.
136  int calcDest_(Fragment::sequence_id_t) const;
137 
138  void setupTableListener_();
139 
140  void startTableReceiverThread_();
141 
142  void receiveTableUpdatesLoop_();
143 private:
144 
145  std::map<int, std::unique_ptr<artdaq::TransferInterface>> destinations_;
146  std::set<int> enabled_destinations_;
147 
148  detail::FragCounter sent_frag_count_;
149 
150  bool broadcast_sends_;
151  bool non_blocking_mode_;
152  size_t send_timeout_us_;
153  size_t send_retry_count_;
154 
155  bool use_routing_master_;
156  detail::RoutingMasterMode routing_master_mode_;
157  std::atomic<bool> should_stop_;
158  int table_port_;
159  std::string table_address_;
160  int ack_port_;
161  std::string ack_address_;
162  struct sockaddr_in ack_addr_;
163  int ack_socket_;
164  int table_socket_;
165  std::map<Fragment::sequence_id_t, int> routing_table_;
166  mutable std::mutex routing_mutex_;
167  boost::thread routing_thread_;
168  mutable std::atomic<size_t> routing_wait_time_;
169 
170  int routing_timeout_ms_;
171  int routing_retry_count_;
172 };
173 
174 inline
175 size_t
177 count() const
178 {
179  return sent_frag_count_.count();
180 }
181 
182 inline
183 size_t
185 slotCount(size_t rank) const
186 {
187  return sent_frag_count_.slotCount(rank);
188 }
189 #endif //ARTDAQ_DAQRATE_DATASENDERMANAGER_HH
RoutingMasterMode
Mode indicating whether the RoutingMaster is routing events by Sequence ID or by Send Count...
size_t destinationCount() const
Get the number of configured destinations.
Sends Fragment objects using TransferInterface plugins. Uses Routing Tables if confgiured, otherwise will Round-Robin Fragments to the destinations.
virtual ~DataSenderManager()
DataSenderManager Destructor.
size_t slotCount(size_t rank) const
Get the count of Fragment objects sent by this DataSenderManager to a given destination.
Keep track of the count of Fragments received from a set of sources.
Definition: FragCounter.hh:20
std::pair< int, TransferInterface::CopyStatus > sendFragment(Fragment &&frag)
Send the given Fragment. Return the rank of the destination to which the Fragment was sent...
DataSenderManager(const fhicl::ParameterSet &ps)
DataSenderManager Constructor.
size_t GetRoutingTableEntryCount() const
Gets the current size of the Routing Table, in case other parts of the system want to use this inform...
size_t count() const
Return the count of Fragment objects sent by this DataSenderManagerq.
size_t slotCount(size_t slot) const
Get the current count for the requested slot.
Definition: FragCounter.hh:142
std::set< int > enabled_destinations() const
Get the list of enabled destinations.
size_t count() const
Get the total number of Fragments received.
Definition: FragCounter.hh:128