Skip to content

Commit 6b69d06

Browse files
authored
Merge pull request #1798 from davidBar-On/issue-1711-library_function_to_get_json_output
Add callback function to get JSON Output strings
2 parents 296222d + 76c60ee commit 6b69d06

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/iperf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ struct iperf_test
343343
int verbose; /* -V option - verbose mode */
344344
int json_output; /* -J option - JSON output */
345345
int json_stream; /* --json-stream */
346+
void (*json_callback) (struct iperf_test *, char *); /* allow user apps to receive the
347+
JSON strings,instead of writing them to the output file */
346348
int zerocopy; /* -Z option - use sendfile */
347349
int debug; /* -d option - enable debug */
348350
enum debug_level debug_level; /* -d option option - level of debug messages to show */

src/iperf_api.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,12 @@ iperf_set_test_json_stream(struct iperf_test *ipt, int json_stream)
697697
ipt->json_stream = json_stream;
698698
}
699699

700+
void
701+
iperf_set_test_json_callback(struct iperf_test *ipt, void (*callback)(struct iperf_test *, char *))
702+
{
703+
ipt->json_callback = callback;
704+
}
705+
700706
int
701707
iperf_has_zerocopy( void )
702708
{
@@ -2864,12 +2870,16 @@ JSONStream_Output(struct iperf_test * test, const char * event_name, cJSON * obj
28642870
char *str = cJSON_PrintUnformatted(event);
28652871
if (str == NULL)
28662872
return -1;
2867-
if (pthread_mutex_lock(&(test->print_mutex)) != 0) {
2868-
perror("iperf_json_finish: pthread_mutex_lock");
2869-
}
2870-
fprintf(test->outfile, "%s\n", str);
2871-
if (pthread_mutex_unlock(&(test->print_mutex)) != 0) {
2872-
perror("iperf_json_finish: pthread_mutex_unlock");
2873+
if (test->json_callback != NULL) {
2874+
(test->json_callback)(test, str);
2875+
} else {
2876+
if (pthread_mutex_lock(&(test->print_mutex)) != 0) {
2877+
perror("iperf_json_finish: pthread_mutex_lock");
2878+
}
2879+
fprintf(test->outfile, "%s\n", str);
2880+
if (pthread_mutex_unlock(&(test->print_mutex)) != 0) {
2881+
perror("iperf_json_finish: pthread_mutex_unlock");
2882+
}
28732883
}
28742884
iflush(test);
28752885
cJSON_free(str);
@@ -3060,6 +3070,8 @@ iperf_defaults(struct iperf_test *testp)
30603070
testp->settings->rcv_timeout.usecs = (DEFAULT_NO_MSG_RCVD_TIMEOUT % SEC_TO_mS) * mS_TO_US;
30613071
testp->zerocopy = 0;
30623072

3073+
testp->json_callback = NULL;
3074+
30633075
memset(testp->cookie, 0, COOKIE_SIZE);
30643076

30653077
testp->multisend = 10; /* arbitrary */
@@ -5035,14 +5047,18 @@ iperf_json_finish(struct iperf_test *test)
50355047
if (test->json_output_string == NULL) {
50365048
return -1;
50375049
}
5038-
if (pthread_mutex_lock(&(test->print_mutex)) != 0) {
5039-
perror("iperf_json_finish: pthread_mutex_lock");
5040-
}
5041-
fprintf(test->outfile, "%s\n", test->json_output_string);
5042-
if (pthread_mutex_unlock(&(test->print_mutex)) != 0) {
5043-
perror("iperf_json_finish: pthread_mutex_unlock");
5050+
if (test->json_callback != NULL) {
5051+
(test->json_callback)(test, test->json_output_string);
5052+
} else {
5053+
if (pthread_mutex_lock(&(test->print_mutex)) != 0) {
5054+
perror("iperf_json_finish: pthread_mutex_lock");
5055+
}
5056+
fprintf(test->outfile, "%s\n", test->json_output_string);
5057+
if (pthread_mutex_unlock(&(test->print_mutex)) != 0) {
5058+
perror("iperf_json_finish: pthread_mutex_unlock");
5059+
}
5060+
iflush(test);
50445061
}
5045-
iflush(test);
50465062
}
50475063
cJSON_Delete(test->json_top);
50485064
}

src/iperf_api.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ void iperf_set_test_template( struct iperf_test *ipt, const char *tmp_templat
197197
void iperf_set_test_reverse( struct iperf_test* ipt, int reverse );
198198
void iperf_set_test_json_output( struct iperf_test* ipt, int json_output );
199199
void iperf_set_test_json_stream( struct iperf_test* ipt, int json_stream );
200+
void iperf_set_test_json_callback(struct iperf_test *ipt, void (*callback)(struct iperf_test *, char *));
200201
int iperf_has_zerocopy( void );
201202
void iperf_set_test_zerocopy( struct iperf_test* ipt, int zerocopy );
202203
void iperf_set_test_get_server_output( struct iperf_test* ipt, int get_server_output );
@@ -419,7 +420,7 @@ enum {
419420
IERVRSONLYRCVTIMEOUT = 32, // Client receive timeout is valid only in reverse mode
420421
IESNDTIMEOUT = 33, // Illegal message send timeout
421422
IEUDPFILETRANSFER = 34, // Cannot transfer file using UDP
422-
IESERVERAUTHUSERS = 35, // Cannot access authorized users file
423+
IESERVERAUTHUSERS = 35, // Cannot access authorized users file
423424
/* Test errors */
424425
IENEWTEST = 100, // Unable to create a new test (check perror)
425426
IEINITTEST = 101, // Test initialization failed (check perror)

0 commit comments

Comments
 (0)