/* * Kyle Box * EECS 338 Spring 2007 * Assignment 7: RPC Programming * * Written and compiled on Debian Linux 2.4.29 * Compiled on gcc version 3.3.5 with: * gcc -Wall judy.c cookie_clnt.c -o judy */ #include #include #include #include "cookie.h" #include "print.h" // getcookie // Performs the RPC getmemycookie as well as necessary error handling int getcookie(CLIENT *c) { int arg = 1, *res; res = getmemycookie_1(&arg, c); if (res == (int *)NULL) { clnt_perror(c, "Judy error: "); fprintf(stderr, "Terminating Judy.\n"); terminate_1(&arg, c); clnt_destroy(c); exit(1); } return *res; } int main(int argc, char *argv[]) { char *host; CLIENT *clnt; int ret, arg = 1; struct timeval tv; gettimeofday(&tv, 0); srand(tv.tv_usec); // Check if arguments are correct if (argc < 2) { fprintf(stderr, "Usage: judy server_host\n"); exit(1); } // Set up the client host = argv[1]; clnt = clnt_create(host, COOKIEPROG, COOKIEVERS, "udp"); if (clnt == NULL) { clnt_pcreateerror(host); exit(1); } // Main loop while (1) { // delay usleep(rand() % 1000000); print("Judy is requesting a cookie..."); ret = getcookie(clnt); if (ret == -2) { // No cookies left print("Judy was unable to get a cookie: No cookies left."); print("Terminating Judy."); fflush(stdout); break; } else if (ret == -1) { // Tina has not received two cookies print("Judy was unable to get a cookie: Tina has not received two cookies."); } else if (ret == 1) { // Successfully got a cookie print("Judy got a cookie."); } else { // Unknown result fprintf(stderr, "Judy: UNKNOWN ERROR, terminating...\n"); break; } } // Tell mother we're dying and terminate terminate_1(&arg, clnt); clnt_destroy(clnt); return 0; }