ProtocolBufferConverter » History » Version 10

S. Wrede, 08/05/2011 05:54 PM

1 1 J. Moringen
h1. ProtocolBufferConverter
2 1 J. Moringen
3 5 J. Moringen
*Warning: this tutorial explains how to use RSB's converter mechanism for domain data which is represented in Google protocol buffer messages. However, Google's protocol buffer tutorial advises against using protocol buffer messages directly as domain classes (see "Protocol Buffers and O-O Design" in "the protocol buffer tutorial":http://code.google.com/apis/protocolbuffers/docs/cpptutorial.html).*
4 4 J. Moringen
5 1 J. Moringen
h2. Introduction
6 1 J. Moringen
7 5 J. Moringen
Google protocol buffers are an approach for IDL-driven generation of data-holder classes and associated serialization and deserialization code. In RSB, arbitrary protocol buffer messages can be processed using the converter mechanism. Depending on the programming language, the particular protocol buffer messages, that will be used, may have to be registered in the converter system in some way. However, after the registration, serialization and deserialization works transparently without further implementation or configuration work.
8 1 J. Moringen
9 1 J. Moringen
Advantages of this approach over manual converter writing (as described [[Writing_Converters|here]]) include:
10 1 J. Moringen
11 1 J. Moringen
* IDL-defined message format (as opposed to an informal "definition" through a particular implementation)
12 1 J. Moringen
* Proper handling of machine Endianess
13 1 J. Moringen
* Proper handling of machine word sizes
14 1 J. Moringen
* Some degree of data validation
15 1 J. Moringen
* Introspection support
16 5 J. Moringen
* Generated data-holder classes and (de)serializers
17 1 J. Moringen
** Available in multiple programming languages without additional effort
18 5 J. Moringen
** Inter-language consistency of data-holder classes and serialized representations
19 1 J. Moringen
20 1 J. Moringen
This page describes how to use the @ProtocolBufferConverter@ class with user-defined protocol buffer messages. The running example of a fictional @SimpleImage@ data-type (introduced [[Writing_Converters|here]]) is reused.
21 1 J. Moringen
22 1 J. Moringen
h2. Message Definition
23 1 J. Moringen
24 1 J. Moringen
A @SimpleImage@ consists of
25 1 J. Moringen
26 1 J. Moringen
* a width (a non-negative integer)
27 1 J. Moringen
* a height (a non-negative integer)
28 1 J. Moringen
* image data (an array of bytes)
29 1 J. Moringen
30 1 J. Moringen
This structure is reflected in the protocol buffer message definition:
31 1 J. Moringen
32 1 J. Moringen
source:trunk/cpp/core/examples/protobuf_converter/SimpleImage.proto
33 1 J. Moringen
34 6 S. Wrede
h2. Code
35 1 J. Moringen
36 6 S. Wrede
As explained above, no code has to written for the data-holder class or its (de)serialization. However, it is necessary to register a template instantiation of the @ProtocolBufferConverter@ class in the converter registry. The @sender@ and @receiver@ program demonstrate how to do this and additionally send and receive @SimpleImage@ messages respectively.
37 1 J. Moringen
38 6 S. Wrede
h3. C++
39 6 S. Wrede
40 1 J. Moringen
source:trunk/cpp/core/examples/protobuf_converter/sender.cpp
41 1 J. Moringen
source:trunk/cpp/core/examples/protobuf_converter/receiver.cpp
42 6 S. Wrede
43 6 S. Wrede
h3. Java
44 6 S. Wrede
45 6 S. Wrede
Java support for Protocol Buffer encoding of user-level datatypes is available since RSB version 0.4.
46 6 S. Wrede
47 6 S. Wrede
source:trunk/java/core/examples/tutorial/protobuf/InformerExample.java
48 6 S. Wrede
source:trunk/java/core/examples/tutorial/protobuf/ListenerExample.java
49 7 S. Wrede
50 7 S. Wrede
To invoke the protocol buffer compiler and generate the necessary data holders and (de)-serialization classes, you may adapt the following ant snippet (taken from RSBJava's build.xml) to you project:
51 7 S. Wrede
52 7 S. Wrede
<pre>
53 8 S. Wrede
<!-- required parameters -->
54 8 S. Wrede
<property name="pbuf.protoc" location="/usr/bin/protoc" />
55 7 S. Wrede
56 8 S. Wrede
<!-- protocol buffer generation -->
57 8 S. Wrede
<target name="protocol">
58 8 S. Wrede
	<apply executable="${pbuf.protoc}">
59 8 S. Wrede
		<arg value="--java_out=examples" />
60 8 S. Wrede
		<arg value="--proto_path=${basedir}/examples/tutorial/protocol" />
61 8 S. Wrede
		<fileset dir="examples/tutorial/protocol" includes="*.proto" />
62 8 S. Wrede
	</apply>
63 8 S. Wrede
</target>
64 7 S. Wrede
</pre>
65 7 S. Wrede
66 10 S. Wrede
This target takes the proto files you provide from @pbuf.protopath@ and generates the corresponding Protocol Buffer Java source classes to @${basedir}/examples/tutorial/protocol@. In turn, you need to include these classes as regular Java source classes in subsequent build steps of your project. Additionally, you may want to add a dependency on the protocol target in your compile target(s):
67 10 S. Wrede
68 10 S. Wrede
<pre>
69 10 S. Wrede
<!-- ... -->
70 10 S. Wrede
<target name="compile" depends="init, protocol" description="compile the source ">
71 10 S. Wrede
<!-- ... -->
72 10 S. Wrede
</pre>