artdaq_demo  v3_06_01
UDPReceiver.hh
1 #ifndef artdaq_ots_Generators_UDPTFGTest_hh
2 #define artdaq_ots_Generators_UDPTFGTest_hh
3 
4 // The UDP Receiver class recieves UDP data from an OtSDAQ applicance and
5 // puts that data into UDPFragments for further ARTDAQ analysis.
6 //
7 // It currently assumes two things to be true:
8 // 1. The first word of the UDP packet is an 8-bit flag with information
9 // about the status of the sender
10 // 2. The second word is an 8-bit sequence ID, used for detecting
11 // dropped UDP datagrams
12 
13 // Some C++ conventions used:
14 
15 // -Append a "_" to every private member function and variable
16 #include "artdaq-core/Data/Fragment.hh"
17 #include "artdaq/Generators/CommandableFragmentGenerator.hh"
18 #include "fhiclcpp/fwd.h"
19 
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
22 #include <sys/socket.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 #include <array>
27 #include <atomic>
28 #include <list>
29 #include <queue>
30 
31 namespace demo {
35 enum class CommandType : uint8_t
36 {
37  Read = 0,
38  Write = 1,
39  Start_Burst = 2,
40  Stop_Burst = 3,
41 };
42 
46 enum class ReturnCode : uint8_t
47 {
48  Read = 0,
49  First = 1,
50  Middle = 2,
51  Last = 3,
52 };
53 
57 enum class DataType : uint8_t
58 {
59  Raw = 0,
60  JSON = 1,
61  String = 2,
62 };
63 
68 {
70  uint8_t dataSize;
71  uint64_t address;
72  uint64_t data[ 182 ];
73 };
74 
75 typedef std::array<uint8_t, 1500> packetBuffer_t;
76 typedef std::list<packetBuffer_t> packetBuffer_list_t;
77 
81 class UDPReceiver : public artdaq::CommandableFragmentGenerator
82 {
83 public:
97  explicit UDPReceiver( fhicl::ParameterSet const& ps );
98 
99 private:
100  // The "getNext_" function is used to implement user-specific
101  // functionality; it's a mandatory override of the pure virtual
102  // getNext_ function declared in CommandableFragmentGenerator
103 
104  bool getNext_( artdaq::FragmentPtrs& output ) override;
105 
106  void start() override;
107 
108  void stop() override;
109 
110  void stopNoMutex() override {} // nothing special needs to be done in this method
111  void pause() override;
112 
113  void resume() override;
114 
115  DataType getDataType( uint8_t byte ) { return static_cast<DataType>( ( byte & 0xF0 ) >> 4 ); }
116  ReturnCode getReturnCode( uint8_t byte ) { return static_cast<ReturnCode>( byte & 0xF ); }
117 
118  void send( CommandType flag );
119 
120  // FHiCL-configurable variables. Note that the C++ variable names
121  // are the FHiCL variable names with a "_" appended
122 
123  int dataport_;
124  std::string ip_;
125 
126  // The packet number of the next packet. Used to discover dropped packets
127  uint8_t expectedPacketNumber_;
128 
129  // Socket parameters
130  struct sockaddr_in si_data_;
131  int datasocket_;
132  bool sendCommands_;
133 
134  packetBuffer_list_t packetBuffers_;
135 
136  bool rawOutput_;
137  std::string rawPath_;
138 };
139 } // namespace demo
140 
141 #endif /* artdaq_demo_Generators_ToySimulator_hh */
CommandType type
The type of this CommandPacket.
Definition: UDPReceiver.hh:69
uint8_t dataSize
How many words of data are in the packet.
Definition: UDPReceiver.hh:70
ReturnCode
Enumeration describing status codes that indicate current sender position in the stream.
Definition: UDPReceiver.hh:46
An artdaq::CommandableFragmentGenerator which receives data in the form of UDP datagrams.
Definition: UDPReceiver.hh:81
std::list< packetBuffer_t > packetBuffer_list_t
A std::list of packetbuffer_t objects.
Definition: UDPReceiver.hh:76
CommandType
Enumeration describing valid command types.
Definition: UDPReceiver.hh:35
uint64_t address
The destination of the CommandPacket.
Definition: UDPReceiver.hh:71
DataType
Enumeration describing potential data types.
Definition: UDPReceiver.hh:57
std::array< uint8_t, 1500 > packetBuffer_t
An array of 1500 bytes (MTU length)
Definition: UDPReceiver.hh:75
UDPReceiver(fhicl::ParameterSet const &ps)
UDPReceiver Constructor.
Struct defining UDP packet used for communicating with data receiver.
Definition: UDPReceiver.hh:67
uint64_t data[182]
The data for the CommandPacket.
Definition: UDPReceiver.hh:72