Files
Jungfraujoch/viewer/JFJochHttpReader.h
T
leonarski_f 84228bf8be
Build Packages / Create release (push) Successful in 24s
Build Packages / build:viewer:macos-arm64:nocuda (push) Successful in 3m29s
Build Packages / build:rugnux:macos-arm64:nocuda (push) Successful in 2m43s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 8m27s
Build Packages / build:rugnux:linux-x86_64:cuda (push) Successful in 9m53s
Build Packages / build:viewer:linux-x86_64:nocuda (push) Successful in 9m58s
Build Packages / build:viewer:linux-x86_64:cuda (push) Successful in 11m22s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 13m39s
Build Packages / build:viewer:windows-x86_64:nocuda (push) Successful in 18m37s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 16m32s
Build Packages / build:viewer:windows-x86_64:cuda (push) Successful in 24m11s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m30s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 19m3s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 20m23s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 19m41s
Build Packages / Generate python client (push) Successful in 50s
Build Packages / Build documentation (push) Successful in 1m16s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 21m0s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 18m38s
Build Packages / build:rugnux:windows-x86_64:cuda (push) Successful in 14m33s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 17m55s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 20m50s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 18m38s
Build Packages / Unit tests (push) Successful in 1h46m14s
v1.0.0-rc.173 (#83)
* jfjoch_broker: Optional per-dataset authentication - statistics, images and plots can require a bearer token, which jfjoch_viewer supports.
* jfjoch_viewer: Dark mode and a theme-matched colour scheme, a magnifier panel, and simpler contrast and background controls.
* Rugnux: Multiple performance improvements on GPU and CPU (CPU-only processing up to 40% faster, faster image decoding on ARM), with unchanged results.
* Rugnux: `--model` rigid-body refinement runs on the GPU, and the model-validation check is faster and more reliable.
* Rugnux: Improved scaling and merging - error model, outlier rejection, absorption correction and French-Wilson amplitudes now agree more closely with XDS and ctruncate.
* Rugnux: Improved integration - radial background on powder and ice rings, crowded rotation data keep their reflections, and CPU-only builds integrate large unit cells as GPU builds do.
* Rugnux: More robust detector geometry - measured beam centre, X-ray bandwidth and goniometer rate, and geometry refinement accepted only on significant evidence.
* Rugnux: Merged files are written in the standard setting, or in the setting of a reference MTZ, structure-factor mmCIF or model, with its free-R flags.
* Rugnux: Richer report - ice and powder rings, further lattices, superstructure candidates and mosaicity, with warnings worded as prompts to check.
* Rugnux: Clear error messages when a data set needs more GPU or host memory than is available.

Reviewed-on: #83
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-09-29 15:57:32 +02:00

101 lines
4.5 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include "../reader/JFJochReader.h"
#include "../common/BrokerStatus.h"
#include "../common/ImageBuffer.h"
#include "../common/ROIDefinition.h"
#include <stdexcept>
// libcurl's easy handle, kept opaque here so this widely-included header does NOT pull in
// <curl/curl.h>. On Windows curl.h drags in <windows.h>, whose min/max/ERROR/LoadImage macros
// would then clobber every viewer translation unit that includes this header (breaking std::min /
// std::max in gemmi, the `ERROR` enum, the LoadImage() calls, ...). CURL is `typedef void` in
// libcurl; <curl/curl.h> is included only in JFJochHttpReader.cpp.
using CURL = void;
// The broker refused the request with 401/403: the current dataset is protected by tokens and the
// one we hold (or its absence) does not open it. Distinct from "broker unreachable" so the viewer
// can report it quietly instead of raising an error dialog.
struct HttpUnauthorized : public std::runtime_error {
using std::runtime_error::runtime_error;
};
class JFJochHttpReader : public JFJochReader {
mutable std::mutex http_mutex;
std::string addr;
// Sent as "Authorization: Bearer <token>" with every request while non-empty.
std::string bearer_token;
// Persistent libcurl handle: created lazily on the first request (i.e. as soon as an address
// is selected) and then reused for every subsequent request so the TCP/TLS connection is kept
// alive. libcurl transparently reopens the connection if it was dropped (idle timeout,
// server-side session limit, ...). curl_mutex serialises access because a single easy handle
// must never be used from two threads at once; it is a leaf lock (nothing else is taken while
// it is held), so it cannot deadlock with http_mutex.
mutable std::mutex curl_mutex;
mutable CURL *curl_handle = nullptr;
std::optional<int64_t> last_image_buffer_counter;
bool last_op_http_sync = false;
// Cache of the (constant-per-acquisition) pixel mask, keyed on arm date, so the per-refresh
// dataset rebuild reuses one shared mask instead of reconstructing it every tick.
std::shared_ptr<const PixelMask> cached_pixel_mask;
std::string cached_pixel_mask_arm_date;
// Minimal HTTP result: whether a response arrived (transport-level success), the HTTP status
// code, and the (possibly binary) response body.
struct HttpResult {
bool ok = false;
long status = 0;
std::string body;
};
// Single libcurl request helper behind every endpoint call. method is "GET" or "PUT"; for PUT
// the body is sent with the given content_type.
HttpResult Request(const std::string &method, const std::string &path,
const std::string &body = {}, const std::string &content_type = {}) const;
// Tear down the persistent connection (on disconnect, when a new address is selected, and in
// the destructor); the next Request transparently re-opens it.
void ResetConnection() const;
ImageBufferStatus GetImageBufferStatus() const;
bool LoadImage_i(std::shared_ptr<JFJochReaderDataset> &dataset,
DataMessage& message,
std::vector<uint8_t> &buffer,
int64_t image_number,
bool update_dataset) override;
std::shared_ptr<JFJochReaderDataset> UpdateDataset_i();
std::vector<float> GetPlot_i(const std::string &plot_type, float fill_value = 0.0) const;
public:
~JFJochHttpReader() override;
void ReadURL(const std::string& url);
// The dataset token; kept across Close() so a reconnect to the same broker reuses it.
void Token(const std::string &token);
uint64_t GetNumberOfImages() const override;
void Close() override;
// Refresh dataset/plots if the image buffer changed since the last poll (returns nullptr if
// unchanged). Always writes the current number of images to num_images_out. Does not load an image.
std::shared_ptr<const JFJochReaderDataset> RefreshDatasetIfChanged(int64_t &num_images_out);
void UploadUserMask(const std::vector<uint32_t>& mask);
[[nodiscard]] ROIDefinition GetROIDefinitions() const; // GET /config/roi
void UploadROIDefinitions(const ROIDefinition &rois) const; // PUT /config/roi
BrokerStatus GetBrokerStatus() const;
bool ReadRawImage(int64_t image_number, JFJochReaderRawImage &image) override;
std::vector<SpotToSave> ReadSpots(int64_t image) const override;
};