/* C1.C // Client */ #include "mysocket.h" void main () { int menu_flag; int Xmit; int i; int div; int remain; char ch; int sd; int SD; int Pid; struct sockaddr sockname; struct MyMessage MSG_IN; struct MyMessage MSG_OUT; int Debug; int MSize; /* DEFAULTS */ MSize = sizeof( MSG_IN ); menu_flag = 1; Debug = 0; Pid = getpid(); MSG_OUT.Pid = Pid; MSG_OUT.Money = 3.0; /* INTEGER PID TO ASCII */ strcpy( MSG_OUT.fName, "c" ); remain = Pid; div = 10000; while( div >= 1 ) { ch = (char) ((remain / div) + 48); strcat( MSG_OUT.fName, &ch ); remain = remain % div; div = div / 10; } printf( "\n\t CONTACTING SERVER \n\n" ); /* SOCKET SB0000 */ SD = socket( AF_UNIX, SOCK_STREAM, 0 ); sockname.sa_family=AF_UNIX; strcpy( sockname.sa_data, "sb0000" ); if( connect( SD, &sockname, SIZEOFSOCKNAME ) == -1 ) { perror( "Bad Connection in Contacting Server ! \n\n" ); exit( 99 ); } /* REQUEST A CHANNEL */ MSG_OUT.Msg1 = IDM_OPEN; strcpy( MSG_OUT.Text, "Please Open" ); write( SD, &MSG_OUT, MSize ); while( ( i = read( SD, &MSG_IN, MSize ) ) > 0 ) { printf( "\t Client Received Text ==>%s<== \n", MSG_IN.Text ); printf( "\t\t From Server Pid [%d] It was %d bytes in size. \n", MSG_IN.Pid, i ); printf( "\n\t Please Wait ... \r" ); } printf(" \r"); if( MSG_IN.Msg1 == IDM_OK && strcmp(MSG_OUT.fName,MSG_IN.fName) == 0 ) printf( "\t Connect \n\t Msg1 = %d \n\t Msg2 = %d \n", MSG_IN.Msg1, MSG_IN.Msg2 ); else { printf( "\t Can't Connect \n\t Msg1 = %d \n\t Msg2 = %d \n", MSG_IN.Msg1, MSG_IN.Msg2 ); exit( 98 ); } printf( " \r\n\t Press ENTER to Continue. \n" ); system( "read x" ); /* START THE LOOP */ do { system( "clear" ); printf( "\t Vending Machine Client (v.1a) Pid = %d Channel = %s \n\n", Pid, MSG_OUT.fName ); printf( "\t\t USER COMMANDS\t\t SYSTEM COMMANDS \n\n"); printf( "\t\t a) Information. \t\t e) Send Wait Cmd. \n" ); printf( "\t\t b) Buy a Beer. \t\t f) ShutDown Server & Exit. \n" ); printf( "\t\t c) Buy Candy. \t\t g) Open Channel to Server. \n" ); printf( "\t\t d) Buy a Pop. \t\t h) Close Channel to Server. \n" ); printf( "\t\t q) Quit. \t\t i) Debugger " ); if( Debug ) printf( "OFF \n" ); else printf( "ON \n"); printf( "\n Enter Cmd ==>" ); fflush( stdin ); ch = getchar(); switch( ch ) { /* ASK FOR INFORMATION */ case 'a': MSG_OUT.Msg1 = IDM_INFO; strcpy( MSG_OUT.Text, "Information Request" ); Xmit = 1; break; /* BUY A BEER */ case 'b': MSG_OUT.Msg1 = IDM_BUY; MSG_OUT.Msg2 = BEER; strcpy( MSG_OUT.Text, "Buy Beer" ); Xmit = 1; break; /* BUY CANDY */ case 'c': MSG_OUT.Msg1 = IDM_BUY; MSG_OUT.Msg2 = CANDY; strcpy( MSG_OUT.Text, "Buy Candy Bar" ); Xmit = 1; break; /* BUY A POP */ case 'd': MSG_OUT.Msg1 = IDM_BUY; MSG_OUT.Msg2 = POP; strcpy( MSG_OUT.Text, "Buy Pop" ); Xmit = 1; break; /* ASK SERVER TO SLEEP( 10 ) */ case 'e': MSG_OUT.Msg1 = IDM_WAIT; strcpy( MSG_OUT.Text, "Please Wait" ); Xmit = 1; break; /* ASK SERVER TO SHUTDOWN ITSELF AND ALL CHANNELS */ case 'f': MSG_OUT.Msg1 = IDM_SHUTDOWN; strcpy( MSG_OUT.Text, "ShutDown" ); Xmit = 1; menu_flag = 0; break; /* ASK SERVER TO OPEN A CHANNEL */ case 'g': MSG_OUT.Msg1 = IDM_OPEN; SD = socket( AF_UNIX, SOCK_STREAM, 0 ); sockname.sa_family=AF_UNIX; strcpy( sockname.sa_data, "sb0000" ); if( connect( SD, &sockname, SIZEOFSOCKNAME ) == -1 ) { perror( "\n Bad Connection in Client OPEN ! \n" ); exit( 99 ); } strcpy( MSG_OUT.Text, "Please Open Again" ); write( SD, &MSG_OUT, MSize ); printf( "\n\t Please Wait . \r" ); while( ( i = read( SD, &MSG_IN, MSize ) ) > 0 ) { printf( "\r\t Client Received Text ==>%s<== \n", MSG_IN.Text ); printf( "\t\t From Server Pid [%d] Msg1 = %d Msg2 = %d \n", MSG_IN.Pid, MSG_IN.Msg1, MSG_IN.Msg2 ); printf( "\n\t Please Wait .... \r" ); } Xmit = 0; break; /* ASK SERVER TO CLOSE CURRENT CHANNEL */ case 'h': MSG_OUT.Msg1 = IDM_CLOSE; strcpy( MSG_OUT.Text, "Close Channel" ); Xmit = 1; break; /* TOGGLE DEGUGGER FLAF */ case 'i': if( Debug ) Debug = 0; else Debug = 1; printf("\r \n"); Xmit = 0; break; /* QUIT THE CLIENT, ASK THE SERVER TO CLOSE THE CURRENT CHANNEL */ case 'q': MSG_OUT.Msg1 = IDM_CLOSE; strcpy( MSG_OUT.Text, "Close Channel" ); menu_flag = 0; Xmit = 1; printf( "\n" ); break; /* INVALID LETTER ENTERED FROM MENU SELTION */ default: printf( "\n\t\t Valid lower case letters only ! \n\n " ); Xmit = 0; break; } /* switch( ch ) */ if( Xmit ) { /* IF WE SHOULD TRANSMIT */ sd = socket( AF_UNIX, SOCK_STREAM, 0 ); sockname.sa_family=AF_UNIX; strcpy( sockname.sa_data, MSG_OUT.fName ); if( connect( sd, &sockname, SIZEOFSOCKNAME ) == -1 ) { perror( "\n Bad Connection in Client LOOP !! \n" ); exit( 97 ); } write( sd, &MSG_OUT, MSize ); printf( "\n\t Please Wait \r" ); while( ( i = read( sd, &MSG_IN, MSize ) ) > 0 ) { /* GET THE REPLY FROM THE SERVER */ if( Debug ) { printf( "\r\t Client Received Text ==>%s<== \n", MSG_IN.Text ); printf( "\t\t From Server Pid [%d] Msg1 = %d Msg2 = %d \n", MSG_IN.Pid, MSG_IN.Msg1, MSG_IN.Msg2 ); printf( "\n\t Press ENTER to Continue. \n" ); system( "read x" ); } switch( MSG_IN.Msg1 ) { case IDM_INFO: system( "clear" ); printf( "\n\t Vending Machine Client by Louis Guzik \n" ); printf( "\n\t\t Money Remaining = $%2.2f \n", MSG_OUT.Money ); printf( "\n\t\t $%1.2f Beers = %d \n" , MSG_IN.CostBeer, MSG_IN.Beers ); printf( "\n\t\t $%1.2f Candy Bars = %d \n" , MSG_IN.CostCandy, MSG_IN.Candybars ); printf( "\n\t\t $%1.2f Pops = %d \n" , MSG_IN.CostPop, MSG_IN.Pops ); break; case IDM_SALE: system( "clear" ); MSG_OUT.Money = MSG_IN.Money; printf( "\n\t\t %s \n", MSG_IN.Text ); printf( "\n\t\t Money Remaining = $%2.2f \n", MSG_OUT.Money ); break; case IDM_NOSALE: system( "clear" ); printf( "\n\t\t Money Remaining = $%2.2f \n", MSG_OUT.Money ); printf( "\n\t\t %s \n", MSG_IN.Text ); break; default: printf( "\r\t Client Received Text ==>%s<== \n", MSG_IN.Text ); printf( "\t\t From Server Pid [%d] Msg1 = %d Msg2 = %d \n", MSG_IN.Pid, MSG_IN.Msg1, MSG_IN.Msg2 ); break; } /* switch( MSG_IN.Msg1 ) */ printf( "\n\t Please Wait ... \r" ); } /* while( read( MSG_IN ) ) */ } /* if( Xmit ) */ printf( "\t Press ENTER to Continue. \n" ); system( "read x" ); } while( menu_flag ); close( sd ); printf( "\t Client End. \n\n" ); } /* main */