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 using Parameters = fhicl::WrappedTable<Config>;
00065
00069 RequestSender() = delete;
00070
00074 RequestSender(RequestSender const&) = delete;
00075
00080 RequestSender& operator=(RequestSender const&) = delete;
00081
00086 RequestSender(const fhicl::ParameterSet& pset);
00090 virtual ~RequestSender();
00091
00092
00097 void SetRequestMode(detail::RequestMessageMode mode);
00098
00103 detail::RequestMessageMode GetRequestMode() const { return request_mode_; }
00104
00109 void SendRequest(bool endOfRunOnly = false);
00110
00116 void AddRequest(Fragment::sequence_id_t seqID, Fragment::timestamp_t timestamp);
00117
00122 void RemoveRequest(Fragment::sequence_id_t seqID);
00123
00128 void SendRoutingToken(int nSlots);
00129
00134 size_t GetSentTokenCount() const { return tokens_sent_.load(); }
00135 private:
00136
00137
00138 bool send_requests_;
00139 std::atomic<bool> initialized_;
00140 mutable std::mutex request_mutex_;
00141 mutable std::mutex request_send_mutex_;
00142 std::map<Fragment::sequence_id_t, Fragment::timestamp_t> active_requests_;
00143 std::string request_address_;
00144 int request_port_;
00145 size_t request_delay_;
00146 size_t request_shutdown_timeout_us_;
00147 int request_socket_;
00148 struct sockaddr_in request_addr_;
00149 std::string multicast_out_addr_;
00150 detail::RequestMessageMode request_mode_;
00151
00152 bool send_routing_tokens_;
00153 int token_port_;
00154 int token_socket_;
00155 std::string token_address_;
00156 std::atomic<int> request_sending_;
00157 std::atomic<size_t> tokens_sent_;
00158
00159 private:
00160 void setup_requests_();
00161
00162 void do_send_request_();
00163
00164 void setup_tokens_();
00165
00166 void send_routing_token_(int nSlots);
00167 };
00168 }
00169 #endif