GobySoft / dccl

Dynamic Compact Control Language
Other
17 stars 13 forks source link

Question: Dynamically loading messages #95

Closed mwlock closed 1 year ago

mwlock commented 1 year ago

Hi, I am wondering if there is currently a way to dynamically load messages into the codec from a list? More explicitly, am I able to select the messages to load at runtime as opposed to compile time? In the Python example shown below, the message loaded is defined by a string and could likely be changed dynamically, is this possible for cpp as well?

codec = dccl.Codec()
codec.load("NavigationReport")

For more context, I am looking for an effective way to map ROS messages to DCCL messages so that ROS messages can be republished once received over the acoustic channel. I would like to avoid having to change the cpp file every time we add a new message type (at least manually).

tsaubergine commented 1 year ago

Hi -

Yes this is definitely (conceptually) possible.

using dccl::Codec;
using dccl::DynamicProtobufManager;
Codec codec;
codec.load(DynamicProtobufManager::find_descriptor("NavigationReport"));

Before you can do this you have to ensure that "NavigationReport" is known to the DynamicProtobufManager via one of the "google::protobuf::DescriptorPool"s, either by being compiled in or loaded via a shared library using Codec::load_library() (in which case it will automatically find it (google::protobuf::DescriptorPool::generated_pool()), loaded at runtime from the proto file (DynamicProtobufManager::load_from_proto_file("/path/to/nav.proto")) added via the FileDescriptorProto (essentially a protobuf message that defines a .proto file) (DynamicProtobufManager::add_protobuf_file(const google::protobuf::FileDescriptorProto& proto)), or managed in your own google::protobuf::DescriptorDatabase (DynamicProtobufManager::add_database(...)).

See:

mwlock commented 1 year ago

Works like a charm, thank you! 👼

tsaubergine commented 1 year ago

Glad to hear it!

mwlock commented 9 months ago

Hey @tsaubergine

Sorry to come back to this, but I am finding an error after loading protos and trying to create two different messages sequentially.

In short, I have two messages which I load. Then I create a protobuf message with

static std::shared_ptr<google::protobuf::Message> dccl_msg = DynamicProtobufManager::new_protobuf_message(protoMsgName);

The first message is correct (regardless of which message type I chose to create first), but the second message created just has the same type as the first. Is there something I am doing incorrectly?

I have double checked that the protoMsgName param is correctly set.

tsaubergine commented 8 months ago

I would suggest reading up on what C++ static means in this context: https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables