Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
54 changes: 42 additions & 12 deletions plotjuggler_plugins/ParserProtobuf/protobuf_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@
#include "PlotJuggler/fmt/format.h"
#include "PlotJuggler/svg_util.h"


namespace gp = google::protobuf;

ProtobufParser::ProtobufParser(const std::string &topic_name,
const std::string type_name,
const gp::FileDescriptorSet &descriptor_set,
PlotDataMapRef &data)
: MessageParser(topic_name, data)
, _proto_pool(&_proto_database)
{
ProtobufParser::ProtobufParser(const std::string& topic_name,
const google::protobuf::Descriptor* descriptor, PlotDataMapRef& data)
: MessageParser(topic_name, data)
, _proto_pool(&_proto_database)
, _msg_descriptor(descriptor)
{
for (auto i = 0; i < _msg_descriptor->field_count(); ++i)
{
if ("timestamp" == _msg_descriptor->field(i)->name())
{
_timestamp_field_id = i;
break;
}
}
}

ProtobufParser::ProtobufParser(const std::string& topic_name, const std::string type_name,
const gp::FileDescriptorSet& descriptor_set,
PlotDataMapRef& data)
: MessageParser(topic_name, data), _proto_pool(&_proto_database)
{
gp::FileDescriptorProto unused;

for (int i = 0; i < descriptor_set.file_size(); ++i)
Expand All @@ -37,6 +49,15 @@ ProtobufParser::ProtobufParser(const std::string &topic_name,
{
throw std::runtime_error("Cannot get message descriptor");
}

for (auto i = 0; i < _msg_descriptor->field_count(); ++i)
{
if ("timestamp" == _msg_descriptor->field(i)->name())
{
_timestamp_field_id = i;
break;
}
}
}

bool ProtobufParser::parseMessage(const MessageRef serialized_msg,
Expand All @@ -53,9 +74,18 @@ bool ProtobufParser::parseMessage(const MessageRef serialized_msg,
}

std::function<void(const google::protobuf::Message&, const std::string&, const bool)> ParseImpl;

double timestamp_updated = timestamp;

ParseImpl = [&](const google::protobuf::Message& msg, const std::string& prefix, const bool is_map)
if (_timestamp_field_id)
{
const gp::Reflection* ref_tmp = mutable_msg->GetReflection();
const gp::Descriptor* desc_tmp = mutable_msg->GetDescriptor();
timestamp_updated = ref_tmp->GetDouble(*mutable_msg, desc_tmp->field(*_timestamp_field_id));
}

ParseImpl = [&](const google::protobuf::Message& msg, const std::string& prefix,
const bool is_map) {
const gp::Reflection* reflection = msg.GetReflection();
const gp::Descriptor* descriptor = msg.GetDescriptor();
// std::vector<const FieldDescriptor*> reflection_fields;
Expand Down Expand Up @@ -155,7 +185,7 @@ bool ProtobufParser::parseMessage(const MessageRef serialized_msg,
reflection->GetRepeatedEnum(msg, field, index);

auto& series = this->getStringSeries(key + suffix);
series.pushBack({timestamp, tmp->name()});
series.pushBack({ timestamp_updated, tmp->name() });
is_double = false;
}break;
case gp::FieldDescriptor::CPPTYPE_STRING:{
Expand All @@ -168,7 +198,7 @@ bool ProtobufParser::parseMessage(const MessageRef serialized_msg,
continue;
}
auto& series = this->getStringSeries(key + suffix);
series.pushBack({timestamp, tmp});
series.pushBack({ timestamp_updated, tmp });
is_double = false;
}break;
case gp::FieldDescriptor::CPPTYPE_MESSAGE:
Expand Down Expand Up @@ -220,7 +250,7 @@ bool ProtobufParser::parseMessage(const MessageRef serialized_msg,
if( is_double )
{
auto& series = this->getSeries(key + suffix);
series.pushBack({timestamp, value});
series.pushBack({ timestamp_updated, value });
}
}
}
Expand Down
14 changes: 3 additions & 11 deletions plotjuggler_plugins/ParserProtobuf/protobuf_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@

#include "error_collectors.h"
#include "PlotJuggler/messageparser_base.h"
#include <optional>

using namespace PJ;

class ProtobufParser : public MessageParser
{
public:
ProtobufParser(const std::string& topic_name,
const google::protobuf::Descriptor* descriptor,
PlotDataMapRef& data)
: MessageParser(topic_name, data)
, _proto_pool(&_proto_database)
, _msg_descriptor(descriptor)
{
}
const google::protobuf::Descriptor* descriptor, PlotDataMapRef& data);

ProtobufParser(const std::string& topic_name,
const std::string type_name,
Expand All @@ -35,14 +30,11 @@ class ProtobufParser : public MessageParser
bool parseMessage(const MessageRef serialized_msg, double& timestamp) override;

protected:
std::optional<unsigned int> _timestamp_field_id;

google::protobuf::SimpleDescriptorDatabase _proto_database;
google::protobuf::DescriptorPool _proto_pool;

google::protobuf::DynamicMessageFactory _msg_factory;
const google::protobuf::Descriptor* _msg_descriptor = nullptr;

};