Skip to content

Commit 2f330ac

Browse files
Adrian Duriskasedmicha
authored andcommitted
Core: Introduce periodic message type
1 parent b0212b2 commit 2f330ac

File tree

8 files changed

+204
-0
lines changed

8 files changed

+204
-0
lines changed

include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(SUB_HEADERS
1414
ipfixcol2/message_garbage.h
1515
ipfixcol2/message_ipfix.h
1616
ipfixcol2/message_session.h
17+
ipfixcol2/message_periodic.h
1718
ipfixcol2/plugins.h
1819
ipfixcol2/session.h
1920
ipfixcol2/utils.h

include/ipfixcol2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <ipfixcol2/message_garbage.h>
6161
#include <ipfixcol2/message_session.h>
6262
#include <ipfixcol2/message_ipfix.h>
63+
#include <ipfixcol2/message_periodic.h>
6364

6465
#include <ipfixcol2/plugins.h>
6566
#include <ipfixcol2/session.h>

include/ipfixcol2/message.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ enum ipx_msg_type {
7777
IPX_MSG_TERMINATE = (1 << 3),
7878
// An internal configuration message (only for internal usage)
7979
//IPX_MSG_CONFIG = (1 << 4)
80+
/** A periodic message */
81+
IPX_MSG_PERIODIC = (1 << 5)
8082
};
8183

8284
/** The data type of the base message */
@@ -107,6 +109,7 @@ ipx_msg_destroy(ipx_msg_t *msg);
107109
#include <ipfixcol2/message_ipfix.h>
108110
#include <ipfixcol2/message_garbage.h>
109111
#include <ipfixcol2/message_session.h>
112+
#include <ipfixcol2/message_periodic.h>
110113

111114
/**
112115
* \brief Cast from a base message to a session message
@@ -147,6 +150,19 @@ ipx_msg_base2ipfix(ipx_msg_t *msg)
147150
return (ipx_msg_ipfix_t *) msg;
148151
}
149152

153+
/**
154+
* \brief Cast base message to periodic message
155+
*
156+
* \param[in] msg Pointer to the base message
157+
* \return A pointer to a periodic message
158+
*/
159+
static inline ipx_msg_periodic_t *
160+
ipx_msg_base2periodic(ipx_msg_t *msg)
161+
{
162+
assert(ipx_msg_get_type(msg) == IPX_MSG_PERIODIC);
163+
return (ipx_msg_periodic_t *) msg;
164+
}
165+
150166
/**@}*/
151167

152168
#ifdef __cplusplus
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
#ifndef IPX_MESSAGE_PERIODIC_H
11+
#define IPX_MESSAGE_PERIODIC_H
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
#include <ipfixcol2/message.h>
18+
19+
typedef struct ipx_msg_periodic ipx_msg_periodic_t;
20+
21+
/**
22+
* \brief Cast periodic message to base message
23+
*
24+
* \param[in] msg Pointer to the periodic message
25+
* \return A pointer to a base message
26+
*/
27+
static inline ipx_msg_t *
28+
ipx_msg_periodic2base(ipx_msg_periodic_t *msg)
29+
{
30+
return (ipx_msg_t *) msg;
31+
}
32+
33+
/**
34+
* \brief Get sequence number of periodic message
35+
*
36+
* \param[in] msg Pointer to the periodic message
37+
* \return Periodic messages sequence number
38+
*/
39+
IPX_API uint64_t
40+
ipx_msg_periodic_get_seq_num(ipx_msg_periodic_t *msg);
41+
42+
/**
43+
* \brief Get created time of periodic message
44+
*
45+
* \param[in] msg Pointer to the periodic message
46+
* \return Periodic messages created time
47+
*/
48+
IPX_API struct timespec
49+
ipx_msg_periodic_get_created(ipx_msg_periodic_t *msg);
50+
51+
/**
52+
* \brief Get last intermediate plugin processed time of periodic message
53+
*
54+
* \param[in] msg Pointer to the periodic message
55+
* \return Periodic messages last intermediate plugin processed time
56+
*/
57+
IPX_API struct timespec
58+
ipx_msg_periodic_get_last_processed(ipx_msg_periodic_t *msg);
59+
60+
#ifdef __cplusplus
61+
}
62+
#endif
63+
#endif

src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ set(CORE_SOURCE
4141
message_garbage.c
4242
message_ipfix.c
4343
message_ipfix.h
44+
message_periodic.c
4445
message_session.c
4546
message_terminate.c
4647
message_terminate.h

src/core/message_base.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,8 @@ ipx_msg_destroy(ipx_msg_t *msg)
6767
case IPX_MSG_TERMINATE:
6868
ipx_msg_terminate_destroy(ipx_msg_base2terminate(msg));
6969
break;
70+
case IPX_MSG_PERIODIC:
71+
ipx_msg_periodic_destroy(ipx_msg_base2periodic(msg));
72+
break;
7073
}
7174
}

src/core/message_periodic.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

src/core/message_periodic.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
#ifndef IPFIXCOL_MESSAGE_PERIODIC_INTERNAL_H
11+
#define IPFIXCOL_MESSAGE_PERIODIC_INTERNAL_H
12+
13+
#include <ipfixcol2.h>
14+
#include "message_base.h"
15+
16+
/**
17+
* \brief Create a periodic message
18+
*
19+
* \param[in,out] seq Pointer to the periodic messages sequence counter
20+
* \return On success returns a pointer to the message. Otherwise returns NULL.
21+
*/
22+
ipx_msg_periodic_t *
23+
ipx_msg_periodic_create(uint64_t seq);
24+
25+
/**
26+
* \brief Destroy a periodic message
27+
*
28+
* \param[in] msg Pointer to the periodic message to be destroyed
29+
*/
30+
void
31+
ipx_msg_periodic_destroy(ipx_msg_periodic_t *msg);
32+
33+
/**
34+
* \brief Set last intermediate processed time
35+
*/
36+
void
37+
ipx_msg_periodic_update_last_processed(ipx_msg_periodic_t *msg);
38+
39+
#endif

0 commit comments

Comments
 (0)