Line data Source code
1 : #ifndef artdaq_DAQrate_TokenSender_hh
2 : #define artdaq_DAQrate_TokenSender_hh
3 :
4 : namespace fhicl {
5 : class ParameterSet;
6 : }
7 : #include "fhiclcpp/types/Atom.h"
8 : #include "fhiclcpp/types/Comment.h"
9 : #include "fhiclcpp/types/ConfigurationTable.h"
10 : #include "fhiclcpp/types/Name.h"
11 :
12 : #include <arpa/inet.h>
13 : #include <netinet/in.h>
14 : #include <sys/socket.h>
15 : #include <sys/types.h>
16 : #include <atomic>
17 : #include <chrono>
18 : #include <cstdint>
19 : #include <map>
20 : #include <memory>
21 : #include <string>
22 :
23 : namespace artdaq {
24 :
25 : /**
26 : * \brief The TokenSender contains methods used to send data requests and Routing tokens
27 : */
28 : class TokenSender
29 : {
30 : public:
31 : /// <summary>
32 : /// Configuration for Routing token sending
33 : ///
34 : /// This configuration should be the same for all processes sending routing tokens to a given RoutingManager.
35 : /// </summary>
36 : struct Config
37 : {
38 : /// "use_routing_manager" (Default: false) : Whether to send tokens to a RoutingManager
39 : fhicl::Atom<bool> use_routing_manager{fhicl::Name{"use_routing_manager"}, fhicl::Comment{"True if using the Routing Manager"}, false};
40 : /// "routing_token_port" (Default: 35555) : Port to send tokens on
41 : fhicl::Atom<int> routing_token_port{fhicl::Name{"routing_token_port"}, fhicl::Comment{"Port to send tokens on"}, 35555};
42 : /// "routing_manager_hostname" (Default: "localhost") : Hostname or IP of RoutingManager
43 : fhicl::Atom<std::string> routing_token_host{fhicl::Name{"routing_manager_hostname"}, fhicl::Comment{"Hostname or IP of RoutingManager"}, "localhost"};
44 : };
45 : /// Used for ParameterSet validation (if desired)
46 : using Parameters = fhicl::WrappedTable<Config>;
47 :
48 : /**
49 : * \brief Default Constructor is deleted
50 : */
51 : TokenSender() = delete;
52 :
53 : /**
54 : * \brief Copy Constructor is deleted
55 : */
56 : TokenSender(TokenSender const&) = delete;
57 :
58 : /**
59 : * \brief Copy Assignment operator is deleted
60 : * \return TokenSender copy
61 : */
62 : TokenSender& operator=(TokenSender const&) = delete;
63 :
64 : TokenSender(TokenSender&&) = delete; ///< Move Constructor is deleted
65 : TokenSender& operator=(TokenSender&&) = delete; ///< Move-assignment operator is deleted
66 :
67 : /**
68 : * \brief TokenSender Constructor
69 : * \param pset ParameterSet used to configured TokenSender. See artdaq::TokenSender::Config
70 : */
71 : explicit TokenSender(const fhicl::ParameterSet& pset);
72 : /**
73 : * \brief TokenSender Destructor
74 : */
75 : virtual ~TokenSender();
76 :
77 : /**
78 : * \brief Send a RoutingToken message indicating that slots are available
79 : * \param nSlots Number of slots available
80 : * \param run_number Run number for token
81 : * \param rank Rank of token
82 : */
83 : void SendRoutingToken(int nSlots, int run_number, int rank = my_rank);
84 :
85 : /**
86 : * \brief Get the count of number of tokens sent
87 : * \return The number of tokens sent by TokenSender
88 : */
89 0 : size_t GetSentTokenCount() const { return tokens_sent_.load(); }
90 :
91 : /**
92 : * \brief Set the run number to be used in request messages
93 : * \param run Run number
94 : */
95 0 : void SetRunNumber(uint32_t run) { run_number_ = run; }
96 :
97 : /**
98 : * \brief Determine if routing token sends are enabled
99 : * \return If routing tokens will be sent by this TokenSender
100 : */
101 0 : bool RoutingTokenSendsEnabled() { return send_routing_tokens_; }
102 :
103 : private:
104 : std::atomic<bool> initialized_;
105 :
106 : bool send_routing_tokens_;
107 : int token_port_;
108 : int token_socket_;
109 : std::string token_address_;
110 : std::atomic<size_t> tokens_sent_;
111 : uint32_t run_number_;
112 :
113 : private:
114 : void setup_tokens_();
115 :
116 : void send_routing_token_(int nSlots, int run_number, int rank);
117 : };
118 : } // namespace artdaq
119 : #endif /* artdaq_DAQrate_TokenSender_hh */
|