| // product_process.cpp | |
| int main(int argc, char* argv[]) { | |
| if (argc != 5) { | |
| logError("Usage: product_process <product_name> <product_fifo> <sync_fd> <warehouse_count>", argv[0]); | |
| exit(EXIT_FAILURE); | |
| } | |
| std::string productName = argv[1]; | |
| std::string fifoPath = argv[2]; | |
| logInfo(std::string(PROCESS_START_MESSAGE) + productName, argv[0]); | |
| int fifoFd; | |
| try { | |
| FifoWrapper fifo(fifoPath, O_RDONLY, 0666); | |
| fifoFd = fifo.getFd(); | |
| } catch (const std::exception& e) { | |
| logError(e.what(), argv[0]); | |
| exit(EXIT_FAILURE); | |
| } | |
| logInfo(std::string(FIFO_READ_MESSAGE) + fifoPath, argv[0]); | |
| char buffer[MAX_BUFFER_SIZE]; | |
| ssize_t bytesRead; | |
| int totalQuantity = 0; | |
| double totalValue = 0.0; | |
| std::queue<std::pair<int, double>> inventoryQueue; | |
| while ((bytesRead = readAll(fifoFd, buffer, sizeof(buffer) - 1)) > 0) { | |
| buffer[bytesRead] = '\0'; | |
| std::istringstream bufferStream(buffer); | |
| std::string line; | |
| while (std::getline(bufferStream, line)) { | |
| if (line.empty()) | |
| continue; | |
| std::stringstream lineStream(line); | |
| std::string warehouseFile, productNameRead, quantityStr, unitPriceStr, transactionType; | |
| std::getline(lineStream, warehouseFile, ','); | |
| std::getline(lineStream, productNameRead, ','); | |
| std::getline(lineStream, quantityStr, ','); | |
| std::getline(lineStream, unitPriceStr, ','); | |
| std::getline(lineStream, transactionType, ','); | |
| transactionType = trimWhitespace(transactionType); | |
| int quantity = std::stoi(quantityStr); | |
| double unitPrice = std::stod(unitPriceStr); | |
| if (transactionType == "input") { | |
| char inputTransactionMsg[256]; | |
| snprintf(inputTransactionMsg, sizeof(inputTransactionMsg), INPUT_TRANSACTION_MESSAGE, productName.c_str(), quantity, unitPrice); | |
| logInfo(inputTransactionMsg, argv[0]); | |
| inventoryQueue.emplace(quantity, unitPrice); | |
| totalQuantity += quantity; | |
| totalValue += quantity * unitPrice; | |
| } else if (transactionType == "output") { | |
| char outputTransactionMsg[256]; | |
| snprintf(outputTransactionMsg, sizeof(outputTransactionMsg), OUTPUT_TRANSACTION_MESSAGE, productName.c_str(), quantity); | |
| logInfo(outputTransactionMsg, argv[0]); | |
| int remainingQty = quantity; | |
| while (remainingQty > 0 && !inventoryQueue.empty()) { | |
| auto& front = inventoryQueue.front(); | |
| int availableQty = front.first; | |
| double costPrice = front.second; | |
| if (availableQty <= remainingQty) { | |
| totalQuantity -= availableQty; | |
| totalValue -= availableQty * costPrice; | |
| remainingQty -= availableQty; | |
| inventoryQueue.pop(); | |
| } else { | |
| totalQuantity -= remainingQty; | |
| totalValue -= remainingQty * costPrice; | |
| front.first -= remainingQty; | |
| remainingQty = 0; | |
| } | |
| } | |
| if (remainingQty > 0) { | |
| char warningMsg[256]; | |
| snprintf(warningMsg, sizeof(warningMsg), INSUFFICIENT_INVENTORY_WARNING, productName.c_str()); | |
| logError(warningMsg, argv[0]); | |
| totalQuantity -= remainingQty; | |
| totalValue -= remainingQty * unitPrice; | |
| } | |
| } | |
| } | |
| } | |
| close(fifoFd); | |
| // Wait for all warehouses to complete before calculating final values | |
| int syncFd = atoi(argv[3]); | |
| int warehouseCount = atoi(argv[4]); | |
| char dummy; | |
| for (int i = 0; i < warehouseCount; i++) { | |
| ssize_t res = read(syncFd, &dummy, 1); | |
| if (res == -1) { | |
| logError("Failed to read from sync pipe: " + std::string(strerror(errno)), argv[0]); | |
| exit(EXIT_FAILURE); | |
| } | |
| } | |
| close(syncFd); | |
| // Now calculate final values and write results | |
| char finalQuantityMsg[256]; | |
| snprintf(finalQuantityMsg, sizeof(finalQuantityMsg), FINAL_QUANTITY_MESSAGE, productName.c_str(), totalQuantity); | |
| logInfo(finalQuantityMsg, argv[0]); | |
| char finalValueMsg[256]; | |
| snprintf(finalValueMsg, sizeof(finalValueMsg), FINAL_VALUE_MESSAGE, productName.c_str(), totalValue); | |
| logInfo(finalValueMsg, argv[0]); | |
| std::string resultPipePath = "/tmp/product_" + productName + "_result_pipe"; | |
| try { | |
| // Create and open the result FIFO for writing | |
| // Ensure that the FIFO exists; if not, create it | |
| if (mkfifo(resultPipePath.c_str(), 0666) == -1 && errno != EEXIST) { | |
| throw std::runtime_error("Failed to create result pipe: " + std::string(strerror(errno))); | |
| } | |
| FifoWrapper resultFifo(resultPipePath, O_WRONLY, 0666); | |
| int resultPipeFd = resultFifo.getFd(); | |
| std::stringstream resultStream; | |
| resultStream.setf(std::ios::fixed); | |
| resultStream.precision(2); | |
| resultStream << "Product: " << productName << "\n" | |
| << "Total Quantity: " << totalQuantity << "\n" | |
| << "Total Value: " << totalValue << "\n"; | |
| std::string resultData = resultStream.str(); | |
| if (writeAll(resultPipeFd, resultData.c_str(), resultData.size()) == -1) { | |
| logError("Failed to write to result 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); | |
| } | |
| unlink(fifoPath.c_str()); | |
| char processCompletionMsg[256]; | |
| snprintf(processCompletionMsg, sizeof(processCompletionMsg), PROCESS_COMPLETION_MESSAGE, productName.c_str()); | |
| logInfo(processCompletionMsg, argv[0]); | |
| return 0; | |
| } | |
Xet Storage Details
- Size:
- 7.21 kB
- Xet hash:
- a536ea8a78374affda64a3dbbe98a0b8a7a9c37a9ab746ede2ca086f9374458d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.