Files
Jungfraujoch/image_pusher/ImagePusher.h
T
leonarski_fandClaude Opus 5.5 509a54ef92 Broker: tell a reader which data files of the current collection are closed
So that rugnux can process a collection while it is written, the broker reports each data file once
a writer has closed it and renamed it into place:

- Writer: FileWriter takes a callback, called for every closed data file. StreamWriter uses it on TCP
  to send an acknowledgement with ack_for = FILE_CLOSED (new TCPFrameType 10): file number and the
  images the file holds. TCP protocol version 5. Files closed at END are reported before the END
  acknowledgement.
- LiveCollection (image_pusher): the start message the writer of the master file received, the
  closed files, the end. Fed by the TCP pusher (FILE_CLOSED acknowledgements; end once every writer
  acknowledged END) and by the in-process HDF5 pusher (the FileWriter callback). The ZeroMQ pusher
  has no back channel and offers none.
- Two routes, protected by the dataset's bearer tokens like the other dataset routes:
  - GET /live/start.cbor: that start message;
  - GET /live/events: server-sent events (collection, file, end, superseded) with the history first
    and a keepalive every 10 s. One follower at a time; a second gets 409.
  Documented in jfjoch_api.yaml; they add no schema, so the generated C++ model is unchanged.
- The HTTP server's thread pool is set explicitly (16, up to 64): an event stream holds a thread for
  the whole collection.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi
2026-10-08 15:52:21 +02:00

84 lines
3.5 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <vector>
#include "../common/DiffractionExperiment.h"
#include "../common/JFJochMessages.h"
#include "../common/ZeroCopyReturnValue.h"
#include "../common/Logger.h"
#include "../frame_serialize/CBORStream2Serializer.h"
enum class ImagePusherType {HDF5, CBOR, TCP, ZMQ, Test, None};
class LiveCollection;
struct ImagePusherStatus {
ImagePusherType pusher_type = ImagePusherType::None;
std::vector<std::string> address = {};
size_t connected_writers = 0;
std::optional<size_t> images_written = 0;
std::optional<size_t> images_write_error = 0;
std::vector<int64_t> writer_fifo_utilization;
};
struct ImagePusherQueueElement {
const uint8_t *image_data;
ZeroCopyReturnValue *z;
bool end;
};
void PrepareCBORImage(DataMessage& message,
const DiffractionExperiment &experiment,
void *image, size_t image_size);
class ImagePusher {
protected:
std::vector<uint8_t> serialization_buffer;
CBORStream2Serializer serializer;
ImagePusher();
public:
// Dry run before the detector is armed: ask the writer whether it could write this run's
// output, and throw if it could not. Only transports with a back-channel can answer, so the
// default is to say nothing - the ZeroMQ pusher is fire-and-forget and keeps failing at the
// final rename instead. Called from the same serialized control thread as StartDataCollection.
virtual void Preflight(StartMessage& message) {}
virtual void StartDataCollection(StartMessage& message) = 0;
virtual bool EndDataCollection(const EndMessage& message) = 0; // Non-blocking
virtual bool SendImage(const uint8_t *image_data, size_t image_size, int64_t image_number) = 0;
// Returns true if the image was accepted (handed to the writer / enqueued for
// sending), false if it was dropped (e.g. no TCP connection, or the enqueue
// deadline expired). Ownership is settled either way: on a false return the
// slot has already been release()d. The authoritative count of images actually
// delivered is GetImagesWritten() (ACK-based for TCP).
virtual bool SendImage(ZeroCopyReturnValue &z);
virtual bool SendCalibration(const CompressedImage& message) = 0;
virtual std::string Finalize(); // Ensure that all streams are closed, can throw exception
virtual std::string GetWriterNotificationSocketAddress() const;
virtual ~ImagePusher() = default;
virtual std::string PrintSetup() const = 0;
virtual std::vector<std::string> GetAddress() const { return {}; }
virtual std::optional<uint64_t> GetImagesWritten() const { return std::nullopt; }
virtual std::optional<uint64_t> GetImagesWriteError() const { return std::nullopt; }
virtual size_t GetConnectedWriters() const { return 0; };
virtual ImagePusherType GetType() const = 0;
virtual std::vector<int64_t> GetWriterFifoUtilization() const { return {}; }
// Which data files of the current collection are closed, for a reader following it. Only the
// pushers whose writers report closed files have one.
virtual LiveCollection *GetLiveCollection() { return nullptr; }
ImagePusherStatus GetStatus() const {
return ImagePusherStatus{
GetType(),
GetAddress(),
GetConnectedWriters(),
GetImagesWritten(),
GetImagesWriteError(),
GetWriterFifoUtilization()
};
}
};