forked from sysrepo/sysrepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oper_data_poll_example.c
97 lines (80 loc) · 2.47 KB
/
oper_data_poll_example.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
/**
* @file oper_data_poll_example.c
* @author Michal Vasko <[email protected]>
* @brief example of an application subscribing for oper data polling
*
* @copyright
* Copyright (c) 2022 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libyang/libyang.h>
#include "sysrepo.h"
volatile int exit_application = 0;
static void
sigint_handler(int signum)
{
(void)signum;
exit_application = 1;
}
int
main(int argc, const char **argv)
{
sr_conn_ctx_t *connection = NULL;
sr_session_ctx_t *session = NULL;
sr_subscription_ctx_t *subscr = NULL;
int rc = SR_ERR_OK, valid_ms, gen_diff = 0;
const char *mod_name, *xpath;
if (argc < 4) {
printf("%s <module-to-subscribe> <xpath-to-subscribe> <poll-interval-ms> [gen-diff]\n", argv[0]);
return EXIT_FAILURE;
}
mod_name = argv[1];
xpath = argv[2];
valid_ms = atoi(argv[3]);
if ((argc > 4) && !strcmp(argv[4], "gen-diff")) {
gen_diff = 1;
}
printf("Application will poll oper get subscription on module \"%s\" xpath \"%s\" every %d ms%s.\n\n",
mod_name, xpath, valid_ms, gen_diff ? " and generate changes" : "");
/* turn logging on */
sr_log_stderr(SR_LL_WRN);
/* connect to sysrepo */
rc = sr_connect(0, &connection);
if (rc != SR_ERR_OK) {
goto cleanup;
}
/* start session in operational datastore */
rc = sr_session_start(connection, SR_DS_OPERATIONAL, &session);
if (rc != SR_ERR_OK) {
goto cleanup;
}
/* start the oper poll subscription */
rc = sr_oper_poll_subscribe(session, mod_name, xpath, valid_ms, gen_diff ? SR_SUBSCR_OPER_POLL_DIFF : 0, &subscr);
if (rc != SR_ERR_OK) {
goto cleanup;
}
printf("\n\n ========== OPERATIONAL DATA ARE BEING POLLED ==========\n\n");
/* loop until ctrl-c is pressed / SIGINT is received */
signal(SIGINT, sigint_handler);
signal(SIGPIPE, SIG_IGN);
while (!exit_application) {
sleep(1000);
}
printf("Application exit requested, exiting.\n");
cleanup:
sr_unsubscribe(subscr);
sr_disconnect(connection);
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}