
/* 
 * client - send & receive messages to/from server.
 *
 * Programming Assignment #7.
 *
 * CSc 645  Spring 1995  Jim Warhol
 *
 * client sends a series of messages to a server whose remote host name and
 * port number are specified by the user. Upon receipt of each message the
 * server will process the command request (if recognizable), or echo the
 * request back to the client process. The server presently recognizes only
 * the allocate and terminate commands. Allocate simulates the allocation
 * of a resource managed by the server; terminate causes the server to cease
 * waiting for client requests and terminate execution.
 *
 * The client and server may use either datagram or a reliable stream
 * connection messaging method, depending on which remote procedure call 
 * library routines are used to compile the programs.
 *
 * Compile with: cc -o Apps/client client.c RPCdg.c common.c
 *    or
 *               cc -o Apps/client client.c RPCst.c common.c
 *
 * Execute with: client hostname portnum
 *    or
 *               client hostname portnum terminate
 *
 * Where hostname is the name of the remote host on which the server is 
 * executing and portnum is the port number of the server on that host.
 *
 * If the first command form is used, the client process will send three
 * test messages to the server, and print each response as it is received.
 * The first two messages are simply echoed by the server; the third is
 * an allocate command, which will cause the server to return a different
 * response message to the client.
 *
 * If the second command form is used (with the terminate option), the
 * client process sends only the terminate message/command to the server,
 * requesting that it shut down processing.
 *
 * Related components: server.c, common.h, common.c, RPGdg.c, RPGst.c.
 *
 */

#include "common.h"

/* client function prototypes */

int initialize(int argc, char *argv[], char **host, int *port);

/* main program */

void main(int argc, char *argv[])
{

   char *hostname;			/* pointer to host name */
   int portnum;				/* port number for server */
   char *response;			/* pointer to response from server */
   int terminate;			/* TRUE if terminate parameter spec. */

   /* initialize client process - process command parameters */

   terminate = initialize(argc, argv, &hostname, &portnum);
   printn("client: Initiating communication with port #%d ", portnum);
   prints("on host %s.\n", hostname);

   /* create connection to server */

   st = RPC_client_init(hostname, portnum);	/* init. client communication */
   if (st < 0) error("client: Error initiating client-server communication.");

   if (!terminate) {

      /* send three test messages to the server and print responses */

      response = RPC_send("This is a test message"); /* send test message */
      prints("client: Message from server:  %s\n", response);

      response = RPC_send("My name is Jim Warhol"); /* send test message */
      prints("client: Message from server:  %s\n", response);

      response = RPC_send("allocate");	/* request resource allocation */
      prints("client: Message from server:  %s\n", response);

   }
   else {

      /* send termination command to server */

      response = RPC_send("terminate");	/* request server termination */
      prints("client: Message from server:  %s\n", response);

   }

   /* terminate client communication and end program */

   RPC_client_term();
   exit(0);

}

/*
 * int initialize(int argc, char *argv[], char **host, int *port);
 *
 * initialize processes the client command parameters. The first parameter
 * indicates the name of the remote host on which the server resides; the
 * the second parameter indicates the port number of the server on that host.
 * The third parameter, if specified, must be "terminate", which indicates
 * that the client is to send a termination request to the server.
 *
 * initialize returns TRUE if the third command parameter is "terminate",
 * otherwise FALSE.
 *
 */

int initialize(int argc, char *argv[], char **host, int *port)
{

   int terminate;			/* TRUE if terminate specified */

   /* make sure correct parameters are specified */

   terminate = (argc == 4 && strncmp(argv[3], "terminate", 10) == 0);

   if (!(argc == 3 || terminate))
      error("client: You must specify either:\n"
         "   client hostname port#  or  client hostname port# terminate");

   /* save address of host name string */

   *host = argv[1];

   /* get port number parameter */

   if (sscanf(argv[2], "%d", port) != 1)
      error("client: Error in port number parameter.");
   return terminate;

}

