|
| 1 | +#include "psirng-wrapper.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <cstdlib> |
| 5 | +#include <stdexcept> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +psirng_wrapper & psirng_wrapper::instance() { |
| 9 | + static psirng_wrapper instance; |
| 10 | + return instance; |
| 11 | +} |
| 12 | + |
| 13 | +psirng_wrapper::psirng_wrapper() { |
| 14 | + const char* psirng_host = std::getenv("PSIRNG_HOST"); |
| 15 | + const char* psirng_grpc_port = std::getenv("PSIRNG_GRPC_PORT"); |
| 16 | + const char* psirng_cert_path = std::getenv("PSIRNG_CERT_PATH"); |
| 17 | + |
| 18 | + bool bool_psijent_fallback = false; |
| 19 | + if (const char * psijent_fallback = std::getenv("PSIJENT_FALLBACK")) { |
| 20 | + std::string str_psijent_fallback(psijent_fallback); |
| 21 | + std::transform( |
| 22 | + str_psijent_fallback.begin(), |
| 23 | + str_psijent_fallback.end(), |
| 24 | + str_psijent_fallback.begin(), |
| 25 | + [](const unsigned char c) { |
| 26 | + return static_cast<char>(std::tolower(c)); |
| 27 | + } |
| 28 | + ); |
| 29 | + bool_psijent_fallback = str_psijent_fallback == "yes" || |
| 30 | + str_psijent_fallback == "on" || |
| 31 | + str_psijent_fallback == "true" || |
| 32 | + str_psijent_fallback == "1"; |
| 33 | + } |
| 34 | + |
| 35 | + int result; |
| 36 | + bool should_init_psijent = false; |
| 37 | + |
| 38 | + if (psirng_host && psirng_grpc_port && psirng_cert_path) { |
| 39 | + result = psirngclient_init(&psirngclient_ptr, psirng_host, std::atoi(psirng_grpc_port), psirng_cert_path); |
| 40 | + if (result != PSIRNGCLIENT_RESULT_OK) { |
| 41 | + if (bool_psijent_fallback) { |
| 42 | + should_init_psijent = true; |
| 43 | + } else { |
| 44 | + throw std::runtime_error("failed to initialize psirng client: " + std::to_string(result)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (!psirngclient_ishealthy(psirngclient_ptr)) { |
| 49 | + psirngclient_free(psirngclient_ptr); |
| 50 | + if (bool_psijent_fallback) { |
| 51 | + should_init_psijent = true; |
| 52 | + } else { |
| 53 | + throw std::runtime_error("psirng is not healthy"); |
| 54 | + } |
| 55 | + } |
| 56 | + } else { |
| 57 | + if (bool_psijent_fallback) { |
| 58 | + should_init_psijent = true; |
| 59 | + } else { |
| 60 | + throw std::runtime_error("psirng is not configured"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (should_init_psijent) { |
| 65 | + result = psijent_init(&psijent_ptr); |
| 66 | + if (result != PSIJENT_RESULT_OK) { |
| 67 | + throw std::runtime_error("failed to initialize psijent"); |
| 68 | + } |
| 69 | + |
| 70 | + result = psijent_start(psijent_ptr); |
| 71 | + if (result != PSIJENT_RESULT_OK) { |
| 72 | + psijent_free(psijent_ptr); |
| 73 | + throw std::runtime_error("failed to start psijent"); |
| 74 | + } |
| 75 | + |
| 76 | + if (const char * mantissa_length = std::getenv("PSIJENT_MANTISSA_LENGTH")) { |
| 77 | + psijent_mantissa_length = std::atoi(mantissa_length); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +psirng_wrapper::~psirng_wrapper() { |
| 83 | + if (psirngclient_ptr) { |
| 84 | + psirngclient_free(psirngclient_ptr); |
| 85 | + } |
| 86 | + |
| 87 | + if (psijent_ptr) { |
| 88 | + psijent_free(psijent_ptr); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +double psirng_wrapper::uniform01() { |
| 93 | + const psirng_wrapper & instance = psirng_wrapper::instance(); |
| 94 | + |
| 95 | + int result; |
| 96 | + double value = 0.0; |
| 97 | + |
| 98 | + if (instance.psijent_ptr) { |
| 99 | + result = psijent_randuniform(instance.psijent_ptr, &value, 1, instance.psijent_mantissa_length); |
| 100 | + if (result != PSIJENT_RESULT_OK) { |
| 101 | + throw std::runtime_error("psijent_randuniform failed: " + std::to_string(result)); |
| 102 | + } |
| 103 | + } else { |
| 104 | + result = psirngclient_randuniform(instance.psirngclient_ptr, &value, 1, 0.0, 1.0); |
| 105 | + if (result != PSIRNGCLIENT_RESULT_OK) { |
| 106 | + throw std::runtime_error("psirngclient_randuniform failed: " + std::to_string(result)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return value; |
| 111 | +} |
0 commit comments