Skip to content

Commit 9ec4aa0

Browse files
romanmichalvasko
roman
authored andcommitted
cli UPDATE add client monitoring support
1 parent 5391a73 commit 9ec4aa0

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ set(LIBYANG_DEP_SOVERSION 3.0.0)
4949
set(LIBYANG_DEP_SOVERSION_MAJOR 3)
5050

5151
# libnetconf2 required version
52-
set(LIBNETCONF2_DEP_VERSION 3.5.0)
53-
set(LIBNETCONF2_DEP_SOVERSION 4.4.0)
52+
set(LIBNETCONF2_DEP_VERSION 3.5.2)
53+
set(LIBNETCONF2_DEP_SOVERSION 4.4.2)
5454
set(LIBNETCONF2_DEP_SOVERSION_MAJOR 4)
5555

5656
# sysrepo required version

cli/commands.c

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ COMMAND commands[];
7070
extern int done;
7171
struct nc_session *session;
7272
volatile int interleave;
73-
int timed;
73+
int timed, monitor;
7474

7575
static int cmd_disconnect(const char *arg, char **tmp_config_file);
7676

@@ -1264,6 +1264,12 @@ cmd_timed_help(void)
12641264
printf("timed [--help] [on | off]\n");
12651265
}
12661266

1267+
static void
1268+
cmd_monitor_help(void)
1269+
{
1270+
printf("monitor [--help] [on | off]\n");
1271+
}
1272+
12671273
#ifdef NC_ENABLED_SSH_TLS
12681274

12691275
static void
@@ -2557,6 +2563,61 @@ cmd_disconnect(const char *UNUSED(arg), char **UNUSED(tmp_config_file))
25572563
return EXIT_SUCCESS;
25582564
}
25592565

2566+
void
2567+
monitoring_clb(struct nc_session *sess, void *user_data)
2568+
{
2569+
int was_rawmode = 0;
2570+
2571+
(void)sess;
2572+
(void)user_data;
2573+
2574+
/* needed for the case that the user is typing a command */
2575+
if (lss.rawmode) {
2576+
was_rawmode = 1;
2577+
linenoiseDisableRawMode(lss.ifd);
2578+
printf("\n");
2579+
}
2580+
2581+
fprintf(stdout, "Connection reset by peer.\n");
2582+
fflush(stdout);
2583+
2584+
/* set the global session variable to NULL */
2585+
session = NULL;
2586+
2587+
if (was_rawmode) {
2588+
linenoiseEnableRawMode(lss.ifd);
2589+
linenoiseRefreshLine();
2590+
}
2591+
}
2592+
2593+
static int
2594+
cmd_monitor(const char *arg, char **UNUSED(tmp_config_file))
2595+
{
2596+
char *args = strdupa(arg);
2597+
char *cmd = NULL;
2598+
2599+
strtok(args, " ");
2600+
if ((cmd = strtok(NULL, " ")) == NULL) {
2601+
fprintf(stdout, "Connection state will %sbe monitored.\n", monitor ? "" : "not ");
2602+
} else {
2603+
if (!strcmp(cmd, "on")) {
2604+
if (nc_client_monitoring_thread_start(monitoring_clb, NULL, NULL)) {
2605+
ERROR(__func__, "Monitoring thread failed to start.");
2606+
return 1;
2607+
}
2608+
monitor = 1;
2609+
} else if (!strcmp(cmd, "off")) {
2610+
monitor = 0;
2611+
nc_client_monitoring_thread_stop();
2612+
} else {
2613+
ERROR(__func__, "Unknown option %s.", cmd);
2614+
cmd_monitor_help();
2615+
}
2616+
}
2617+
2618+
return 0;
2619+
}
2620+
25602621
static int
25612622
cmd_status(const char *UNUSED(arg), char **UNUSED(tmp_config_file))
25622623
{
@@ -6575,6 +6636,7 @@ COMMAND commands[] = {
65756636
"ietf-subscribed-notifications <modify-subscription> operation with ietf-yang-push augments"
65766637
},
65776638
{"modify-sub", cmd_modifysub, cmd_modifysub_help, "ietf-subscribed-notifications <modify-subscription> operation"},
6639+
{"monitor", cmd_monitor, cmd_monitor_help, "Monitor client connection status"},
65786640
{"outputformat", cmd_outputformat, cmd_outputformat_help, "Set the output format of all the data"},
65796641
{"resync-sub", cmd_resyncsub, cmd_resyncsub_help, "ietf-yang-push <resync-subscription> operation"},
65806642
{"searchpath", cmd_searchpath, cmd_searchpath_help, "Set the search path for models"},

cli/doc/netopeer2-cli.1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,16 @@ Current time minus the given number of seconds.
11491149
.PP
11501150

11511151

1152+
.SS monitor
1153+
Monitor the client connection status.
1154+
.PP
1155+
1156+
.B monitor
1157+
[\-\-help] [on | off]
1158+
.RE
1159+
.RE
1160+
1161+
11521162
.SS outputformat
11531163
Set the format for all the output data. XML is the default.
11541164
.PP

cli/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
int done;
4141
extern struct nc_session *session;
42+
extern int monitor;
4243

4344
static void
4445
lnc2_print_clb(const struct nc_session *UNUSED(session), NC_VERB_LEVEL level, const char *msg)
@@ -220,6 +221,9 @@ main(void)
220221
if (session) {
221222
nc_session_free(session, NULL);
222223
}
224+
if (monitor) {
225+
nc_client_monitoring_thread_stop();
226+
}
223227
nc_client_destroy();
224228

225229
return 0;

0 commit comments

Comments
 (0)