int on_new_entry()

in tools/tcpmon/src/tcpmon.c [42:247]


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);


void sig_handler(
    int signal);

int resend_request(
    const axutil_env_t * env,
    int status);

int
main(
    int argc,
    char **argv)
{
    axutil_env_t *env = NULL;
    int c;
    int listen_port = 9099,
        target_port = 9090; /* the default port simple axis2 server is run on 9090  */
    int test_bit = 0;
    int format_bit = 0;  /* pretty print the request/response SOAP messages */
    int ii = 1;

    if (!axutil_strcmp(argv[1], "-h"))
    {
        printf
            ("Usage : %s [-lp LISTEN_PORT] [-tp TARGET_PORT] [-th TARGET_HOST] [-f LOG_FILE] [options]\n",
             argv[0]);
        fprintf(stdout, " Options :\n");
        fprintf(stdout,
                "\t-lp LISTEN_PORT \tport number to listen on, default is 9099\n");
        fprintf(stdout,
                "\t-tp TARGET_PORT \tport number to connect and re-direct messages, default is 9090\n");
        fprintf(stdout,
                "\t-th TARGET_HOST \ttarget host to connect, default is localhost\n");
        fprintf(stdout,
                "\t-f  LOG_FILE    \tfile to write the messages to, default is %s\n",
                tcpmon_traffic_log);
        fprintf(stdout,
                "\t--format        \tenable xml formatting\n");
        fprintf(stdout,
                "\t--test          \tenable testing last request/response by logging it separately\n");
        fprintf(stdout, " Help :\n\t-h              \tdisplay this help screen.\n\n");
        return 0;
    }

    env = axutil_env_create_all("axis2_tcpmon.log", AXIS2_LOG_LEVEL_DEBUG);

    signal(SIGINT, sig_handler);
    system_env = env;
#ifndef WIN32
    signal(SIGPIPE, sig_handler);
#endif

    target_host = axutil_strdup(env, "localhost"); 

    while (ii < argc)
    {
        if (!strcmp("-lp", argv[ii]))
        {
            ii++;
            if (argv[ii])
            {
                listen_port = atoi(argv[ii++]);
                if (listen_port == 0)
                {
                    printf("INVALID value for listen port\n");
                    printf("Use -h for help\n");
                    AXIS2_FREE(env->allocator, target_host);
                    if (env)
                    {
                        axutil_env_free((axutil_env_t *) env);
                        env = NULL;
                    }
                    return 0;
                }
            }
        }
        else if (!strcmp("-tp", argv[ii]))
        {
            ii++;
            if (argv[ii])
            {
                target_port = atoi(argv[ii++]);
                if (target_port == 0)
                {
                    printf("INVALID value for target port\n");
                    printf("Use -h for help\n");
                    AXIS2_FREE(env->allocator, target_host);
                    if (env)
                    {
                        axutil_env_free((axutil_env_t *) env);
                        env = NULL;
                    }
                    return 0;
                }
            }
        }
        else if (!strcmp("-th", argv[ii]))
        {
            ii++;
            if (argv[ii])
            {
                AXIS2_FREE(env->allocator, target_host);
                target_host = (char *) axutil_strdup(env, argv[ii++]);
            }
        }
        else if (!strcmp("--test", argv[ii]))
        {
            ii++;
            test_bit = 1;
        }
        else if (!strcmp("--format", argv[ii]))
        {
            ii++;
            format_bit = 1;
        }
        else if (!strcmp("-f", argv[ii]))
        {
            ii++;
            if (argv[ii])
            {
                tcpmon_traffic_log = argv[ii++];
            }
        }
        else
        {
            printf("INVALID value for tcpmon \n");
            printf("Use -h for help\n");
            AXIS2_FREE(env->allocator, target_host);
            if (env)
            {
                axutil_env_free((axutil_env_t *) env);
                env = NULL;
            }
            return 0;
        }
    }

    if (!(listen_port && target_port && target_host))
    {
        printf("ERROR: essential argument missing \n");
        printf
            ("Please recheck values of listen_port (-lp), target_port(-tp) and target_host (-th)\n");
        AXIS2_FREE(env->allocator, target_host);
        if (env)
        {
            axutil_env_free((axutil_env_t *) env);
            env = NULL;
        }
        return 0;
    }

    printf("Listen port : %d Target port : %d Target host: %s\n",
           listen_port, target_port, target_host);
    session = tcpmon_session_create(env);

    TCPMON_SESSION_SET_LISTEN_PORT(session, env, listen_port);
    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_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);

    do
    {
        c = getchar();
        if (c == 'f')
        {
            format_bit = format_bit ? 0 : 1;
            TCPMON_SESSION_SET_FORMAT_BIT(session, env, format_bit);
        }
        else if (c == 'r')
        {
            resend_request(env, 0);
        }
    }
    while (c != 'q');
    printf("\n\n");

    TCPMON_SESSION_STOP(session, env);
    TCPMON_SESSION_FREE(session, env);
    AXIS2_FREE(env->allocator, target_host);
    if (env)
    {
        axutil_env_free((axutil_env_t *) env);
        env = NULL;
    }
    return 0;
}