I'm trying to create 3 messages, start_now, stop_now, and HeartBeat,as defined in the .proto file below. I have already compiled the .proto file using the c++ compiler and have the necessary .h file. It is my understanding that the "oneof" definition allows for only one of the fields to be set at a time. My goal is to create a message,Serialize it using SerializeToString(), then on the receiving end I want to utilize the ParseToString() to get back my message in readable form.
syntax = "proto3";
package jomba;
message SimpleMessage {
}
message Jacket {
uint32 value = 1;
}
message Message {
oneof payload {
SimpleMessage start_now = 1;
SimpleMessage stop_now = 2;
Jacket heart_beat = 3;
}
}
Within the generated header file there is an enum created within class Message which shows the individual fields of the Message. I don't see any set_() functions within this class to generate any of the messages.
enum PayloadCase {
kStartNow = 1,
kStopNow = 2,
kHeartBeat = 3,
PAYLOAD_NOT_SET = 0,
};
Related
I would like to create this protobuf schema in google cloud pub/sub console.
I have a little local script where I successfully serialized and deserialized data with this schema with protobuf.
syntax = "proto2";
package mypackage;
message VideoImpression {
optional string user_id = 1;
optional string candidate_id = 2;
optional int64 event_timestamp = 3;
}
message VideoImpressionsList {
repeated VideoImpression video_impressions = 1;
}
When I save the schema I get this error:
Too many message types specified in schema definition.
I tried splitting both messages into seperate schema definitions but then it would complaint that e.g. when saving message VideoImpressionsList:
"VideoImpression" is not defined.
How can I make pub/sub to accept my above schema with two message types defined?
Thanks for any help
If you want to use one message type inside another, you should define one inside the other like this:
syntax = "proto2";
package mypackage;
message VideoImpressionsList {
message VideoImpression {
optional string user_id = 1;
optional string candidate_id = 2;
optional int64 event_timestamp = 3;
}
repeated VideoImpression video_impressions = 1;
}
I would to send user data, so i added a field in the message file :
class MpeiMacHeader extends MacHeaderBase
{
MPEIMacType type;
uint32_t data;
}
This is what the received message looks like :
msg fields
I didn't success to get the field data in my code. I tried several expressions, i could only get : msg fields
But when I try to compile with msg->content in my code :
no member named 'content' in 'omnetpp::cMessage'
How to get this field in my code ? There is a getData() in generated files, but i didn't find how to use it.
I tried to get the data in handleSelfMessage(), I found a solution using handleLowerPacket() :
void MpeiMac::handleLowerPacket(Packet *packet)
{
if (packet->hasBitError())
{
...
}
else
{
const auto& hdr = packet->peekAtFront<MpeiMacHeader>();
packet->setKind(hdr->getType());
if( hostID == 0 )
data = hdr->getData();
// simply pass the massage as self message, to be processed by the FSM.
handleSelfMessage(packet);
}
}
Maybe this section can explain : inet_developers-guide :
My goal is to restore the lost .proto files written by someone else from existing c++ protobuf messages. By using the Descriptor and EnumDescriptor I was able to do the following:
const google::protobuf::EnumDescriptor* logOptionDesc =
bgs::protocol::LogOption_descriptor();
std::string logOptionStr = logOptionDesc->DebugString();
bgs::protocol::EntityId entityId;
const google::protobuf::Descriptor* entityIdDesc = entityId.GetDescriptor();
std::string entityIdStr = entityIdDesc->DebugString();
The logOptionStr string I got looked something like this:
enum LogOption {
HIDDEN = 1;
HEX = 2;
}
and entityIdStr:
message EntityId {
required fixed64 high = 1 [(.bgs.protocol.log) = HEX];
required fixed64 low = 2 [(.bgs.protocol.log) = HEX];
}
Notice the EntityId message contains some field options. Without resolving this dependency I cannot generate a FileDescriptor that can help me restore the .proto files. I suspect the EntityId string should look something like the following:
import "LogOption.proto";
package bgs.protocol;
extend google.protobuf.FieldOptions {
optional LogOptions log = HEX;
}
message EntityId {
required fixed64 high = 1 [(.bgs.protocol.log) = HEX];
required fixed64 low = 2 [(.bgs.protocol.log) = HEX];
}
Is it possible to restore the .proto files that require additional information such as package, field options and imports? What else do I need to do to restore the .proto files?
I am following the tutorial for protocol buffers and I keep running into different errors while compiling. my addressbook.proto file is in /Users/flexmaster411/protobuffer
protoc -I=/Users/flexmaster411/protobuffer --python_out= /Users/flexmaster411/protobuffer/addressbook.proto /Users/flexmaster411/protobuffer
I keep getting the following error even though I have syntax = "proto3" on my proto file
[libprotobuf WARNING google/protobuf/compiler/parser.cc:471] No syntax specified for the proto file. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
Not sure if I have correctly done the destination folders set up which is causing this or not Any help appreciated
syntax = "proto3";
package tutorial;
message Person {
string name = 1;
int32 id = 2; // Unique ID number for this person.
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
}
// Our address book file is just one of these.
message AddressBook {
repeated Person people = 1;
}
It looks like you reversed the order of the parameters. /Users/flexmaster411/protobuffer is your output directory, so it should appear with --python_out. Since you specified it second, protoc thinks you're telling it that /Users/flexmaster411/protobuffer is an input. So it's trying to open a directory and then parse it as a .proto file. Amusingly, read() on a directory returns no data, which protoc interprets as a perfectly valid .proto file that simply doesn't declare anything! But it then gives you a warning because this empty file doesn't have any syntax line.
I think what you meant to type is:
protoc -I=/Users/flexmaster411/protobuffer --python_out=/Users/flexmaster411/protobuffer /Users/flexmaster411/protobuffer/addressbook.proto
I have a C++ program that sends out various events, e.g. StatusEvent and DetectionEvent with different proto message definitions to a 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 linked.
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 looked at this question but I'm not sure if extensions are the right way to go here. A short code snippet pointing the way will be much appreciated. Examples on protobuf are so rare!
Thanks!
It seems extensions are indeed the way to go but I've got one last point to clear up. Here's the proto definition that I have so far:
// 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, e.g. 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;
}
My problem now is (1) how to know which specific event that the Event message contains and (2) make sure that it contains only one such event (according to the definition, it can contain both a StatusEvent and a DetectionEvent).
I would not use Protocol Buffers for that, but that's perhaps a combination of little use and other habits.
Anyway, I think I would use an abstract class here, to ease general handling and to contain routing information. Class that would not be defined using protobuf, and would contain a protobuf message.
class Message
{
public:
Type const& GetType() const;
Origin const& GetOrigin() const;
Destination const& GetDestination() const;
// ... other informations
template <class T>
void GetContent(T& proto) const
{
proto.ParseFromIstream(&mContent); // perhaps a try/catch ?
}
private:
// ...
std::stringstream mContent;
};
With this structure, you have both general and specific handling at the tip of your fingers:
void receive(Message const& message)
{
LOG("receive - " << message.GetType() << " from " << message.GetOrigin()
<< " to " << message.GetDestination());
if (message.GetType() == "StatusEvent")
{
StatusEvent statusEvent;
message.Decode(statusEvent);
// do something
}
else if (message.GetType() == "DetectionEvent")
{
DetectionEvent detectionEvent;
message.Decode(detectionEvent);
// do something
}
else
{
LOG("receive - Unhandled type");
}
}
Of course, it would be prettier if you used a std::unordered_map<Type,Handler> instead of a hardcoded if / else if + / else chain, but the principle remains identical:
Encode the type of message sent in the header
Decode only the header upon reception and dispatch based on this type
Decode the protobuf message in a part of the code where the type is known statically