UniRec 3.3.2
Loading...
Searching...
No Matches
unirec.cpp
Go to the documentation of this file.
1#include <unirec++/unirec.hpp>
3
4namespace Nemea {
5
6Unirec::Unirec(const TrapModuleInfo& trapModuleInfo)
7 : m_availableInputInterfaces(0)
8 , m_availableOutputInterfaces(0)
9 , m_trapModuleInfo(trapModuleInfo)
10{
11}
12
13void Unirec::init(int& argc, char** argv)
14{
15 trap_ifc_spec_t trapIfcSpec;
16
17 parseCommandLine(argc, argv, trapIfcSpec);
18
19 if (trap_init(&m_trapModuleInfo.m_moduleInfo, trapIfcSpec) != TRAP_E_OK) {
20 trap_free_ifc_spec(trapIfcSpec);
21 throw std::runtime_error("Unirec::init() has failed. " + std::string(trap_last_error_msg));
22 }
23
24 trap_free_ifc_spec(trapIfcSpec);
25
28}
29
30void Unirec::parseCommandLine(int& argc, char** argv, trap_ifc_spec_t& trapIfcSpec)
31{
32 int ret = trap_parse_params(&argc, argv, &trapIfcSpec);
33 if (ret == TRAP_E_OK) {
34 return;
35 }
36
37 if (ret == TRAP_E_HELP) {
38 trap_print_help(&m_trapModuleInfo.m_moduleInfo);
39 throw HelpException();
40 }
41
42 throw std::runtime_error(
43 "Libtrap::parseCommandLine() has failed. " + std::string(trap_last_error_msg));
44}
45
47{
49 throw std::runtime_error("TODO");
50 }
51 uint8_t interfaceID = --m_availableInputInterfaces;
52 return UnirecInputInterface(interfaceID);
53}
54
56{
58 throw std::runtime_error("TODO");
59 }
60 uint8_t interfaceID = --m_availableOutputInterfaces;
61 return UnirecOutputInterface(interfaceID);
62}
63
65{
67 throw std::runtime_error("TODO");
68 }
69
70 uint8_t inputInterfaceID = --m_availableInputInterfaces;
71 uint8_t outputInterfaceID = --m_availableOutputInterfaces;
72 return UnirecBidirectionalInterface(inputInterfaceID, outputInterfaceID);
73}
74
75ur_field_id_t Unirec::defineUnirecField(const std::string& fieldName, ur_field_type_t fieldType)
76{
77 int ret = ur_define_field(fieldName.c_str(), fieldType);
78 if (ret < 0) {
79 throw std::runtime_error(
80 "Unirec::defineUnirecField() has failed. Error code=[" + std::to_string(ret) + "]");
81 }
82 return static_cast<ur_field_id_t>(ret);
83}
84
86{
87 trap_finalize();
89}
90
91} // namespace Nemea
This exception is thrown when the libtrap command-line argument contains help flag.
Class representing information about a trap module.
trap_module_info_t m_moduleInfo
A class that provides a bidirectional interface for sending and receiving unirec records.
A class that provides an interface for receiving data in the Unirec format using the TRAP library.
A class for sending UniRec records through a Trap interface.
Unirec(const TrapModuleInfo &trapModuleInfo)
Definition unirec.cpp:6
bool isBidirectionalInterfaceAvailable() const noexcept
Definition unirec.hpp:40
UnirecBidirectionalInterface buildBidirectionalInterface()
Definition unirec.cpp:64
void parseCommandLine(int &argc, char **argv, trap_ifc_spec_t &trapIfcSpec)
Definition unirec.cpp:30
UnirecInputInterface buildInputInterface()
Builds and returns a UnirecInputInterface object if an input interface is available.
Definition unirec.cpp:46
TrapModuleInfo m_trapModuleInfo
Definition unirec.hpp:50
void init(int &argc, char **argv)
Definition unirec.cpp:13
bool isInputInterfaceAvailable() const noexcept
Definition unirec.hpp:38
bool isOutputInterfaceAvailable() const noexcept
Definition unirec.hpp:39
uint8_t m_availableInputInterfaces
Definition unirec.hpp:48
ur_field_id_t defineUnirecField(const std::string &fieldName, ur_field_type_t fieldType)
Definition unirec.cpp:75
UnirecOutputInterface buildOutputInterface()
Definition unirec.cpp:55
uint8_t m_availableOutputInterfaces
Definition unirec.hpp:49
void ur_finalize()
Deallocate UniRec structures Deallocate UniRec structures at the end of a program....
Definition unirec.c:740
int ur_define_field(const char *name, ur_field_type_t type)
Define new UniRec field Define new UniRec field at run-time. It adds new field into existing structur...
Definition unirec.c:642
Defines custom exception classes.
ur_field_type_t
Definition unirec.h:95
int16_t ur_field_id_t
Type of UniRec field identifiers.
Definition unirec.h:136