Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Toggles and Debug Symbols for Release Build #338

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ else()
add_definitions(-D_WIN32_WINNT=0x600)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4267 /wd4244")
add_compile_options("$<$<NOT:$<CONFIG:Debug>>:/Zi>")
add_link_options("$<$<NOT:$<CONFIG:Debug>>:/DEBUG>")
add_link_options("$<$<NOT:$<CONFIG:Debug>>:/OPT:REF>")
add_link_options("$<$<NOT:$<CONFIG:Debug>>:/OPT:ICF>")
endif()

find_package(Threads REQUIRED)
Expand Down Expand Up @@ -55,21 +59,22 @@ include_directories("${CMAKE_CURRENT_BINARY_DIR}" "./src" "./third_party/grpc" "
# LabVIEW support for grpc and protobuf
#----------------------------------------------------------------------
add_library(labview_grpc_server SHARED
src/grpc_client.cc
src/grpc_server.cc
src/grpc_interop.cc
src/any_support.cc
src/cluster_copier.cc
src/event_data.cc
src/feature_toggles.cc
src/grpc_client.cc
src/grpc_interop.cc
src/grpc_load.cc
src/grpc_server.cc
src/lv_interop.cc
src/lv_message.cc
src/lv_message_value.cc
src/lv_proto_server_reflection_plugin.cc
src/lv_proto_server_reflection_service.cc
src/message_element_metadata_owner.cc
src/any_support.cc
src/unpacked_fields.cc
src/grpc_load.cc
src/lv_proto_server_reflection_service.cc
src/lv_proto_server_reflection_plugin.cc
)
)
target_link_libraries(labview_grpc_server
${_REFLECTION}
${_GRPC_GRPCPP}
Expand All @@ -80,8 +85,9 @@ target_link_libraries(labview_grpc_server
# server VIs from a .proto file
#----------------------------------------------------------------------
add_library(labview_grpc_generator SHARED
src/proto_parser.cc
src/feature_toggles.cc
src/lv_interop.cc
src/proto_parser.cc
)
target_link_libraries(labview_grpc_generator
${_REFLECTION}
Expand Down
49 changes: 49 additions & 0 deletions src/feature_toggles.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "feature_toggles.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>

// Function to read feature configurations from an INI file
void FeatureConfig::readConfigFromFile(const std::string& filePath) {
ni-sujain marked this conversation as resolved.
Show resolved Hide resolved
std::ifstream configFile(filePath);
if (!configFile.is_open()) {
return;
}

std::string line;
std::string currentSection; // For handling INI sections

while (std::getline(configFile, line)) {
// Trim leading and trailing whitespaces
line.erase(line.find_last_not_of(" \t") + 1);
line.erase(0, line.find_first_not_of(" \t"));

// Skip comments and empty lines
if (line.empty() || line[0] == ';') {
continue;
}

// Check for section header
if (line[0] == '[' && line[line.length() - 1] == ']') {
currentSection = line.substr(1, line.length() - 2);
} else {
// Parse key-value pairs
std::istringstream iss(line);
std::string key, value;
if (std::getline(iss, key, '=') && std::getline(iss, value)) {
// Append section name to key for uniqueness
std::string fullKey = currentSection.empty() ? key : currentSection + "_" + key;
featureFlags[fullKey] = (value == "true");
}
}
}

configFile.close();
}

// Function to check if a feature is enabled
bool FeatureConfig::isFeatureEnabled(const std::string& featureName) const {
auto it = featureFlags.find(featureName);
return (it != featureFlags.end()) ? it->second : false;
}
27 changes: 27 additions & 0 deletions src/feature_toggles.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>

class FeatureConfig {
private:
std::map<std::string, bool> featureFlags;

// Constructor to initialize with default values
FeatureConfig() {
featureFlags["gRPC"] = true; // Enable gRPC by default as an example, this will never be overridden by config file
}

public:
// Singleton instance
static FeatureConfig& getInstance() {
static FeatureConfig instance;
return instance;
}

// Function to read feature configurations from an INI file
void readConfigFromFile(const std::string& filePath);

// Function to check if a feature is enabled
bool isFeatureEnabled(const std::string& featureName) const;
};
22 changes: 22 additions & 0 deletions src/grpc_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <grpcpp/support/channel_arguments.h>
#include <ctime>
#include <chrono>
#include "feature_toggles.h"

namespace grpc_labview
{
Expand Down Expand Up @@ -276,6 +277,9 @@ LIBRARY_EXPORT int32_t ClientUnaryCall(
int32_t timeoutMs,
grpc_labview::gRPCid* contextId)
{
// Instantiating the feature toggles singleton that will read the feature configuration file
FeatureConfig::getInstance().readConfigFromFile("feature_config.ini");
CPattar-NI marked this conversation as resolved.
Show resolved Hide resolved

auto client = clientId->CastTo<grpc_labview::LabVIEWgRPCClient>();
if (!client)
{
Expand Down Expand Up @@ -401,6 +405,9 @@ LIBRARY_EXPORT int32_t ClientBeginClientStreamingCall(
int32_t timeoutMs,
grpc_labview::gRPCid* contextId)
{
// Instantiating the feature toggles singleton that will read the feature configuration file
FeatureConfig::getInstance().readConfigFromFile("feature_config.ini");
CPattar-NI marked this conversation as resolved.
Show resolved Hide resolved

auto client = clientId->CastTo<grpc_labview::LabVIEWgRPCClient>();
if (!client)
{
Expand Down Expand Up @@ -455,6 +462,9 @@ LIBRARY_EXPORT int32_t ClientBeginServerStreamingCall(
int32_t timeoutMs,
grpc_labview::gRPCid* contextId)
{
// Instantiating the feature toggles singleton that will read the feature configuration file
FeatureConfig::getInstance().readConfigFromFile("feature_config.ini");

auto client = clientId->CastTo<grpc_labview::LabVIEWgRPCClient>();
if (!client)
{
Expand Down Expand Up @@ -517,6 +527,9 @@ LIBRARY_EXPORT int32_t ClientBeginBidiStreamingCall(
int32_t timeoutMs,
grpc_labview::gRPCid* contextId)
{
// Instantiating the feature toggles singleton that will read the feature configuration file
FeatureConfig::getInstance().readConfigFromFile("feature_config.ini");

auto client = clientId->CastTo<grpc_labview::LabVIEWgRPCClient>();
if (!client)
{
Expand Down Expand Up @@ -563,6 +576,9 @@ LIBRARY_EXPORT int32_t ClientBeginBidiStreamingCall(
//---------------------------------------------------------------------
LIBRARY_EXPORT int32_t ClientBeginReadFromStream(grpc_labview::gRPCid* callId, grpc_labview::MagicCookie* occurrencePtr)
{
// Instantiating the feature toggles singleton that will read the feature configuration file
FeatureConfig::getInstance().readConfigFromFile("feature_config.ini");

auto reader = callId->CastTo<grpc_labview::StreamReader>();
auto call = callId->CastTo<grpc_labview::ClientCall>();

Expand Down Expand Up @@ -592,6 +608,9 @@ LIBRARY_EXPORT int32_t ClientBeginReadFromStream(grpc_labview::gRPCid* callId, g
//---------------------------------------------------------------------
LIBRARY_EXPORT int32_t ClientCompleteReadFromStream(grpc_labview::gRPCid* callId, int* success, int8_t* responseCluster)
{
// Instantiating the feature toggles singleton that will read the feature configuration file
FeatureConfig::getInstance().readConfigFromFile("feature_config.ini");

auto reader = callId->CastTo<grpc_labview::StreamReader>();
auto call = callId->CastTo<grpc_labview::ClientCall>();
if (!reader || !call)
Expand All @@ -618,6 +637,9 @@ LIBRARY_EXPORT int32_t ClientCompleteReadFromStream(grpc_labview::gRPCid* callId
//---------------------------------------------------------------------
LIBRARY_EXPORT int32_t ClientWriteToStream(grpc_labview::gRPCid* callId, int8_t* requestCluster, int* success)
{
// Instantiating the feature toggles singleton that will read the feature configuration file
FeatureConfig::getInstance().readConfigFromFile("feature_config.ini");

auto writer = callId->CastTo<grpc_labview::StreamWriter>();
if (!writer)
{
Expand Down
4 changes: 4 additions & 0 deletions src/grpc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <future>
#include <grpcpp/impl/server_initializer.h>
#include "lv_proto_server_reflection_plugin.h"
#include "feature_toggles.h"

//---------------------------------------------------------------------
//---------------------------------------------------------------------
Expand Down Expand Up @@ -178,6 +179,9 @@ namespace grpc_labview
std::string serverKeyPath,
ServerStartEventData *serverStarted)
{
// Instantiating the feature toggles singleton that will read the feature configuration file
FeatureConfig::getInstance().readConfigFromFile("feature_config.ini");
CPattar-NI marked this conversation as resolved.
Show resolved Hide resolved

std::string server_address;
if (address.length() != 0)
{
Expand Down
4 changes: 4 additions & 0 deletions src/lv_interop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstring>
#include <memory>
#include <grpcpp/grpcpp.h>
#include "feature_toggles.h"

#ifndef _WIN32
#include <dlfcn.h>
Expand Down Expand Up @@ -51,6 +52,9 @@ namespace grpc_labview
//---------------------------------------------------------------------
void InitCallbacks()
{
// Instantiating the feature toggles singleton that will read the feature configuration file
FeatureConfig::getInstance().readConfigFromFile("feature_config.ini");
CPattar-NI marked this conversation as resolved.
Show resolved Hide resolved

if (NumericArrayResizeImp != nullptr)
{
return;
Expand Down
Loading