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