| // main.cpp | |
| // Define Constants for Messages | |
| // Maximum Buffer Size | |
| // Function to Cleanup FIFOs | |
| void cleanupFifos(const std::vector<std::string>& fifoPaths) { | |
| for (const auto& path : fifoPaths) { | |
| unlink(path.c_str()); | |
| } | |
| } | |
| int main(int argc, char* argv[]) { | |
| // Ensure the log file is created in the current directory | |
| std::string logFilePath = "./application.log"; | |
| std::ofstream logFile(logFilePath); | |
| if (!logFile.is_open()) { | |
| std::cerr << "Cannot open log file: " << strerror(errno) << std::endl; | |
| exit(EXIT_FAILURE); | |
| } | |
| logFile.close(); | |
| logInfo(SYSTEM_START_MESSAGE, argv[0]); | |
| std::string storesDirectory; | |
| if (argc >= 2) { | |
| storesDirectory = argv[1]; | |
| } else { | |
| storesDirectory = "./stores"; | |
| } | |
| const std::string partsFilePath = storesDirectory + "/parts.csv"; | |
| int totalRows; | |
| std::vector<std::string> allProducts = getProductList(partsFilePath, totalRows); | |
| char rowsMessage[256]; | |
| snprintf(rowsMessage, sizeof(rowsMessage), ROWS_IN_PARTS_FILE_MESSAGE, (size_t)totalRows); | |
| logInfo(rowsMessage, argv[0]); | |
| std::vector<std::string> userSelectedProducts; | |
| getUserSelectedProducts(allProducts, userSelectedProducts); | |
| if (userSelectedProducts.empty()) { | |
| logError("No valid products selected by the user", argv[0]); | |
| exit(EXIT_FAILURE); | |
| } | |
| std::vector<std::string> warehouseFiles = retrieveWarehouseFiles(storesDirectory); | |
| char processCreationMsg[256]; | |
| snprintf(processCreationMsg, sizeof(processCreationMsg), PROCESS_CREATION_MESSAGE, | |
| userSelectedProducts.size(), warehouseFiles.size()); | |
| logInfo(processCreationMsg, argv[0]); | |
| std::map<std::string, std::string> productFifos; | |
| std::vector<std::string> allFifoPaths; // For cleanup | |
| for (const std::string& product : userSelectedProducts) { | |
| std::string fifoPath = "/tmp/product_" + product + "_fifo"; | |
| if (mkfifo(fifoPath.c_str(), 0666) == -1 && errno != EEXIST) { | |
| char fifoCreationErrorMsg[256]; | |
| snprintf(fifoCreationErrorMsg, sizeof(fifoCreationErrorMsg), | |
| FIFO_CREATION_FAILURE_TEMPLATE, product.c_str(), strerror(errno)); | |
| logError(fifoCreationErrorMsg, argv[0]); | |
| cleanupFifos(allFifoPaths); | |
| exit(EXIT_FAILURE); | |
| } | |
| productFifos[product] = fifoPath; | |
| allFifoPaths.push_back(fifoPath); | |
| } | |
| std::map<std::string, pid_t> productProcesses; | |
| std::map<std::string, int> productSyncPipes; | |
| for (const std::string& product : userSelectedProducts) { | |
| int sync_pipe_product[2]; | |
| if (pipe(sync_pipe_product) == -1) { | |
| logError("Failed to create sync pipe for product " + product + ": " + std::strerror(errno), argv[0]); | |
| cleanupFifos(allFifoPaths); | |
| exit(EXIT_FAILURE); | |
| } | |
| productSyncPipes[product] = sync_pipe_product[0]; // Read end | |
| pid_t pid = fork(); | |
| if (pid == -1) { | |
| char forkFailureMsg[256]; | |
| snprintf(forkFailureMsg, sizeof(forkFailureMsg), FORK_FAILURE_TEMPLATE, strerror(errno)); | |
| logError(forkFailureMsg, argv[0]); | |
| cleanupFifos(allFifoPaths); | |
| exit(EXIT_FAILURE); | |
| } | |
| if (pid == 0) { | |
| // Child process | |
| close(sync_pipe_product[1]); // Close write end | |
| char syncFdStr[16]; | |
| snprintf(syncFdStr, sizeof(syncFdStr), "%d", sync_pipe_product[0]); | |
| char warehouseCountStr[16]; | |
| snprintf(warehouseCountStr, sizeof(warehouseCountStr), "%zu", warehouseFiles.size()); | |
| execl("./product_process", "product_process", | |
| product.c_str(), | |
| productFifos[product].c_str(), | |
| syncFdStr, | |
| warehouseCountStr, | |
| (char*)NULL); | |
| perror(EXECL_FAILURE_MESSAGE); | |
| exit(EXIT_FAILURE); | |
| } else { | |
| // Parent process | |
| close(sync_pipe_product[0]); // Close read end | |
| char processCreationMsg[256]; | |
| snprintf(processCreationMsg, sizeof(processCreationMsg), PRODUCT_PROCESS_CREATION_MESSAGE, product.c_str(), pid); | |
| logInfo(processCreationMsg, argv[0]); | |
| productProcesses[product] = pid; | |
| } | |
| } | |
| std::vector<pid_t> warehouseProcesses; | |
| for (const std::string& warehouseFile : warehouseFiles) { | |
| std::string warehouseFifoPath = "/tmp/warehouse_" + warehouseFile + "_fifo"; | |
| if (mkfifo(warehouseFifoPath.c_str(), 0666) == -1 && errno != EEXIST) { | |
| char fifoCreationErrorMsg[256]; | |
| snprintf(fifoCreationErrorMsg, sizeof(fifoCreationErrorMsg), | |
| FIFO_CREATION_FAILURE_TEMPLATE, warehouseFile.c_str(), strerror(errno)); | |
| logError(fifoCreationErrorMsg, argv[0]); | |
| cleanupFifos(allFifoPaths); | |
| exit(EXIT_FAILURE); | |
| } | |
| allFifoPaths.push_back(warehouseFifoPath); | |
| pid_t pid = fork(); | |
| if (pid == -1) { | |
| char forkFailureMsg[256]; | |
| snprintf(forkFailureMsg, sizeof(forkFailureMsg), FORK_FAILURE_TEMPLATE, strerror(errno)); | |
| logError(forkFailureMsg, argv[0]); | |
| cleanupFifos(allFifoPaths); | |
| exit(EXIT_FAILURE); | |
| } | |
| if (pid == 0) { | |
| // Child process | |
| // For warehouse_process, pass the sync_fd if necessary | |
| // Assuming warehouse_process takes <warehouse_file> <warehouse_fifo> <sync_fd> | |
| // Here, 'sync_fd_placeholder' is a placeholder; adjust as needed | |
| execl("./warehouse_process", "warehouse_process", | |
| warehouseFile.c_str(), | |
| warehouseFifoPath.c_str(), | |
| "sync_fd_placeholder", // Replace with actual sync_fd if needed | |
| (char*)NULL); | |
| perror(EXECL_FAILURE_MESSAGE); | |
| exit(EXIT_FAILURE); | |
| } else { | |
| // Parent process | |
| // Write productsString to warehouse FIFO | |
| std::string productsString; | |
| for (const auto& product : userSelectedProducts) { | |
| productsString += product + ","; | |
| } | |
| if (!productsString.empty()) { | |
| productsString.pop_back(); // Remove trailing comma | |
| } | |
| // Open FIFO for writing | |
| int fd = open(warehouseFifoPath.c_str(), O_WRONLY); | |
| if (fd == -1) { | |
| char fifoOpenErrorMsg[256]; | |
| snprintf(fifoOpenErrorMsg, sizeof(fifoOpenErrorMsg), "Failed to open FIFO for warehouse %s: %s", warehouseFile.c_str(), strerror(errno)); | |
| logError(fifoOpenErrorMsg, argv[0]); | |
| cleanupFifos(allFifoPaths); | |
| exit(EXIT_FAILURE); | |
| } | |
| if (writeAll(fd, productsString.c_str(), productsString.size()) == -1) { | |
| logError(PIPE_WRITE_ERROR_MESSAGE, argv[0]); | |
| } | |
| close(fd); | |
| warehouseProcesses.push_back(pid); | |
| } | |
| } | |
| // Wait for warehouse processes | |
| for (pid_t pid : warehouseProcesses) { | |
| waitpid(pid, NULL, 0); | |
| } | |
| double grandTotalProfit = 0.0; | |
| // Wait for product processes | |
| for (const auto& entry : productProcesses) { | |
| const std::string& product = entry.first; | |
| pid_t pid = entry.second; | |
| waitpid(pid, NULL, 0); | |
| unlink(productFifos[product].c_str()); | |
| } | |
| logInfo(MAIN_PROCESS_COMPLETION_MESSAGE, argv[0]); | |
| logInfo(RESULTS_HEADER_MESSAGE, argv[0]); | |
| for (const std::string& product : userSelectedProducts) { | |
| char resultRetrievalMsg[256]; | |
| snprintf(resultRetrievalMsg, sizeof(resultRetrievalMsg), RESULT_RETRIEVAL_MESSAGE, product.c_str()); | |
| logInfo(resultRetrievalMsg, argv[0]); | |
| std::string resultPipePath = "/tmp/product_" + product + "_result_pipe"; | |
| int resultPipeFd = open(resultPipePath.c_str(), O_RDONLY); | |
| if (resultPipeFd != -1) { | |
| char buffer[MAX_BUFFER_SIZE]; | |
| ssize_t bytesRead; | |
| std::cout << "=== Result for Product: " << product << " ===\n"; | |
| while ((bytesRead = readAll(resultPipeFd, buffer, sizeof(buffer) - 1)) > 0) { | |
| buffer[bytesRead] = '\0'; | |
| std::cout << buffer; | |
| } | |
| close(resultPipeFd); | |
| unlink(resultPipePath.c_str()); | |
| } else { | |
| char noDataAvailableMsg[256]; | |
| snprintf(noDataAvailableMsg, sizeof(noDataAvailableMsg), NO_DATA_AVAILABLE_MESSAGE, product.c_str()); | |
| std::cout << noDataAvailableMsg << "\n"; | |
| } | |
| } | |
| for (const std::string& warehouseFile : warehouseFiles) { | |
| std::string profitPipePath = "/tmp/warehouse_" + warehouseFile + "_profit_pipe"; | |
| int profitPipeFd = open(profitPipePath.c_str(), O_RDONLY); | |
| if (profitPipeFd != -1) { | |
| char buffer[MAX_BUFFER_SIZE]; | |
| ssize_t bytesRead; | |
| double warehouseTotalProfit = 0.0; | |
| char warehouseProfitHeaderMsg[256]; | |
| snprintf(warehouseProfitHeaderMsg, sizeof(warehouseProfitHeaderMsg), | |
| WAREHOUSE_PROFIT_HEADER_MESSAGE, warehouseFile.c_str()); | |
| std::cout << warehouseProfitHeaderMsg << "\n"; | |
| std::string content; | |
| while ((bytesRead = readAll(profitPipeFd, buffer, sizeof(buffer) - 1)) > 0) { | |
| buffer[bytesRead] = '\0'; | |
| content += buffer; | |
| std::cout << buffer; | |
| } | |
| size_t pos = content.find("Total Profit: "); | |
| if (pos != std::string::npos) { | |
| warehouseTotalProfit = std::stod(content.substr(pos + 14)); // Adjusted index | |
| grandTotalProfit += warehouseTotalProfit; | |
| } | |
| close(profitPipeFd); | |
| unlink(profitPipePath.c_str()); | |
| } else { | |
| std::cout << "No total profit data available for warehouse: " + warehouseFile + "\n"; | |
| } | |
| } | |
| char grandTotalProfitMsg[256]; | |
| snprintf(grandTotalProfitMsg, sizeof(grandTotalProfitMsg), GRAND_TOTAL_PROFIT_MESSAGE, grandTotalProfit); | |
| std::cout << grandTotalProfitMsg << "\n"; | |
| logInfo(grandTotalProfitMsg, argv[0]); | |
| logInfo(SYSTEM_COMPLETION_MESSAGE, argv[0]); | |
| return 0; | |
| } | |
Xet Storage Details
- Size:
- 11.9 kB
- Xet hash:
- 359bb266d08b3342f78367f0d169ff259d5825740b144735056f7aca906e20f9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.