Files
Jungfraujoch/image_pusher/ZMQStream2Pusher.cpp
leonarski_f 8280fde0a2
Some checks failed
Build Packages / build:rpm (rocky8_nocuda) (push) Has been cancelled
Build Packages / build:rpm (rocky9_nocuda) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Has been cancelled
Build Packages / build:rpm (rocky8_sls9) (push) Has been cancelled
Build Packages / build:rpm (rocky9_sls9) (push) Has been cancelled
Build Packages / build:rpm (rocky8) (push) Has been cancelled
Build Packages / build:rpm (rocky9) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2204) (push) Has been cancelled
Build Packages / build:rpm (ubuntu2404) (push) Has been cancelled
Build Packages / Generate python client (push) Has been cancelled
Build Packages / Build documentation (push) Has been cancelled
Build Packages / Unit tests (push) Has been cancelled
Build Packages / Create release (push) Has been cancelled
ImagePusher: Make ZMQStream2Pusher similar to HDF5FilePusher, with a message queue and sending thread
2026-02-28 23:25:32 +01:00

124 lines
4.5 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "ZMQStream2Pusher.h"
#include "../frame_serialize/CBORStream2Serializer.h"
ZMQStream2Pusher::ZMQStream2Pusher(const std::vector<std::string> &addr,
std::optional<int32_t> send_buffer_high_watermark,
std::optional<int32_t> send_buffer_size)
: serialization_buffer(256*1024*1024),
serializer(serialization_buffer.data(), serialization_buffer.size())
{
if (addr.empty())
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "No writer ZMQ address provided");
for (const auto &a : addr) {
auto s = std::make_unique<ZMQStream2PusherSocket>(a, send_buffer_high_watermark, send_buffer_size);
socket.emplace_back(std::move(s));
}
}
bool ZMQStream2Pusher::SendImage(const uint8_t *image_data, size_t image_size, int64_t image_number) {
if (!socket.empty()) {
auto socket_number = (image_number / images_per_file) % socket.size();
return socket[socket_number]->Send(image_data, image_size);
}
return false;
}
void ZMQStream2Pusher::SendImage(ZeroCopyReturnValue &z) {
if (!socket.empty()) {
auto socket_number = (z.GetImageNumber() / images_per_file) % socket.size();
socket[socket_number]->SendImage(z);
} else
z.release();
}
void ZMQStream2Pusher::StartDataCollection(StartMessage& message) {
if (message.images_per_file < 1)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Images per file cannot be zero or negative");
images_per_file = message.images_per_file;
run_number = message.run_number;
run_name = message.run_name;
for (int i = 0; i < socket.size(); i++) {
message.socket_number = i;
if (i > 0)
message.write_master_file = false; // Only writer on first socket is asked to write master file
serializer.SerializeSequenceStart(message);
if (!socket[i]->Send(serialization_buffer.data(), serializer.GetBufferSize()))
throw JFJochException(JFJochExceptionCategory::ZeroMQ, "Timeout on pushing start message on addr "
+ socket[i]->GetEndpointName());
}
for (const auto & i : socket)
i->StartWriterThread();
}
bool ZMQStream2Pusher::SendCalibration(const CompressedImage &message) {
if (socket.empty())
return false;
serializer.SerializeCalibration(message);
return socket[0]->Send(serialization_buffer.data(), serializer.GetBufferSize());
}
bool ZMQStream2Pusher::EndDataCollection(const EndMessage& message) {
serializer.SerializeSequenceEnd(message);
bool ret = true;
for (auto &s: socket) {
s->StopWriterThread();
if (!s->Send(serialization_buffer.data(), serializer.GetBufferSize()))
ret = false;
}
return ret;
}
std::vector<std::string> ZMQStream2Pusher::GetAddress() {
std::vector<std::string> ret;
for (auto &p: socket)
ret.push_back(p->GetEndpointName());
return ret;
}
std::string ZMQStream2Pusher::Finalize() {
std::string ret;
if (writer_notification_socket) {
for (int i = 0; i < socket.size(); i++) {
auto n = writer_notification_socket->Receive(run_number, run_name);
if (!n)
ret += "Writer " + std::to_string(i) + ": no end notification received within 1 minute from collection end";
else if (n->socket_number >= socket.size())
ret += "Writer " + std::to_string(i) + ": mismatch in socket number";
else if (!n->ok)
ret += "Writer " + std::to_string(i) + ": " + n->error;
}
}
return ret;
}
std::string ZMQStream2Pusher::GetWriterNotificationSocketAddress() const {
if (writer_notification_socket)
return writer_notification_socket->GetEndpointName();
else
return "";
}
ZMQStream2Pusher &ZMQStream2Pusher::WriterNotificationSocket(const std::string &addr) {
writer_notification_socket = std::make_unique<ZMQWriterNotificationPuller>(addr, std::chrono::minutes(1));
return *this;
}
std::string ZMQStream2Pusher::PrintSetup() const {
std::string output = "ZMQStream2Pusher: Sending images to the following ZeroMQ sockets: ";
for (const auto &s: socket)
output += s->GetEndpointName() + " ";
return output;
}