How to use Protocol Buffers

Alternative to XML and databases, the Protocol Buffers format was created by Google to save structured data in a dual form, easy to read
text, and binary, compact and easy to handle for programs.
The format is extensible.

Why Protocol Buffers?

This format has been defined by Google as an alternative to XML in
order to deal more quickly files, especially for its own needs for its
millions of servers consuming quantities of data.
The easiest way to process data, is that
they are in the form of an object in memory and remain permanently in a
file, where the principle of serialization, which conssits to save an object
in its entirety in a file.
This can be done in XML, but as JSON provides a more direct way to do
this in JavaScript, PB provides a direct means to do so for other
programming languages like C++ and Java. JSON is based on dynamic variables of JavaScript, while PB defines the type of data.
JSON is better than XML to JavaScript, PB is
better suited than XML for other languages because it has no the drawbacks of XML:
large consumer of space and big work of data conversion. It retains its advantages: flexibility and easy reading.

Although PB only work yet with C++ and Java, contributors are working to bring it in other platforms
including PHP and Silverlight. It should belong to the landscape of the
Web in the near future.

How works PB?

The compiler provided by Google, from the description of a proto document, generates a class for software. This class contains methods that will automatically encode data in a binary file or load a binary file and decode it.
Later it will be possible to extend the definition and we can continue to read the files already created.

So you follow the following steps:
- You define the structure of the protocol.
- The compiler generates a class, which is a subclass of Message class.
- You write a program using this class.
- You compile it including the generated class and the protobuf library.
- Data can be saved in a proto file by a serialization inherited from the Message class.
- You can also create a Web service and set the channel to a client.
- Your data is exchanged between the client and server depending on your protocol.

Using Protocol Buffers

Here are the steps to create a first program with Protocol Buffers.

1) Download the archives

You will need two archives for the compiler and the library to include.
On Google's Protocol buffer site, choose the archives for your system.

Extract the content, which creates a Protobuf directory and let appears the protoc compiler.

2) Compile the protobuf library

The process depends on the language,
the compiler and the system as explained in README.txt in the root
directory of Protobuf extracted from the archive.
To illustrate our example we will use Visual Studio Express on Windows.

From VSE launch and load the project file located in the vsprojects directory.
Start compiling, it will probably be preceded by a conversion because
the project is made compatible with older versions of VSE.
The makefile will recreate quantities of files, but what interests us is:

protobuf.lib
protobuf.dll

Possibly accompanied by protobuf.dll files with an extension added.

3) Create a development environment

For practical reasons, you should create a new directory for your project. You can call it "proto" for example.
In this directory, copy:

The demo.data() method provides access to the contents of the data field of our proto definition. This interface is generated by the Protoc compiler.
It also generates the method demo.set_data () which allows you to change the contents of the field.

Full source code in the archive.

7) Compile the hello program

The batch file will be completed by two lines. The first to compile the file generated by Protoc which produces hello.pb.obj, the second to compile our own script: