/******************************************************************* * 测试: 测试arrayToFile函数 * 产生一组ProxyInfo并将其写入writeFile中 * 参数: writeFile - 输出的文件 */ void testArrayToFile(char* writeFile) { printf("------------------------------------------------------------\n"); printf("Start testArrayToFile.\n"); int write_fd; // 输出数组 if ((write_fd = open(writeFile, O_WRONLY | O_CREAT, S_IRWXU)) == -1) { fprintf(stderr,"Open %s Error:%s\n",writeFile,strerror(errno)); exit(1); } arrayToFile(g_testProxys, write_fd, sizeof(g_testProxys[0]), g_arraySize); close(write_fd); printf("testArrayToFile success.\n"); }// testArrayToFile
//retrieves and sends the requested flight info to the client. void handleMsg(){ loadInFile(); receive(); printf("Handling client %s\n", inet_ntoa(clntAddr.sin_addr)); if(msgFromClient.quit == TRUE){ printf("%s","Exiting Server..."); close(sock); exit(0); } int index = search(msgFromClient.flightID); if(index == -1){ msgToClient.stat = FAIL; sendStruct(); } else{ // get data out array struct flightData info = dataArray[index]; strcpy(msgToClient.flightID,info.flightID); // If the client wants the capacity if(msgFromClient.op == CAP){ if(msgFromClient.seat == ALL){ msgToClient.numberOfSeats = (info.eCap + info.pCap); msgToClient.stat = SUCCESS; } else if (msgFromClient.seat == ECONOMY){ msgToClient.numberOfSeats = info.eCap; msgToClient.stat = SUCCESS; } else if (msgFromClient.seat == PREMIUM){ msgToClient.numberOfSeats = info.pCap; msgToClient.stat = SUCCESS; } } //If the client wants seat availability else if (msgFromClient.op == AVAILABLE){ if(msgFromClient.seat == ALL){ msgToClient.numberOfSeats = (info.availEconCap + info.availPremCap); msgToClient.stat = SUCCESS; } else if (msgFromClient.seat == ECONOMY){ msgToClient.numberOfSeats = info.availEconCap; msgToClient.stat = SUCCESS; } else if (msgFromClient.seat == PREMIUM){ msgToClient.numberOfSeats = info.availPremCap; msgToClient.stat = SUCCESS; } } //If the client wants to purchase seats else if (msgFromClient.op == PURCHASE){ int toPurchase = msgFromClient.numberOfSeats; if (msgFromClient.seat == ECONOMY){ int sum = info.availEconCap - toPurchase; if(sum < 0){ msgToClient.stat = NO_SEAT; msgToClient.numberOfSeats = info.availEconCap; } else{ dataArray[index].availEconCap = sum; msgToClient.numberOfSeats = sum; msgToClient.stat = SUCCESS; } } else if (msgFromClient.seat == PREMIUM){ int sum = info.availPremCap - toPurchase; if(sum < 0){ msgToClient.stat = NO_SEAT; msgToClient.numberOfSeats = info.availPremCap; } else{ dataArray[index].availPremCap = sum; msgToClient.numberOfSeats = sum; msgToClient.stat = SUCCESS; } } } arrayToFile(); sendStruct(); } }