/* * 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 tina.c cookie_clnt.c -o tina */ #include #include #include #include "cookie.h" #include "print.h" // getcookie // Performs the RPC getmemycookie as well as necessary error handlingv int getcookie(CLIENT *c) { int arg = 0, *res; res = getmemycookie_1(&arg, c); if (res == (int *)NULL) { clnt_perror(c, "Tina error: "); fprintf(stderr, "Terminating Tina.\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 = 0; struct timeval tv; gettimeofday(&tv, 0); srand(tv.tv_usec); // Check if arguments are correct if (argc < 2) { fprintf(stderr, "Usage: tina 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("Tina is requesting a cookie..."); ret = getcookie(clnt); if (ret == -2) { // No cookies left print("Tina was unable to get a cookie: No cookies left."); print("Terminating Tina."); fflush(stdout); break; } else if (ret == 1) { // Successfully got a cookie print("Tina got a cookie."); } else { // Unknown result fprintf(stderr, "Tina: UNKNOWN ERROR, terminating...\n"); break; } } // Tell mother we're dying and terminate terminate_1(&arg, clnt); clnt_destroy(clnt); return 0; }