// Copyright (2019-2024) Paul Scherrer Institute #include "ZMQWriterNotificationPuller.h" #include "nlohmann/json.hpp" ZMQWriterNotificationPuller::ZMQWriterNotificationPuller(const std::string &addr, std::chrono::milliseconds timeout) : socket(ZMQSocketType::Pull) { socket.ReceiveTimeout(timeout); socket.Bind(addr); } std::optional ZMQWriterNotificationPuller::Receive(uint64_t run_number, const std::string &run_name) { ZMQMessage msg; // Loop to ensure that messages with wrong run_number or run_name are filtered while (socket.Receive(msg)) { try { nlohmann::json j = nlohmann::json::parse(std::string((char *) msg.data(), msg.size())); if ((j["run_number"] == run_number) && (j["run_name"] == run_name)) { ZMQWriterNotificationOutput ret{}; ret.ok = j["ok"]; ret.processed_images = j["processed_images"]; ret.socket_number = j["socket_number"]; return ret; } } catch (const std::exception &e) { // Cannot parse properly } } return {}; } std::string ZMQWriterNotificationPuller::GetEndpointName() { return socket.GetEndpointName(); }