McStas Readout Master 0.3.3
Loading...
Searching...
No Matches
Sender.h
Go to the documentation of this file.
1// Copyright (C) 2022 European Spallation Source, ERIC. See LICENSE file
2//===----------------------------------------------------------------------===//
7//===----------------------------------------------------------------------===//
8#pragma once
9
10#include "cluon-complete.hpp"
11
12#include <string>
13#include <utility>
14#include <optional>
15#include <random>
16
17#include "Structs.h"
18#include "Readout.h"
19#include "SenderConfigs.h"
20#include "enums.h"
21#include "hdf_interface.h"
22#include "version.hpp"
23#include "efu_time.h"
24
41class RL_API Sender {
42public:
43 Sender(
44 std::string IpAddress, const int UDPPort, const int TCPPort, const DetectorType detector_type, const ReadoutType readout_type, efu_time p = efu_time(1), efu_time t = efu_time()
45 ): detector_type(detector_type),
46 readout_type(readout_type),
47 ipaddr(std::move(IpAddress)),
48 port(UDPPort),
49 tcp_port(TCPPort),
50 period(p),
51 time(t),
52 sender{ipaddr, static_cast<uint16_t>(UDPPort)}
53 {
54 // hp = reinterpret_cast<PacketHeaderV0 *>(&buffer[0]);
55 auto prev = time - period;
56 setPulseTime(time.high(), time.low(), prev.high(), prev.low());
57 newPacket();
58 }
59
60 explicit Sender(const SenderConfig & config, efu_time p = efu_time(1), efu_time t = efu_time())
61 : Sender(config.ip_address, config.udp_port, config.tcp_port, config.detector_type, config.readout_type, p, t) {}
62
63 ~Sender() {
64 // ensure any buffered readouts are sent before the object is destroyed
65 if (DataSize > static_cast<int>(sizeof(struct PacketHeaderV0))) {
66 send();
67 }
68 }
69
73 void addReadout(uint8_t Ring, uint8_t FEN, double tof, double weight, const void * data);
75 void addReadout(uint8_t Ring, uint8_t FEN, efu_time t, const void * data);
76 // Specializations for handled data types
77 void addReadout(uint8_t Ring, uint8_t FEN, efu_time t, const CAEN_readout_t * data);
78 void addReadout(uint8_t Ring, uint8_t FEN, efu_time t, const TTLMonitor_readout_t * data);
79 void addReadout(uint8_t Ring, uint8_t FEN, efu_time t, const CDT_readout_t * data);
80 void addReadout(uint8_t Ring, uint8_t FEN, efu_time t, const VMM3_readout_t * data);
81 void addReadout(uint8_t Ring, uint8_t FEN, efu_time t, const BM0_readout_t * data);
82 void addReadout(uint8_t Ring, uint8_t FEN, efu_time t, const BM2_readout_t * data);
83 void addReadout(uint8_t Ring, uint8_t FEN, efu_time t, const BMI_readout_t * data);
84
85
88 int send();
89
91 void setPulseTime(uint32_t PHI, uint32_t PLO, uint32_t PPHI, uint32_t PPLO);
92
98
102 void fold_tof(const bool fold) { fold_tof_ = fold; }
103
104 // Query the current pulse and previous pulse times
105 [[nodiscard]] std::pair<uint32_t, uint32_t> lastPulseTime() const;
106 [[nodiscard]] std::pair<uint32_t, uint32_t> prevPulseTime() const;
107 [[nodiscard]] std::pair<uint32_t, uint32_t> lastEventTime() const;
108
110 void newPacket();
111
113 int command_shutdown() const;
114
116 int verbose(const Verbosity v){
117 switch (v) {
118 case Verbosity::details: verbosity=3; break;
119 case Verbosity::info: verbosity=2; break;
120 case Verbosity::warnings: verbosity=1; break;
121 case Verbosity::errors: verbosity=0; break;
122 case Verbosity::silent: verbosity=-1; break;
123 default: verbosity=0;
124 }
125 return verbosity;
126 }
127 int verbose(const int v){verbosity = v; return verbosity;}
128private:
129
130 void check_size_and_send();
131
134 void maybe_advance_pulse();
135
136 // Packet header
137 uint32_t phi{0}; // pulse and prev pulse high and low
138 uint32_t plo{0};
139 uint32_t pphi{0};
140 uint32_t pplo{0};
141
142 uint32_t lasthi{0};
143 uint32_t lastlo{0};
144
145 int SeqNum{0};
146 int OutputQueue{0};
147 DetectorType detector_type;
148 ReadoutType readout_type;
149
150 // TX Buffer
151 // PacketHeaderV0 *hp{};
152 char buffer[9000]{};
153 const int MaxDataSize{8950};
154 int DataSize{0};
155 // IP and port number
156 std::string ipaddr;
157 int port{9000};
158 int tcp_port{8888};
159 int verbosity{0};
160 bool fold_tof_{false};
161
162 efu_time period, time;
163 cluon::UDPSender sender;
164
165 std::mutex time_mutex, send_mutex;
166};
Umbrella header for the C API used by the McStas components.
ESS and Bifrost readout data structures.
Buffers ESS readout records and transmits them as UDP packets to one EFU.
Definition Sender.h:41
void newPacket()
Initialize a new packet with no readouts.
void addReadout(uint8_t Ring, uint8_t FEN, efu_time t, const void *data)
Add a single readout with an explicit event time.
int command_shutdown() const
Tell the remote EFU to shut down via its TCP command port.
int send()
Transmit the current data buffer; the caller is responsible for starting a new packet (newPacket()) b...
void setPulseTime(uint32_t PHI, uint32_t PLO, uint32_t PPHI, uint32_t PPLO)
Update the pulse and previous pulse times (high/low pairs).
void begin_pulse()
Start a new pulse: flush any buffered readouts, wait until the next pulse-grid tick has passed on the...
int verbose(const Verbosity v)
Set verbosity via enum.
Definition Sender.h:116
void fold_tof(const bool fold)
Choose whether add-by-time-of-flight readouts are stamped at pulse + (tof % period) — attributing eac...
Definition Sender.h:102
void addReadout(uint8_t Ring, uint8_t FEN, double tof, double weight, const void *data)
Add one readout with its event time set to the current pulse time plus tof; the weight is ignored (st...
ESS EFU two-integer timestamp: whole seconds since the UNIX epoch plus ticks of the 88....
Definition efu_time.h:19
One Event Formation Unit endpoint for a (detector, readout) pair.
uint16_t tcp_port
command port
uint16_t udp_port
event-packet port