-
Notifications
You must be signed in to change notification settings - Fork 24
/
example_module.c
227 lines (190 loc) · 7.75 KB
/
example_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* \file example_module.c
* \brief Example of NEMEA module.
* \author Vaclav Bartos <[email protected]>
* \author Marek Svepes <[email protected]>
* \date 2016
*/
/*
* Copyright (C) 2013,2014,2015,2016 CESNET
*
* LICENSE TERMS
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``as is'', and any express or implied
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the company or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <signal.h>
#include <getopt.h>
#include <libtrap/trap.h>
#include <unirec/unirec.h>
#include "fields.h"
/**
* Definition of fields used in unirec templates (for both input and output interfaces)
*/
UR_FIELDS (
uint32 BAR,
uint32 FOO,
uint32 BAZ
)
trap_module_info_t *module_info = NULL;
/**
* Definition of basic module information - module name, module description, number of input and output interfaces
*/
#define MODULE_BASIC_INFO(BASIC) \
BASIC("Example module", \
"This module serves as an example of module implementation in TRAP platform. It receives UniRec" \
"containing two numbers (FOO and BAR) and sends UniRec containing the same numbers and their sum (BAZ).", 1, 1)
//BASIC(char *, char *, int, int)
/**
* Definition of module parameters - every parameter has short_opt, long_opt, description,
* flag whether an argument is required or it is optional and argument type which is NULL
* in case the parameter does not need argument.
* Module parameter argument types: int8, int16, int32, int64, uint8, uint16, uint32, uint64, float, string
*/
#define MODULE_PARAMS(PARAM) \
PARAM('m', "mult", "Multiplies the sum of received numbers with ARG of the parameter.", required_argument, "int32")
//PARAM(char, char *, char *, no_argument or required_argument, char *)
/**
* To define positional parameter ("param" instead of "-m param" or "--mult param"), use the following definition:
* PARAM('-', "", "Parameter description", required_argument, "string")
* There can by any argument type mentioned few lines before.
* This parameter will be listed in Additional parameters in module help output
*/
static int stop = 0;
/**
* Function to handle SIGTERM and SIGINT signals (used to stop the module)
*/
TRAP_DEFAULT_SIGNAL_HANDLER(stop = 1)
int main(int argc, char **argv)
{
int ret;
signed char opt;
int mult = 1;
/* **** TRAP initialization **** */
/*
* Macro allocates and initializes module_info structure according to MODULE_BASIC_INFO and MODULE_PARAMS
* definitions on the lines 71 and 84 of this file. It also creates a string with short_opt letters for getopt
* function called "module_getopt_string" and long_options field for getopt_long function in variable "long_options"
*/
INIT_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS)
/*
* Let TRAP library parse program arguments, extract its parameters and initialize module interfaces
*/
TRAP_DEFAULT_INITIALIZATION(argc, argv, *module_info);
/*
* Register signal handler.
*/
TRAP_REGISTER_DEFAULT_SIGNAL_HANDLER();
/*
* Parse program arguments defined by MODULE_PARAMS macro with getopt() function (getopt_long() if available)
* This macro is defined in config.h file generated by configure script
*/
while ((opt = TRAP_GETOPT(argc, argv, module_getopt_string, long_options)) != -1) {
switch (opt) {
case 'm':
mult = atoi(optarg);
break;
default:
fprintf(stderr, "Invalid arguments.\n");
FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS);
TRAP_DEFAULT_FINALIZATION();
return -1;
}
}
/* **** Create UniRec templates **** */
ur_template_t *in_tmplt = ur_create_input_template(0, "FOO,BAR", NULL);
if (in_tmplt == NULL){
fprintf(stderr, "Error: Input template could not be created.\n");
return -1;
}
ur_template_t *out_tmplt = ur_create_output_template(0, "FOO,BAR,BAZ", NULL);
if (out_tmplt == NULL){
ur_free_template(in_tmplt);
fprintf(stderr, "Error: Output template could not be created.\n");
return -1;
}
// Allocate memory for output record
void *out_rec = ur_create_record(out_tmplt, 0);
if (out_rec == NULL){
ur_free_template(in_tmplt);
ur_free_template(out_tmplt);
fprintf(stderr, "Error: Memory allocation problem (output record).\n");
return -1;
}
/* **** Main processing loop **** */
// Read data from input, process them and write to output
while (!stop) {
const void *in_rec;
uint16_t in_rec_size;
// Receive data from input interface 0.
// Block if data are not available immediately (unless a timeout is set using trap_ifcctl)
ret = TRAP_RECEIVE(0, in_rec, in_rec_size, in_tmplt);
// Handle possible errors
TRAP_DEFAULT_RECV_ERROR_HANDLING(ret, continue, break);
// Check size of received data
if (in_rec_size < ur_rec_fixlen_size(in_tmplt)) {
if (in_rec_size <= 1) {
break; // End of data (used for testing purposes)
} else {
fprintf(stderr, "Error: data with wrong size received (expected size: >= %hu, received size: %hu)\n",
ur_rec_fixlen_size(in_tmplt), in_rec_size);
break;
}
}
// PROCESS THE DATA
// Read FOO and BAR from input record and compute their sum
uint32_t baz = ur_get(in_tmplt, in_rec, F_FOO) +
ur_get(in_tmplt, in_rec, F_BAR);
// Fill output record
ur_copy_fields(out_tmplt, out_rec, in_tmplt, in_rec);
ur_set(out_tmplt, out_rec, F_BAZ, mult * baz);
// Send record to interface 0.
// Block if ifc is not ready (unless a timeout is set using trap_ifcctl)
ret = trap_send(0, out_rec, ur_rec_fixlen_size(out_tmplt));
// Handle possible errors
TRAP_DEFAULT_SEND_ERROR_HANDLING(ret, continue, break);
}
/* **** Cleanup **** */
// Do all necessary cleanup in libtrap before exiting
TRAP_DEFAULT_FINALIZATION();
// Release allocated memory for module_info structure
FREE_MODULE_INFO_STRUCT(MODULE_BASIC_INFO, MODULE_PARAMS)
// Free unirec templates and output record
ur_free_record(out_rec);
ur_free_template(in_tmplt);
ur_free_template(out_tmplt);
ur_finalize();
return 0;
}