/************************************************************************* * Base worker routine * * Child process looks at req_buf to determine requested operation. * Executes assoicated account routine, sends response back to client. *************************************************************************/ void process_request(char *req_buf, int conn) { char resp_buf[COMM_BUF_SIZE]; int trans_id; /* The transaction type of the request */ sscanf(req_buf, "%d", &trans_id); DPRINTF(("SERVER: child process %d, processing request \n", child_pid)); /* "p()" num active semaphore */ num_active_sem_buf.sem_num = 0; num_active_sem_buf.sem_op = -1; /* "p()" */ num_active_sem_buf.sem_flg = SEM_UNDO; if (semop(num_active_sem_id, &num_active_sem_buf, 1) < 0) { perror("SERVER: child process, num_active p semop() failed %d\n"); exit (1); } switch(trans_id) { case OPEN_ACCT_TRANS: open_account(resp_buf); break; case DEPOSIT_TRANS: deposit(req_buf, resp_buf); break; case WITHDRAW_TRANS: withdraw(req_buf, resp_buf); break; case BALANCE_TRANS: balance(req_buf, resp_buf); break; default: handle_bad_trans_id(req_buf, resp_buf); break; } server_comm_send_response(conn, resp_buf); DPRINTF(("SERVER: child process %d, response sent\n", child_pid)); /* "v()" num active semaphore */ num_active_sem_buf.sem_num = 0; num_active_sem_buf.sem_op = 1; /* "v()" */ num_active_sem_buf.sem_flg = SEM_UNDO; if (semop(num_active_sem_id, &num_active_sem_buf, 1) < 0) { perror("SERVER child process, num_active v semop() failed %d\n"); exit (1); } exit(0); }
void* atm_start(void* atm_data) { ATM* My_ATM = (ATM*)atm_data; //Getting my current atm serial number FILE* command_file = fopen(My_ATM->file_name,"r"); if(command_file == NULL) { printf("NO FILE FOUND\n"); exit(1); } char* line = NULL; //A string that should hold our current line ssize_t read; //For the line read size_t len =0; //For the lmine read char* delim = (char*)malloc(sizeof(char)*3); //Delimeters strcpy(delim," \t\n\0"); char* args[5]; //pbank bank = bank_init(account_ARR); flag = false; while((read = getline(&line, &len, command_file)) != -1) //We got a new line { bool illegal_command = false; //printf("%s",line); //NEW ARGS args[0] = NULL; args[1] = NULL; args[2] = NULL; args[3] = NULL; args[4] = NULL; //NEW ARGS args[0] = strtok(line,delim); //Command /*printf("%s",line); args[0] = (char*)malloc(sizeof(*line)); strcpy(args[0],line);*/ if(args[0] == NULL) //Checking we actually got the argument { illegal_command = true; //printf("%d",read); printf("CANT READ THE COMMAND\n"); free(line); pthread_exit(NULL); } //printf("%s\n",args[0]); int args_num = 1; if(!(strcmp(args[0],"O") && strcmp(args[0],"D") && strcmp(args[0],"W") && strcmp(args[0],"B") && strcmp(args[0],"T" ))) { for(int j=1; (args[j] = strtok(NULL,delim))!=NULL; j++) { args_num++; if(args_num > 5) { illegal_command = true; break; } } /*Illegal command check*/ /*Illegal command check*/ /*Illegal command check*/ /*Illegal command check*/ //Time to decide which command this is if(!strcmp(args[0],"O")) { open_account(args[1],args[2],args[3],My_ATM->serial); } else if(!strcmp(args[0],"D")) { deposit(args[1],args[2],args[3],My_ATM->serial); } else if(!strcmp(args[0],"B")) { balance(args[1],args[2],My_ATM->serial); } else if(!strcmp(args[0],"W")) { withdraw(args[1],args[2],args[3],My_ATM->serial); } else if(!strcmp(args[0],"T")) { transfer(args[1],args[2],args[3],args[4],My_ATM->serial); } } } /*free(args[0]); free(args[1]); free(args[2]); free(args[3]); free(args[4]);*/ free(line); free(delim); fclose(command_file); flag=true; pthread_exit(NULL); }
extern int main(int argc, char **argv) { char req_buf[COMM_BUF_SIZE], resp_buf[COMM_BUF_SIZE]; /* The character string buffers passed from and to client programs. The strings follow a format defined in atm.h and the parameters are read in and out using sptring processing calls sprintf() and sscanf(). */ int conn; /* A handle on which connection (client) the request came from. Not used initially but included so that in the future different requests could be processed cuncurrently. */ int trans_id; /* The transaction type of the current request */ int done=0; DPRINTF(("SERVER: main() pid %d, ppid %d, pgrp %d\n",getpid(), getppid(), getpgrp())); atm_server_init(argc, argv); for(;;) { server_comm_get_request(&conn, req_buf); sscanf(req_buf, "%d", &trans_id); DPRINTF(("SERVER: main(), request received\n")); switch(trans_id) { case OPEN_ACCT_TRANS: open_account(resp_buf); break; case DEPOSIT_TRANS: deposit(req_buf, resp_buf); break; case WITHDRAW_TRANS: withdraw(req_buf, resp_buf); break; case BALANCE_TRANS: balance(req_buf, resp_buf); break; case SHUTDOWN: if (shutdown_req(req_buf, resp_buf)) done = 1; break; default: handle_bad_trans_id(req_buf, resp_buf); break; } server_comm_send_response(conn, resp_buf); DPRINTF(("SERVER: main(), response sent\n")); if(done) break; } DPRINTF(("SERVER: main(), shuting down\n")); server_comm_shutdown(); return 0; }