UniRec  3.2.0
bidirectionalInterface.cpp
Go to the documentation of this file.
1 
11 
12 #include <libtrap/trap.h>
13 
14 namespace Nemea {
15 
17 {
18  if (m_sendEoFonExit) {
19  sendEoF();
20  }
21 
23 }
24 
26  uint8_t inputInterfaceID,
27  uint8_t outputInterfaceID)
28  : m_template(nullptr)
29  , m_inputInterfaceID(inputInterfaceID)
30  , m_outputInterfaceID(outputInterfaceID)
31  , m_sequenceNumber(0)
32  , m_prioritizedDataPointer(nullptr)
33  , m_sendEoFonExit(true)
34 {
36 }
37 
38 std::optional<UnirecRecordView> UnirecBidirectionalInterface::receive()
39 {
40  const void* receivedData;
41  uint16_t dataSize = 0;
42 
44  receivedData = m_prioritizedDataPointer;
45  m_prioritizedDataPointer = nullptr;
46  return UnirecRecordView(receivedData, m_template, m_sequenceNumber);
47  }
48 
49  int errorCode = trap_recv_with_seq_number(m_inputInterfaceID, &receivedData, &dataSize, &m_sequenceNumber);
50  if (errorCode == TRAP_E_TIMEOUT) {
51  return std::nullopt;
52  }
53  if (errorCode == TRAP_E_FORMAT_CHANGED) {
54  m_prioritizedDataPointer = receivedData;
55  throw FormatChangeException();
56  }
57  handleReceiveErrorCodes(errorCode);
58 
59  if (dataSize <= 1) {
60  throw EoFException();
61  }
62 
63  return UnirecRecordView(receivedData, m_template, m_sequenceNumber);
64 }
65 
67 {
68  if (errorCode == TRAP_E_OK) {
69  return;
70  }
71  if (errorCode == TRAP_E_NOT_INITIALIZED) {
72  throw std::runtime_error(
73  "UnirecBidirectionalInterface::receive() has failed. Trap interface is not "
74  "initialized.");
75  }
76  if (errorCode == TRAP_E_TERMINATED) {
77  throw std::runtime_error(
78  "UnirecBidirectionalInterface::receive() has failed. Trap interface is terminated.");
79  }
80  if (errorCode == TRAP_E_NOT_SELECTED) {
81  throw std::runtime_error(
82  "UnirecBidirectionalInterface::receive() has failed. Interface ID out of range.");
83  }
84  throw std::runtime_error(
85  "UnirecBidirectionalInterface::receive() has failed. Return code: "
86  + std::to_string(errorCode) + ", msg: " + trap_last_error_msg);
87 }
88 
89 void UnirecBidirectionalInterface::setRequieredFormat(const std::string& templateSpecification)
90 {
91  int ret = trap_set_required_fmt(m_inputInterfaceID, TRAP_FMT_UNIREC, templateSpecification.c_str());
92  if (ret != TRAP_E_OK) {
93  throw std::runtime_error(
94  "UnirecBidirectionalInterface::setRequieredFormat() has failed. Unable to set required "
95  "format.");
96  }
97 
98  changeInternalTemplate(templateSpecification);
99 }
100 
101 void UnirecBidirectionalInterface::changeInternalTemplate(const std::string& templateSpecification)
102 {
103  m_template = ur_define_fields_and_update_template(templateSpecification.c_str(), m_template);
104  if (m_template == nullptr) {
105  throw std::runtime_error(
106  "UnirecBidirectionalInterface::changeTemplate() has failed. Template could not be "
107  "edited.");
108  }
109 
110  trap_set_data_fmt(m_outputInterfaceID, TRAP_FMT_UNIREC, templateSpecification.c_str());
111 
113  if (ret != TRAP_E_OK) {
114  throw std::runtime_error("UnirecBidirectionalInterface::changeTemplate() has failed.");
115  }
116 
118  if (ret != TRAP_E_OK) {
119  throw std::runtime_error("UnirecBidirectionalInterface::changeTemplate() has failed.");
120  }
121 
123 }
124 
126 {
127  uint8_t dataType;
128  const char* spec = nullptr;
129 
130  int ret = trap_get_data_fmt(TRAPIFC_INPUT, m_inputInterfaceID, &dataType, &spec);
131  if (ret != TRAP_E_OK) {
132  throw std::runtime_error(
133  "UnirecBidirectionalInterface::changeTemplate() has failed. Data format was not "
134  "loaded.");
135  }
136 
138 }
139 
141 {
142  trap_ifcctl(TRAPIFC_INPUT, m_inputInterfaceID, TRAPCTL_SETTIMEOUT, timeout);
143 }
144 
146 {
147  return UnirecRecord(m_template, maxVariableFieldsSize);
148 }
149 
151 {
152  int errorCode = trap_send(m_outputInterfaceID, unirecRecord.data(), unirecRecord.size());
153  return handleSendErrorCodes(errorCode);
154 }
155 
157 {
158  int errorCode
159  = trap_send(m_outputInterfaceID, unirecRecordView.data(), unirecRecordView.size());
160  return handleSendErrorCodes(errorCode);
161 }
162 
164 {
165  if (errorCode == TRAP_E_TIMEOUT) {
166  return false;
167  }
168  if (errorCode == TRAP_E_OK) {
169  return true;
170  }
171  if (errorCode == TRAP_E_NOT_INITIALIZED) {
172  throw std::runtime_error(
173  "UnirecBidirectionalInterface::send() has failed. Trap interface is not initialized.");
174  }
175  if (errorCode == TRAP_E_TERMINATED) {
176  throw std::runtime_error(
177  "UnirecBidirectionalInterface::send() has failed. Trap interface is terminated.");
178  }
179  if (errorCode == TRAP_E_BAD_IFC_INDEX) {
180  throw std::runtime_error(
181  "UnirecBidirectionalInterface::send() has failed. Interface ID out of range.");
182  }
183  throw std::runtime_error(
184  "UnirecBidirectionalInterface::send() has failed. Return code: " + std::to_string(errorCode)
185  + ", msg: " + trap_last_error_msg);
186 }
187 
189 {
190  trap_send_flush(m_outputInterfaceID);
191 }
192 
194 {
195  m_sendEoFonExit = false;
196 }
197 
199 {
200  trap_ifcctl(TRAPIFC_OUTPUT, m_outputInterfaceID, TRAPCTL_SETTIMEOUT, timeout);
201 }
202 
204 {
205  trap_ifcctl(TRAPIFC_OUTPUT, m_outputInterfaceID, TRAPCTL_AUTOFLUSH_TIMEOUT, timeout);
206 }
207 
209 {
210  char dummy[1] = { 0 };
211  trap_send(m_outputInterfaceID, dummy, sizeof(dummy));
212 }
213 
215 {
216  InputInteraceStats inputStats;
217 
218  struct input_ifc_stats ifcStats = {};
219  trap_get_input_ifc_stats(m_inputInterfaceID, &ifcStats);
220 
221  inputStats.receivedBytes = ifcStats.received_bytes;
222  inputStats.receivedRecords = ifcStats.received_records;
223  inputStats.missedRecords = ifcStats.missed_records;
224  return inputStats;
225 }
226 
227 } // namespace Nemea
Defines a bidirectional interface for sending and receiving unirec records using the TRAP interface p...
An exception that is thrown when the end of the input stream is reached.
An exception that is thrown when the record format changes.
void setSendAutoflushTimeout(int timeout)
Sets the autoflush timeout for the output Trap interface.
~UnirecBidirectionalInterface()
Destructor for the UnirecBidirectionalInterface class.
bool handleSendErrorCodes(int errorCode) const
void handleReceiveErrorCodes(int errorCode) const
void sendFlush() const
Flushes any pending UniRec records in the Trap interface.
void changeTemplate()
Changes the Unirec template used by the bidirectional interface.
void changeInternalTemplate(const std::string &templateSpecification)
InputInteraceStats getInputInterfaceStats() const
Gets the statistics for the input interface.
void setReceiveTimeout(int timeout)
Sets the receive timeout for the interface. This method sets the timeout for receiving UniRec records...
UnirecRecord createUnirecRecord(size_t maxVariableFieldsSize=UR_MAX_SIZE)
Creates a new UniRec record with the specified maximum variable fields size.
void doNotsendEoFOnExit()
Disables sending an end-of-file marker on exit.
void setSendTimeout(int timeout)
Sets the send timeout for the Trap interface.
std::optional< UnirecRecordView > receive()
Receives data from the interface and returns an optional UnirecRecordView object.
void setRequieredFormat(const std::string &templateSpecification)
Sets the required Unirec format specification.
bool send(UnirecRecord &unirecRecord) const
Sends a UniRec record through the Trap interface.
UnirecBidirectionalInterface(uint8_t inputInterfaceID, uint8_t outputInterfaceID)
Provides a view into a UniRec record.
size_t size() const noexcept
Returns the size of the UniRec record.
const void * data() const noexcept
Returns a const pointer to the data of the UniRec record.
A class for working with UniRec records and their fields.
const void * data() const noexcept
Returns a pointer to the data of the UniRec record.
size_t size() const noexcept
Returns the size of the UniRec record.
ur_template_t * ur_define_fields_and_update_template(const char *ifc_data_fmt, ur_template_t *tmplt)
Defined new fields and expand an UniRec template Define new fields (function ur_define_set_of_fields)...
Definition: unirec.c:616
#define ur_set_output_template(ifc, tmplt)
Set UniRec template to ouput interface.
Definition: unirec.h:847
void ur_free_template(ur_template_t *tmplt)
Destroy UniRec template Free all memory allocated for a template created previously by ur_create_temp...
Definition: unirec.c:1094
#define ur_set_input_template(ifc, tmplt)
Set UniRec template to input interface.
Definition: unirec.h:863
Structure to store statistics related to an input interface.