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