|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Adrian Duriska <[email protected]> |
| 4 | + * @brief Periodic message |
| 5 | + * |
| 6 | + * Copyright: (C) 2024 CESNET, z.s.p.o. |
| 7 | + * SPDX-License-Identifier: BSD-3-Clause |
| 8 | + */ |
| 9 | + |
| 10 | +#include <stddef.h> |
| 11 | +#include <assert.h> |
| 12 | +#include <sys/time.h> |
| 13 | + |
| 14 | +#include <ipfixcol2.h> |
| 15 | +#include "message_periodic.h" |
| 16 | + |
| 17 | +/** |
| 18 | + * \brief Structure of a periodic message |
| 19 | + */ |
| 20 | +struct ipx_msg_periodic { |
| 21 | + struct ipx_msg msg_header; |
| 22 | + /** Sequential number of the message */ |
| 23 | + uint64_t seq; |
| 24 | + /** Timestamp, when the message was created */ |
| 25 | + struct timespec created; |
| 26 | + /** Timestamp, when the message left the last intermediate plugin */ |
| 27 | + struct timespec last_processed; |
| 28 | +}; |
| 29 | + |
| 30 | +static_assert(offsetof(struct ipx_msg_periodic, msg_header.type) == 0, |
| 31 | + "Message header must be the first element of each IPFIXcol message."); |
| 32 | + |
| 33 | +ipx_msg_periodic_t * |
| 34 | +ipx_msg_periodic_create(uint64_t seq) |
| 35 | +{ |
| 36 | + struct ipx_msg_periodic *msg; |
| 37 | + |
| 38 | + msg = malloc(sizeof(*msg)); |
| 39 | + if (!msg) { |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + |
| 43 | + ipx_msg_header_init(&msg->msg_header, IPX_MSG_PERIODIC); |
| 44 | + clock_gettime(CLOCK_MONOTONIC, &msg->created); |
| 45 | + msg->last_processed = msg->created; |
| 46 | + msg->seq = seq; |
| 47 | + |
| 48 | + return msg; |
| 49 | +} |
| 50 | + |
| 51 | +void |
| 52 | +ipx_msg_periodic_destroy(ipx_msg_periodic_t *msg) |
| 53 | +{ |
| 54 | + ipx_msg_header_destroy(&msg->msg_header); |
| 55 | + free(msg); |
| 56 | +} |
| 57 | + |
| 58 | +uint64_t |
| 59 | +ipx_msg_periodic_get_seq_num(ipx_msg_periodic_t *msg) |
| 60 | +{ |
| 61 | + return msg->seq; |
| 62 | +} |
| 63 | + |
| 64 | +struct timespec |
| 65 | +ipx_msg_periodic_get_created(ipx_msg_periodic_t *msg) |
| 66 | +{ |
| 67 | + return msg->created; |
| 68 | +} |
| 69 | + |
| 70 | +struct timespec |
| 71 | +ipx_msg_periodic_get_last_processed(ipx_msg_periodic_t *msg) |
| 72 | +{ |
| 73 | + return msg->last_processed; |
| 74 | +} |
| 75 | + |
| 76 | +void |
| 77 | +ipx_msg_periodic_update_last_processed(ipx_msg_periodic_t *msg) |
| 78 | +{ |
| 79 | + clock_gettime(CLOCK_MONOTONIC, &msg->last_processed); |
| 80 | +} |
0 commit comments