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

Allow topic and service to construct messages from description files #428

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Changes from 1 commit
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
67 changes: 41 additions & 26 deletions src/Factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <google/protobuf/text_format.h>

#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
Expand Down Expand Up @@ -115,40 +116,54 @@ class DynamicFactory
// Split all the directories containing .desc files.
std::vector<std::string> descDirs = split(_paths, ':');

for (const std::string &descDir : descDirs)
auto loadDescFile = [this](const std::string &descFile)
{
for (DirIter dirIter(descDir); dirIter != DirIter(); ++dirIter)
// Ignore files without the correct extensions.
if (descFile.rfind(".desc") == std::string::npos &&
descFile.rfind(".proto") == std::string::npos &&
descFile.rfind(".proto.bin") == std::string::npos)
return;

// Parse the .desc file.
std::ifstream ifs(descFile);
if (!ifs.is_open())
{
// Ignore files without the .desc extension.
if ((*dirIter).rfind(".desc") == std::string::npos)
continue;
std::cerr << "DynamicFactory(): Unable to open [" << descFile << "]"
<< std::endl;
return;
}

// Parse the .desc file.
std::ifstream ifs(*dirIter);
if (!ifs.is_open())
{
std::cerr << "DynamicFactory(): Unable to open [" << *dirIter << "]"
<< std::endl;
continue;
}
google::protobuf::FileDescriptorSet fileDescriptorSet;
if (!fileDescriptorSet.ParseFromIstream(&ifs))
{
std::cerr << "DynamicFactory(): Unable to parse descriptor set from ["
<< descFile << "]" << std::endl;
return;
}

google::protobuf::FileDescriptorSet fileDescriptorSet;
if (!fileDescriptorSet.ParseFromIstream(&ifs))
// Place the real descriptors in the descriptor pool.
for (const google::protobuf::FileDescriptorProto &fileDescriptorProto :
fileDescriptorSet.file())
{
if (!this->pool.BuildFile(fileDescriptorProto))
{
std::cerr << "DynamicFactory(): Unable to parse descriptor set from ["
<< *dirIter << "]" << std::endl;
continue;
std::cerr << "DynamicFactory(). Unable to place descriptors from ["
<< descFile << "] in the descriptor pool" << std::endl;
}
}
};

// Place the real descriptors in the descriptor pool.
for (const google::protobuf::FileDescriptorProto &fileDescriptorProto :
fileDescriptorSet.file())
for (const std::string &descDir : descDirs)
{
if (!std::filesystem::is_directory(descDir))
{
loadDescFile(descDir);
}
else
{
for (DirIter dirIter(descDir); dirIter != DirIter(); ++dirIter)
{
if (!pool.BuildFile(fileDescriptorProto))
{
std::cerr << "DynamicFactory(). Unable to place descriptors from ["
<< *dirIter << "] in the descriptor pool" << std::endl;
}
loadDescFile(*dirIter);
}
}
}
Expand Down
Loading