
/*
 * common.h - Common definitions used by client.c, server.c, RPCdg.c & RPCst.c.
 *
 * CSc 645 Spring 1995  Jim Warhol
 *
 * common.h is a header file used to define various compile-time constants,
 * function prototypes and data definitions used by the common.c shared
 * common library routines, the client.c/server.c network programs, the
 * SMlib.c shared memory library routines, by the remote procedure call 
 * library routines RPCdg.c/RPCst.c, and by the startup.c startup program.
 *
 * Most values defined herein are used in two or more of the above modules.
 * 
 */

/* general compile-time constants */

#define TRUE		1
#define FALSE		0

#define RPC_ID		"jrw"		/* RPC msg header validation value */

#define	MAXCLIENTS	5		/* max number of simultaneous clients */
#define MAXDELAY	4		/* max number of seconds for client to 
					   delay before issuing SM request */

#define	RPCMSGLEN	256		/* maximum RPC buffer size */
#define RPCDATALEN	RPCMSGLEN-sizeof(struct RPCheader) /* max data length */

/* shared memory library function code definitions */

#define SM_fn_codes	10		/* start of SM function codes */
#define SM_assign_ID	11		/* request new client ID from server */
#define SM_Tread	12		/* shared memory table read function */
#define SM_Twrite	13		/* shared memory table write function */
#define SM_unassign_ID	14		/* unassign client ID */

/* shared memory library SM-level function return status codes */

#define SM_codes	100		/* start of SM return status codes */
#define SM_request_ok	101		/* SM request processed successfully */
#define SM_bad_request	102		/* unknown SM request from client */
#define SM_no_ID_avail	103		/* no client ID available to assign */
#define SM_bad_ID	104		/* bad operation client ID specified 
					   on client SM request */

/* shared memory library Tread/Twrite user-level function error status codes */

#define SM_TR_codes	200		/* start of user-level error codes */
#define SM_inactive_ID	201		/* client ID specified not active */
#define SM_no_data	202		/* client ID specified has not written
					   data yet */

/* common function prototypes */

void error(char errmsg[]);		/* print error message & abort */
void errorn(char errmsg[], int n);	/* print message w/integer & abort */
void errornn(char errmsg[], int n1, int n2); /* print msg w/2 ints & abort */
void errornnn(char errmsg[], int n1, int n2, int n3); /* msg w/3 ints & abort */
void print(char msg[]);			/* write line to standard output */
void printn(char msg[], int n);	/* write line with integer to standard output */
void printnn(char msg[], int n1, int n2); /* write line with 2 integers */
void printnnn(char msg[], int n1, int n2, int n3); /* write line with 3 ints */
void printns(char msg[], int n, char string[]); /* prt line w/int. & string */
void prints(char msg[], char string[]); /* write line w/string to std. out */
void printsn(char msg[], char string[], int n); /* prt line w/string & int. */
void printsnns(char msg[], char string1[], int n1, int n2, char string2[]);
					/* line w/string, 2 ints & string */

/* SMlib (Shared Memory library) client-callable function prototypes */

int SM_init(char *hostname, int portnum); /* initialize SM client processing */
void SM_term();				/* terminate SM client processing */

int Tread(int sourceID, int *value1, int *value2); /* shared mem. table read */
void Twrite(int value1, int value2);	/* shared memory table write */

/* SMlib (Shared Memory library) server-callable function prototypes */

void SM_server(void (*TRfuncp)(int sourceID, int *value1, int *value2), 
               void (*TWfuncp)(int sourceID, int value1, int value2));
					/* initialize & execute SM server;
					   specify Tread/Twrite processors */

/* RPC (Remote Procedure Call) library function prototypes */

int RPC_client_init(char *machine, int port); /* init. client communication */
void RPC_client_term();			/* terminate client communication */
char *RPC_send(char *message);		/* send message to server */
int RPC_server_init(int (*funcp)(char *clientmsg, char **response));
					/* initialize & execute server */

/* common structure definitions */

/* SMmessage defines the structure of a SM-level message packet */

struct SMmessage {
   struct SMheader {			/* header portion of SM message */
      int originating_clientID;		/* ID of originating client */
      int operation;			/* operation: 1 = Tread, 2 = Twrite */
      int operationID;			/* destination ID for Tread/Twrite
					   (must equal clientID for Twrite) */
      int status_from_server;		/* return status from server */
   } hdr;
   struct SMdata {			/* data portion of SM message */
      int value1;			/* first value to be written */
      int value2;			/* second value to be written */
   } data;
};

/* RPCmessage defines the structure of an RPC-level message packet */

struct RPCmessage {
   struct RPCheader {
      char ID[sizeof(RPC_ID)];		/* RPC validation ID */
   } header;
   char data[RPCDATALEN];		/* data portion of RPC message */
};

/* common data definitions */

int st;					/* return status */
int i;					/* scratch variable */

