/* 
 * server - receive and process client messages.
 *
 * Programming Assignment #7.
 *
 * CSc 645  Spring 1995  Jim Warhol
 *
 * server is the companion program to client. Together the two programs
 * provide a client/server mechanism for processing messages. The client
 * process(es) send messages to the server, which processes specific 
 * commands (if recognized), or simply echoes the messages back to the 
 * client (if the messages do not contain recognizable instructions). 
 * The server loops waiting for client messages until a terminate server 
 * command is received from any client process.
 *
 * The server presently recognizes two commands: allocate and terminate.
 * The allocate command simulates allocation of a server-controlled resource
 * (such as a data file, sequencing control information, etc.); it returns
 * a message indicating that the resource has been allocated. The terminate
 * command causes the server to stop waiting for client messages and exits.
 *
 * The server can be compiled using either the datagram or the connection-
 * oriented stream message service remote procedure call library routines.
 *
 * Compile with: cc -o Apps/server server.c RPCdg.c common.c
 *    or
 *               cc -o Apps/server server.c RPCst.c common.c
 *
 * Execute with: server &  (runs in background)
 *
 * The user must note the local port number printed at server startup 
 * and se this port number on all client calls so the client processes 
 * can locate the server.
 *
 * Related components: client.c, common.h, common.c, RPCdg.c, RPCst.c.
 *
 */

#include "common.h"

/* main program */

void main()
{

   /* initiate remote procedure call server library routine */

   st = RPC_server_init(myserver);	/* initialize and execute server */
   if (st < 0) error("server: Error initializing/executing server.");

   /* terminate server */

   print("server: Terminating server.\n");
   exit(0);				/* terminate program */

}

/*
 * int myserver(char *clientmsg, char **response);
 *
 * myserver performs client service processing upon receipt of a request
 * from a client. RPC_server_init() calls myserver with the first parameter
 * containing a pointer to the character string received from the client
 * process. myserver returns a response string based upon the client message.
 *
 * myserver recognizes two valid commands: allocate and terminate. The 
 * allocate command returns the reply shown in allocatemsg below. The
 * terminate command returns the terminatemsg reply below. All other client
 * messages are echoed back to the client.
 *
 * myserver returns TRUE if the server has been requested to terminate,
 * otherwise FALSE.
 *
 */

int myserver(char *clientmsg, char **response)
{

   static char allocatemsg[] = "Allocating (simulated) resources to client.";
   static char terminatemsg[] = "Terminating server.";

   /* echo message from client to server */

   prints("server: Message from client:  %s\n", clientmsg);

   /* check for known client request */

   if (strncmp(clientmsg, "terminate", 10) == 0) {
      print("server: client has requested server termination.\n");
      *response = terminatemsg;	/* reply with termination message */
   }
   else if (strncmp(clientmsg, "allocate", 9) == 0) {
      *response = allocatemsg;	/* reply with allocation processed message */
   }
   else {	/* unknown request from client */
      *response = clientmsg;	/* echo client message */
   }

   /* return response status of TRUE if server termination requested */

   return (*response == terminatemsg ? TRUE : FALSE);

}

