ProtocolBufferConverter » History » Version 13

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

1 1 J. Moringen
h1. ProtocolBufferConverter
2 1 J. Moringen
3 12 S. Wrede
{{>toc}}
4 12 S. Wrede
5 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).*
6 4 J. Moringen
7 1 J. Moringen
h2. Introduction
8 1 J. Moringen
9 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.
10 1 J. Moringen
11 1 J. Moringen
Advantages of this approach over manual converter writing (as described [[Writing_Converters|here]]) include:
12 1 J. Moringen
13 1 J. Moringen
* IDL-defined message format (as opposed to an informal "definition" through a particular implementation)
14 1 J. Moringen
* Proper handling of machine Endianess
15 1 J. Moringen
* Proper handling of machine word sizes
16 1 J. Moringen
* Some degree of data validation
17 1 J. Moringen
* Introspection support
18 5 J. Moringen
* Generated data-holder classes and (de)serializers
19 1 J. Moringen
** Available in multiple programming languages without additional effort
20 5 J. Moringen
** Inter-language consistency of data-holder classes and serialized representations
21 1 J. Moringen
22 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.
23 1 J. Moringen
24 1 J. Moringen
h2. Message Definition
25 1 J. Moringen
26 1 J. Moringen
A @SimpleImage@ consists of
27 1 J. Moringen
28 1 J. Moringen
* a width (a non-negative integer)
29 1 J. Moringen
* a height (a non-negative integer)
30 1 J. Moringen
* image data (an array of bytes)
31 1 J. Moringen
32 1 J. Moringen
This structure is reflected in the protocol buffer message definition:
33 1 J. Moringen
34 1 J. Moringen
source:trunk/cpp/core/examples/protobuf_converter/SimpleImage.proto
35 1 J. Moringen
36 6 S. Wrede
h2. Code
37 1 J. Moringen
38 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.
39 1 J. Moringen
40 6 S. Wrede
h3. C++
41 6 S. Wrede
42 1 J. Moringen
source:trunk/cpp/core/examples/protobuf_converter/sender.cpp
43 1 J. Moringen
source:trunk/cpp/core/examples/protobuf_converter/receiver.cpp
44 6 S. Wrede
45 6 S. Wrede
h3. Java
46 6 S. Wrede
47 6 S. Wrede
Java support for Protocol Buffer encoding of user-level datatypes is available since RSB version 0.4.
48 6 S. Wrede
49 6 S. Wrede
source:trunk/java/core/examples/tutorial/protobuf/InformerExample.java
50 6 S. Wrede
source:trunk/java/core/examples/tutorial/protobuf/ListenerExample.java
51 7 S. Wrede
52 13 S. Wrede
Please note that converter instances of the same type can only be registered once in the default converter repository. If a converter is already registered for the same type, you will receive an @IllegealArgumentException@. Hence, the current best practice is to register all types used in an application once upon startup. In future RSB versions, this will be handled via the configuration subsystem.
53 13 S. Wrede
54 13 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 your project by adding it to your ant build file (usually @build.xml@):
55 7 S. Wrede
56 7 S. Wrede
<pre>
57 8 S. Wrede
<!-- required parameters -->
58 8 S. Wrede
<property name="pbuf.protoc" location="/usr/bin/protoc" />
59 7 S. Wrede
60 8 S. Wrede
<!-- protocol buffer generation -->
61 8 S. Wrede
<target name="protocol">
62 8 S. Wrede
	<apply executable="${pbuf.protoc}">
63 8 S. Wrede
		<arg value="--java_out=examples" />
64 8 S. Wrede
		<arg value="--proto_path=${basedir}/examples/tutorial/protocol" />
65 8 S. Wrede
		<fileset dir="examples/tutorial/protocol" includes="*.proto" />
66 8 S. Wrede
	</apply>
67 8 S. Wrede
</target>
68 7 S. Wrede
</pre>
69 7 S. Wrede
70 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):
71 10 S. Wrede
72 10 S. Wrede
<pre>
73 10 S. Wrede
<target name="compile" depends="init, protocol" description="compile the source ">
74 10 S. Wrede
<!-- ... -->
75 10 S. Wrede
</pre>