UniRec  3.0.0
inputInterface.cpp
Go to the documentation of this file.
1 
10 
11 #include <libtrap/trap.h>
12 
13 namespace NemeaPlusPlus {
14 
16 {
18 }
19 
21  : m_interfaceID(interfaceID)
22  , m_prioritizedDataPointer(nullptr)
23  , m_EoFOnNextReceive(false)
24 {
26 }
27 
28 std::optional<UnirecRecordView> UnirecInputInterface::receive()
29 {
30  const void* receivedData;
31  uint16_t dataSize = 0;
32 
33  if (isEoFReceived()) {
34  throw EoFException();
35  }
36 
38  receivedData = m_prioritizedDataPointer;
39  m_prioritizedDataPointer = nullptr;
40  return UnirecRecordView(receivedData, m_template);
41  }
42 
43  int errorCode = trap_recv(m_interfaceID, &receivedData, &dataSize);
44  if (errorCode == TRAP_E_TIMEOUT) {
45  return std::nullopt;
46  }
47  if (errorCode == TRAP_E_FORMAT_CHANGED) {
48  m_prioritizedDataPointer = receivedData;
49  throw FormatChangeException();
50  }
51  handleReceiveErrorCodes(errorCode);
52 
53  if (dataSize <= 1) {
54  m_EoFOnNextReceive = true;
55  }
56 
57  return UnirecRecordView(receivedData, m_template);
58 }
59 
61 {
62  return m_EoFOnNextReceive;
63 }
64 
66 {
67  if (errorCode == TRAP_E_OK) {
68  return;
69  }
70  if (errorCode == TRAP_E_NOT_INITIALIZED) {
71  throw std::runtime_error(
72  "UnirecInputInterface::receive() has failed. Trap interface is not initialized.");
73  }
74  if (errorCode == TRAP_E_TERMINATED) {
75  throw std::runtime_error(
76  "UnirecInputInterface::receive() has failed. Trap interface is terminated.");
77  }
78  if (errorCode == TRAP_E_NOT_SELECTED) {
79  throw std::runtime_error(
80  "UnirecInputInterface::receive() has failed. Interface ID out of range.");
81  }
82  throw std::runtime_error(
83  "UnirecInputInterface::receive() has failed. Return code: " + std::to_string(errorCode)
84  + ", msg: " + trap_last_error_msg);
85 }
86 
87 void UnirecInputInterface::setRequieredFormat(const std::string& templateSpecification)
88 {
89  int ret = trap_set_required_fmt(m_interfaceID, TRAP_FMT_UNIREC, templateSpecification.c_str());
90  if (ret != TRAP_E_OK) {
91  throw std::runtime_error(
92  "UnirecInputInterface::setRequieredFormat() has failed. Unable to set required "
93  "format.");
94  }
95 }
96 
98 {
99  trap_ifcctl(TRAPIFC_INPUT, m_interfaceID, TRAPCTL_SETTIMEOUT, timeout);
100 }
101 
103 {
104  uint8_t dataType;
105  const char* spec = nullptr;
106 
107  int ret = trap_get_data_fmt(TRAPIFC_INPUT, m_interfaceID, &dataType, &spec);
108  if (ret != TRAP_E_OK) {
109  throw std::runtime_error(
110  "UnirecInputInterface::changeTemplate() has failed. Data format was not "
111  "loaded.");
112  }
113 
115  if (m_template == nullptr) {
116  throw std::runtime_error(
117  "UnirecInputInterface::changeTemplate() has failed. Template could not be "
118  "edited.");
119  }
120 
122  if (ret != TRAP_E_OK) {
123  throw std::runtime_error("UnirecInputInterface::changeTemplate() has failed.");
124  }
125 }
126 
127 } // namespace NemeaPlusPlus
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 changeTemplate()
Changes the Unirec template used by the input interface.
void handleReceiveErrorCodes(int errorCode) const
void setTimeout(int timeout)
Sets the receive timeout for the interface. This method sets the timeout for receiving UniRec records...
UnirecInputInterface(uint8_t interfaceID)
void setRequieredFormat(const std::string &templateSpecification)
Sets the required Unirec format specification.
~UnirecInputInterface()
Destructor for UnirecInputInterface class. This method frees the memory used by the Unirec template.
std::optional< UnirecRecordView > receive()
Receives data from the interface and returns an optional UnirecRecordView object.
Provides a view into a 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
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
Provides an interface for receiving UniRec records using the TRAP protocol.