Files
Jungfraujoch/grpc/JFJochReceiverClient.cpp

161 lines
6.1 KiB
C++

// Copyright (2019-2022) Paul Scherrer Institute
// SPDX-License-Identifier: GPL-3.0-or-later
#include <grpcpp/grpcpp.h>
#include "JFJochReceiverClient.h"
#include "../common/JFJochException.h"
void JFJochReceiverClient::Connect(const std::string& addr)
{
if (addr.empty()) _stub.reset();
else {
grpc::ChannelArguments ch_args;
ch_args.SetMaxReceiveMessageSize(GRPC_MAX_MESSAGE_SIZE);
auto ch = grpc::CreateCustomChannel(addr, grpc::InsecureChannelCredentials(), ch_args);
_stub = std::make_unique<JFJochProtoBuf::gRPC_JFJochReceiver::Stub>(ch);
}
};
void JFJochReceiverClient::Start(const DiffractionExperiment &experiment, const JFCalibration *calibration) {
JFJochProtoBuf::ReceiverInput receiver_input;
*receiver_input.mutable_jungfraujoch_settings() = experiment;
if (calibration != nullptr)
*receiver_input.mutable_calibration() = *calibration;
if (_stub) {
grpc::ClientContext context;
JFJochProtoBuf::Empty empty;
auto status = _stub->Start(&context, receiver_input, &empty);
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
"JFJochReceiver: " + status.error_message());
}
};
void JFJochReceiverClient::Cancel() {
if (_stub) {
grpc::ClientContext context;
JFJochProtoBuf::Empty empty;
auto status = _stub->Cancel(&context, empty, &empty);
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
"JFJochReceiver: " + status.error_message());
}
};
void JFJochReceiverClient::Abort() {
if (_stub) {
grpc::ClientContext context;
JFJochProtoBuf::Empty empty;
auto status = _stub->Abort(&context, empty, &empty);
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
"JFJochReceiver: " + status.error_message());
}
};
JFJochProtoBuf::ReceiverOutput JFJochReceiverClient::Stop() {
JFJochProtoBuf::ReceiverOutput ret;
if (_stub) {
grpc::ClientContext context;
JFJochProtoBuf::Empty empty;
auto status = _stub->Stop(&context, empty, &ret);
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
"JFJochReceiver: " + status.error_message());
}
return ret;
};
JFJochProtoBuf::ReceiverStatus JFJochReceiverClient::GetStatus() {
JFJochProtoBuf::ReceiverStatus ret;
if (_stub) {
grpc::ClientContext context;
JFJochProtoBuf::Empty empty;
auto status = _stub->GetStatus(&context, empty, &ret);
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
"JFJochReceiver: " + status.error_message());
}
return ret;
}
void JFJochReceiverClient::SetDataProcessingSettings(const JFJochProtoBuf::DataProcessingSettings &settings) {
JFJochProtoBuf::Empty ret;
if (_stub) {
grpc::ClientContext context;
auto status = _stub->SetDataProcessingSettings(&context, settings, &ret);
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
"JFJochReceiver: " + status.error_message());
}
}
JFJochProtoBuf::PreviewFrame JFJochReceiverClient::GetPreviewFrame() {
JFJochProtoBuf::PreviewFrame ret;
if (_stub) {
grpc::ClientContext context;
JFJochProtoBuf::Empty empty;
auto status = _stub->GetPreviewFrame(&context, empty, &ret);
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
"JFJochReceiver: " + status.error_message());
} else {
std::vector<uint16_t> fake_image(640*480);
for (int i = 0; i < 640 * 480; i++)
fake_image[i] = i % 65536;
ret.set_image_number(0);
ret.set_width(640);
ret.set_height(480);
ret.set_pixel_depth(2);
ret.set_data(fake_image.data(), fake_image.size() * sizeof(uint16_t));
}
return ret;
}
JFJochProtoBuf::ReceiverNetworkConfig JFJochReceiverClient::GetNetworkConfig() {
JFJochProtoBuf::ReceiverNetworkConfig ret;
if (_stub) {
grpc::ClientContext context;
JFJochProtoBuf::Empty empty;
auto status = _stub->GetNetworkConfig(&context, empty, &ret);
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
"JFJochReceiver: " + status.error_message());
} else {
// For tests to work, dummy receiver needs to replay with nonsense MAC addresses
auto d1 = ret.add_device();
d1->set_mac_addr("00:00:00:00:00:00");
d1->set_ipv4_addr("10.10.50.1");
d1->set_udp_port(1234);
auto d2 = ret.add_device();
d2->set_mac_addr("00:00:00:00:00:01");
d2->set_ipv4_addr("10.10.50.2");
d2->set_udp_port(1234);
}
return ret;
}
JFJochProtoBuf::Plot JFJochReceiverClient::GetPlots(const JFJochProtoBuf::PlotRequest &request) {
JFJochProtoBuf::Plot ret;
if (_stub) {
grpc::ClientContext context;
JFJochProtoBuf::Empty empty;
auto status = _stub->GetDataProcessingPlots(&context, request, &ret);
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
"JFJochReceiver: " + status.error_message());
} else {
// TODO: Write some dummy plots
}
return ret;
}
JFJochProtoBuf::RadialIntegrationProfiles JFJochReceiverClient::GetRadialIntegrationProfiles() {
JFJochProtoBuf::RadialIntegrationProfiles ret;
if (_stub) {
grpc::ClientContext context;
JFJochProtoBuf::Empty empty;
auto status = _stub->GetRadialIntegrationProfiles(&context, empty, &ret);
if (!status.ok()) throw JFJochException(JFJochExceptionCategory::gRPCError,
"JFJochReceiver: " + status.error_message());
} else {
// TODO: Write some dummy plots
}
return ret;
}