[protobuf] Serialize message in C++, parse using Java
I've Google for a day now and could not find full information on the following problem. I want to serialize protobuf messages in C++, send them to a JMS (using activemq-cpp API) and parse in my Java server. Based on what I found in my searches, here's my C++ function that serializes the message: /* * Serialize given protobuf message and send to Active MQ JMS using the producer. */ void MessageProducer::send( const diva::messaging::Message& proto_mesg ) { using namespace google::protobuf::io; long bufLength = proto_mesg.ByteSize() + CodedOutputStream::VarintSize32( proto_mesg.ByteSize() ); unsigned char buf[bufLength]; ZeroCopyOutputStream* raw_output = new ArrayOutputStream( buf, bufLength ); CodedOutputStream* coded_output = new CodedOutputStream(raw_output); // Prepend the message size to wire message. coded_output->WriteVarint32( proto_mesg.ByteSize() ); proto_mesg.SerializeToCodedStream(coded_output); // Create an ACtive JMS message and insert task & module information in header. cms::BytesMessage* message = m_session->createBytesMessage(); // Write serialized protobuf message to the JMS message and send. message->writeBytes( buf, 0, bufLength ); m_producer->send( message ); delete message; delete coded_output; delete raw_output; } And here's the part in Java that parses teh received messages from JMS: // Create a byte array for received message. BytesMessage receivedMessage = (BytesMessage)received_message; logger.info("received message in NAC"); byte[] mesg_bytes = new byte[(int)receivedMessage.getBodyLength()]; int num_read = receivedMessage.readBytes(mesg_bytes); if ( num_read != receivedMessage.getBodyLength() ) { throw new Exception("Error reading message into byte array"); } // Create registry for all possible DIVA messages. ExtensionRegistry er = ExtensionRegistry.newInstance(); DivaBase.registerAllExtensions(er); // Parse the received message. diva.messaging.DivaBase.Message m = diva.messaging.DivaBase.Message.parseFrom( mesg_bytes, er ); Currently, I get Problem parsing message received in NAC: com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag. Problem parsing message received in NAC: java.lang.ClassCastException: org.apache.activemq.command.ActiveMQTextMessage cannot be cast to javax.jms.BytesMessage errors for each message and cannot see what I'm doing wrong. Thanks for any comments on the code and/or pointers you might provide. -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
[protobuf] Re: beginner question about extensions
Duh, the solution was simple, I was confused with the repeated entry fields. On Aug 11, 5:25 pm, Jun8 wrote: > myevents::Event e; > e.set_task_id( taskID ); > e.set_module_name( moduleName ); > > // Create a status event > myevents::StatusEvent* se = e.AddExtension( myevents::status_event ); Should be myevents::StatusEvent* se = e.MutableExtension(diva::events::status_event); Now, I can send the event. To test my reception I do myevents::Event e; e.ParseFromString(text); std::cout << "Received event: " << std::endl << e.DebugString() << std::endl; However, this produces libprotobuf ERROR google/protobuf/message_lite.cc:123] Can't parse message of type "myevents.Event" because it is missing required fields: task_id, module_name Received event: Received event: task_id: 1 module_name: "face_detector" [diva.events.status_event] { type: MODULE_START data { key: "keyframe_width" value: "360" } } I couldn't figure out the error message, the fields are clearly there. -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
[protobuf] beginner question about extensions
Hi, I want to implement a hierarchy of events using protobuf. Specifically, I have a main Event message type. These Events currently can be either be a StatusEvent or a DetectionEvent. Here's the proto definitions I came up with: // 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; } // Extend the base Event with additional types of events. extend Event { optional StatusEvent status_event = 100; optional DetectionEvent detection_event = 101; } // A bounding box detected in a frame representing a region of interest. message DetectionEvent { optional int64 frame = 1; // frame number (starting at 1) where optional int64 time = 2; optional string label = 3; } // Change of status in current module. message StatusEvent { enum EventType { MODULE_START = 1; MODULE_END = 2; MODULE_FATAL = 3; } required EventType type = 1; // Optional key-value pairs for data to be passed on. message Data { required string key = 1; required string value = 2; } repeated Data data = 2; } This compiles with protoc. My problem is, after I create an Event, e, I don't know how to add an extension event. I though the way to go was myevents::Event e; e.set_task_id( taskID ); e.set_module_name( moduleName ); // Create a status event myevents::StatusEvent* se = e.AddExtension( myevents::status_event ); se->set_type( myevents::StatusEvent::MODULE_START ); myevents::StatusEvent::Data *data = se->add_data(); data->set_key( "frame_width" ); data->set_value( 100 ); When I do this, I get the following compilation error on my protobuf header file: ‘Add’ is not a member of ‘google::protobuf::internal::MessageTypeTraits’ so clearly this is not the way to go. What is the correct way to do this? Any help would be much appreciated. Thanks! -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.