Skip to content

Commit

Permalink
cli UPDATE add client monitoring support
Browse files Browse the repository at this point in the history
  • Loading branch information
roman committed Oct 4, 2024
1 parent 5391a73 commit 53877b1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ set(LIBYANG_DEP_SOVERSION 3.0.0)
set(LIBYANG_DEP_SOVERSION_MAJOR 3)

# libnetconf2 required version
set(LIBNETCONF2_DEP_VERSION 3.5.0)
set(LIBNETCONF2_DEP_SOVERSION 4.4.0)
set(LIBNETCONF2_DEP_VERSION 3.5.2)
set(LIBNETCONF2_DEP_SOVERSION 4.4.2)
set(LIBNETCONF2_DEP_SOVERSION_MAJOR 4)

# sysrepo required version
Expand Down
61 changes: 60 additions & 1 deletion cli/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ COMMAND commands[];
extern int done;
struct nc_session *session;
volatile int interleave;
int timed;
int timed, monitor;

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

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

static void
cmd_monitor_help(void)
{
printf("monitor [--help] [on | off]\n");
}

#ifdef NC_ENABLED_SSH_TLS

static void
Expand Down Expand Up @@ -2557,6 +2563,58 @@ cmd_disconnect(const char *UNUSED(arg), char **UNUSED(tmp_config_file))
return EXIT_SUCCESS;
}

void
monitoring_clb(const struct nc_session *sess, void *user_data)
{
int was_rawmode = 0;

(void)sess;
(void)user_data;

/* needed for the case that the user is typing a command */
if (lss.rawmode) {
was_rawmode = 1;
linenoiseDisableRawMode(lss.ifd);
printf("\n");
}

fprintf(stdout, "Connection reset by peer.\n");
fflush(stdout);

/* set the global session variable to NULL */
session = NULL;

if (was_rawmode) {
linenoiseEnableRawMode(lss.ifd);
linenoiseRefreshLine();
}
}

static int
cmd_monitor(const char *arg, char **UNUSED(tmp_config_file))
{
char *args = strdupa(arg);
char *cmd = NULL;

strtok(args, " ");
if ((cmd = strtok(NULL, " ")) == NULL) {
fprintf(stdout, "Connection state will %sbe monitored.\n", monitor ? "" : "not ");
} else {
if (!strcmp(cmd, "on")) {
monitor = 1;
nc_client_monitoring_thread_start(monitoring_clb, NULL, NULL);
} else if (!strcmp(cmd, "off")) {
monitor = 0;
nc_client_monitoring_thread_stop();
} else {
ERROR(__func__, "Unknown option %s.", cmd);
cmd_monitor_help();
}
}

return 0;
}

static int
cmd_status(const char *UNUSED(arg), char **UNUSED(tmp_config_file))
{
Expand Down Expand Up @@ -6575,6 +6633,7 @@ COMMAND commands[] = {
"ietf-subscribed-notifications <modify-subscription> operation with ietf-yang-push augments"
},
{"modify-sub", cmd_modifysub, cmd_modifysub_help, "ietf-subscribed-notifications <modify-subscription> operation"},
{"monitor", cmd_monitor, cmd_monitor_help, "Monitor client connection status"},
{"outputformat", cmd_outputformat, cmd_outputformat_help, "Set the output format of all the data"},
{"resync-sub", cmd_resyncsub, cmd_resyncsub_help, "ietf-yang-push <resync-subscription> operation"},
{"searchpath", cmd_searchpath, cmd_searchpath_help, "Set the search path for models"},
Expand Down
10 changes: 10 additions & 0 deletions cli/doc/netopeer2-cli.1
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,16 @@ Current time minus the given number of seconds.
.PP


.SS monitor
Monitor the client connection status.
.PP

.B monitor
[\-\-help] [on | off]
.RE
.RE


.SS outputformat
Set the format for all the output data. XML is the default.
.PP
Expand Down
4 changes: 4 additions & 0 deletions cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

int done;
extern struct nc_session *session;
extern int monitor;

static void
lnc2_print_clb(const struct nc_session *UNUSED(session), NC_VERB_LEVEL level, const char *msg)
Expand Down Expand Up @@ -220,6 +221,9 @@ main(void)
if (session) {
nc_session_free(session, NULL);
}
if (monitor) {
nc_client_monitoring_thread_stop();
}
nc_client_destroy();

return 0;
Expand Down

0 comments on commit 53877b1

Please sign in to comment.