00001 #ifndef artdaq_DAQrate_RequestSender_hh
00002 #define artdaq_DAQrate_RequestSender_hh
00003
00004 #include "artdaq/DAQdata/Globals.hh"
00005 #include "artdaq-core/Data/RawEvent.hh"
00006 #include "artdaq-utilities/Plugins/MetricManager.hh"
00007 #include "artdaq/DAQrate/detail/RequestMessage.hh"
00008 #include "fhiclcpp/ParameterSet.h"
00009 #include "fhiclcpp/types/Table.h"
00010
00011 #include <map>
00012 #include <memory>
00013 #include <chrono>
00014 #include <future>
00015 #include <stdint.h>
00016 #include <arpa/inet.h>
00017 #include <netinet/in.h>
00018 #include <sys/types.h>
00019 #include <sys/socket.h>
00020
00021 namespace artdaq
00022 {
00023
00027 class RequestSender
00028 {
00029 public:
00030 struct RoutingTokenConfig
00031 {
00032 fhicl::Atom<bool> use_routing_master{ fhicl::Name{ "use_routing_master" }, fhicl::Comment{ "True if using the Routing Master" }, false };
00033 fhicl::Atom<int> routing_token_port{ fhicl::Name{ "routing_token_port" },fhicl::Comment{ "Port to send tokens on" },35555 };
00034 fhicl::Atom<std::string> routing_token_host{ fhicl::Name{ "routing_master_hostname" }, fhicl::Comment{ "Hostname or IP of RoutingMaster" },"localhost" };
00035 };
00036
00037 struct Config
00038 {
00039 fhicl::Atom<bool> send_requests{ fhicl::Name{ "send_requests" }, fhicl::Comment{ "Enable sending Data Request messages" }, false };
00040 fhicl::Atom<int> request_port{ fhicl::Name{"request_port"}, fhicl::Comment{"Port to send DataRequests on"},3001 };
00041 fhicl::Atom<size_t> request_delay_ms{ fhicl::Name{"request_delay_ms"}, fhicl::Comment{"How long to wait before sending new DataRequests"}, 10 };
00042 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 };
00043 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" };
00044 fhicl::Atom<std::string> request_address{ fhicl::Name{"request_address"}, fhicl::Comment{ "Multicast address to send DataRequests to" }, "227.128.12.26" };
00045 fhicl::Table<RoutingTokenConfig> routing_token_config{ fhicl::Name{"routing_token_config"}, fhicl::Comment{"FHiCL table containing RoutingToken configuration"} };
00046 };
00047 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
00048 using Parameters = fhicl::WrappedTable<Config>;
00049 #endif
00050
00054 RequestSender() = delete;
00055
00059 RequestSender(RequestSender const&) = delete;
00060
00065 RequestSender& operator=(RequestSender const&) = delete;
00066
00085 RequestSender(const fhicl::ParameterSet& pset);
00089 virtual ~RequestSender();
00090
00091
00096 void SetRequestMode(detail::RequestMessageMode mode);
00097
00102 detail::RequestMessageMode GetRequestMode() const { return request_mode_; }
00103
00108 void SendRequest(bool endOfRunOnly = false);
00109
00115 void AddRequest(Fragment::sequence_id_t seqID, Fragment::timestamp_t timestamp);
00116
00121 void RemoveRequest(Fragment::sequence_id_t seqID);
00122
00127 void SendRoutingToken(int nSlots);
00128 private:
00129
00130
00131 bool send_requests_;
00132 mutable std::mutex request_mutex_;
00133 mutable std::mutex request_send_mutex_;
00134 std::map<Fragment::sequence_id_t, Fragment::timestamp_t> active_requests_;
00135 std::string request_address_;
00136 int request_port_;
00137 size_t request_delay_;
00138 size_t request_shutdown_timeout_us_;
00139 int request_socket_;
00140 struct sockaddr_in request_addr_;
00141 std::string multicast_out_addr_;
00142 detail::RequestMessageMode request_mode_;
00143
00144 bool send_routing_tokens_;
00145 int token_port_;
00146 int token_socket_;
00147 std::string token_address_;
00148 std::atomic<int> request_sending_;
00149
00150 private:
00151 void setup_requests_();
00152
00153 void do_send_request_();
00154
00155 void setup_tokens_();
00156
00157 void send_routing_token_(int nSlots);
00158 };
00159 }
00160 #endif