How to program Protocol Buffers with C++, PHP and Rust

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
protocol buffers is an extensive mechanism developed by Google for civilizing is to trade data it is especially useful for developing applications or services that need to communicate with each other or simply for data storage Google use protocol buffers to store and transfer the script to the data within its platform protocol buffers allow us to realize our structured data in a very compact binary format it also allowed to a serialized is binary format to recover the data in the original format protocol buffers is cross-platform and is independent of the programming language that we used to develop think in a traditional format like XML of JSON with a smaller faster and simpler in order to specify the data structures to be serialize a message description language is provided by protocol buffers this specification is safe it in files with the extension proto profiles are compile it with proto C which is the protocol buffers compiler compilation generates the necessary code to serialize and deserialize messages the compiler can generate the source code in the predominant language of our interest the provided programming language interoperability allow us to develop highly the corporate services that need performant data exchange even if those services are developed using different technologies protocol buffers was designed realising simplicity and performance it was originally developed to be lighter and faster than the traditional XML format so it is easier to reuse it in services and applications that require a high performance distributive system skills of musicians real-time applications Internet of Things etc in this extensive tutorial we will use protocol buffers with the C++ PHP and rust languages we will create a JSON file containing an object with information and logo of a company we will define and compile the data structure with the protocol buffers compiler from the data of the JSON object we will create and sell you is a portable file that will be stored in a banana file using C++ as last we will serialize the proto buffer using the code generated by the compiler in C++ PHP and Dresden the main goal of this tutorial is to understand how critical buffers works and how we can use them in our applications writen in C++ PHP and rust I hope you find it useful first of all we will download and install the protocol buffers compiler using the web browser we visit elite hub repository github.com / Google slides portable in the religious section we will look for the latest available version and then copy the tunnel URL the coulomb bastion is the 3.11 dot for [Music] [Music] you the log animals in the file [Music] next combine the source code and install the protocol buffers compiler we can check that it has been installed properly by running the command proto Minos Minos person we start by creating the project folder that we will name protocol buffers underscore demo in the root of the project we create the proto 3 sub folder where we will put the files with the product session we launch or IV or favorite sauce called ater and then import the project now we specify the data structure we want to serialize by defining a message type in a file with the proto extension in this tutorial we will create a very simple example of a profile with a definition of the company message inside the proto free folder we create the company load profile this will be the protocol buffer that we will use in this tutorial [Music] within the company message we also defined the enumerated type company category with different company categories [Music] [Music] the company missiles have the fields ID name description at every employees active level and created [Music] the field name blower will contain the logo of the company in JPEG format and the field name created will contain the text time of the data when the company was created as you can see this message is quite simple althought with protocol buffers we can define much more complex data structures once the message is the finite we will run the protocol buffer compiler to generate the source code in C++ PHP and Russ in order to civilize and deserialize messages of type company in this tutorial we will make some implementations in C++ PHP and Russ so we will create a specific folder for each one of them inside each of these folders we'll create the prototype folder where we will put the source code generated by the protocol buffer compiler [Music] [Music] [Music] the protocol buffers compiler allows you to compile a proto file in the programming language of your interest it means if we want to generate the corresponding source code in PHP we must indicates the compiler that we want the generated code to be PHP the compiler uses two arguments the first argument indicates the programming language and the destination folder of the source code to be generated the second parameter indicates the proto file we want to compile we place the generic source codes in the respective products to folders we previously created now we have a look at the source code generated in the PHP language we continue with the generation of the source code in C++ following the same procedure we did for the PHP language now we'll have a look at the source code generated in a C++ language unfortunately the protocol buffers compiler currently is not able to generate code directly in rust however the compiler has an option to indicate what guidelines must follow in order to generate source code in other programming languages there is a library directory in class that provides a fusion to run a protocol buffers compiler to generate code in rust next we will implement an application in rust that allow us to combine multiple buffers to generate the code in rust now we create the subfolder protocols minnows crushed minerals click inside the source / drast folder next we create the file cargo dot T of M L where we will define the manifest of the package to implement apart from the name M version of the package we also indicate the Tappan Zee needed breast is a high-level programming language that let you make software with a performance similar to support made in love level languages such as C or C++ the main feature of rust is the way the memory is managed so the life cycle of any variable or memory location in your program must be clearly defined and must follow specific guidelines that quarantine a safe usage in runtime that is why the problems made in Russia don't need a garbage collector now let's start to implement the package courtesy miners' trust mineral screen in the main dot RS file [Music] you [Music] [Music] I would like to point out that despite having the experience of having developed some applications in rest I am still too far off mastering this programming language that is why during this implementation in rust you will see compilation errors and some bug fixes I strongly encourage you to first write in a specialized book and practice a lot if you want to 11 crushed in death [Music] [Music] [Music] [Music] [Music] you [Music] you you you [Music] [Music] [Music] [Music] you [Music] once we finished implementation of our protocol buffers compiler for rust we will proceed to generate the source code now we'll have a look at the source code generated in rust [Music] the next thing to do is to create a simple make file to easily compile the company profile in the three predominant languages if in the future the structure of the company message needs a day we can easily compile it in just one step [Music] [Music] [Music] [Music] you [Music] you [Music] now it's time to save all the changes we made so far we initialized the git repository locally create the eat ignore file and make the first commit [Music] [Music] now that we have compacted our protocol buffer we will begin with a practical exercise to learn how to serialize and deserialize messages we will define a JSON object with the data of the company Microsoft and save it in a file later we will implement the product to partner Jason and serialized each data using a proto buffer that we will save in a banana file we will appreciate the difference between the size of the JSON file and the perturb alpha above all we will observe how easy it is to simulate data using or the VAR buffers [Music] in the Egyptian document we will include an image in JPEG format of the reverse of law this will be possible by encoding the image in base64 [Music] [Music] [Music] now let's copy embeds then go to the image in the field law of the JSON document now we will implement the Super's plus program that will get the data from the JSON file and serialize it in a brothel buffer we start by creating the empty file create underscore for Tabata dot CPP and the folder filled parties where we will include all the dependencies we need [Music] to the gold image of the company we can use any C++ library that handles basic ste for encodings I usually use the implementation provided by the developer Romanian error which consists of a single header file and a single implementation file [Music] [Music] clone this repository inside the field parties follow [Music] in order to pass the JSON the woman I have used the well-known reputation library [Music] one of the features of trepidation is that header files also include the implementation [Music] clone the repository inside the field parties father first create the makefile to compile the burden any dependencies [Music] [Music] and now let's start implementing the problem create underscore of the buffer we include the colors of the dependencies you you the program will be executed from the command line by passing two arguments the first our woman is used to specify the input file that contains the JSON document and the second argument is used to specify the output file where the serialized proto buffer will be safe [Music] [Music] we will load the content of the JSON file in a memory buffer in this time I want to clarify that my priority in this tutorial is to simplify the code as much as possible having said that you should take care of the memory management to avoid segmentation faults or memory leaks [Music] [Music] we create a message of type company and fill its fields using the available sensors in the code that we have previously generated when compiling the protocol buffer [Music] [Music] [Music] you [Music] [Music] [Music] we will feel the lower film by decoding the image in base64 that recording its original binary encoding [Music] next we will feel the creative field converting the data string to the UNIX timestamp format [Music] finally we serialize and save the proto buffer to the output file specify it in the program argument [Music] [Music] we can buy the problem with the common make create underscore pratap affair and fix all the bugs [Music] [Music] once the program has been compiled we will execute it indicating in the first woman the file containing the JSON with the data of Microsoft and in the second argument we indicate the file name where we will save the generated proto buffer [Music] [Music] if we compare the JSON file with the file containing the birth of affair we observe a difference in the size of both although the usage size of the proto buffer is the most remarkable feature it is important to say that this bit of serializing and deserializing proto buffers is far superior compared to that of traditional structured formats this is ideal to be used in high-performance and by Romans before going to the next part of this tutorial we will make a new commit to save all the changes we made so far [Music] in the previous part of this tutorial we have implemented the serialization of the data of a company using protocol buffers in this final part we will do the opposite we will serialize the proto buffer will have generated and save it in a binary file and then we will show the data on the screen and save the lower in a file taking advantage of the fact that we have just implemented this visualization in C++ we will start the first example of the serialization using the same programming language let's create the file grid underscore rota buffer dot CPP you [Music] the program will get through easier moments the file containing the protobuf I would cellulite the data of a company we create a message of type company and initialize it by this realizing the protocol file stored in the specified file [Music] we display the data on the screen using the by level guitars in the code you [Music] finally we save the content of the lava field in heaven and file name it lot jpg you now we will modify the makefile in order to compile the program [Music] compile the program with a common make read and the score Prada waffle once the program has been compiled we will run it indicating in the first third woman the Venera file containing the proto buffer of the company we have generated in the previous part of this tutorial engage of success it will show on the screen the date of the microsoft company and the file level dot jpg should have been created let's check that the data shown is correct comparing the displayed values with the values we used in the JSON file [Music] before going to the implementation in PHP we will make a new comment to save the latest changes [Music] [Music] [Music] now we start with implementation in PHP to deserialize the proto buffer we will follow the exact same procedure however the implementation in PHP is a bit more tricky we will have to install and include the dependency google / / towpath in order to install this dependency we first install the composer tool in our computer composer is a dependency manager for PHP installing composer on Mac OS is not trivial and it requires following the steps I show in the video [Music] [Music] [Music] [Music] you [Music] [Music] once installed we make sure that it has been properly installed just by executing the common composer Minos Minos version the current latest version is 1.10.10 the we will install the dependency by executing the common composer require google / / Tawaf [Music] [Music] [Music] you let's start with implementation by creating the file grid underscore or the buffer dot PHP [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] you you [Music] you you you as you can see the code of this program is similar to the code we previously made in C++ PHP is an interpreted programming language it means we can skip the compilation and run the program with the common PHP followed by the file read underscore protobuf a PHP and the file name of the photo buffer we fix the box and retry it again until it works properly finally we see how the data is shown on the screen and the file logo dot jpg is generated we ensure again that the file logo dot jpg has been created properly opening it with the image preview finally we continue with the implementation in rust inside the suits / drafts folder we create a subfolder named grid minnows or the buffer and inside it we define the package great Minos proto buffer in the file cargo dot t-- o-- ml [Music] you [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] let's compile the program with the common cargo build we fix their errors and retire again until the program compiles [Music] you once the product has been compiled we will execute it indicating in the first a woman that we nerdy file with the proto buffers generated in the previous part of this tutorial finally we see how the data is also shown on the screen and the file logo dot jpg is generated we ensure again that the file logo dot jpg has been created properly opening it with the image Preble and finally to conclude this tutorial we will make the last commit to save the latest changes in the code [Music] [Music]
Info
Channel: AlbiaSoft
Views: 2,992
Rating: undefined out of 5
Keywords: protocol buffers, protobuffers, protoc, C++, PHP, Rust, programming, software programming, software development
Id: sm9IXMAZrPk
Channel Id: undefined
Length: 79min 40sec (4780 seconds)
Published: Sun Apr 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.