#include <stdio.h>
#include <axis2_client.h>

#define MAX_COUNTER			9999

/* Function Prototypes */
static axiom_node_t *build_om_from_file( const axutil_env_t * env, axis2_char_t *fileName );

int main( int argc, char **argv )
{
	int nRc = 0;
	long counter = 0;

	if( argc < 4 )
	{
		printf( "\nUsage: %s AXIS_HOME EndPoint SoapAction\n", argv[0] );
		return -1;
	}

   do
	{
		const axutil_env_t 				* env = NULL;
		axiom_node_t       				*payload       = NULL;
      axis2_svc_client_t 				*svc_client 	= NULL;

		const axis2_char_t            *address       = NULL;
		axis2_endpoint_ref_t          *endpoint_ref  = NULL;
		axutil_string_t               *soap_action   = NULL;
		axis2_options_t               *options       = NULL;
		axiom_node_t                  *ret_node      = NULL;

		/* Create Environment */
		env = axutil_env_create_all( "AXIS_LEAK1.log", AXIS2_LOG_LEVEL_CRITICAL );
		if( !env )
		{
			nRc = 1;
			printf("Create env FAILED!\n");
			break;
		}

		/* Read payload from the file */
		payload = build_om_from_file(env, "AXIS_LEAK1.XML" );
		if( !payload )
		{
			nRc = 2;
			printf("Build AXIOM FAILED!\n");
			break;
		}

		/* Create Client */
		svc_client = axis2_svc_client_create(env, argv[1] );
		if( !svc_client )
		{
			nRc = 3;
			printf("Create Client FAILED!\n");
			break;
		}

		address = argv[2];
		endpoint_ref = axis2_endpoint_ref_create(env, address);

		/* Create and Set options */
		options = axis2_options_create(env);
		axis2_options_set_to(options, env, endpoint_ref);
		soap_action = axutil_string_create(env, argv[3] );
		axis2_options_set_soap_action(options, env, soap_action);
		axutil_string_free( soap_action, env);
		axis2_options_set_soap_version(options, env, AXIOM_SOAP11);
		axis2_svc_client_set_options(svc_client, env, options);

		printf( "Enter any key to start the loop...\n" );
		getch();

		/* Loop to test for memory leaks */
		do
		{			
			/* Send request */
			ret_node = axis2_svc_client_send_receive(svc_client, env, payload);
			if( ret_node )
			{
/*						
						axis2_char_t *om_str = NULL;
						om_str = axiom_node_to_string(ret_node, env);
						if( om_str )
							printf("%s\n", om_str);
						AXIS2_FREE(env->allocator, om_str);
*/
				axiom_node_free_tree(ret_node, env);
				ret_node = NULL;
			}
			else
			{
				printf("Send/Recv FAILED!\n");
				nRc = 4;
				break;
			}
			printf( "." );

		} while( counter++ < MAX_COUNTER );

		printf( "Enter any key to continue...\n" );
		getch();

		/* Free Payload */
		if( payload )
		{
			axiom_node_free_tree(payload, env);
			payload = NULL;
		}
		/* Free Client */
		if( svc_client )
		{
			axis2_svc_client_free(svc_client, env);
			svc_client = NULL;
		}
		/* Free Environment */
		if( env )
		{
			axutil_env_free((axutil_env_t *) env);
			env = NULL;
		}
	} while( 0 );

	printf( "\nCounter: %d\n", counter );
	printf( "\nReturn Code:%d\n", nRc );
	printf( "\nEnter any key to terminate...\n" );
	getch();

	return nRc;
}
/*
 *Function Name:build_om_from_file
 *
 *Parameters:
 *
 *Description: Returns OM created from an XML File
 *
 *Returns:
 *
 */

static axiom_node_t *build_om_from_file( const axutil_env_t * env, axis2_char_t *fileName )
{
	axiom_xml_reader_t *r = NULL;
	axiom_stax_builder_t *sb = NULL;
	axiom_document_t *doc = NULL;
	do
	{
		r = axiom_xml_reader_create_for_file(env, fileName, NULL);
		if( r )
		{
			sb = axiom_stax_builder_create(env, r);
			if( sb )
			{
				doc = axiom_stax_builder_get_document(sb, env);
				if( doc )
				{
					return axiom_document_build_all(doc, env);
				}
			}
		}
	}while( 0 );

	return NULL;
}
