Пример #1
0
int print_method() {
  char buffer[BUFFER_SIZE];
  while(1) {
    fgets(buffer, BUFFER_SIZE-1, stdin);
    if(!strcmp(buffer, EXIT))
      finish(0);
    printf(BAR);
    print_server_list();
    printf(BAR);
    printf("My Queue ");
    print_job_queue(my_queue);
    printf(BAR);
    printf("Backup Queue ");
    print_job_queue(backup_queue);
    printf(BAR);
  }
}
Пример #2
0
int main(int argc, char **argv)
{
    char buffer[NET_BUF_LEN];  // for command-line input
    int servers_found = 0;
    int server_number = -1;
    Uint32 timer = 0;

    //Scan local network to find running server:
    servers_found = LAN_DetectServers();

    if(servers_found < 1)
    {
        fprintf(stderr, "No server could be found - exiting.\n");
        exit(EXIT_FAILURE);
    }
    else if(servers_found  == 1)  //One server - connect without player intervention
    {
        fprintf(stderr, "Single server found - connecting automatically...");

        if(!LAN_AutoSetup(0))  //i.e.first (and only) entry in list
        {
            fprintf(stderr, "setup_client() failed - exiting.\n");
            exit(EXIT_FAILURE);
        }

        fprintf(stderr, "connected\n");
    } 


    else  // More than one server - will have to get player selection:
    {
        while(server_number < 0 || server_number >= servers_found)
        {
            fprintf(stderr, "The following TuxMath servers were detected:\n");
            print_server_list();
            fprintf(stderr, "Enter the SERVER NUMBER you would like to connect to:\n");
            scanf("%d", &server_number);
            if(server_number < 0 || server_number >= servers_found)
                fprintf(stderr, "Illegal value - try again.\n");
        }
        if(!LAN_AutoSetup(server_number))  //i.e.first (and only) entry in list
        {
            fprintf(stderr, "setup_client() failed - exiting.\n");
            exit(EXIT_FAILURE);
        }

        fprintf(stderr, "connected\n");
    }


    /* Now we are connected - get nickname from player: */
    {
        char name[NAME_SIZE];
        char* p;

        fprintf(stderr, "Please enter your name:\n>\n");
        fgets(buffer, NAME_SIZE, stdin);
        p = strchr(buffer, '\n');  //get rid of newline character
        if(p)
            *p = '\0';
        strncpy(name, buffer, NAME_SIZE);
        /* If no nickname received, use default: */
        if(strlen(name) == 1)
            strcpy(name, "Anonymous Coward");

        snprintf(buffer, NET_BUF_LEN, "%s", name);
        LAN_SetName(name);
    }

    fprintf(stderr, "Welcome to the Tux Math Test Client!\n");
    fprintf(stderr, "Type:\n"
            "'game' to start math game;\n"
            "'exit' to end client leaving server running;\n"
            "'quit' to end both client and server\n>\n"); 


    /* Set stdin to be non-blocking: */
    fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);


    quit = 0;
    while(!quit)
    { 
        // See if we have any messages from server:
        game_check_msgs();

        //Get user input from command line and send it to server: 
        /*now display the options*/
        if(read_stdin_nonblock(buffer, NET_BUF_LEN))
        { 
            //Figure out if we are trying to quit:
            if( (strncmp(buffer, "exit", 4) == 0)
                    ||(strncmp(buffer, "quit", 4) == 0))

            {
                quit = 1;
            }
            else if (strncmp(buffer, "game", 4) == 0)
            {
                // Begin the actual math game
                playgame();
                fprintf(stderr, "Math game finished.\n\n");
                fprintf(stderr, "Type:\n"
                        "'game' to start math game;\n"
                        "'exit' to end client leaving server running;\n"
                        "'quit' to end both client and server\n>\n"); 
            }
            else
            {
                fprintf(stderr, "Command not recognized. Type:\n"
                        "'game' to start math game;\n"
                        "'exit' to end client leaving server running;\n"
                        "'quit' to end both client and server\n\n>\n");
            }
        }
        //Limit loop to once per 10 msec so we don't eat all CPU
        Throttle(10, &timer);
    }

    LAN_Cleanup();

    return EXIT_SUCCESS;
}