Files
Jungfraujoch/reader/JFJochImageBufferReader.h
T
leonarski_fandClaude Opus 5.5 f215300a07 ImageBuffer reader: give up on an image MAX_IMAGE_LAG (100) behind the newest, and a unit test
Images reach the buffer in order give or take a few, so one still missing when the buffer holds an
image 100 past it is lost, not late: the read returns false instead of waiting for the end of the
collection. The newest image number comes from ImageBuffer::GetStatus, which scans every slot under
the buffer's lock, so it is asked only every 100 polls (~10 Hz).

The test puts CBOR images into an ImageBuffer the way the receiver does and checks read-back of the
pixels, waiting for an image not yet there, the lag limit, the end of the collection and the bounds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 14:27:10 +02:00

44 lines
1.8 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <atomic>
#include <string>
#include <vector>
#include "JFJochReader.h"
#include "../common/ImageBuffer.h"
// Reads a collection inside the broker while it arrives: the dataset comes from its start message,
// the images from the broker's image buffer, where the receiver puts each one as it forwards it. A
// read of an image not there yet waits for it; after Ended it returns false for an image that never
// came, and so does a read of an image still missing when one MAX_IMAGE_LAG past it is there. The
// buffer must hold the whole collection (slots >= images) - a slot taken again for a later
// image would hand out that image instead.
class JFJochImageBufferReader : public JFJochReader {
public:
constexpr static int64_t MAX_IMAGE_LAG = 100;
explicit JFJochImageBufferReader(ImageBuffer &buffer);
void Open(const StartMessage &start);
// No image comes after this: the collection is over, or abandoned.
void Ended();
[[nodiscard]] uint64_t GetNumberOfImages() const override;
void Close() override;
bool ReadRawImage(int64_t image_number, JFJochReaderRawImage &image) override;
std::vector<SpotToSave> ReadSpots(int64_t image) const override { return {}; }
private:
bool LoadImage_i(std::shared_ptr<JFJochReaderDataset> &dataset, DataMessage &message,
std::vector<uint8_t> &buffer, int64_t image_number, bool update_dataset) override;
// The image's CBOR message, as the receiver put it in the buffer; false when it never came.
bool WaitForMessage(int64_t image_number, std::vector<uint8_t> &cbor);
ImageBuffer &buffer_;
uint64_t number_of_images_ = 0;
std::atomic<bool> ended_ = false;
};