Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI session state monitoring support #1649

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ endif()
# Generic version of not only the library. Major version is reserved for really big changes of the project,
# minor version changes with added functionality (new tool, functionality of the tool or library, ...) and
# micro version is changed with a set of small changes or bugfixes anywhere in the project.
set(NP2SRV_VERSION 2.2.31)
set(NP2SRV_VERSION 2.2.32)

# libyang required version
set(LIBYANG_DEP_VERSION 2.2.0)
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
64 changes: 63 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,61 @@ cmd_disconnect(const char *UNUSED(arg), char **UNUSED(tmp_config_file))
return EXIT_SUCCESS;
}

void
monitoring_clb(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")) {
if (nc_client_monitoring_thread_start(monitoring_clb, NULL, NULL)) {
ERROR(__func__, "Monitoring thread failed to start.");
return 1;
}
monitor = 1;
} 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 +6636,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