| // warehouse_process.cpp | |
| int main(int argc, char* argv[]) { | |
| if (argc != 4) { | |
| logError("Usage: warehouse_process <warehouse_file> <warehouse_fifo> <sync_fd>", argv[0]); | |
| exit(EXIT_FAILURE); | |
| } | |
| std::string warehouseFile = argv[1]; | |
| std::string warehouseFifo = argv[2]; | |
| logInfo(PROCESS_START_MESSAGE + warehouseFile, argv[0]); | |
| // Open warehouse FIFO for reading | |
| int fifoFd; | |
| try { | |
| FifoWrapper fifo(warehouseFifo, O_RDONLY, 0666); | |
| fifoFd = fifo.getFd(); | |
| } catch (const std::exception& e) { | |
| logError(e.what(), argv[0]); | |
| exit(EXIT_FAILURE); | |
| } | |
| char buffer[MAX_BUFFER_SIZE]; | |
| ssize_t bytesRead = readAll(fifoFd, buffer, sizeof(buffer) - 1); | |
| if (bytesRead <= 0) { | |
| logError(READ_FAILURE_MESSAGE + std::string(strerror(errno)), argv[0]); | |
| exit(EXIT_FAILURE); | |
| } | |
| buffer[bytesRead] = '\0'; | |
| if (buffer[0] == '\0') { | |
| logError(EMPTY_PRODUCT_LIST_MESSAGE, argv[0]); | |
| exit(EXIT_FAILURE); | |
| } | |
| close(fifoFd); | |
| std::vector<std::string> products = tokenizeString(buffer, ','); | |
| std::set<std::string> productSet(products.begin(), products.end()); | |
| std::map<std::string, std::queue<std::pair<int, double>>> inventory; | |
| std::map<std::string, double> profitPerProduct; | |
| std::map<std::string, int> fifoFileDescriptors; | |
| std::vector<std::string> allFifoPaths; // For cleanup | |
| for (const std::string& product : productSet) { | |
| std::string fifoPath = "/tmp/product_" + product + "_fifo"; | |
| try { | |
| FifoWrapper productFifo(fifoPath, O_WRONLY, 0666); | |
| int fd = productFifo.getFd(); | |
| fifoFileDescriptors[product] = fd; | |
| allFifoPaths.push_back(fifoPath); | |
| } catch (const std::exception& e) { | |
| char fifoErrorMsg[256]; | |
| snprintf(fifoErrorMsg, sizeof(fifoErrorMsg), FIFO_OPEN_FAILURE_TEMPLATE, product.c_str(), strerror(errno)); | |
| logError(fifoErrorMsg, argv[0]); | |
| continue; | |
| } | |
| } | |
| std::string warehouseFilePath = "./stores/" + warehouseFile; | |
| std::vector<Transaction> transactions = parseTransactionData(warehouseFilePath); | |
| for (const Transaction& transaction : transactions) { | |
| if (productSet.find(transaction.productName) != productSet.end()) { | |
| if (inventory.find(transaction.productName) == inventory.end()) { | |
| inventory[transaction.productName] = std::queue<std::pair<int, double>>(); | |
| profitPerProduct[transaction.productName] = 0.0; | |
| } | |
| std::string transactionType = trimWhitespace(transaction.transactionType); | |
| char transactionProcessingMsg[256]; | |
| snprintf(transactionProcessingMsg, sizeof(transactionProcessingMsg), TRANSACTION_PROCESSING_MESSAGE, transaction.productName.c_str(), transactionType.c_str(), transaction.quantity, transaction.unitPrice); | |
| logInfo(transactionProcessingMsg, argv[0]); | |
| if (transactionType == "input") { | |
| char addingToInventoryMsg[256]; | |
| snprintf(addingToInventoryMsg, sizeof(addingToInventoryMsg), ADDING_TO_INVENTORY_MESSAGE, transaction.quantity, transaction.unitPrice); | |
| logInfo(addingToInventoryMsg, argv[0]); | |
| inventory[transaction.productName].emplace(transaction.quantity, transaction.unitPrice); | |
| } else if (transactionType == "output") { | |
| int remainingQuantity = transaction.quantity; | |
| double salePrice = transaction.unitPrice; | |
| while (remainingQuantity > 0 && !inventory[transaction.productName].empty()) { | |
| auto& front = inventory[transaction.productName].front(); | |
| int availableQuantity = front.first; | |
| double costPrice = front.second; | |
| if (availableQuantity <= remainingQuantity) { | |
| profitPerProduct[transaction.productName] += availableQuantity * (salePrice - costPrice); | |
| remainingQuantity -= availableQuantity; | |
| inventory[transaction.productName].pop(); | |
| } else { | |
| profitPerProduct[transaction.productName] += remainingQuantity * (salePrice - costPrice); | |
| front.first -= remainingQuantity; | |
| remainingQuantity = 0; | |
| } | |
| } | |
| if (remainingQuantity > 0) { | |
| char insufficientInventoryMsg[256]; | |
| snprintf(insufficientInventoryMsg, sizeof(insufficientInventoryMsg), INSUFFICIENT_INVENTORY_MESSAGE, transaction.productName.c_str()); | |
| logError(insufficientInventoryMsg, argv[0]); | |
| char remainingQuantityMsg[256]; | |
| snprintf(remainingQuantityMsg, sizeof(remainingQuantityMsg), REMAINING_QUANTITY_TEMPLATE, remainingQuantity, salePrice); | |
| logError(remainingQuantityMsg, argv[0]); | |
| profitPerProduct[transaction.productName] += remainingQuantity * 0.0; | |
| } | |
| } else { | |
| char invalidTransactionTypeMsg[256]; | |
| snprintf(invalidTransactionTypeMsg, sizeof(invalidTransactionTypeMsg), INVALID_TRANSACTION_TYPE_TEMPLATE, transaction.productName.c_str(), transactionType.c_str()); | |
| logError(invalidTransactionTypeMsg, argv[0]); | |
| continue; | |
| } | |
| auto it = fifoFileDescriptors.find(transaction.productName); | |
| if (it != fifoFileDescriptors.end()) { | |
| int fd = it->second; | |
| std::stringstream messageStream; | |
| messageStream << warehouseFile << "," << transaction.productName << "," << transaction.quantity << "," << transaction.unitPrice << "," << transaction.transactionType << "\n"; | |
| std::string message = messageStream.str(); | |
| if (writeAll(fd, message.c_str(), message.size()) == -1) { | |
| char fifoWriteFailureMsg[256]; | |
| snprintf(fifoWriteFailureMsg, sizeof(fifoWriteFailureMsg), FIFO_WRITE_FAILURE_TEMPLATE, transaction.productName.c_str(), strerror(errno)); | |
| logError(fifoWriteFailureMsg, argv[0]); | |
| } | |
| } else { | |
| logError("No open FIFO found for product " + transaction.productName, argv[0]); | |
| } | |
| } | |
| } | |
| // Close all FIFOs | |
| for (const auto& entry : fifoFileDescriptors) { | |
| close(entry.second); | |
| } | |
| double totalProfit = 0.0; | |
| for (const auto& entry : profitPerProduct) { | |
| const std::string& productName = entry.first; | |
| double profit = entry.second; | |
| totalProfit += profit; | |
| std::string profitFile = "/tmp/warehouse_" + warehouseFile + "_" + productName + "_profit.txt"; | |
| std::ofstream outFile(profitFile); | |
| if (outFile.is_open()) { | |
| outFile << "Warehouse: " << warehouseFile << "\n" | |
| << "Product: " << productName << "\n" | |
| << "Profit: " << profit << "\n"; | |
| outFile.close(); | |
| char profitFileWriteSuccessMsg[256]; | |
| snprintf(profitFileWriteSuccessMsg, sizeof(profitFileWriteSuccessMsg), PROFIT_FILE_WRITE_SUCCESS_TEMPLATE, productName.c_str(), warehouseFile.c_str(), profitFile.c_str()); | |
| logInfo(profitFileWriteSuccessMsg, argv[0]); | |
| } else { | |
| char profitFileWriteFailureMsg[256]; | |
| snprintf(profitFileWriteFailureMsg, sizeof(profitFileWriteFailureMsg), PROFIT_FILE_WRITE_FAILURE_TEMPLATE, profitFile.c_str()); | |
| logError(profitFileWriteFailureMsg, argv[0]); | |
| } | |
| } | |
| for (const auto& entry : inventory) { | |
| const std::string& productName = entry.first; | |
| const std::queue<std::pair<int, double>>& batches = entry.second; | |
| double totalRemainingValue = 0.0; | |
| std::queue<std::pair<int, double>> tempQueue = batches; | |
| while (!tempQueue.empty()) { | |
| const auto& batch = tempQueue.front(); | |
| totalRemainingValue += batch.first * batch.second; | |
| tempQueue.pop(); | |
| } | |
| std::string remainingValueFile = "/tmp/warehouse_" + warehouseFile + "_" + productName + "_remaining_value.txt"; | |
| std::ofstream outFile(remainingValueFile); | |
| if (outFile.is_open()) { | |
| outFile << "Warehouse: " << warehouseFile << "\n" | |
| << "Product: " << productName << "\n" | |
| << "Total Remaining Value: " << totalRemainingValue << "\n"; | |
| outFile.close(); | |
| char remainingValueMsg[256]; | |
| snprintf(remainingValueMsg, sizeof(remainingValueMsg), "Remaining value for product %s in warehouse %s written to %s", productName.c_str(), warehouseFile.c_str(), remainingValueFile.c_str()); | |
| logInfo(remainingValueMsg, argv[0]); | |
| } else { | |
| logError("Failed to write remaining value file " + remainingValueFile, argv[0]); | |
| } | |
| } | |
| std::string profitPipePath = "/tmp/warehouse_" + warehouseFile + "_profit_pipe"; | |
| try { | |
| // Create and open the profit FIFO for writing | |
| // Ensure that the FIFO exists; if not, create it | |
| if (mkfifo(profitPipePath.c_str(), 0666) == -1 && errno != EEXIST) { | |
| throw std::runtime_error("Failed to create profit pipe: " + std::string(strerror(errno))); | |
| } | |
| FifoWrapper profitFifo(profitPipePath, O_WRONLY, 0666); | |
| int profitPipeFd = profitFifo.getFd(); | |
| std::stringstream profitStream; | |
| profitStream << "Warehouse: " << warehouseFile << "\n" | |
| << "Total Profit: " << totalProfit << "\n"; | |
| std::string profitData = profitStream.str(); | |
| if (writeAll(profitPipeFd, profitData.c_str(), profitData.size()) == -1) { | |
| logError("Failed to write to profit pipe: " + std::string(strerror(errno)), argv[0]); | |
| } | |
| // FifoWrapper destructor will handle closing and unlinking | |
| } catch (const std::exception& e) { | |
| logError(e.what(), argv[0]); | |
| exit(EXIT_FAILURE); | |
| } | |
| // Signal completion through sync pipe | |
| int syncFd = atoi(argv[3]); | |
| char done = 1; | |
| if (writeAll(syncFd, &done, 1) == -1) { | |
| logError("Failed to write to sync pipe: " + std::string(strerror(errno)), argv[0]); | |
| } | |
| close(syncFd); | |
| char processCompletionMsg[256]; | |
| snprintf(processCompletionMsg, sizeof(processCompletionMsg), PROCESS_COMPLETION_MESSAGE, warehouseFile.c_str()); | |
| logInfo(processCompletionMsg, argv[0]); | |
| return 0; | |
| } | |
Xet Storage Details
- Size:
- 12.3 kB
- Xet hash:
- e1e34590500629b1e931b0a7cafb1c4bbed4a4116d59c0b894c7743df7b55641
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.