#include "fep.h"
#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.
*/
void fep_program_32(char *host)
{
	CLIENT *clnt;

    /* Declerations 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

	/* 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, FEP_PROGRAM, FEP_VERSION, "tcp");

	/* Check if client creation failed */
	if (clnt == (CLIENT *) NULL)
	{
		fprintf(stderr, "can't create client to %s\n", host);
        /* Show why rpc handle couldn't be created */
        clnt_pcreateerror("");
		exit(1);
    }

    /* Populate fep_shortmessage data */
	printf("setting 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 = 0;
	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;

    /* Make RPC Call to transfer reply to ATMS */
	printf("transferring data\n");
	rv = fep_reply_xfer_32(&fep_reply_xfer_32_arg, clnt);
	printf("checking reply`\n");

	/* If ATMS reply call fails */
	if (rv == (void *) NULL)
	{
		clnt_perror(clnt, "call failed");
    }
    /* If recieved reply is successful, but empty */
	else if ((char *) rv == "")
	{
		printf("rv is empty\n");
    }
    /* If recieved reply is successful */
	else
	{
		printf("Result = %s\n", (char *)rv);
    }

    /* Destroy client */
	printf("destroying client\n");
	clnt_destroy(clnt);
	printf("returning to main\n");
}

/* Creates a single client and sends an fep_reply to the ATMS */
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;
}
