Ejemplo n.º 1
0
void clientHandler(int clientSocket)
{
    // connection error
    if(clientSocket < 0){
        printf("%s", "Error connecting to clientSocket.\n");
        exit(EXIT_FAILURE);
    }

    int in_session = -1;

    // read from client
    char buffer[256]; // buffer for socket messages
    while(1){
        bzero((void*) buffer, 256);
        if((read(clientSocket, buffer, 255)) <= 0){ // no response
            exit(EXIT_SUCCESS);
        }

        // parse command
        char *command = strtok(buffer, " \n");
        char *commandArg = strtok(NULL, " \n");

        if(strcmp(command, "open") == 0){ // open account
           openAccount(clientSocket, commandArg, &in_session);
        }
        else if(strcmp(command, "start") == 0){ // start account session
            startAccount(clientSocket, commandArg, &in_session);
        }
        else if(strcmp(command, "credit") == 0){ // credit account
            creditAccount(clientSocket, commandArg, &in_session);
        }
        else if(strcmp(command, "debit") == 0){ // debit account
            debitAccount(clientSocket, commandArg, &in_session);
        }
        else if(strcmp(command, "balance") == 0){ // get balance
            getBalance(clientSocket, &in_session);
        }
        else if(strcmp(command, "finish") == 0){ // finish
            finishSession(clientSocket, &in_session);
        }
        else{
            printf("%s", "Client done goofed, abort mission.");
            exit(EXIT_FAILURE);
        }
    }

    return;
}
Ejemplo n.º 2
0
/**
* Processes the input from the client, given the command, argument, and state
* of the client. 
*
* The 'state' is simply whether or not the client is in a session.
* -1: Client wishes to exit
*  0: Client is connected, but is not in an account session
*  1: Client is connected and is in an account session
*
*/
int process_input(char *buffer, int state, client_t **client){
	
	int i;
	char *parsed = NULL;
	int len = strlen(buffer);
	for(i = 0; i < len; i++){
		if(buffer[i] == '\n'){
			if(i == 0){
				/*If it's just a newline*/
				break;
			}
			parsed = malloc(sizeof(char)*(i+1));
			strncpy(parsed, buffer, i);
			parsed[i] = '\0';
			break;
		}
	}
	if(parsed == NULL){
		printf("Error parsing buffer.\n");
		return state;
	}

	printf("CLIENT> %s\n", parsed);
	

	Node **list = &((*client)->head);

	char *command, *arg;
	char name[MAX_ARG_LEN];
	command = strtok(parsed, " ");
	arg = strtok(NULL, " ");
	memset(name, '\0', sizeof(name));
	if(arg != NULL){
		strcpy(name, arg);
	}


	if(strcmp(parsed, "exit") == 0){
		printf("Client exits!\n");
		if(state == 1){
			printf("Finishing customer session.\n");
			Node *node = findAccount((*client)->current_acc, *list);
			Account *acc = node->account;
			finishAccount(&acc, state);
			free((*client)->current_acc);
		}
		update_client_count(-1);
		free(parsed);
		return -1;
	}else if(strcmp(command, "open") == 0){
		if(state == 1){
			printf("Still in customer session!\n");
			free(parsed);
			return 1;
		}
		int open = openAccount(list, name);
		char *msg = "Account opened for ";
		printf("%s%s.\n", msg, name);
		if(open == -1){
			printf("Error opening account!\n");
			free(parsed);
			return 0;
		}
		free(parsed);
		return 0;
	}else if(strcmp(command, "start") == 0){
		if(state == 0){
			Node *ptr = findAccount(name, *list);
			if(ptr == NULL){
				free(parsed);
				return 0;
			}
			Account *acc = ptr->account;
			int n = startAccount(&acc, list);
			if(n == 0){
				printf("Account is still in use.\n");
				free(parsed);
				return 0;
			}
			char *current = malloc(strlen(name)*sizeof(char));
			strcpy(current, name);
			(*client)->current_acc = current;
			free(parsed);
			return 1;
		}else{
			printf("Still in session!\n");
			free(parsed);
			return 1;
		}
	}else if(strcmp(command, "credit") == 0){
		if(state == 1){
			float new = updateAccount((*client)->current_acc, list, atof(arg));
			printf("Balance of %s is now %.2f.\n", (*client)->current_acc, new);
			free(parsed);
			return 1;
		}else{
Ejemplo n.º 3
0
void *new_session(void *arg) {
    
    connection_t * conn;
    char acctName[100];
    char buffer[256];
    char *buf;
    long n;
    int rc;
    //long addr = 0;
    
    if (!arg)
        pthread_exit(0);
    conn = (connection_t *)arg;
    
    
    while(1){
        int x, index;
        double amount;
        double balance;
        bzero(buffer,256);
        bzero(acctName,100);
        n = read(conn->sock,buffer,255);
        if (n < 0){
            printf("Error reading from socket\n");
            pthread_exit(NULL);
        }
        
        if(strncasecmp(buffer, "Exit", 4) == 0){
            
            if(conn->session > -1)
                bank->accounts[conn->session].session = -1;
            
            conn->session = -1;
            
            printf("Client %d exited\n", conn->sock);
            n = write(conn->sock, "Exiting...", 10);
            close(conn->sock);
            free(conn);
            pthread_exit(NULL);
        }
        if(strncasecmp(buffer, "Open", 4) == 0){
            //n = write(conn->sock,"Open",4);
            
            
            buf = buffer + 4;
            
            sscanf(buf, "%s", acctName);
            
            if(addAccount(acctName) == 0){
                printf("Client %d added account: %s\n", conn->sock, acctName);
                n = write(conn->sock,"Added account\n", 30);
            }else{
                printf("Client %d could not add account: %s. The account already exists or there is no space available.\n", conn->sock, acctName);
                n = write(conn->sock, "Error: could not add account. The account already exists or there is no space available.\n", 100);
            }
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
            
        } else if(strncasecmp(buffer, "Start", 5) == 0){
            //printf("Here is the command: %s\n",buffer);
            //n = write(conn->sock,"Start",5);
            
            if(conn->session != -1){
                printf("Client %d tried to start an account but client is already in session\n", conn->pid);
                n = write(conn->sock,"Client is already in session\n", 30);
                if (n < 0){
                    printf("Error writing to socket\n");
                    pthread_exit(NULL);
                }
                continue;
            }
            
            buf = buffer + 5;
            
            sscanf(buf, "%s", acctName);
            
            x = startAccount(acctName);
            
            if(x >= 0){
                printf("Client %d started account: %s\n", conn->pid, acctName);
                n = write(conn->sock,"Started account\n", 30);
                conn->session = x;
            }else if(x == -1){
                printf("Client %d could not start account: %s. Account does not exist.\n", conn->pid, acctName);
                n = write(conn->sock, "Error: could not start account. Account does not exist.\n", 100);
            }else{
                printf("Client %d could not start account: %s. Account is in session.\n", conn->pid, acctName);
                n = write(conn->sock, "Error: could not start account. Account is in session.\n", 100);
                
            }
            
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
        } else if(strncasecmp(buffer, "Credit", 6) == 0){
            //printf("Here is the command: %s\n",buffer);
            //n = write(conn->sock,"Credit",6);
            
            if(conn->session == -1){
                printf("Client %d tried to credit account, but client is not in session.\n", conn->sock);
                n = write(conn->sock,"Client is not in session.\n", 30);
                if (n < 0){
                    printf("Error writing to socket\n");
                    pthread_exit(NULL);
                }
                continue;
            }
            
            buf = buffer + 6;
            
            sscanf(buf, "%lf", &amount);
            
            creditAccount(conn->session, amount);
            
            printf("Client %d deposited $%.2f\n", conn->sock, amount);
            n = write(conn->sock, "Credited account.\n", 30);
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
        } else if(strncasecmp(buffer, "Debit", 5) == 0){
            //printf("Here is the command: %s\n",buffer);
            //n = write(conn->sock,"Debit",5);
            
            if(conn->session == -1){
                printf("Client %d tried to debit account, but client is not in session.\n", conn->sock);
                n = write(conn->sock,"Client is not in session.\n", 30);
                if (n < 0){
                    printf("Error writing to socket\n");
                    pthread_exit(NULL);
                }
                continue;
            }
            
            buf = buffer + 5;
            
            sscanf(buf, "%lf", &amount);
            
            x = debitAccount(conn->session, amount);
            
            if(x == 0){
                printf("Client %d withdrew $%.2f\n", conn->sock, amount);
                n = write(conn->sock, "Debited account.\n", 30);
            }else{
                printf("Client %d tried to debit account, but does not have enough funds.\n", conn->sock, amount);
                n = write(conn->sock, "Not enough funds.\n", 30);
            }
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
            
        } else if(strncasecmp(buffer, "Balance", 7) == 0){
            //printf("Here is the command: %s\n",buffer);
            //n = write(conn->sock,"Balance",7);
            
            if(conn->session == -1){
                printf("Client %d requested balance, but client is not in session.\n", conn->sock);
                n = write(conn->sock,"Client is not in session.\n", 30);
                if (n < 0){
                    printf("Error writing to socket\n");
                    pthread_exit(NULL);
                }
                continue;
            }
            
            buf = buffer;
            
            index = conn->session;
            
            //printf("%d\n", index);
            
            balance = bank->accounts[index].balance;
            
            printf("Client %d requested balance.\n", conn->sock);
            
            printf("Printed balance of %.2f\n", balance);
            
            sprintf(buf, "Balance for account %s is $%.2f\n", &(bank->accounts[conn->session].name), &balance);
            
            n = write(conn->sock, buf, 100);
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
        } else if(strncasecmp(buffer, "Finish", 6) == 0){
            //printf("Here is the command: %s\n",buffer);
            //n = write(conn->sock,"Finish",6);
            
            if(conn->session == -1){
                printf("Client %d tried to finish session, but client is not in session.\n", conn->sock);
                n = write(conn->sock,"Client is not in session.\n", 30);
                if (n < 0){
                    printf("Error writing to socket\n");
                    pthread_exit(NULL);
                }
                continue;
            }
            
            bank->accounts[conn->session].session = -1;
            
            conn->session = -1;
            
            
            printf("Client %d finished session.\n", conn->sock);
            n = write(conn->sock, "Finished session\n", 30);
            
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
        } else {
            printf("Invalid command\n");
            n = write(conn->sock,"Invalid commmand", 16);
            if (n < 0){
                printf("Error writing to socket\n");
                pthread_exit(NULL);
            }
        }
    }
}