#include "fep.h"
#include "fep_print.cpp"
#include <stdio.h>
#include <stdlib.h>

/* Author: John A. Torres
   The fep_program_32 function creates a new RPC Client which
   sends an fep_reply structure to the ATMS Server. After the
   reply is sent, the ATMS Sends a response and the RPC Client
   is destroyed.
 */

/* Handles the ATMS's response to RPC Call */
void handle_ATMS_response(CLIENT *clnt, void *response)
{
	/* If ATMS reply call fails */
	if (response == NULL)
	{
		clnt_perror(clnt, "RPC call failed");
	}
	/* If ATMS reply is successful */
	else
	{
		printf("Successful RPC call to ATMS...\n");
		printf("Printing ATMS RPC Call response...\n");
		print_reply_ret(*(reply_ret *) (&response));
	}
}

fep_reply generate_reply()
{
	/* Declarations to construct an fep_reply */
	fep_answer_info fai;
	fep_pollerror fpe;
	fep_answer_short_msg fasm;
	fep_shortanswer fsa;
	fep_reply  fep_reply_xfer_32_arg; // the reply to be sent to ATMS

	/* Populate fep_shortmessage data */
	printf("Setting fep_reply values...\n");

	fasm.message_len = 5;
	fasm.message[0] = 0x0a;
	fasm.message[1] = 0x0d;
	fasm.message[2] = 0x11;
	fasm.message[3] = 0x12;
	fasm.message[4] = 0x13;

	fpe.msgerror = (enum answererror) 0;
	fpe.state = (enum Polling_FSM_States) 0x00;
	fpe.perrno = 0;
	fpe.termination = 0;
	fpe.count = 0;

	fai.poll_time = 1111443466;
	fai.status = (enum replystatus) 1;
	fai.poll_user_info1 = 1;
	fai.poll_user_info2 = 2;
	fai.retries = 0;
	fai.poll_error_count = 1;
	fai.pollerror[0] = fpe;

	fsa.info = fai;
	fsa.msg = fasm;

	fep_reply_xfer_32_arg.reply = 0;
	fep_reply_xfer_32_arg.schedule = 0;
	fep_reply_xfer_32_arg.lineinfo = 0;
	fep_reply_xfer_32_arg.kind = (enum polltype)0;
	fep_reply_xfer_32_arg.flag = (enum replykind) 0;
	fep_reply_xfer_32_arg.schedule_sequence = 0;
	fep_reply_xfer_32_arg.global_sequence = 0;
	fep_reply_xfer_32_arg.schedule_time = 1111443466;
	fep_reply_xfer_32_arg.user_info1 = 0;
	fep_reply_xfer_32_arg.user_info2 = 0;
	fep_reply_xfer_32_arg.system_key = 0;
	fep_reply_xfer_32_arg.answers.size = 1;
	fep_reply_xfer_32_arg.answers.fep_answer_list_u.shortp.count = 1;
	fep_reply_xfer_32_arg.answers.fep_answer_list_u.shortp.answers[0] = fsa;

	return fep_reply_xfer_32_arg;
}

/* Creates an RPC client, populates the fep_reply data structure,
   makes an RPC Call to the ATMS Server to pass the fep_reply,
   destroys the RPC Client and returns
 */
void fep_program_32(char *host)
{
	CLIENT *clnt;

	/* Var to recieve message back from ATMS */
	void  *rv;

	/* Create RPC Client to communicate with ATMS */
	printf("Preparing to create RPC client...\n");
	clnt = clnt_create(host, /*100090,*/ 103121, 32, "tcp");

	/* Check if client creation failed */ 
	if (clnt == (CLIENT *) NULL)
	{
		fprintf(stderr, "Can't create client to %s\n", host);
		exit(1);
	}

	/* Make RPC Call to transfer reply to ATMS */
	printf("Transferring data to ATMS Server...\n");
	fep_reply reply = generate_reply();
	rv = fep_reply_xfer_32(&reply, clnt);

	/* Handle ATMS response to RPC Call */
	printf("Handling ATMS response...\n");
	handle_ATMS_response(clnt, rv);

	/* Destroy client */
	printf("Destroying client...\n");
	clnt_destroy(clnt);
	printf("Returning to main...\n");
}

/* Main driver function to create an RPC Client, make a single RPC Call to the
   ATMS Server */
int main(int argc, char *argv[]) {
	char *host;

	if (argc < 2)
	{
		printf("usage:  %s server_host\n", argv[0]);
		exit(1);
	}

	/* Create RPC Client to send an fep_reply to ATMS */
	host = argv[1];
	fep_program_32(host);

	return 0;
}
