UniRec  3.3.2
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  , m_isInitialized(false)
21 {
22 }
23 
25 {
27  sendEoF();
28  }
29 
31 }
32 
33 bool UnirecOutputInterface::send(UnirecRecord& unirecRecord) const
34 {
35  int errorCode = trap_send(m_interfaceID, unirecRecord.data(), unirecRecord.size());
36  return handleSendErrorCodes(errorCode);
37 }
38 
39 bool UnirecOutputInterface::send(UnirecRecordView& unirecRecordView) const
40 {
41  int errorCode = trap_send(m_interfaceID, unirecRecordView.data(), unirecRecordView.size());
42  return handleSendErrorCodes(errorCode);
43 }
44 
46 {
47  if (errorCode == TRAP_E_TIMEOUT) {
48  return false;
49  }
50  if (errorCode == TRAP_E_OK) {
51  return true;
52  }
53  if (errorCode == TRAP_E_NOT_INITIALIZED) {
54  throw std::runtime_error(
55  "UnirecOutputInterface::send() has failed. Trap interface is not initialized.");
56  }
57  if (errorCode == TRAP_E_TERMINATED) {
58  throw std::runtime_error(
59  "UnirecOutputInterface::send() has failed. Trap interface is terminated.");
60  }
61  if (errorCode == TRAP_E_BAD_IFC_INDEX) {
62  throw std::runtime_error(
63  "UnirecOutputInterface::send() has failed. Interface ID out of range.");
64  }
65  throw std::runtime_error(
66  "UnirecOutputInterface::send() has failed. Return code: " + std::to_string(errorCode)
67  + ", msg: " + trap_last_error_msg);
68 }
69 
71 {
72  trap_send_flush(m_interfaceID);
73 }
74 
76 {
77  m_sendEoFonExit = false;
78 }
79 
81 {
82  trap_ifcctl(TRAPIFC_OUTPUT, m_interfaceID, TRAPCTL_SETTIMEOUT, timeout);
83 }
84 
86 {
87  trap_ifcctl(TRAPIFC_OUTPUT, m_interfaceID, TRAPCTL_AUTOFLUSH_TIMEOUT, timeout);
88 }
89 
91 {
92  char dummy[1] = { 0 };
93  trap_send(m_interfaceID, dummy, sizeof(dummy));
94 }
95 
96 void UnirecOutputInterface::changeTemplate(const std::string& templateFields)
97 {
98  if (ur_define_set_of_fields(templateFields.c_str()) != UR_OK) {
99  throw std::runtime_error(
100  "UnirecOutputInterface::changeTemplate() has failed. Template fields could not be "
101  "defined!");
102  }
103 
104  std::unique_ptr<char*> fieldNames
105  = std::make_unique<char*>(ur_ifc_data_fmt_to_field_names(templateFields.c_str()));
106 
107  if (!fieldNames) {
108  throw std::runtime_error(
109  "UnirecOutputInterface::changeTemplate() has failed. Field specs could not be "
110  "converted.");
111  }
113  m_template = ur_create_output_template(m_interfaceID, *fieldNames, nullptr);
114  if (!m_template) {
115  throw std::runtime_error(
116  "UnirecOutputInterface::changeTemplate() has failed. Output template could not be "
117  "created.");
118  }
120 
121  m_isInitialized = true;
122 }
123 
125 {
126  return UnirecRecord(m_template, maxVariableFieldsSize);
127 }
128 
129 } // 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