/*************************************************************************** * file: client.c * * This file contains the client code for the room reservation application. ****************************************************************************/ #include #include #include #include "room_res.h" /* the header file created by the IDL compiler */ /************************************************************************** * function: main * returns: int * * This is the main function for the room reservation application. The * only thing it does is parse the command line and then call the * appropriate command. Everything else happens at the server end. **************************************************************************/ int main (int argc, char *argv[]) { int count = 1; char *char_ptr; char description[MAX_STRING]; char name[MAX_STRING]; int length = 1; long result; int okayToReserve = 1; int okayToCancel = 1; roomnum_t room_num = -1; date_t date = 0; restime_t time = 0; int listRooms = FALSE; int cancelRoom = FALSE; int num_people = 0; int i = 0; listOut_t outBuffer; /******************************** * Initialize the local storage * arrays and the server. *********************************/ InitServer(); /* This is a remote procedure call */ for (i = 0; i < 256; i++) { description[i] = '\0'; name[i] = '\0'; } /***************************** * Parse the command line ******************************/ char_ptr = argv[count]; if (argc > 1) { while (count < argc) { char_ptr++; switch (*char_ptr) { case 'r': count++; char_ptr = argv[count]; room_num = atoi (argv[count]); break; case 'd': count++; char_ptr = argv[count]; date = atoi (argv[count]); break; case 't': count++; char_ptr = argv[count]; time = atoi (argv[count]); break; case 'n': count++; char_ptr = argv[count]; num_people = atoi (argv[count]); break; case 'l': count++; char_ptr = argv[count]; length = atoi (argv[count]); break; case 'm': count++; strcpy (description, argv[count]); break; case 'w': count++; strcpy (name, argv[count]); break; case 'L': listRooms = TRUE; break; case 'C': cancelRoom = TRUE; break; } count++; char_ptr = argv[count]; } } char_ptr = argv[count]; /********************************************* * Check to make sure all necessary args * are present, then call requested function. **********************************************/ /********************** * Make a reservation ***********************/ if (!listRooms && !cancelRoom) { if (!date) { fprintf (stderr, "Error!! Date must be specified "); fprintf (stderr, "using -d flag\n"); fflush (stderr); okayToReserve = 0; } if (!time) { fprintf (stderr, "Error!! Time must be specified "); fprintf (stderr, "using -t flag\n"); fflush (stderr); okayToReserve = 0; } if (!num_people) { fprintf (stderr, "Error!! Number of people must be specified "); fprintf (stderr, "using -n flag\n"); fflush (stderr); okayToReserve = 0; } if (name[0] == '\0') { fprintf (stderr, "Error!! A name must be specified "); fprintf (stderr, "using -w flag\n"); fflush (stderr); okayToReserve = 0; } if (description[0] == '\0') { fprintf (stderr, "Error!! A description must be specified "); fprintf (stderr, "using -m flag\n"); fflush (stderr); okayToReserve = 0; } if (okayToReserve) /* do the remote procedure call */ { result = ReserveRoom (time, date, num_people, room_num, length, name, description, outBuffer); printf ("%s\n", outBuffer); } } /************************ * Cancel a reservation *************************/ if (cancelRoom) { if (room_num == (-1) ) { fprintf (stderr, "Error!! The room number to cancel must be "); fprintf (stderr, "specified using -r flag\n"); fflush (stderr); okayToCancel = 0; } if (!date) { fprintf (stderr, "Error!! Date must be specified "); fprintf (stderr, "using -d flag\n"); fflush (stderr); okayToCancel = 0; } if (!time) { fprintf (stderr, "Error!! Time must be specified "); fprintf (stderr, "using -t flag\n"); fflush (stderr); okayToCancel = 0; } if (okayToCancel) { result = Cancel (room_num, date, time, outBuffer); printf ("%s\n", outBuffer); } } /****************** * List the rooms *******************/ if (listRooms && (room_num > 0) && !date && !time) { ListReservationsByRoom (room_num, outBuffer); printf ("%s\n", outBuffer); } else if (listRooms) { ListReservations (date, room_num, outBuffer); printf ("%s\n", outBuffer); } /********************** * Clean up the Server ***********************/ ShutDown (); return 0; }