100 lines
3.4 KiB
C++
100 lines
3.4 KiB
C++
// Copyright (2019-2022) Paul Scherrer Institute
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include <fstream>
|
|
#include <random>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include "../common/Logger.h"
|
|
#include "../common/NetworkAddressConvert.h"
|
|
|
|
#include "JFJochBroker.h"
|
|
#include "../grpc/gRPCServer_Template.h"
|
|
|
|
int main (int argc, char **argv) {
|
|
if (argc > 3) {
|
|
std::cout << "Usage ./jfjoch_broker {<JSON config> {<TCP gRPC port>}}" << std::endl;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
uint16_t grpc_port = 5232;
|
|
if (argc >= 3) grpc_port = atoi(argv[2]);
|
|
|
|
Logger logger("jfjoch_broker");
|
|
|
|
nlohmann::json input;
|
|
if (argc > 1) {
|
|
std::ifstream file(argv[1]);
|
|
try {
|
|
input = nlohmann::json::parse(file);
|
|
} catch (const nlohmann::json::exception &e) {
|
|
logger.Error("JSON Parsing exception: " + std::string(e.what()));
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
DiffractionExperiment experiment(2, {4,4}, 8, 36);
|
|
experiment.PedestalG0Frames(2000).PedestalG1Frames(300).PedestalG2Frames(300);
|
|
experiment.MaskChipEdges(true).MaskModuleEdges(true);
|
|
|
|
try {
|
|
experiment.SourceName(input.at("source_name").get<std::string>());
|
|
experiment.SourceNameShort(input.at("source_name_short").get<std::string>());
|
|
} catch (std::exception &e) {
|
|
logger.Warning("Source name metadata error: {}", e.what() );
|
|
}
|
|
|
|
try {
|
|
experiment.InstrumentName(input.at("instrument_name").get<std::string>());
|
|
experiment.InstrumentNameShort(input.at("instrument_name_short").get<std::string>());
|
|
} catch (std::exception &e) {
|
|
logger.Warning("Instrument name metadata error: {}", e.what() );
|
|
}
|
|
|
|
std::random_device generator;
|
|
|
|
uint8_t base_ipv4_addr_net = UINT8_MAX;
|
|
if (input.contains("ipv4_subnet"))
|
|
base_ipv4_addr_net = input["ipv4_subnet"];
|
|
|
|
std::uniform_int_distribution<uint8_t> ipv4_subnet_distribution(0,UINT8_MAX-1);
|
|
if (base_ipv4_addr_net == UINT8_MAX)
|
|
base_ipv4_addr_net = ipv4_subnet_distribution(generator);
|
|
experiment.IPv4Subnet("10.1." + std::to_string(base_ipv4_addr_net) + ".0");
|
|
logger.Info("Base IPv4 address for FPGA and detector modules: "
|
|
+ IPv4AddressToStr(experiment.GetDestIPv4Address(0)));
|
|
|
|
JFJochBroker broker(experiment);
|
|
|
|
if (input.contains("receiver_addr"))
|
|
broker.Services().Receiver(input["receiver_addr"]);
|
|
|
|
if (input.contains("writer")) {
|
|
if (input["writer"].is_array()) {
|
|
for (const auto &j: input["writer"])
|
|
broker.Services().Writer(j["addr_grpc"], j["addr_zmq"]);
|
|
} else {
|
|
broker.Services().Writer(input["writer"]["addr_grpc"], input["writer"]["addr_zmq"]);
|
|
}
|
|
}
|
|
if (input.contains("detector_addr"))
|
|
broker.Services().Detector(input["detector_addr"]);
|
|
|
|
|
|
if (input.contains("gain_file") && (input["gain_file"].is_array())) {
|
|
for (int i = 0; i < input["gain_file"].size(); i++) {
|
|
try {
|
|
broker.LoadGainFile(input["gain_file"][i].get<std::string>());
|
|
} catch (const std::exception &e) {
|
|
logger.ErrorException(e);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
}
|
|
|
|
std::string grpc_addr = "0.0.0.0:" + std::to_string(grpc_port);
|
|
|
|
auto server = gRPCServer(grpc_addr, broker);
|
|
logger.Info("gRPC configuration listening on address " + grpc_addr);
|
|
server->Wait();
|
|
} |