artdaq  v3_12_02
RequestMessage.hh
1 #ifndef artdaq_DAQrate_detail_RequestMessage_hh
2 #define artdaq_DAQrate_detail_RequestMessage_hh
3 
4 #include "TRACE/tracemf.h" // Pre-empt TRACE/trace.h from Fragment.hh.
5 #include "artdaq-core/Data/Fragment.hh"
6 #define MAX_REQUEST_MESSAGE_SIZE 65000
7 
8 #include "artdaq/DAQdata/Globals.hh"
9 
10 #include <iostream>
11 #include <vector>
12 
13 namespace artdaq {
14 namespace detail {
15 struct RequestPacket;
16 struct RequestHeader;
17 class RequestMessage;
18 
22 enum class RequestMessageMode : uint8_t
23 {
24  Normal = 0,
25  EndOfRun = 1,
26 };
27 
34 inline std::ostream& operator<<(std::ostream& o, RequestMessageMode m)
35 {
36  switch (m)
37  {
39  o << "Normal";
40  break;
42  o << "EndOfRun";
43  break;
44  }
45  return o;
46 }
47 
48 } // namespace detail
49 } // namespace artdaq
50 
55 {
56 public:
58  uint32_t header{0}; // TRIG, or 0x54524947
59  Fragment::sequence_id_t sequence_id{Fragment::InvalidSequenceID};
60  Fragment::timestamp_t timestamp{Fragment::InvalidTimestamp};
61 
62  RequestPacket() = default;
63 
69  RequestPacket(const Fragment::sequence_id_t& seq, const Fragment::timestamp_t& ts)
70  : header(0x54524947)
71  , sequence_id(seq)
72  , timestamp(ts)
73  {}
74 
79  bool isValid() const { return header == 0x54524947; }
80 };
81 
86 {
88  uint32_t header{0x48454452}; // HEDR, or 0x48454452
89  uint32_t packet_count{0};
90  int rank{my_rank};
91  uint32_t run_number{0};
93 
94  RequestHeader() = default;
95 
100  bool isValid() const { return header == 0x48454452; }
101 };
102 
107 {
108 public:
113  : header_()
114  , packets_()
115  {}
116 
121  std::vector<uint8_t> GetMessage()
122  {
123  auto size = sizeof(RequestHeader) + packets_.size() * sizeof(RequestPacket);
124  header_.packet_count = packets_.size();
125  assert(size < MAX_REQUEST_MESSAGE_SIZE);
126  auto output = std::vector<uint8_t>(size);
127  memcpy(&output[0], &header_, sizeof(RequestHeader));
128  memcpy(&output[sizeof(RequestHeader)], &packets_[0], packets_.size() * sizeof(RequestPacket));
129 
130  return output;
131  }
132 
138  {
139  header_.mode = mode;
140  }
141 
146  void setRank(int rank)
147  {
148  header_.rank = rank;
149  }
150 
156  void setRunNumber(int run)
157  {
158  header_.run_number = run;
159  }
160 
165  size_t size() const { return packets_.size(); }
166 
172  void addRequest(const Fragment::sequence_id_t& seq, const Fragment::timestamp_t& time)
173  {
174  packets_.emplace_back(RequestPacket(seq, time));
175  }
176 
181  static size_t max_request_count()
182  {
183  return (MAX_REQUEST_MESSAGE_SIZE - sizeof(RequestHeader)) / sizeof(RequestPacket);
184  }
185 
186 private:
187  RequestHeader header_;
188  std::vector<RequestPacket> packets_;
189 };
190 
191 #endif // artdaq_DAQrate_detail_RequestMessage
std::vector< uint8_t > GetMessage()
Get the contents of the RequestMessage.
uint32_t run_number
The Run with which this request should be associated.
End of Run mode (Used to end request processing on receiver)
void addRequest(const Fragment::sequence_id_t &seq, const Fragment::timestamp_t &time)
Add a request for a sequence ID and timestamp combination.
RequestMessageMode mode
Communicates additional information to the Request receiver.
bool isValid() const
Check the magic bytes of the packet.
Fragment::timestamp_t timestamp
The timestamp of the request.
void setRunNumber(int run)
Set the run number in the header for this request. This will be the Run for which the request is vali...
A RequestMessage consists of a RequestHeader and zero or more RequestPackets. They will usually be se...
static size_t max_request_count()
Get the maximum number of requests that can be sent in a single RequestMessage.
void setMode(RequestMessageMode mode)
Set the Request Message Mode for this request.
Header of a RequestMessage. Contains magic bytes for validation and a count of expected RequestPacket...
uint32_t packet_count
The number of RequestPackets in this Request message.
void setRank(int rank)
Set the rank in the header for this request. This will be the rank from which the request originates...
int rank
Rank of the sender.
The RequestPacket contains information about a single data request.
RequestMessageMode
Mode used to indicate current run conditions to the request receiver.
size_t size() const
Get the number of RequestPackets in the RequestMessage.
bool isValid() const
Check the magic bytes of the packet.
RequestMessage()
Default Constructor.
RequestPacket(const Fragment::sequence_id_t &seq, const Fragment::timestamp_t &ts)
Create a RequestPacket using the given sequence ID and timestmap.
Fragment::sequence_id_t sequence_id
The sequence ID that responses to this request should use.