static void _14ManageSocket(TPTransportCTX *context) { printf("Manage: \n"); printf("\t1: Reset\n"); printf("\t2: Pause\n"); printf("\t3: Resume\n"); printf("> "); int ret = -1; scanf("%d", &ret); cleanStdin(); if (ret == 1) { ret = TPTransportSocketReset(context); } else if (ret == 2) { ret = TPTransportSocketPause(context); } else if (ret == 3) { ret = TPTransportSocketResume(context); } else { return ; } if (ret != 0) { fprintf(stderr, "Client manage socket return (%d)\n", ret); } }
int getPuzzleSize() { int num = 0; char ch; do { printf("\nChoose a puzzle SIZE(%d-%d)-> ", MIN_PUZZLE_SIZE, MAX_PUZZLE_SIZE); // continue until exactly one number entered } while(((scanf("%d%c", &num, &ch) != 2 || ch != '\n') && cleanStdin()) || num < MIN_PUZZLE_SIZE || num > MAX_PUZZLE_SIZE); return num; }
int getShuffleCount() { int num = 0; char ch; do { printf("\nEnter number of SHUFFLES(1-1000)-> "); // continue until exactly one number entered } while(((scanf("%d%c", &num, &ch) != 2 || ch != '\n') && cleanStdin()) || num < 1 || num > 1000); return num; }
char getNextCommand(Puzzle *puzzle) { char ch, n_ch; do { printf("\nSLIDE the numbers to order the tiles.\n"); printDirections(puzzle->directions, false); printf("-> "); // continue until exactly one char entered } while(tolower((scanf("%c%c", &ch, &n_ch) != 2 || n_ch != '\n') && cleanStdin()) || (!isDataInList(puzzle->directions, getData(directionKeys, tolower(ch))) && !isKeyInList(optionKeys, tolower(ch)))); return tolower(ch); }
/** * \brief Solicita un string * \param requestMessage Es el mensaje a ser mostrado para solicitar el dato * \param requestMessage Es el mensaje a ser mostrado en caso de error * \param input Array donde se cargará el texto ingresado * \return - * */ void getValidString(char requestMessage[],char errorMessage[], char input[]) { while(1) { if (!getStringLetras(requestMessage,input)) { printf ("%s\n",errorMessage); continue; } cleanStdin(); break; } }
static int callTransport() { int ret; START: printf("Select Router or Client \n"); printf("\t1: Router\n"); printf("\t2: Client\n"); printf("\t0: Exit\n"); printf("> "); ret = -1; scanf("%d", &ret); cleanStdin(); if (ret == 1) { TPRouterCTX *router = NULL; char configFile[256]; printf("config file:\n"); printf("> "); memset(configFile, 0, sizeof(configFile)); scanf("%s", configFile); ret = TPRouterStartFromConfigFile(&router, configFile); if (ret != 0) { printf("startRouter return (%d)\n", ret); return ret; } printf("[----Router----]\n"); do { printf("\t0: Exit\n"); printf("> "); ret = -1; scanf("%d", &ret); cleanStdin(); } while (ret != 0); ret = TPRouterStop(router); if (ret == 0) { printf("Router is stopped\n"); } } else if (ret == 2) { void (*funcHandle[FUNCHANDLE_N+1])(TPTransportCTX *); funcHandle[1] = _1ReadMessage; funcHandle[2] = _2SendToClient; funcHandle[3] = _3SendToUser; funcHandle[4] = _4SendToGroup; funcHandle[5] = _5SendTagToClient; funcHandle[6] = _6SendTagToGroup; funcHandle[7] = _7ListChatHistory; funcHandle[8] = _8NewGroup; funcHandle[9] = _9JoinGroup; funcHandle[10] = _10LeaveGroup; funcHandle[11] = _11ListUser; funcHandle[12] = _12ListGroup; funcHandle[13] = _13ListMemberInGroup; funcHandle[14] = _14ManageSocket; funcHandle[15] = _15GetShadowFromRouter; funcHandle[16] = _16ListChatHistoryByRange; funcHandle[17] = _17ListChatHistoryFromShadow; TPTransportCTX *context = NULL; char configFile[256]; TPDeviceID deviceID; CLIENT: printf("[----Client----]\n"); printf("\t1: Start Client\n"); printf("\t2: Login User\n"); printf("\t3: Logout User\n"); printf("\t0: StopClient\n"); printf("> "); ret = -1; scanf("%d", &ret); cleanStdin(); if (ret == 1) { printf("config file:\n"); printf("> "); memset(configFile, 0, sizeof(configFile)); scanf("%s", configFile); ret = TPTransportInitFromConfigFile(&context, configFile); if (ret == 0) { TPTagFuncCallSet(context, callHello, 1); getIdFromConfig(context, configFile, (char *)deviceID); char path[256]; system("mkdir -p chat_history"); sprintf(path, "./chat_history/%s.db", deviceID); } else { fprintf(stderr, "Client startClient return (%d)\n", ret); } // fix tParam->myContext = context; tParam->starter=1; tParam->total=0; } else if (ret == 2) { TPUserID userID; printf("userID:\n"); printf("> "); memset(&userID, 0, 16); scanf("%s", (char *)&userID); // fix strcpy(tParam->myUser[tParam->total],(char*)userID); tParam->total++; ret = TPLoginUser(context, &userID); if (ret != 0) { fprintf(stderr, "Client loginUser return (%d)\n", ret); goto CLIENT; } LOGIN: printf("[%s]\n", (char *)&userID); printf("\t1: Read a message\n"); printf("\t2: Send message to Client by DeviceID\n"); printf("\t3: Send message to Client by Name\n"); printf("\t4: Send message to Group\n"); printf("\t5: Send tag to Client by DeviceID\n"); printf("\t6: Send tag to Group\n"); printf("\t7: List chat history\n"); printf("\t8: New group\n"); printf("\t9: Join group\n"); printf("\t10: Leave group\n"); printf("\t11: List user\n"); printf("\t12: List group\n"); printf("\t13: List member in group\n"); printf("\t14: Manage Socket\n"); printf("\t15: Request shadow peer\n"); printf("\t16: List chat By range\n"); printf("\t17: List chat By range From Shadow(must request shadow first)\n"); printf("\t0: Back\n"); INPUT: printf("> "); ret = -1; scanf("%d", &ret); cleanStdin(); if (ret > 0 && ret <= FUNCHANDLE_N) { funcHandle[ret](context); } if (ret == 0) { goto CLIENT; } else if (ret == -1) { goto LOGIN; } goto INPUT; } else if (ret == 3) { TPUserID userID; printf("userID:\n"); printf("> "); memset(&userID, 0, 16); scanf("%s", (char *)&userID); ret = TPLogoutUser(context, &userID); if (ret != 0) { fprintf(stderr, "Client logoutUser return (%d)\n", ret); } // fix int i; for(i=0;i<tParam->total;i++){ if(strcmp(tParam->myUser[i],(char*)userID) == 0){ memset(tParam->myUser[i],0,sizeof(tParam->myUser[0])); } } } else if (ret == 0) { ret = TPTransportClose(context); if (ret == 0) { printf("Client is stopped\n"); } else { fprintf(stderr, "Client stopClient return (%d)\n", ret); } goto EXIT; } goto CLIENT; } else if (ret == 0) { goto EXIT; } else { goto START; } EXIT: printf("Exit\n"); return 0; }