ProtocolBufferConverter » History » Version 18

J. Wienke, 08/10/2011 02:26 PM

1 1 J. Moringen
h1. ProtocolBufferConverter
2 1 J. Moringen
3 12 S. Wrede
{{>toc}}
4 12 S. Wrede
5 18 J. Wienke
_This document represents a working policy that has some drawbacks which should be known. For further details, please have a look at the last paragraph of this page._
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 1 J. Moringen
h3. C++
39 1 J. Moringen
40 15 J. Moringen
As explained above, no code has to be 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.
41 15 J. Moringen
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 15 J. Moringen
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 @IllegalArgumentException@. Hence, the current best practice is to register all types used in an application upon startup. In future RSB versions, this will be handled via the configuration subsystem.
53 13 S. Wrede
54 15 J. Moringen
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 source:trunk/java/core/build.xml) to your project by adding it to your ant build file (usually @build.xml@):
55 7 S. Wrede
56 15 J. Moringen
<pre><code class="xml">
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 15 J. Moringen
</code></pre>
69 1 J. Moringen
70 1 J. Moringen
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 1 J. Moringen
72 15 J. Moringen
<pre><code class="xml">
73 1 J. Moringen
<target name="compile" depends="init, protocol" description="compile the source ">
74 1 J. Moringen
<!-- ... -->
75 15 J. Moringen
</code></pre>
76 15 J. Moringen
77 15 J. Moringen
h3. Python
78 15 J. Moringen
79 15 J. Moringen
source:trunk/python/core/examples/protobuf/informer.py
80 15 J. Moringen
source:trunk/python/core/examples/protobuf/listener.py
81 15 J. Moringen
82 15 J. Moringen
h3. Common Lisp
83 16 J. Moringen
84 16 J. Moringen
source:trunk/cl/cl-rsb/examples/protocol-buffers/sender.lisp
85 16 J. Moringen
source:trunk/cl/cl-rsb/examples/protocol-buffers/receiver.lisp
86 17 J. Moringen
87 17 J. Moringen
h2. Implementations
88 17 J. Moringen
89 17 J. Moringen
|_.Language   |_.File(s)                                                              |
90 17 J. Moringen
| C++         | source:trunk/cpp/core/src/rsb/converter/ProtocolBufferConverter.h,source:trunk/cpp/core/src/rsb/converter/ProtocolBufferConverter.cpp |
91 17 J. Moringen
| Java        | source:trunk/java/core/src/rsb/converter/ProtocolBufferConverter.java |
92 17 J. Moringen
| Python      | source:"trunk/python/core/rsb/transport/converter.py"                 |
93 1 J. Moringen
| Common Lisp | source:trunk/cl/cl-rsb/src/converter/protocol-buffers.lisp            |
94 18 J. Wienke
95 18 J. Wienke
96 18 J. Wienke
h2. Known Limitation of this Approach
97 18 J. Wienke
98 18 J. Wienke
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).