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:
00035 struct RoutingTokenConfig
00036 {
00038 fhicl::Atom<bool> use_routing_master{ fhicl::Name{ "use_routing_master" }, fhicl::Comment{ "True if using the Routing Master" }, false };
00040 fhicl::Atom<int> routing_token_port{ fhicl::Name{ "routing_token_port" },fhicl::Comment{ "Port to send tokens on" },35555 };
00042 fhicl::Atom<std::string> routing_token_host{ fhicl::Name{ "routing_master_hostname" }, fhicl::Comment{ "Hostname or IP of RoutingMaster" },"localhost" };
00043 };
00044
00048 struct Config
00049 {
00051 fhicl::Atom<bool> send_requests{ fhicl::Name{ "send_requests" }, fhicl::Comment{ "Enable sending Data Request messages" }, false };
00053 fhicl::Atom<int> request_port{ fhicl::Name{"request_port"}, fhicl::Comment{"Port to send DataRequests on"},3001 };
00055 fhicl::Atom<size_t> request_delay_ms{ fhicl::Name{"request_delay_ms"}, fhicl::Comment{"How long to wait before sending new DataRequests"}, 10 };
00057 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 };
00059 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" };
00061 fhicl::Atom<std::string> request_address{ fhicl::Name{"request_address"}, fhicl::Comment{ "Multicast address to send DataRequests to" }, "227.128.12.26" };
00062 fhicl::Table<RoutingTokenConfig> routing_token_config{ fhicl::Name{"routing_token_config"}, fhicl::Comment{"FHiCL table containing RoutingToken configuration"} };
00063 };
00064 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
00065 using Parameters = fhicl::WrappedTable<Config>;
00066 #endif
00067
00071 RequestSender() = delete;
00072
00076 RequestSender(RequestSender const&) = delete;
00077
00082 RequestSender& operator=(RequestSender const&) = delete;
00083
00088 RequestSender(const fhicl::ParameterSet& pset);
00092 virtual ~RequestSender();
00093
00094
00099 void SetRequestMode(detail::RequestMessageMode mode);
00100
00105 detail::RequestMessageMode GetRequestMode() const { return request_mode_; }
00106
00111 void SendRequest(bool endOfRunOnly = false);
00112
00118 void AddRequest(Fragment::sequence_id_t seqID, Fragment::timestamp_t timestamp);
00119
00124 void RemoveRequest(Fragment::sequence_id_t seqID);
00125
00130 void SendRoutingToken(int nSlots);
00131 private:
00132
00133
00134 bool send_requests_;
00135 mutable std::mutex request_mutex_;
00136 mutable std::mutex request_send_mutex_;
00137 std::map<Fragment::sequence_id_t, Fragment::timestamp_t> active_requests_;
00138 std::string request_address_;
00139 int request_port_;
00140 size_t request_delay_;
00141 size_t request_shutdown_timeout_us_;
00142 int request_socket_;
00143 struct sockaddr_in request_addr_;
00144 std::string multicast_out_addr_;
00145 detail::RequestMessageMode request_mode_;
00146
00147 bool send_routing_tokens_;
00148 int token_port_;
00149 int token_socket_;
00150 std::string token_address_;
00151 std::atomic<int> request_sending_;
00152
00153 private:
00154 void setup_requests_();
00155
00156 void do_send_request_();
00157
00158 void setup_tokens_();
00159
00160 void send_routing_token_(int nSlots);
00161 };
00162 }
00163 #endif