Exemplo n.º 1
0
int main ()
{
   
   // testing for all possible good and bad data

   // testing with good data 
   bank_account* b = create_account("Ploni Amoni", 250000, "0123456789876");

/* ALL OF THESE WITH BAD DATA "WORK" BY DISPLAYING GUIDING MESSAGES TO THE USER WITHOUT BOMBING OUT THE SYSTEM */
      
   // testing with incorrect account name
   // Running caused Segmentation fault (core dumped)
   // bank_account* b = create_account("", 150000, "0123456789876");

   // testing with incorrect amount
   // error: causes a Segmentation fault (core dumped)
   // bank_account* b = create_account("Bais Hillel", -100, "0123456789876");

   // testing with incorrect account_num length
   // bank_account* b = create_account("Bais Hillel", 150000, "0123456789");
   
   // testing with incorrect account_num chars instead of ints
   //bank_account* b = create_account("Bais Hillel", 250000, "01A3B5C7D9E7F");


/* MUST HANDLE EACH OF THESE CASES THAT BOMBED OUT!!! */

   // testing with incorrect number of parameters
   // bank_account* b = create_account("Bais Hillel", 0123456789876);
   
   // error:  with "badamount": incompatible type for argument 2 of âcreate_accountâ
   // bank_account.h:20: note: expected âdoubleâ but argument is of type âchar *â

   // bank_account* b = create_account("Bais Hillel", "badamount", "0123456789876");

   // error with badamount:  âbadamountâ undeclared (first use in this function)

   //bank_account* b = create_account("Bais Hillel", badamount, "0123456789876");


   if(b == NULL)
   {
      printf("Invalid account");   
   }
   
   if(b->balance < 0)            
   {
      printf("Invalid account");
   }
     
   else
   {
      display_account(b);
      delete_account(b);
   }
   
   return 0;
}
Exemplo n.º 2
0
void* client_connect(void* info){
    thread_info* new_info = (thread_info* )info;
    int sockfd = new_info -> sockfd;
    std::string addr = new_info -> address;
    int port = new_info -> port;

    std::cout << sockfd << " " << addr << " " << port << '\n';
    /*int read_size;
    char client_message[2000];
    char welcome_message[200];
    strcpy(welcome_message, "Welcome to simple P2P server !\n");

    write(sockfd, welcome_message, strlen(welcome_message));
	//test block

	login::Login login;
	login.set_id("testID");
	login.set_passwd("testPasswd");
	int pkg_size = login.ByteSize() + 4;
	char* pkt = new char[pkg_size];
	google::protobuf::io::ArrayOutputStream aos(pkt, pkg_size);
	google::protobuf::io::CodedOutputStream* coded_output = new google::protobuf::io::CodedOutputStream(&aos);
	coded_output -> WriteVarint32(login.ByteSize());
	login.SerializeToCodedStream(coded_output);

	write(sockfd, pkt, pkg_size);*/
	//test block
    update();
    login::Login login;
    while( true ){
        int count;
        char buffer[4];
        count = recv(sockfd, buffer, 4, MSG_PEEK);
        if(count == -1)
            perror("Recv with error");
        else{
            ACTION request;
            request = readAction(sockfd, readHdr(buffer));
            if ( request == LOGIN ) {
                login = login_check(sockfd, addr, port);
            } else if ( request == REGIST ) {
                regist_check(sockfd);
            } else if ( request == DELETEACCOUNT ) {
                delete_account(sockfd);
            } else if ( request == SEARCHINFO ) {
                search_info(sockfd);
            } else if ( request == DOWNLOAD ) {
                download_p2p(sockfd);
            } else if ( request == CHAT ) {
                chat(sockfd);
            } else if (request == LOGOUT ) {
                logout(sockfd, file_sets);
                break;
            } else if (request == RECVFILEINFO ) {
                recv_file_info(sockfd, file_sets, login);
            } else if (request == ONLINEINFO){
                send_online_info(sockfd);
            } else if (request == PORTREQUEST) {
                //send_port(sockfd);
            } else {
                printf("%sBugssssssssssss%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET);
            }
        }
    }


    /*while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ){
		client_message[read_size] = '\0';
        write(sock , client_message , strlen(client_message));
		memset(client_message, 0, 2000);
    }

    if(read_size == 0) {
        puts("Client disconnected");
        fflush(stdout);
    }else if(read_size == -1){
        perror("recv failed");
    }*/
    //printf("Connection left\n");
    return 0;
}