Class DefaultTripStream
java.lang.Object
org.tentackle.trip.impl.DefaultTripStream
- All Implemented Interfaces:
TripStream
Default implementation of a
TripStream.
As it doesn't make sense that multiple threads read from or write to the same stream concurrently, streams are not thread-safe and attaching streams to threads must be synchronized externally.
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultTripStream(Dictionary dictionary, Serializer serializer, Deserializer deserializer) Creates a new stream. -
Method Summary
Modifier and TypeMethodDescription<T> voidaddReference(Reference<T> reference, T object, boolean deduplicate) Adds a reference to the stream.
The method is invoked by the receiver to add an object received from the sender.voidclear()Clears the stream's state.
The sender and receiver should invoke this method to mark the boundary between messages.voidClears all references that belong to modifiable objects.
Invoked in the middle of a roundtrip, i.e., at the client side, after the request was sent to the server and at the server side, before the response is sent back to the client.
This keeps the references of immutable objects on both sides and reduces the size of the response since for those objects, only the reference number is sent back without the referenced value.TripType<?> Derives the concrete type from the given OpCode.
Same asTripStream.deriveType(OpCode)but reads the opcode.TripType<?> deriveType(OpCode opCode) Derives the concrete type from the given OpCode.
If the returned derived type is dictionary-based, the next opcode must be read from the stream, which is either a reference or an ordinal.Class<?> Gets the class for the given class name.Gets the number of object references per class.
Provided for debugging purposes only!Gets the deserializer.Gets the dictionary.getDictionaryEntry(int ordinal) Returns the dictionary entry for the given ordinal.
Throws aTripRuntimeExceptionif the ordinal is invalid.getDictionaryEntry(Class<?> clazz) Returns the entry for the given class.<T> Reference<T> getReference(Class<T> clazz, int referenceId) Gets the reference for the given reference id.<T> Reference<T> getReference(T object, boolean deduplicate) Gets an existing or creates a new reference for an object.
If de-duplicatable, the reference will be mapped by the object's equals() method.Gets the serializer.<T> TripType<T> Gets the TripType for the given class.
If there is a predefined or application-specific TripType, that is returned.intreadComponentOrdinal(OpCode opCode) Reads a component ordinal from the stream.intreadDictionaryOrdinal(OpCode opCode) Reads a dictionary ordinal from the stream.intreadNextComponentOrdinal(int lastOrdinal, OpCode opCode) Reads the next ordinal from the stream, using optimization.Receives the dictionary entry and adds it to the dictionary.
Same asTripStream.receiveDictionary(OpCode)but reads the opcode.receiveDictionary(OpCode opCode) Receives the dictionary entry and adds it to the dictionary.Receives the next OpCode from the stream.voidsendDictionary(Class<?> clazz) Updates the dictionary entry if the class is new for the stream and sends the class definition.
Otherwise, sends the ordinal of the dictionary entry.voidSends a TRIP error.
The error opcode is sent first, followed by the throwable's stack trace.voidwriteComponentOrdinal(int ordinal) Writes a component ordinal to the stream.voidwriteDictionaryOrdinal(int ordinal) Writes a dictionary ordinal to the stream.intwriteNextComponentOrdinal(int lastOrdinal, TripComponent<?, ?> component) Writes the next ordinal to the stream, using optimization.
-
Constructor Details
-
DefaultTripStream
Creates a new stream.- Parameters:
dictionary- the dictionary to useserializer- the serializer to usedeserializer- the deserializer to use
-
-
Method Details
-
getDictionary
Description copied from interface:TripStreamGets the dictionary.- Specified by:
getDictionaryin interfaceTripStream- Returns:
- the dictionary
-
getDictionaryEntry
Description copied from interface:TripStreamReturns the dictionary entry for the given ordinal.
Throws aTripRuntimeExceptionif the ordinal is invalid.- Specified by:
getDictionaryEntryin interfaceTripStream- Parameters:
ordinal- the ordinal of the entry- Returns:
- the entry
-
getDictionaryEntry
Description copied from interface:TripStreamReturns the entry for the given class.- Specified by:
getDictionaryEntryin interfaceTripStream- Parameters:
clazz- the class of the entry- Returns:
- the entry, null if no such entry exists
-
getType
Description copied from interface:TripStreamGets the TripType for the given class.
If there is a predefined or application-specific TripType, that is returned. Otherwise, the type will be created dynamically by the best matching TripTypeBuilder.Throws
TripRuntimeExceptionif no matching TripType could be found.- Specified by:
getTypein interfaceTripStream- Type Parameters:
T- the data type- Parameters:
clazz- the class of the data type- Returns:
- the trip type, never null
-
getSerializer
Description copied from interface:TripStreamGets the serializer.- Specified by:
getSerializerin interfaceTripStream- Returns:
- the serializer
-
getDeserializer
Description copied from interface:TripStreamGets the deserializer.- Specified by:
getDeserializerin interfaceTripStream- Returns:
- the deserializer
-
getReference
Description copied from interface:TripStreamGets an existing or creates a new reference for an object.
If de-duplicatable, the reference will be mapped by the object's equals() method. Otherwise, the reference will be mapped by an IdentityHashMap.- Specified by:
getReferencein interfaceTripStream- Parameters:
object- the object to checkdeduplicate- true if the object is de-duplicatable, false otherwise- Returns:
- the reference, never null
- See Also:
-
getReference
Description copied from interface:TripStreamGets the reference for the given reference id.Throws a
TripRuntimeExceptionif the reference id is invalid.- Specified by:
getReferencein interfaceTripStream- Type Parameters:
T- the reference type- Parameters:
clazz- the referenced object's classreferenceId- the reference id- Returns:
- the reference, never null
-
addReference
Description copied from interface:TripStreamAdds a reference to the stream.
The method is invoked by the receiver to add an object received from the sender.- Specified by:
addReferencein interfaceTripStream- Parameters:
reference- the reference to addobject- the object the reference belongs todeduplicate- true if the object is de-duplicatable, false otherwise
-
getClass
Description copied from interface:TripStreamGets the class for the given class name.- Specified by:
getClassin interfaceTripStream- Parameters:
className- the class name- Returns:
- the class, never null
-
clear
public void clear()Description copied from interface:TripStreamClears the stream's state.
The sender and receiver should invoke this method to mark the boundary between messages.- Specified by:
clearin interfaceTripStream
-
clearModifiable
public void clearModifiable()Description copied from interface:TripStreamClears all references that belong to modifiable objects.
Invoked in the middle of a roundtrip, i.e., at the client side, after the request was sent to the server and at the server side, before the response is sent back to the client.
This keeps the references of immutable objects on both sides and reduces the size of the response since for those objects, only the reference number is sent back without the referenced value.- Specified by:
clearModifiablein interfaceTripStream
-
writeComponentOrdinal
public void writeComponentOrdinal(int ordinal) Description copied from interface:TripStreamWrites a component ordinal to the stream.- Specified by:
writeComponentOrdinalin interfaceTripStream- Parameters:
ordinal- the ordinal to write
-
readComponentOrdinal
Description copied from interface:TripStreamReads a component ordinal from the stream.- Specified by:
readComponentOrdinalin interfaceTripStream- Parameters:
opCode- the ordinal OpCode- Returns:
- the ordinal
-
writeNextComponentOrdinal
Description copied from interface:TripStreamWrites the next ordinal to the stream, using optimization.- Specified by:
writeNextComponentOrdinalin interfaceTripStream- Parameters:
lastOrdinal- the last ordinal written, -1 for nonecomponent- the component to write the ordinal for- Returns:
- the component ordinal
-
readNextComponentOrdinal
Description copied from interface:TripStreamReads the next ordinal from the stream, using optimization.- Specified by:
readNextComponentOrdinalin interfaceTripStream- Parameters:
lastOrdinal- the last ordinal read, -1 for noneopCode- the ordinal OpCode- Returns:
- the ordinal
-
writeDictionaryOrdinal
public void writeDictionaryOrdinal(int ordinal) Description copied from interface:TripStreamWrites a dictionary ordinal to the stream.- Specified by:
writeDictionaryOrdinalin interfaceTripStream- Parameters:
ordinal- the ordinal to write
-
readDictionaryOrdinal
Description copied from interface:TripStreamReads a dictionary ordinal from the stream.- Specified by:
readDictionaryOrdinalin interfaceTripStream- Parameters:
opCode- the ordinal OpCode- Returns:
- the ordinal
-
sendDictionary
Description copied from interface:TripStreamUpdates the dictionary entry if the class is new for the stream and sends the class definition.
Otherwise, sends the ordinal of the dictionary entry.- Specified by:
sendDictionaryin interfaceTripStream- Parameters:
clazz- the object's class
-
receiveDictionary
Description copied from interface:TripStreamReceives the dictionary entry and adds it to the dictionary.- Specified by:
receiveDictionaryin interfaceTripStream- Parameters:
opCode- the opcode to read (already read from the stream)- Returns:
- the dictionary entry
-
receiveDictionary
Description copied from interface:TripStreamReceives the dictionary entry and adds it to the dictionary.
Same asTripStream.receiveDictionary(OpCode)but reads the opcode.- Specified by:
receiveDictionaryin interfaceTripStream- Returns:
- the dictionary entry
-
deriveType
Description copied from interface:TripStreamDerives the concrete type from the given OpCode.
If the returned derived type is dictionary-based, the next opcode must be read from the stream, which is either a reference or an ordinal.- Specified by:
deriveTypein interfaceTripStream- Parameters:
opCode- the opcode (already read from the stream)- Returns:
- the type
-
deriveType
Description copied from interface:TripStreamDerives the concrete type from the given OpCode.
Same asTripStream.deriveType(OpCode)but reads the opcode.- Specified by:
deriveTypein interfaceTripStream- Returns:
- the type
-
receiveOpCode
Description copied from interface:TripStreamReceives the next OpCode from the stream.Checks for the
- Specified by:
receiveOpCodein interfaceTripStream- Returns:
- the OpCode
-
sendError
Description copied from interface:TripStreamSends a TRIP error.
The error opcode is sent first, followed by the throwable's stack trace.- Specified by:
sendErrorin interfaceTripStream- Parameters:
errorOpCode- the error opcodet- the throwable
-
getClassCount
-