Libtrap: Internal development docs  1.16.1
trap_ifc.h
Go to the documentation of this file.
1 /**
2  * \file trap_ifc.h
3  * \brief Interface of TRAP interfaces.
4  * \author Vaclav Bartos <ibartosv@fit.vutbr.cz>
5  * \author Tomas Cejka <cejkat@cesnet.cz>
6  * \author Jan Neuzil <neuzija1@fit.cvut.cz>
7  * \author Marek Svepes <svepemar@fit.cvut.cz>
8  * \author Tomas Jansky <janskto1@fit.cvut.cz>
9  * \date 2013 -2018
10  */
11 /*
12  * Copyright (C) 2013-2018 CESNET
13  *
14  * LICENSE TERMS
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in
23  * the documentation and/or other materials provided with the
24  * distribution.
25  * 3. Neither the name of the Company nor the names of its contributors
26  * may be used to endorse or promote products derived from this
27  * software without specific prior written permission.
28  *
29  * ALTERNATIVELY, provided that this notice is retained in full, this
30  * product may be distributed under the terms of the GNU General Public
31  * License (GPL) version 2 or later, in which case the provisions
32  * of the GPL apply INSTEAD OF those given above.
33  *
34  * This software is provided ``as is'', and any express or implied
35  * warranties, including, but not limited to, the implied warranties of
36  * merchantability and fitness for a particular purpose are disclaimed.
37  * In no event shall the company or contributors be liable for any
38  * direct, indirect, incidental, special, exemplary, or consequential
39  * damages (including, but not limited to, procurement of substitute
40  * goods or services; loss of use, data, or profits; or business
41  * interruption) however caused and on any theory of liability, whether
42  * in contract, strict liability, or tort (including negligence or
43  * otherwise) arising in any way out of the use of this software, even
44  * if advised of the possibility of such damage.
45  *
46  */
47 #ifndef _TRAP_IFC_H_
48 #define _TRAP_IFC_H_
49 
50 #include <stdint.h>
51 #include <sys/time.h>
52 #include <pthread.h>
53 #include <semaphore.h>
54 
55 /** \defgroup trap_ifc TRAP communication module interface
56  * @{
57  */
58 
59 /**
60  * Default max number of clients that can connect to output interface.
61  * It takes effect when no value is given during interface initialization.
62  */
63 #ifndef TRAP_IFC_DEFAULT_MAX_CLIENTS
64 #define TRAP_IFC_DEFAULT_MAX_CLIENTS 64
65 #endif
66 
67 /**
68  * \defgroup trap_ifc_api IFC API
69  *
70  * The set of function that must be implemented for communication interface.
71  * @{
72  */
73 /**
74  * Receive one message via this IFC.
75  *
76  * This function is called from trap_read_from_buffer() when there is a
77  * need to get new data.
78  *
79  * \param[in] p pointer to IFC's private memory allocated by constructor
80  * \param[out] d pointer to memory where this IFC can write received message
81  * \param[out] s size (in bytes) of received message (must be set by this IFC)
82  * \param[in] t timeout, see \ref trap_timeout
83  * \returns TRAP_E_OK on success
84  */
85 typedef int (*ifc_recv_func_t)(void *p, void *d, uint32_t *s, int t);
86 
87 /**
88  * Send one message via this IFC.
89  *
90  * This function is called from trap_store_into_buffer() when there is a
91  * need to send new data.
92  *
93  * \param[in] p pointer to IFC's private memory allocated by constructor
94  * \param[in] d pointer to message that will be sent
95  * \param[in] s size (in bytes) of message that will be sent
96  * \param[in] t timeout, see \ref trap_timeout
97  * \returns TRAP_E_OK on success
98  */
99 typedef int (*ifc_send_func_t)(void *p, const void *d, uint16_t s, int t);
100 
101 /**
102  * Force flush on interface
103  *
104  * \param[in] p pointer to IFC's private memory allocated by constructor
105  */
106 typedef void (*ifc_flush_func_t)(void *p);
107 
108 /**
109  * Disconnect all connected clients to output IFC.
110  *
111  * \param[in] p pointer to IFC's private memory allocated by constructor
112  */
113 typedef void (*ifc_disconn_clients_func_t)(void *p);
114 
115 /**
116  * Terminate IFC - stop sending/receiving.
117  *
118  * \param[in] p pointer to IFC's private memory allocated by constructor
119  */
120 typedef void (*ifc_terminate_func_t)(void *p);
121 
122 /**
123  * Destructor, called to free allocated memory.
124  *
125  * \param[in,out] p pointer to IFC's private memory allocated by constructor
126  */
127 typedef void (*ifc_destroy_func_t)(void *p);
128 
129 /**
130  * Create dump of interface for debug purposes.
131  *
132  * \param[in] p pointer to IFC's private memory allocated by constructor
133  * \param[in] i index of interface (index into the IFC array in the context
134  * #trap_ctx_priv_s)
135  * \param[in] d directory path to generate output
136  */
137 typedef void (*ifc_create_dump_func_t)(void *p, uint32_t i, const char *d);
138 
139 /**
140  * Get number of connected clients.
141  *
142  * \param[in] p pointer to IFC's private memory allocated by constructor
143  * \returns number of clients that are connected to this IFC
144  */
145 typedef int32_t (*ifc_get_client_count_func_t)(void *p);
146 
147 /**
148  * Get json array with client statistics
149  *
150  * \param[in] p pointer to IFC's private memory allocated by constructor
151  * \param[out] client_stats_arr pointer to JSON array to be filled with client statistics
152  * \returns 1 (TRUE) on success, otherwise 0 (FALSE)
153  */
154 typedef int8_t (*ifc_get_client_stats_json_func_t)(void *p, json_t *client_stats_arr);
155 
156 /**
157  * Get identifier of the interface
158  *
159  * \param[in] priv pointer to IFC's private memory allocated by constructor
160  * \returns pointer to char (private memory of the ifc - do not free!).
161  * TCP and UNIXSOCKET ifces return port and name of socket, file ifc returns name of the file,
162  * GENERATOR and BLACKHOLE ifces return NULL.
163  */
164 typedef char * (*ifc_get_id_func_t)(void *priv);
165 
166 /**
167  * Check whether the input interface is connected
168  *
169  * \param[in] priv pointer to IFC's private memory allocated by constructor
170  * \returns 1 (TRUE) if connected, otherwise 0 (FALSE)
171  */
172 typedef uint8_t (*ifc_is_conn_func_t)(void *priv);
173 
174 
175 /**
176  * @}
177  */
178 
179 /** Struct to hold an instance of some input interface. */
180 typedef struct trap_input_ifc_s {
181  ifc_is_conn_func_t is_conn; ///< Pointer to is_connected function
182  ifc_get_id_func_t get_id; ///< Pointer to get_id function
183  ifc_recv_func_t recv; ///< Pointer to receive function
184  ifc_terminate_func_t terminate; ///< Pointer to terminate function
185  ifc_destroy_func_t destroy; ///< Pointer to destructor function
186  ifc_create_dump_func_t create_dump; ///< Pointer to function for generating of dump
187  void *priv; ///< Pointer to instance's private data
188  char *buffer; ///< Internal pointer to buffer for messages
189  char *buffer_pointer; ///< Internal pointer to current message in buffer
190  uint32_t buffer_unread_bytes; ///< Number of unread bytes in buffer.
191  int32_t datatimeout; ///< Timeout for *_recv() calls
192  char ifc_type; ///< Type of interface
193  pthread_mutex_t ifc_mtx; ///< Locking mutex for interface.
194 
195  /**
196  * If 1 do not allow to change timeout by module, it is used to force
197  * timeout of IFC by module's parameter. If 0 - timeout can be changed
198  * by standard way using trap_ctx_ifcctl().
199  */
201 
202  /**
203  * Negotiation state defined as #trap_in_ifc_state_t.
204  */
206 
207  /**
208  * Message format defined by trap_data_format_t.
209  */
210  uint8_t data_type;
211 
212  /**
213  * Message format specifier.
214  *
215  * if data_type is TRAP_FMT_RAW, no data_fmt_spec is expected. Otherwise,
216  * data_fmt_spec contains e.g. UniRec template specifier (string representation)
217  */
219 
220  /**
221  * Required message format defined by trap_data_format_t
222  */
223  uint8_t req_data_type;
224 
225  /**
226  * Required message format specifier.
227  *
228  * if data_type is TRAP_FMT_RAW, no data_fmt_spec is expected. Otherwise,
229  * data_fmt_spec contains e.g. UniRec template specifier (string representation)
230  */
233 
234 /** Struct to hold an instance of some output interface. */
235 typedef struct trap_output_ifc_s {
236  ifc_get_id_func_t get_id; ///< Pointer to get_id function
237  ifc_disconn_clients_func_t disconn_clients; ///< Pointer to disconnect_clients function
238  ifc_send_func_t send; ///< Pointer to send function
239  ifc_flush_func_t flush; ///< Pointer to flush function
240  ifc_terminate_func_t terminate; ///< Pointer to terminate function
241  ifc_destroy_func_t destroy; ///< Pointer to destructor function
242  ifc_create_dump_func_t create_dump; ///< Pointer to function for generating of dump
243  ifc_get_client_count_func_t get_client_count; ///< Pointer to get_client_count function
244  ifc_get_client_stats_json_func_t get_client_stats_json; ///< Pointer to get_client_stats_json function
245  void *priv; ///< Pointer to instance's private data
246  pthread_mutex_t ifc_mtx; ///< Locking mutex for interface.
247  int64_t timeout; ///< Internal structure to send partial data after timeout (autoflush).
248  int32_t datatimeout; ///< Timeout for *_send() calls
249  char ifc_type; ///< Type of interface
250  char bufferswitch; ///< Enable (1) or Disable (0) buffering, default is Enabled (1).
251 
252  /**
253  * If 1 do not allow to change autoflush timeout by module. If 0 - autoflush can
254  * be changed by standard way using trap_ctx_ifcctl().
255  */
257 
258  /**
259  * If 1 do not allow to change bufferswitch by module. If 0 - bufferswitch can
260  * be changed by standard way using trap_ctx_ifcctl().
261  */
263 
264  /**
265  * If 1 do not allow to change timeout by module, it is used to force
266  * timeout of IFC by module's parameter. If 0 - timeout can be changed
267  * by standard way using trap_ctx_ifcctl().
268  */
270 
271  /**
272  * Message format specifier.
273  *
274  * if data_type is TRAP_FMT_RAW, no data_fmt_spec is expected. Otherwise,
275  * data_fmt_spec contains e.g. UniRec template specifier (string representation)
276  */
278 
279  /**
280  * Message format defined by trap_data_format_t
281  */
282  uint8_t data_type;
284 
285 /**
286  * \brief Internal function for setting of timeout structs according to libtrap timeout.
287  *
288  * \param[in] timeout timeout in microseconds, or \ref trap_timeout
289  * \param[out] tm used for select() call when non-blocking
290  * \param[out] tmnblk used for sem_timedwait() call to block on semaphore.
291  */
292 void trap_set_timeouts(int timeout, struct timeval *tm, struct timespec *tmnblk);
293 
294 /**
295  * \brief Internal function for setting of timeout structs according to libtrap timeout.
296  *
297  * \param[in] tm Precomputed timeval, set using e.g. trap_set_timeouts().
298  * \param[out] tmnblk Used for sem_timedwait() call to block on semaphore.
299  */
300 void trap_set_abs_timespec(struct timeval *tm, struct timespec *tmnblk);
301 
302 /**
303  * @}
304  */
305 #endif
ifc_get_id_func_t get_id
Pointer to get_id function.
Definition: trap_ifc.h:182
int(* ifc_send_func_t)(void *p, const void *d, uint16_t s, int t)
Definition: trap_ifc.h:99
int(* ifc_recv_func_t)(void *p, void *d, uint32_t *s, int t)
Definition: trap_ifc.h:85
char *(* ifc_get_id_func_t)(void *priv)
Definition: trap_ifc.h:164
char * data_fmt_spec
Definition: trap_ifc.h:218
ifc_get_client_stats_json_func_t get_client_stats_json
Pointer to get_client_stats_json function.
Definition: trap_ifc.h:244
char * req_data_fmt_spec
Definition: trap_ifc.h:231
struct trap_input_ifc_s trap_input_ifc_t
pthread_mutex_t ifc_mtx
Locking mutex for interface.
Definition: trap_ifc.h:246
void(* ifc_create_dump_func_t)(void *p, uint32_t i, const char *d)
Definition: trap_ifc.h:137
void trap_set_abs_timespec(struct timeval *tm, struct timespec *tmnblk)
Internal function for setting of timeout structs according to libtrap timeout.
Definition: trap.c:1190
void trap_set_timeouts(int timeout, struct timeval *tm, struct timespec *tmnblk)
Internal function for setting of timeout structs according to libtrap timeout.
Definition: trap.c:1211
ifc_get_id_func_t get_id
Pointer to get_id function.
Definition: trap_ifc.h:236
int8_t(* ifc_get_client_stats_json_func_t)(void *p, json_t *client_stats_arr)
Definition: trap_ifc.h:154
void(* ifc_destroy_func_t)(void *p)
Definition: trap_ifc.h:127
void(* ifc_flush_func_t)(void *p)
Definition: trap_ifc.h:106
char * buffer_pointer
Internal pointer to current message in buffer.
Definition: trap_ifc.h:189
int32_t(* ifc_get_client_count_func_t)(void *p)
Definition: trap_ifc.h:145
struct trap_output_ifc_s trap_output_ifc_t
ifc_create_dump_func_t create_dump
Pointer to function for generating of dump.
Definition: trap_ifc.h:242
char ifc_type
Type of interface.
Definition: trap_ifc.h:249
pthread_mutex_t ifc_mtx
Locking mutex for interface.
Definition: trap_ifc.h:193
char * data_fmt_spec
Definition: trap_ifc.h:277
trap_in_ifc_state_t client_state
Definition: trap_ifc.h:205
ifc_get_client_count_func_t get_client_count
Pointer to get_client_count function.
Definition: trap_ifc.h:243
uint8_t req_data_type
Definition: trap_ifc.h:223
ifc_disconn_clients_func_t disconn_clients
Pointer to disconnect_clients function.
Definition: trap_ifc.h:237
char datatimeout_fixed
Definition: trap_ifc.h:269
ifc_terminate_func_t terminate
Pointer to terminate function.
Definition: trap_ifc.h:184
char bufferswitch
Enable (1) or Disable (0) buffering, default is Enabled (1).
Definition: trap_ifc.h:250
ifc_destroy_func_t destroy
Pointer to destructor function.
Definition: trap_ifc.h:185
int32_t datatimeout
Timeout for *_send() calls.
Definition: trap_ifc.h:248
uint8_t data_type
Definition: trap_ifc.h:282
ifc_send_func_t send
Pointer to send function.
Definition: trap_ifc.h:238
int32_t datatimeout
Timeout for *_recv() calls.
Definition: trap_ifc.h:191
char bufferswitch_fixed
Definition: trap_ifc.h:262
uint32_t buffer_unread_bytes
Number of unread bytes in buffer.
Definition: trap_ifc.h:190
ifc_recv_func_t recv
Pointer to receive function.
Definition: trap_ifc.h:183
ifc_create_dump_func_t create_dump
Pointer to function for generating of dump.
Definition: trap_ifc.h:186
char datatimeout_fixed
Definition: trap_ifc.h:200
char * buffer
Internal pointer to buffer for messages.
Definition: trap_ifc.h:188
int64_t timeout
Internal structure to send partial data after timeout (autoflush).
Definition: trap_ifc.h:247
char ifc_type
Type of interface.
Definition: trap_ifc.h:192
ifc_destroy_func_t destroy
Pointer to destructor function.
Definition: trap_ifc.h:241
ifc_terminate_func_t terminate
Pointer to terminate function.
Definition: trap_ifc.h:240
ifc_flush_func_t flush
Pointer to flush function.
Definition: trap_ifc.h:239
void * priv
Pointer to instance&#39;s private data.
Definition: trap_ifc.h:187
ifc_is_conn_func_t is_conn
Pointer to is_connected function.
Definition: trap_ifc.h:181
void * priv
Pointer to instance&#39;s private data.
Definition: trap_ifc.h:245
void(* ifc_terminate_func_t)(void *p)
Definition: trap_ifc.h:120
trap_in_ifc_state_t
Definition: trap.h:242
uint8_t data_type
Definition: trap_ifc.h:210
uint8_t(* ifc_is_conn_func_t)(void *priv)
Definition: trap_ifc.h:172
void(* ifc_disconn_clients_func_t)(void *p)
Definition: trap_ifc.h:113