Jpp  17.3.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
frame_generator.cc
Go to the documentation of this file.
1 #include "frame_generator.hh"
2 #include <datatypespec.hh>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <time.h>
6 #include <arpa/inet.h>
7 #include <unistd.h>
8 
9 #define inplaceEndianSwap32(x) x = ntohl(x);
10 
11 /**
12  * \author cpellegrino
13  */
14 
16 {
17  // Endiann swap
19 
20  // Endiann swap time
23 }
24 
26  const DOMRange_t& dom_range,
27  unsigned int time_slice_duration,
28  unsigned int run_number,
29  unsigned int MTU,
30  unsigned int hitR)
31  :
32  m_delta_ts(time_slice_duration),
33  m_selected((srand(time(0)), rand() % dom_range.size()))
34 {
35  m_headers.reserve(dom_range.size());
36 
37  for (unsigned int i = 0; i < dom_range.size(); i++) {
38  CLBCommonHeader header;
39  header.RunNumber = htonl(run_number);
40  header.DataType = htonl(ttdc);
41  header.UDPSequenceNumber = 0;
42  header.Timestamp.Sec = time(0);
43  header.Timestamp.Tics = 0;
44  header.DOMIdentifier = htonl(dom_range[i]);
45  header.DOMStatus1 = 128;
46  header.DOMStatus2 = 0;
47  header.DOMStatus3 = 0;
48  header.DOMStatus4 = 0;
49 
50  m_headers.push_back(header);
51  }
52 
53 // max seqnumber = NPMT * kHz * Bytes/Hit * ms TS duration / (MTU - size of CLB Common Header)
54  m_max_seqnumber = 31 * hitR * 6 * time_slice_duration / (MTU - sizeof(CLBCommonHeader)) + 1;
55  m_payload_size = 6 * ((MTU - sizeof(CLBCommonHeader)) / 6);
56  m_tv.tv_sec = 0;
57  m_tv.tv_usec = 0;
58 }
59 
61 {
62  ++m_selected;
63  m_selected %= m_headers.size();
64 
65  CLBCommonHeader& common_header = m_headers[m_selected];
66 
67  common_header.UDPSequenceNumber = common_header.UDPSequenceNumber + 1;
68 
69  if (common_header.UDPSequenceNumber == m_max_seqnumber) {
70  common_header.DOMStatus2 = 128;
71 
72  target.resize(sizeof(common_header));
73  } else if (common_header.UDPSequenceNumber == m_max_seqnumber + 1) {
74  if (isTrailer(common_header)) {
75  common_header.UDPSequenceNumber = 0;
76  common_header.DOMStatus2 = 0;
77  common_header.Timestamp.Tics += 62500 * m_delta_ts;
78  if (common_header.Timestamp.Tics >= 62500000) {
79  ++common_header.Timestamp.Sec;
80  common_header.Timestamp.Tics = 0;
81  }
82  } else {
83  assert(!"Programming error: UDPSequenceNumber and trailer not respected.");
84  }
85 
86  target.resize(sizeof(CLBCommonHeader) + m_payload_size);
87  }
88 
89  memcpy(target.data(), &common_header, sizeof(CLBCommonHeader));
90 
92  *static_cast<CLBCommonHeader*>(
93  static_cast<void*>(
94  target.data()
95  )
96  )
97  );
98 
99  if (common_header.UDPSequenceNumber == 0 && m_selected == 0) {
100  int sleep_time = m_delta_ts * 1000;
101 
102  if (m_tv.tv_sec) {
103  timeval tv;
104  gettimeofday(&tv, 0);
105  sleep_time -=
106  (tv.tv_sec - m_tv.tv_sec) * 1000000
107  + (tv.tv_usec - m_tv.tv_usec);
108  }
109 
110  if (sleep_time > 0) {
111  usleep(sleep_time);
112  }
113 
114  gettimeofday(&m_tv, 0);
115  }
116 }
unsigned int m_selected
unsigned int m_delta_ts
uint32_t UDPSequenceNumber
unsigned int m_payload_size
#define inplaceEndianSwap32(x)
std::vector< CLBCommonHeader > m_headers
FrameGenerator(const DOMRange_t &dom_range, unsigned int time_slice_duration, unsigned int run_number, unsigned int MTU, unsigned int hitR)
bool isTrailer(CLBCommonHeader const &header)
unsigned int const ttdc
Definition: datatypes.cpp:7
void getNext(raw_data_t &target)
unsigned int m_max_seqnumber
uint32_t Tics
Definition: utctime.hh:15
uint32_t Sec
Definition: utctime.hh:14
void swap_endianness(CLBCommonHeader &header)