I have a C ++ program that dispatches various events, for example. StatusEvent and DetectionEvent with various definitions of proto-messages to the message service (currently Active MQ, via activemq-cpp APU). I want to write a message listener that receives these messages, parses them, and writes them to cout for debugging purposes. The listener has status_event_pb.h and detection_event_pb.h related.
My question is: how can I parse the received event without knowing its type? I want to do something like (in pseudo code)
receive event type = parseEventType(event); if( type == events::StatusEventType) { events::StatusEvent se = parseEvent(event); // do stuff with se } else { // handle the case when the event is a DetectionEvent }
I reviewed this question , but I'm not sure if extensions are the right way. Short code snippets indicating the method will be highly appreciated. Protobuff examples are so rare!
Thanks!
Extensions seem to really fit, but I have a final question that needs to be clarified. Here is the proto-definition that I still have:
// A general event, can be thought as base Event class for other event types. message Event { required int64 task_id = 1; required string module_name = 2; // module that sent the event extensions 100 to 199; // for different event types } // Extend the base Event with additional types of events. extend Event { optional StatusEvent statusEvent = 100; optional DetectionEvent detectionEvent = 101; } // Contains one bounding box detected in a video frame, // representing a region of interest. message DetectionEvent { optional int64 frame = 2; optional int64 time = 4; optional string label = 6; } // Indicate status change of current module to other modules in same service. // In addition, parameter information that is to be used to other modules can // be passed, eg the video frame dimensions. message StatusEvent { enum EventType { MODULE_START = 1; MODULE_END = 2; MODULE_FATAL = 3; } required EventType type = 1; required string module_name = 2; // module that sent the event // Optional key-value pairs for data to be passed on. message Data { required string key = 1; required string value = 2; } repeated Data data = 3; }
Now my problem is: (1) find out what specific event the Event message contains, and (2) make sure that it contains only one such event (as defined, it can contain both StatusEvent and DetectionEvent ).
source share