UniRec  3.2.0
outputInterface.cpp
Go to the documentation of this file.
1 
8 
9 #include <libtrap/trap.h>
10 #include <stdexcept>
11 
12 #include <memory>
13 
14 namespace Nemea {
15 
17  : m_template(nullptr)
18  , m_interfaceID(interfaceID)
19  , m_sendEoFonExit(true)
20 {
21 }
22 
24 {
25  if (m_sendEoFonExit) {
26  sendEoF();
27  }
28 
30 }
31 
32 bool UnirecOutputInterface::send(UnirecRecord& unirecRecord) const
33 {
34  int errorCode = trap_send(m_interfaceID, unirecRecord.data(), unirecRecord.size());
35  return handleSendErrorCodes(errorCode);
36 }
37 
38 bool UnirecOutputInterface::send(UnirecRecordView& unirecRecordView) const
39 {
40  int errorCode = trap_send(m_interfaceID, unirecRecordView.data(), unirecRecordView.size());
41  return handleSendErrorCodes(errorCode);
42 }
43 
45 {
46  if (errorCode == TRAP_E_TIMEOUT) {
47  return false;
48  }
49  if (errorCode == TRAP_E_OK) {
50  return true;
51  }
52  if (errorCode == TRAP_E_NOT_INITIALIZED) {
53  throw std::runtime_error(
54  "UnirecOutputInterface::send() has failed. Trap interface is not initialized.");
55  }
56  if (errorCode == TRAP_E_TERMINATED) {
57  throw std::runtime_error(
58  "UnirecOutputInterface::send() has failed. Trap interface is terminated.");
59  }
60  if (errorCode == TRAP_E_BAD_IFC_INDEX) {
61  throw std::runtime_error(
62  "UnirecOutputInterface::send() has failed. Interface ID out of range.");
63  }
64  throw std::runtime_error(
65  "UnirecOutputInterface::send() has failed. Return code: " + std::to_string(errorCode)
66  + ", msg: " + trap_last_error_msg);
67 }
68 
70 {
71  trap_send_flush(m_interfaceID);
72 }
73 
75 {
76  m_sendEoFonExit = false;
77 }
78 
80 {
81  trap_ifcctl(TRAPIFC_OUTPUT, m_interfaceID, TRAPCTL_SETTIMEOUT, timeout);
82 }
83 
85 {
86  trap_ifcctl(TRAPIFC_OUTPUT, m_interfaceID, TRAPCTL_AUTOFLUSH_TIMEOUT, timeout);
87 }
88 
90 {
91  char dummy[1] = {0};
92  trap_send(m_interfaceID, dummy, sizeof(dummy));
93 }
94 
95 void UnirecOutputInterface::changeTemplate(const std::string& templateFields)
96 {
97  if (ur_define_set_of_fields(templateFields.c_str()) != UR_OK) {
98  throw std::runtime_error(
99  "UnirecOutputInterface::changeTemplate() has failed. Template fields could not be "
100  "defined!");
101  }
102 
103  std::unique_ptr<char*> fieldNames
104  = std::make_unique<char*>(ur_ifc_data_fmt_to_field_names(templateFields.c_str()));
105 
106  if (!fieldNames) {
107  throw std::runtime_error(
108  "UnirecOutputInterface::changeTemplate() has failed. Field specs could not be "
109  "converted.");
110  }
112  m_template = ur_create_output_template(m_interfaceID, *fieldNames, nullptr);
113  if (!m_template) {
114  throw std::runtime_error(
115  "UnirecOutputInterface::changeTemplate() has failed. Output template could not be "
116  "created.");
117  }
119 }
120 
122 {
123  return UnirecRecord(m_template, maxVariableFieldsSize);
124 }
125 
126 } // namespace Nemea
bool handleSendErrorCodes(int errorCode) const
~UnirecOutputInterface()
Destructor for the UnirecOutputInterface class.
void sendFlush() const
Flushes any pending UniRec records in the Trap interface.
void setTimeout(int timeout)
Sets the send timeout for the Trap interface.
UnirecOutputInterface(uint8_t interfaceID)
void doNotsendEoFOnExit()
Disables sending an end-of-file marker on exit.
UnirecRecord createUnirecRecord(size_t maxVariableFieldsSize=UR_MAX_SIZE)
Creates a new UniRec record with the specified maximum variable fields size.
bool send(UnirecRecord &unirecRecord) const
Sends a UniRec record through the Trap interface.
void changeTemplate(const std::string &templateFields="")
Changes the UniRec template for the Trap interface.
void setAutoflushTimeout(int timeout)
Sets the autoflush timeout for the Trap interface.
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.
#define ur_create_output_template(ifc, fields, errstr)
Create UniRec template and set it to output interface Creates UniRec template (same like ur_create_te...
Definition: unirec.h:806
char * ur_ifc_data_fmt_to_field_names(const char *ifc_data_fmt)
Parses field names from data format Function parses field names from data format and returns pointer ...
Definition: unirec.c:438
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
int ur_define_set_of_fields(const char *ifc_data_fmt)
Define set of new UniRec fields Define new UniRec fields at run-time. It adds new fields into existin...
Definition: unirec.c:564
Defines the UnirecOutputInterface class.
#define UR_OK
No problem.
Definition: unirec.h:90