Author: samisa Date: Tue May 15 20:09:10 2007 New Revision: 538415 URL: http://svn.apache.org/viewvc?view=rev&rev=538415 Log: Applied patch by Mark and also some more improvments to help and command line options
Modified: webservices/axis2/trunk/c/tools/tcpmon/src/tcpmon.c Modified: webservices/axis2/trunk/c/tools/tcpmon/src/tcpmon.c URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/c/tools/tcpmon/src/tcpmon.c?view=diff&rev=538415&r1=538414&r2=538415 ============================================================================== --- webservices/axis2/trunk/c/tools/tcpmon/src/tcpmon.c (original) +++ webservices/axis2/trunk/c/tools/tcpmon/src/tcpmon.c Tue May 15 20:09:10 2007 @@ -26,13 +26,20 @@ #include <axutil_string.h> #define SIZE 1024 +axis2_char_t *tcpmon_traffic_log = "tcpmon_traffic.log"; int on_new_entry(const axutil_env_t *env, tcpmon_entry_t* entry, int status); +int on_new_entry_to_file(const axutil_env_t *env, + tcpmon_entry_t* entry, + int status); int on_error_func(const axutil_env_t *env, char* error_message); +char *str_replace(char *str, const char *search, const char *replace); + + int main(int argc, char** argv) { axutil_env_t* env = NULL; @@ -60,8 +67,13 @@ if (!axutil_strcmp(argv[1], "-h")) { - printf("Usage : %s [OPTIONS] -lp LISTEN_PORT -tp TARGET_PORT -th [TARGET_HOST]\n", argv[0]); - printf("use -h for help\n"); + printf("Usage : %s [-lp LISTEN_PORT] [-tp TARGET_PORT] [-th TARGET_HOST] [-f LOG_FILE]\n", argv[0]); + fprintf(stdout, " Options :\n"); + fprintf(stdout, "\t-lp LISTEN_PORT \t port number to listen on, default is 9090\n"); + fprintf(stdout, "\t-tp TARGET_PORT \t port number to connect and re-direct messages, default is 8080\n"); + fprintf(stdout, "\t-th TARGET_HOST \t target host to connect, default is localhost\n"); + fprintf(stdout, "\t-f LOG_FILE \t file to write the messages to, default is %s\n", tcpmon_traffic_log); + fprintf(stdout, " Help :\n\t-h \t display this help screen.\n\n"); return 0; } @@ -74,7 +86,7 @@ if (listen_port == 0) { printf("INVALID value for listen port\n"); - printf("use -h for help\n"); + printf("Use -h for help\n"); return 0; } @@ -86,7 +98,7 @@ if (target_port == 0) { printf("INVALID value for target port\n"); - printf("use -h for help\n"); + printf("Use -h for help\n"); return 0; } } @@ -105,12 +117,17 @@ ii++; format_bit = 1; } + else if (!strcmp ("-f", argv[ii])) + { + ii++; + tcpmon_traffic_log = argv[ii++]; + } else - { + { printf("INVALID value for tcpmon \n"); - printf("use -h for help\n"); + printf("Use -h for help\n"); return 0; - } + } } if (!(listen_port && target_port && target_host)) @@ -128,7 +145,9 @@ TCPMON_SESSION_SET_TARGET_PORT(session, env, target_port); TCPMON_SESSION_SET_TARGET_HOST(session, env, target_host); TCPMON_SESSION_ON_TRANS_FAULT(session, env, on_error_func); - TCPMON_SESSION_ON_NEW_ENTRY(session, env, on_new_entry); + + TCPMON_SESSION_ON_NEW_ENTRY(session, env, on_new_entry_to_file); + TCPMON_SESSION_SET_TEST_BIT (session, env, test_bit); TCPMON_SESSION_SET_FORMAT_BIT(session, env, format_bit); TCPMON_SESSION_START(session, env); @@ -147,6 +166,105 @@ return 0; } + +int on_new_entry_to_file(const axutil_env_t *env, + tcpmon_entry_t* entry, + int status) +{ + char* plain_buffer = NULL; + char* formated_buffer = NULL; + int format = 0; + FILE *file; + char *convert = NULL; + + file = fopen(tcpmon_traffic_log, "a+"); + + if(NULL == file) { + printf("\ncould not create or open log-file\n"); + return -1; + } + + fprintf(file, "\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n"); + + format = TCPMON_ENTRY_GET_FORMAT_BIT(entry, env); + + if (status == 0) + { + plain_buffer = TCPMON_ENTRY_SENT_DATA(entry, env); + if (plain_buffer) /* this can be possible as no xml present */ + { + formated_buffer = tcpmon_util_format_as_xml + (env, plain_buffer, format); + } + else + { + formated_buffer = ""; + } + /* 2 screen */ + printf("%s\n", "SENDING DATA.."); + printf("/* sending time = %s*/\n", TCPMON_ENTRY_SENT_TIME(entry, env)); + printf("---------------------\n"); + + printf("%s\n\n%s\n\n", TCPMON_ENTRY_SENT_HEADERS(entry, env), formated_buffer); + + /* 2 file */ + fprintf(file, "%s\n", "SENDING DATA.."); + fprintf(file, "/* sending time = %s*/\n", TCPMON_ENTRY_SENT_TIME(entry, env)); + fprintf(file, "---------------------\n"); + + convert = TCPMON_ENTRY_SENT_HEADERS(entry, env); + convert = str_replace(convert, "; ", ";\n\t"); + fprintf(file, "%s", convert); + + convert = formated_buffer; + convert = str_replace(convert, "; ", ";\n\t"); + convert = str_replace(convert, "><", ">\n<"); + fprintf(file, "%s", convert); + + + } + if (status == 1) + { + plain_buffer = TCPMON_ENTRY_ARRIVED_DATA(entry, env); + if (plain_buffer) /* this can be possible as no xml present */ + { + formated_buffer = tcpmon_util_format_as_xml + (env, plain_buffer, format); + } + else + { + formated_buffer = ""; + } + /* 2 screen */ + printf("%s\n", "RETRIEVING DATA.."); + printf("/* retrieving time = %s*/\n", TCPMON_ENTRY_ARRIVED_TIME(entry, env)); + printf("/* time throughput = %s*/\n", TCPMON_ENTRY_TIME_DIFF(entry, env)); + printf("---------------------\n"); + + printf("%s\n\n%s\n\n", TCPMON_ENTRY_ARRIVED_HEADERS(entry, env), formated_buffer); + + /* 2 file */ + fprintf(file, "%s\n", "RETRIEVING DATA.."); + fprintf(file, "/* retrieving time = %s*/\n", TCPMON_ENTRY_ARRIVED_TIME(entry, env)); + fprintf(file, "/* time throughput = %s*/\n", TCPMON_ENTRY_TIME_DIFF(entry, env)); + fprintf(file, "---------------------\n"); + + convert = TCPMON_ENTRY_ARRIVED_HEADERS(entry, env); + convert = str_replace(convert, "; ", ";\n\t"); + fprintf(file, "%s", convert); + + convert = formated_buffer; + convert = str_replace(convert, "; ", ";\n\t"); + convert = str_replace(convert, "><", ">\n<"); + fprintf(file, "%s", convert); + } + + fclose(file); + + return 0; +} + + int on_new_entry(const axutil_env_t *env, tcpmon_entry_t* entry, int status) @@ -173,8 +291,7 @@ printf("/* sending time = %s*/\n", TCPMON_ENTRY_SENT_TIME(entry, env)); printf("---------------------\n"); - printf("%s\n\n%s\n\n", TCPMON_ENTRY_SENT_HEADERS(entry, env), - formated_buffer); + printf("%s\n\n%s\n\n", TCPMON_ENTRY_SENT_HEADERS(entry, env), formated_buffer); } if (status == 1) { @@ -199,6 +316,8 @@ return 0; } + + int on_error_func(const axutil_env_t *env, char* error_message) { @@ -207,6 +326,47 @@ } + + +char *str_replace(char *str, const char *search, const char *replace) { + int size = strlen(str) * 2; + int addmem = size; + int diff = strlen(replace)-strlen(search); + + char *str_return = (char *) malloc(size *sizeof(char)); + char *str_tmp = (char *) malloc(size * sizeof(char)); + char *str_relic; + + if(str_return == NULL || str_tmp == NULL) { + free(str_return); + free(str_tmp); + return "function str_replace : gimme more memory"; + } + + strcpy(str_return, str); + + while( (str_relic = strstr(str_return, search)) != NULL) { + if( strlen(str_return) + diff >= addmem) { + str_return = (char *) realloc(str_return, addmem+=size); + str_tmp = (char *) realloc(str_tmp, addmem); + + if(str_return == NULL || str_tmp == NULL) { + free(str_return); + free(str_tmp); + return "function str_replace : gimme more memory"; + } + } + + strcpy(str_tmp, replace); + strcat(str_tmp, (str_relic+strlen(search)) ); + *str_relic = '\0'; + + strcat(str_return, str_tmp); + } + + free(str_tmp); + return(str_return); +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]