54 lines
2.2 KiB
C++
54 lines
2.2 KiB
C++
// Copyright (2019-2023) Paul Scherrer Institute
|
|
|
|
#include "JFJochWriterHttp.h"
|
|
|
|
void JFJochWriterHttp::start_post(Pistache::Http::ResponseWriter &response) {
|
|
if (writer_future.valid())
|
|
response.send(Pistache::Http::Code::Internal_Server_Error);
|
|
writer_future = writer.RunFuture();
|
|
response.send(Pistache::Http::Code::Ok);
|
|
}
|
|
|
|
void JFJochWriterHttp::wait_till_done_get(Pistache::Http::ResponseWriter &response) {
|
|
wait_till_done(std::chrono::seconds(5), response);
|
|
}
|
|
|
|
void JFJochWriterHttp::check_if_done_get(Pistache::Http::ResponseWriter &response) {
|
|
wait_till_done(std::chrono::seconds(0), response);
|
|
}
|
|
|
|
void JFJochWriterHttp::cancel_post(Pistache::Http::ResponseWriter &response) {
|
|
writer.Cancel();
|
|
}
|
|
|
|
JFJochWriterHttp::JFJochWriterHttp(StreamWriter &in_writer, std::shared_ptr<Pistache::Rest::Router> &rtr)
|
|
: DefaultApi(rtr), writer(in_writer){
|
|
init();
|
|
}
|
|
|
|
void JFJochWriterHttp::wait_till_done(const std::chrono::seconds &timeout, Pistache::Http::ResponseWriter &response) {
|
|
if (!writer_future.valid()) {
|
|
response.send(Pistache::Http::Code::Not_Found);
|
|
} else {
|
|
auto wait_resp = writer_future.wait_for(timeout);
|
|
if (wait_resp == std::future_status::timeout) {
|
|
response.send(Pistache::Http::Code::Gateway_Timeout);
|
|
} else {
|
|
try {
|
|
auto val = writer_future.get();
|
|
org::openapitools::server::model::Writer_statistics resp_struct;
|
|
resp_struct.setNimages(val.image_puller_stats.processed_images);
|
|
resp_struct.setPerformanceMBs(val.image_puller_stats.performance_MBs);
|
|
resp_struct.setPerformanceHz(val.image_puller_stats.performance_Hz);
|
|
nlohmann::json j = resp_struct;
|
|
response.send(Pistache::Http::Code::Ok, j.dump(), MIME(Application, Json));
|
|
} catch (const std::exception &e) {
|
|
org::openapitools::server::model::_wait_till_done_get_500_response resp_struct;
|
|
resp_struct.setMsg(e.what());
|
|
nlohmann::json j = resp_struct;
|
|
response.send(Pistache::Http::Code::Internal_Server_Error, j.dump(), MIME(Application, Json));
|
|
}
|
|
}
|
|
}
|
|
}
|