Skip to content

Commit

Permalink
Add callback function to get JSON strings instead of writing them to …
Browse files Browse the repository at this point in the history
…the output file
  • Loading branch information
davidBar-On committed Nov 17, 2024
1 parent 2575ae8 commit 5a5957f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/iperf.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ struct iperf_test
int verbose; /* -V option - verbose mode */
int json_output; /* -J option - JSON output */
int json_stream; /* --json-stream */
void (*json_callback) (char *); /* used by user apps to receive the JSON strings,
instead of writing them to the output file */
int zerocopy; /* -Z option - use sendfile */
int debug; /* -d option - enable debug */
enum debug_level debug_level; /* -d option option - level of debug messages to show */
Expand Down
42 changes: 29 additions & 13 deletions src/iperf_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,12 @@ iperf_set_test_json_stream(struct iperf_test *ipt, int json_stream)
ipt->json_stream = json_stream;
}

void
iperf_set_test_json_callback(struct iperf_test *ipt, void (*callback)())
{
ipt->json_callback = callback;
}

int
iperf_has_zerocopy( void )
{
Expand Down Expand Up @@ -2821,12 +2827,16 @@ JSONStream_Output(struct iperf_test * test, const char * event_name, cJSON * obj
char *str = cJSON_PrintUnformatted(event);
if (str == NULL)
return -1;
if (pthread_mutex_lock(&(test->print_mutex)) != 0) {
perror("iperf_json_finish: pthread_mutex_lock");
}
fprintf(test->outfile, "%s\n", str);
if (pthread_mutex_unlock(&(test->print_mutex)) != 0) {
perror("iperf_json_finish: pthread_mutex_unlock");
if (test->json_callback != NULL) {
(test->json_callback)(str);
} else {
if (pthread_mutex_lock(&(test->print_mutex)) != 0) {
perror("iperf_json_finish: pthread_mutex_lock");
}
fprintf(test->outfile, "%s\n", str);
if (pthread_mutex_unlock(&(test->print_mutex)) != 0) {
perror("iperf_json_finish: pthread_mutex_unlock");
}
}
iflush(test);
cJSON_free(str);
Expand Down Expand Up @@ -3017,6 +3027,8 @@ iperf_defaults(struct iperf_test *testp)
testp->settings->rcv_timeout.usecs = (DEFAULT_NO_MSG_RCVD_TIMEOUT % SEC_TO_mS) * mS_TO_US;
testp->zerocopy = 0;

testp->json_callback = NULL;

memset(testp->cookie, 0, COOKIE_SIZE);

testp->multisend = 10; /* arbitrary */
Expand Down Expand Up @@ -4989,14 +5001,18 @@ iperf_json_finish(struct iperf_test *test)
if (test->json_output_string == NULL) {
return -1;
}
if (pthread_mutex_lock(&(test->print_mutex)) != 0) {
perror("iperf_json_finish: pthread_mutex_lock");
}
fprintf(test->outfile, "%s\n", test->json_output_string);
if (pthread_mutex_unlock(&(test->print_mutex)) != 0) {
perror("iperf_json_finish: pthread_mutex_unlock");
if (test->json_callback != NULL) {
(test->json_callback)(test->json_output_string);
} else {
if (pthread_mutex_lock(&(test->print_mutex)) != 0) {
perror("iperf_json_finish: pthread_mutex_lock");
}
fprintf(test->outfile, "%s\n", test->json_output_string);
if (pthread_mutex_unlock(&(test->print_mutex)) != 0) {
perror("iperf_json_finish: pthread_mutex_unlock");
}
iflush(test);
}
iflush(test);
}
cJSON_Delete(test->json_top);
}
Expand Down
3 changes: 2 additions & 1 deletion src/iperf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ void iperf_set_test_template( struct iperf_test *ipt, const char *tmp_templat
void iperf_set_test_reverse( struct iperf_test* ipt, int reverse );
void iperf_set_test_json_output( struct iperf_test* ipt, int json_output );
void iperf_set_test_json_stream( struct iperf_test* ipt, int json_stream );
void iperf_set_test_json_callback(struct iperf_test *ipt, void (*callback)());
int iperf_has_zerocopy( void );
void iperf_set_test_zerocopy( struct iperf_test* ipt, int zerocopy );
void iperf_set_test_get_server_output( struct iperf_test* ipt, int get_server_output );
Expand Down Expand Up @@ -419,7 +420,7 @@ enum {
IERVRSONLYRCVTIMEOUT = 32, // Client receive timeout is valid only in reverse mode
IESNDTIMEOUT = 33, // Illegal message send timeout
IEUDPFILETRANSFER = 34, // Cannot transfer file using UDP
IESERVERAUTHUSERS = 35, // Cannot access authorized users file
IESERVERAUTHUSERS = 35, // Cannot access authorized users file
/* Test errors */
IENEWTEST = 100, // Unable to create a new test (check perror)
IEINITTEST = 101, // Test initialization failed (check perror)
Expand Down

0 comments on commit 5a5957f

Please sign in to comment.