SmartSpectra C++ SDK
Measure human vitals from video with SmartSpectra C++ SDK.
Loading...
Searching...
No Matches
handle_alternative_ipc_configurations.hpp
1// handle_alternative_ipc_configurations.hpp
2// Created by Greg on 1/30/25.
3// Copyright (C) 2025 Presage Security, Inc.
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include <filesystem>
10#include <glog/logging.h>
11#include <absl/flags/flag.h>
12#include <absl/status/status.h>
13#include <absl/status/statusor.h>
14#include "stream_backend.hpp"
15#include "file_ipc/file_ipc_configuration.hpp"
16#include "file_ipc/file_ipc_configuration_flags.hpp"
17#include "redis_ipc/redis_ipc_configuration.hpp"
18#include "redis_ipc/redis_ipc_configuration_flags.hpp"
19
20// Forward declare flags
21ABSL_DECLARE_FLAG(std::string, file_ipc_config_path);
22ABSL_DECLARE_FLAG(std::string, redis_ipc_config_path);
23ABSL_DECLARE_FLAG(bool, save_default_file_ipc_config);
24ABSL_DECLARE_FLAG(bool, save_default_redis_ipc_config);
25
26namespace presage::smartspectra::ipc {
27
31template<StreamBackend Backend>
33
34template<>
35struct IpcConfigTraits<StreamBackend::kFile> {
37};
38
39template<>
40struct IpcConfigTraits<StreamBackend::kRedis> {
42};
43
49template<StreamBackend Backend>
50int SaveDefaultIpcConfig() {
51 using Traits = typename IpcConfigTraits<Backend>::Traits;
52 using ConfigType = typename Traits::ConfigType;
53
54 ConfigType default_config;
55 std::filesystem::path config_path = Traits::config_filename;
56
57 auto status = ConfigType::SaveToJson(default_config, config_path);
58
59 if (!status.ok()) {
60 LOG(ERROR) << "Failed to save default " << Traits::backend_name
61 << " configuration: " << status.message();
62 google::ShutdownGoogleLogging();
63 return EXIT_FAILURE;
64 }
65
66 std::filesystem::path absolute_path = std::filesystem::absolute(config_path);
67 LOG(INFO) << "Default " << Traits::backend_name << " configuration saved to: " << absolute_path;
68 LOG(INFO) << "You can now customize this file and use it with appropriate configuration flags";
69 google::ShutdownGoogleLogging();
70 return EXIT_SUCCESS;
71}
72
80template<StreamBackend Backend>
81bool LoadAndPrepareIpcConfig(
82 StreamBackend backend_type,
83 typename IpcConfigTraits<Backend>::Traits::ConfigType& config
84) {
85 using Traits = typename IpcConfigTraits<Backend>::Traits;
86 using ConfigType = typename Traits::ConfigType;
87
88 // Skip if this backend is not being used
89 if (backend_type != Backend) {
90 return true;
91 }
92
93 // Get the config path flag using the trait
94 std::string config_path = [&]() -> std::string {
95 if constexpr (Backend == StreamBackend::kFile) {
96 return absl::GetFlag(FLAGS_file_ipc_config_path);
97 } else if constexpr (Backend == StreamBackend::kRedis) {
98 return absl::GetFlag(FLAGS_redis_ipc_config_path);
99 } else {
100 return "";
101 }
102 }();
103
104 // Load configuration from file if path is specified
105 if (!config_path.empty()) {
106 auto config_result = ConfigType::LoadFromJson(config_path);
107 if (!config_result.ok()) {
108 LOG(ERROR) << "Failed to load " << Traits::backend_name
109 << " configuration from " << config_path
110 << ": " << config_result.status().message();
111 google::ShutdownGoogleLogging();
112 return false;
113 }
114 config = *config_result;
115 LOG(INFO) << "Loaded " << Traits::backend_name << " configuration from: " << config_path;
116 } else {
117 // Use default configuration
118 LOG(INFO) << "Using default " << Traits::backend_name << " configuration (no config file specified)";
119 }
120
121 // Apply command-line flag overrides
122 auto final_config = [&]() -> absl::StatusOr<typename IpcConfigTraits<Backend>::Traits::ConfigType> {
123 if constexpr (Backend == StreamBackend::kFile) {
124 return file_ipc::ApplyFlagsToConfiguration(config);
125 } else if constexpr (Backend == StreamBackend::kRedis) {
126 return redis_ipc::ApplyFlagsToConfiguration(config);
127 } else {
128 return absl::InternalError("Unknown backend type");
129 }
130 }();
131
132 if (!final_config.ok()) {
133 LOG(ERROR) << "Failed to apply flags to " << Traits::backend_name
134 << " configuration: " << final_config.status().message();
135 google::ShutdownGoogleLogging();
136 return false;
137 }
138
139 config = *final_config;
140 Traits::LogFinalConfig(config);
141 return true;
142}
143
144} // namespace presage::smartspectra::ipc
Definition file_ipc_configuration.hpp:128
Definition handle_alternative_ipc_configurations.hpp:32
Definition redis_ipc_configuration.hpp:195