//OUTPUT FUNCTIONS //TODO: SCAN_CHAIN NO BORRA static void output_func_wait_for_user(fsm_t* fsm){ nevera_fsm_t* nevera = (nevera_fsm_t*) fsm; int id = 0; while(1){ display_user_menu(); id = scan_num(); nevera->user_selected = usuario_list_search(nevera->lista_usuarios,id); if(nevera->user_selected != NULL) break; display_user_error(); } nevera->option_selected = -1; nevera->intro = ""; display_user_selected(nevera->user_selected->nombre); }
// Reads input from the user and re-acts according int process_user_messages() { // Initialise local variables char temp = 0; char *temp_packet; static int choice = USERINDEXLOGIN; static char destination_ID = 0; static char *packet_string; static char packet_message[PACKETSTRINGMESSAGESIZE]; static int character_count = 0; // Depending on current "choice", process input in a certain way // (default choice is USERINDEXLOGIN) switch (choice){ // Request user login ID and process initial information case USERINDEXLOGIN: if (login_key) { return 0; } std::cout << "\rEnter Login Character: "; if (_kbhit()) { temp = (char)toupper(_getch()); } if (temp == KEYBOARDESCAPE) { return !(needs_restart = true); } else if (temp <= 'Z' && temp >= 'A') { std::cout << (login_key = temp) << "\n"; } else { return 0; } packet_string = new char[PACKETSTRINGSIZE]; sprintf_s(packet_string, PACKETSTRINGSIZE, "{%c%cL%10d }", login_key, login_key, timeGetTime()); login_timestamp = calculate_timestamp(packet_string); message_queue.push(packet_string); std::cout << "Logging In ID {" << login_key << "}\n"; wait_start = timeGetTime(); choice = USERINDEXPENDING; break; // Wait for official logout confirmation case USERINDEXLOGOUT: if (login_key == 0) { choice = USERINDEXLOGIN; } else if((timeGetTime() - wait_start) >= LOGINKEYTIMEOUT) { std::cout << "Failed To Log Out. Application Must Close\n"; application_alive = false; } break; // Wait for official login confirmation case USERINDEXPENDING: if (login_keystate[login_key - 'A'] == LOGINKEYLOGGEDIN) { std::cout << "Logged in ID {" << login_key << "} successfully\n"; display_user_menu(); choice = USERINDEXMENU; } else if (login_keystate[login_key - 'A'] == LOGINKEYINUSE) { std::cout << "\nUnable to log in ID {" << login_key << "}" << " - Key is in use\n\n"; choice = USERINDEXLOGIN; login_key = 0; } else if ((timeGetTime() - wait_start) >= LOGINKEYTIMEOUT) { std::cout << "Unable to log in ID {" << login_key << "}" << " - Check cable connection\n\n"; choice = USERINDEXLOGIN; login_key = 0; } break; // Request choices from the user to interact with the LAN Messenger // functionality case USERINDEXMENU: std::cout << "\rEnter Menu Selection: "; if (_kbhit()) { temp = (char)toupper(_getch()); } if (temp == 'M' || temp == 'T' || temp == 'A' || temp == 'D' || temp == 'C' || temp == 'X' || temp == 'B' || temp == KEYBOARDESCAPE) { std::cout << temp << "\n"; } else { return 0; } // Depending on current "choice", process input in a certain way switch (temp) { // Displays the Menu options list for the user. case 'M': display_user_menu(); break; // Sends a test packet case 'T': std::cout << "Sending Test Packet...\n"; temp_packet = new char[PACKETSTRINGSIZE]; sprintf_s(temp_packet, PACKETSTRINGSIZE, "{%c%cD%10s }", login_key, login_key, "TESTPACKET"); std::cout << temp_packet << "\n"; message_queue.push(temp_packet); break; // Displays all users currently logged into the LanComms system case 'A': std::cout << "Users Currently Logged In To LanComms:\n"; for (unsigned i = 0; i < LOGINKEYCOUNT; i++) { if (login_keystate[i]) { std::cout << "--- " << (char)('A' + i) << " ---\n"; } } std::cout << "\n"; break; // Begins Destination ID assignment process case 'D': choice = USERINDEXGETADDR; break; // Begins Communication with selected destination ID case 'C': if (destination_ID) { std::cout << "Beginning Communications With ID {" << destination_ID << "}\n\n" << "/--------~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--------\\" << "\n\n"; choice = USERINDEXINPUT; } else { std::cout << "Invalid Command - No Destination ID Set\n\n"; } break; // Begins logging out process case KEYBOARDESCAPE: case 'X': std::cout << "Logging Out ID {" << login_key << "}\n"; temp_packet = new char[PACKETSTRINGSIZE]; sprintf_s(temp_packet, PACKETSTRINGSIZE, "{%c%cX%10s }", login_key, login_key, ""); message_queue.push(temp_packet); wait_start = timeGetTime(); choice = USERINDEXLOGOUT; break; // Toggles "Debug Mode" On or Off case 'B': debug_toggle = !debug_toggle; std::cout << "Toggled Debug Mode to {" << (debug_toggle ? "ON" : "OFF") << "}\n"; break; } break; // Request a target destination for any user created packets case USERINDEXGETADDR: std::cout << "\rEnter Target Destination ID: "; if (_kbhit()) { temp = (char)toupper(_getch()); } if (temp == KEYBOARDESCAPE) { return (choice = USERINDEXMENU); } else if (temp <= 'Z' && temp >= 'A') { std::cout << temp << "\n"; } else { return 0; } if (login_keystate[temp - 'A'] == 0) { std::cout << "Invalid ID - User Is Not Logged In\n"; } else if (temp == login_key) { std::cout << "Invalid ID - ID Belongs To You\n"; } else { destination_ID = temp; std::cout << "Assigned Target Destination ID to {" << destination_ID << "}\n"; choice = USERINDEXMENU; } break; // Process keyboard input for packet creation and sending case USERINDEXINPUT: if (_kbhit()) { temp = (char)_getch(); } switch (temp) { case KEYBOARDNULL: return 0; case KEYBOARDBACKSPACE: std::cout << "\b \b"; packet_message[character_count] = temp; character_count++; break; case KEYBOARDESCAPE: std::cout << "\n\\--------~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--------/\n\n" "Returning To Menu..."; choice = USERINDEXMENU; if (character_count == 0) { break; } case KEYBOARDENTER: std::cout << (packet_message[character_count] = '\n'); character_count = 10; break; default: std::cout << (packet_message[character_count] = temp); character_count++; break; } if (character_count == 10) { character_count = 0; packet_string = new char[PACKETSTRINGSIZE]; _snprintf_s(packet_string, PACKETSTRINGSIZE, _TRUNCATE, "{%c%cD%10s }", destination_ID, login_key, packet_message); message_queue.push(packet_string); for (unsigned i = 0; i < PACKETSTRINGMESSAGESIZE; i++) { packet_message[i] = '\0'; } } break; } return choice; }