Package anbxj
Class Channel_ProtobufSerializer
java.lang.Object
anbxj.Channel_ProtobufSerializer
- All Implemented Interfaces:
Channel_MessageSerializer
A
Channel_MessageSerializer that uses step-specific descriptors
to convert Java objects (e.g., AnBx_WireFormats.SignedMessage,
AnBx_WireFormats.SealedMessage) into typed Protobuf messages
and back.
This serializer is intended for the ProtoBuf serialization
mode (typed Protobuf). For each protocol step, a Channel_ProtobufSerializer.StepDescriptor
must be provided that knows the exact Protobuf message type for that step.
The mapping from step number to its descriptor is injected at construction time.
The serialization process follows two directions:
- Outgoing: The Java object (as produced by the role code) is
converted to a Protobuf message via
Channel_ProtobufSerializer.StepDescriptor.toProtobuf(Object), then serialised to bytes. - Incoming: The raw bytes are parsed by
Channel_ProtobufSerializer.StepDescriptor.parseFrom(byte[])and then transformed back to a Java object viaChannel_ProtobufSerializer.StepDescriptor.toJavaObject(Message).
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAChannel_ProtobufSerializer.StepDescriptorimplementation that wraps a Protobuf message containing an opaquebytespayload.static interfaceDescriptor that handles a single protocol step.static interfaceExtension ofChannel_ProtobufSerializer.StepDescriptorfor steps that carry a user-defined payload consisting of multiple fields. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new typed Protobuf serializer. -
Method Summary
Modifier and TypeMethodDescriptiondeserialize(byte[] data, int step) Deserializes a byte array back into a logical protocol message object.byte[]Serializes a logical protocol message into a byte array.voidsetSession(AnB_Session session) Injects the currentAnB_Sessioninto this serializer.
-
Constructor Details
-
Channel_ProtobufSerializer
public Channel_ProtobufSerializer(Map<Integer, Channel_ProtobufSerializer.StepDescriptor> descriptors) Creates a new typed Protobuf serializer.- Parameters:
descriptors- a map from step number (1-based) to the descriptor that handles that step; must not benull- Throws:
NullPointerException- ifdescriptorsisnull
-
-
Method Details
-
setSession
Description copied from interface:Channel_MessageSerializerInjects the currentAnB_Sessioninto this serializer.The default implementation does nothing. Subclasses that require access to the session (e.g., for cryptographic operations during field extraction) should override this method and store the reference.
- Specified by:
setSessionin interfaceChannel_MessageSerializer- Parameters:
session- the current session (must not benull)
-
serialize
Serializes a logical protocol message into a byte array.- Specified by:
serializein interfaceChannel_MessageSerializer- Parameters:
message- the object to send; the concrete type depends on the protocol step (e.g.,String,Crypto_SealedPair,Object[]for tuples)step- the protocol step number (starting from 1), used to identify the message format- Returns:
- the serialized bytes, ready to be written to the network
- Throws:
IllegalArgumentException- if no descriptor is registered forstepIOException- if serialization fails
-
deserialize
Deserializes a byte array back into a logical protocol message object.- Specified by:
deserializein interfaceChannel_MessageSerializer- Parameters:
data- the raw bytes received from the networkstep- the expected protocol step number, used to identify the message format and recreate the appropriate object- Returns:
- the reconstructed protocol object (e.g.,
String,Crypto_SealedPair,Object[]) - Throws:
IllegalArgumentException- if no descriptor is registered forstepIOException- if deserialization failsClassNotFoundException- if a required class cannot be found (may occur when internal Java serialization is used inside cryptographic wrappers)
-