Akka Protobuf Serialization Example

This page provides Scala code examples for akka.serialization. class InstitutionProtobufSerializer extends SerializerWithStringManifest override def. Akka makes use of serialization when messages leave the JVM boundaries. This can happen in mainly two scenarios: sending messages over the network when using Akka Cluster (do not use Akka Remote directly) or using Akka Persistence. Now here’s the catch: the default serialization technology configured in Akka is nothing but the infamous Java serialization, - Read More.
usingSystem; |
usingAkka.Actor; |
usingAkka.Configuration; |
usingAkka.Serialization.Protobuf; |
usingProtoBuf; |
namespaceApp |
{ |
/// <summary> |
/// Interface used to recognize messages to be serialized by <seecref='ProtobufSerializer'/>. |
/// </summary> |
publicinterfaceIProto { } |
[ProtoContract] |
publicsealedclassTestMessage : IProto |
{ |
[ProtoMember(1)] |
publicintId { get; } |
[ProtoMember(2)] |
publicstringBody { get; } |
// Protobuf-net requires class to provide parameterless constructor |
// however it may be private. |
privateTestMessage() { } |
publicTestMessage(intid, stringbody) |
{ |
Id=id; |
Body=body; |
} |
} |
classProgram |
{ |
staticvoidMain(string[] args) |
{ |
// configure serializer via HOCON |
varconfig=ConfigurationFactory.ParseString(@' |
akka.actor { |
serializers.protobuf = 'Akka.Serialization.Protobuf.ProtobufSerializer, Akka.Serialization.Protobuf' |
serialization-bindings { |
'App.IProto, App' = protobuf |
} |
}'); |
using (varsystem=ActorSystem.Create('system', config)) |
{ |
varmsg=newTestMessage(123, 'Hello world!'); |
varserializer=system.Serialization.FindSerializerForType(msg.GetType()); |
varbin=serializer.ToBinary(msg); |
vardes=serializer.FromBinary(bin, typeof(TestMessage)); |
} |
} |
} |
} |
usingSystem; |
usingSystem.IO; |
usingAkka.Actor; |
namespaceAkka.Serialization.Protobuf |
{ |
/// <summary> |
/// Generic Google Protocol Buffers serializer for Akka.NET using Marc Gravell protobuf-net. |
/// </summary> |
publicsealedclassProtobufSerializer : Serializer |
{ |
publicProtobufSerializer(ExtendedActorSystemsystem) : base(system) |
{ |
} |
publicoverridebyte[] ToBinary(objectobj) |
{ |
using (varstream=newMemoryStream()) |
{ |
ProtoBuf.Serializer.Serialize(stream, obj); |
returnstream.ToArray(); |
} |
} |
publicoverrideobjectFromBinary(byte[] bytes, Typetype) |
{ |
using (varstream=newMemoryStream(bytes)) |
{ |
returnProtoBuf.Serializer.Deserialize(type, stream); |
} |
} |
publicoverrideboolIncludeManifest=>false; |
publicoverrideintIdentifier=>127; |
} |
} |
Protobuf (short for protocolbuffers) is astructured data serialization method developed by Google. Thisextensible format, which is both language- and platform-neutral, isused extensively within Google’s infrastructure, where it isconsidered the 'lingua franca' for data.
Protobuf is gaining momentum among developers and is being used morewidely within open source software projects.

Bluetera II is one of the first open source platforms to haveadopted Protobuf as a medium for seamless information exchange betweenhardware and software.
The Problem With BLE-Based Applications
In many Bluetooth Low-Energy (BLE) applications, adding newfunctionality means either:
- Adding new BLE Characteristics, or
- Changing a proprietary binary structure using some service along thelines of UART-over-BLE.
Based on our experience, both options face significant limitations.
Adding BLE Characteristics is usually a cumbersome, error-proneprocedure, whereas binary structures are unreadable and hard tomaintain. Furthermore, both methods often require changes to theentire SDK (firmware, mobile software, cloud infrastructure, etc.),which breaks backward-compatibility and complicates futuredevelopment.
Colt Semi -Automatic Pistols top. Ace.22 Ace Service.22 Challenger, Huntsman & Targetsman.22 Combat Commander Gold Cup N.M.38 Gold Cup N.M.45 Junior Colt.22 Junior Colt.25 Junior Colt.22,.25 & Colt Auto Cal.25 from 1970 Kit.38 Special (.38 Special Caliber) Kit.38 Special (.45 ACP) Kit Conversion.45 -.22 Kit Conversion.22 -.45 Model 1900. When you release the catch and open the cylinder you should see the serial number in two places,on the frame at the swivel & opposite it where the cylinder is attached to the frame. Take Colt revolver serial numbers for example: SA is Single Action, NF is New Frontier, You could see the same thing on others: SP is Sporter, MT is Match Target, Additionally, Colt also uses the characters in certain situations to illustrate the year of production. Find Serial Number Enter the serial number, without spaces or dashes, to search the database. If multiple models appear for your serial number simply match the date with the appropriate model, as certain vintage firearms can share serial numbers between different models.
Protobuf to the Rescue!
Bluetera uses Protobuf to streamline messages between the BLE moduleand the BLE SDK. This has some nice benefits:
- Language-neutrality - You simply define the schemafile. Protobuf scripts then auto-generate boilerplate code for yourlanguage of choice.
- Platform-neutrality - No more messing about withlittle/big/medium endianness, no more worrying about how toserialize and deserialize a 'double' properly, etc. The frameworktakes care of all that.
- Compatibility - All changes are backward- andforward-compatible. You can add new fields without changing anythingin legacy code.
- Self-delimiting - You no longer have to worry about delimitingconsecutive messages (or even knowing the length of thestructure). The serialization layer takes care of that.
- Efficient - While not a binary format, Protobuf goes to greatlength to encode data efficiently.
Protobuf 'Hello World' - Control Bluetera II’s LEDs
This simple example presents a Desktop WPF application to turnBluetera’s LEDs on and off. Notice how simple it is to add the newmessages. There is very little boilerplate, and most of the code ispure business logic.
The stages are:
- Add a new LED-command to the schema file
- Add a button and a handler to the PC application to send the newcommand
- Add a handler to Bluetera's firmware to actually do the LED on/offwork
You can find the sources for this example in the led-demo
branch ofour GitHub repository. To build andrun this example, you will need toDownloadand install Protobuf.
Step 1 - Modify bluetera_messages.proto
Clone the Bluetera firmwarerepository
Locate
bluetera_messages.proto
and add the following messages:
Step 2 - Add a Button to the PC Application
Clone the Bluetera Windows repository
Copy the
bluetera_messages.proto
file to theprotobuf
folder andgenerate the C# classes by runningsource/gen-messages.bat
. Thiswill generate all necessary boilerplate code (such as theLedCommand
class). Panda antivirus 2011 trial reset.Add a ‘ToggleLed’ button to
MainWindow.xaml
Add the following code to
MainWindow.xaml.cs
Step 3 - Add the Firmware Handler
Generate the boilerplate C code by running
application/messages/gen-messages.bat
. This will create thenecessary types to handle the new message(BLUETERA_UPLINK_MESSAGE_LED_TAG
,bluetera_led_command_t
, etc.)Modify
bluetera_uplink_message_handler()
inmain.c
to handle thenew command:
- Build and update your Bluetera II!
Summary
Protobuf is Google’s way of streamlining data serialization. Mostcurrent applications of the framework aresoftware-specific. Bluetera II is one of the first platforms toadopt this powerful technology as a way of handling interactionsbetween hardware and software.
We hope the LED example above illustrates how Bluetera’s usage ofProtobuf can help you:
- Save a great deal of time,
- Simplify your code,
- Lower the barrier to changes and enhancements, and
- Streamline development in a way that was not previously possible.
Meanwhile, please send us your questions, feedback, and suggestions!
- суббота 28 марта
- 27