Skip to content

Commit

Permalink
Remove singleton behavior for Reflection Plugin Class (#370)
Browse files Browse the repository at this point in the history
* Implement protostring in singleton reflection plugin class

* Created separate singleton class to save protostring

* Added deleteinstance function to ProtoDescriptorString to free memory when all server stops

* Removed extra comment

* Improved code structure

* Reverted lv_interop

* Reverted lv_interop.cc and lv_interop.h
  • Loading branch information
yash-ni authored Jul 26, 2024
1 parent f6ef366 commit c3336ea
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 21 deletions.
42 changes: 40 additions & 2 deletions src/grpc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,44 @@ namespace grpc_labview

_server = nullptr;
}
LVProtoServerReflectionPlugin::GetInstance()->DeleteInstance();
}
grpc_labview::ProtoDescriptorString::getInstance()->deleteInstance();
}

//---------------------------------------------------------------------
//---------------------------------------------------------------------

// Initialize the static members
ProtoDescriptorString* ProtoDescriptorString::m_instance = nullptr;
std::mutex ProtoDescriptorString::m_mutex;

// Return the static class instance. Thread safe.
ProtoDescriptorString* ProtoDescriptorString::getInstance() {
std::unique_lock<std::mutex> lock(m_mutex);
if (m_instance == nullptr) {
m_instance = new ProtoDescriptorString();
}
return m_instance;
}

// Get the descriptor string
std::string ProtoDescriptorString::getDescriptor() {
std::unique_lock<std::mutex> lock(m_mutex);
return m_descriptor;
}

// Set the descriptor string
void ProtoDescriptorString::setDescriptor(std::string str) {
std::unique_lock<std::mutex> lock(m_mutex);
m_refcount++;
m_descriptor = str;
}

// Delete the instaance based on the refcount
void ProtoDescriptorString::deleteInstance() {
std::unique_lock<std::mutex> lock(m_mutex);
if (!--m_refcount) {
delete m_instance;
m_instance = nullptr;
}
}
}
31 changes: 31 additions & 0 deletions src/grpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,35 @@ namespace grpc_labview
void OccurServerEvent(LVUserEventRef event, gRPCid* data);
void OccurServerEvent(LVUserEventRef event, gRPCid* data, std::string eventMethodName);
std::string read_keycert(const std::string &filename);

//---------------------------------------------------------------------
//---------------------------------------------------------------------
class ProtoDescriptorString {
// Static members
static ProtoDescriptorString* m_instance;
static std::mutex m_mutex;

// Non static members
std::string m_descriptor;
int m_refcount = 0; // Not a normal refcount. Its counts the number of time we set the descriptor string.

// Default private constructor to prevent instantiation
ProtoDescriptorString() = default;

// Delete copy constructor and assignment operator
ProtoDescriptorString(const ProtoDescriptorString&) = delete;
ProtoDescriptorString& operator=(const ProtoDescriptorString&) = delete;
public:
// Return the static class instance
static ProtoDescriptorString* getInstance();

// Set the descriptor string
void setDescriptor(std::string);

// Get the descriptor string
std::string getDescriptor();

// Delete the instance based on the refcount
void deleteInstance();
};
}
21 changes: 6 additions & 15 deletions src/lv_proto_server_reflection_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <grpcpp/impl/server_initializer.h>
#include <grpcpp/server_builder.h>
#include <lv_interop.h>
#include <grpc_server.h>
//---------------------------------------------------------------------
//---------------------------------------------------------------------
using grpc::ServerContext;
Expand All @@ -13,8 +14,6 @@ using grpc::ServerBuilder;

namespace grpc_labview
{
LVProtoServerReflectionPlugin* LVProtoServerReflectionPlugin::m_instance = nullptr;

LVProtoServerReflectionPlugin::LVProtoServerReflectionPlugin() : reflection_service_(new grpc_labview::LVProtoServerReflectionService()) {
}

Expand Down Expand Up @@ -49,19 +48,11 @@ namespace grpc_labview
reflection_service_.get()->AddFileDescriptorProto(serializedProto);
}

LVProtoServerReflectionPlugin* LVProtoServerReflectionPlugin::GetInstance() {
if (m_instance == nullptr)
m_instance = new LVProtoServerReflectionPlugin();
return m_instance;
}

void LVProtoServerReflectionPlugin::DeleteInstance() {
m_instance = nullptr;
}

std::unique_ptr< ::grpc::ServerBuilderPlugin> CreateLVProtoReflection() {
return std::unique_ptr< ::grpc::ServerBuilderPlugin>(
LVProtoServerReflectionPlugin::GetInstance());

LVProtoServerReflectionPlugin* reflectionPluginInstance = new LVProtoServerReflectionPlugin();
reflectionPluginInstance->AddFileDescriptorProto(grpc_labview::ProtoDescriptorString::getInstance()->getDescriptor());
return std::unique_ptr< ::grpc::ServerBuilderPlugin>(reflectionPluginInstance);
}

void InitLVProtoReflectionServerBuilderPlugin() {
Expand All @@ -75,6 +66,6 @@ namespace grpc_labview
LIBRARY_EXPORT void DeserializeReflectionInfo(grpc_labview::LStrHandle serializedFileDescriptor)
{
std::string serializedDescriptorStr = grpc_labview::GetLVString(serializedFileDescriptor);
LVProtoServerReflectionPlugin::GetInstance()->AddFileDescriptorProto(serializedDescriptorStr);
grpc_labview::ProtoDescriptorString::getInstance()->setDescriptor(serializedDescriptorStr);
}
}
5 changes: 1 addition & 4 deletions src/lv_proto_server_reflection_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace grpc_labview
{
class LVProtoServerReflectionPlugin : public ::grpc::ServerBuilderPlugin {
public:
LVProtoServerReflectionPlugin();
::std::string name() override;
void InitServer(ServerInitializer* si) override;
void Finish(ServerInitializer* si) override;
Expand All @@ -47,12 +48,8 @@ namespace grpc_labview
bool has_sync_methods() const override;
void AddService(std::string serviceName);
void AddFileDescriptorProto(const std::string& serializedProto);
static LVProtoServerReflectionPlugin* GetInstance();
void DeleteInstance();

private:
LVProtoServerReflectionPlugin(); // hide constructors TODO
static LVProtoServerReflectionPlugin* m_instance;
std::shared_ptr<grpc_labview::LVProtoServerReflectionService> reflection_service_;
};

Expand Down

0 comments on commit c3336ea

Please sign in to comment.