Class DefaultTripStream
- All Implemented Interfaces:
TripStream
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.
The hash guard (see enterHashed()) applies to every stream, whether the peer is authenticated
or not. It can be disabled via setHashGuardEnabled(boolean) or the system property
"tentackle.trip.disableHashGuard".
The allocation budget (see checkAllocation(long)) is unlimited for a new stream. Servers apply
the default limit, which can be changed via
setDefaultAllocationLimit(long) or the system property "tentackle.trip.maxAllocation".
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringSystem property to change the default allocation limit in bytes, 0 if unlimited.static final longDefault maximum number of bytes allocated ahead of their data per message (256 MiB).static final StringSystem property to disable the hash guard. -
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.voidcheckAllocation(long bytes) Accounts for memory allocated ahead of the data it is filled with, such as an array.
Must be invoked before the memory is allocated.voidcheckSize(int size) Checks the number of elements of an array, collection or map read from the stream.
Invoked before anything is allocated for the elements.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.voidEnters a value the receiving container will hash while reading, such as an element of a hash set or the key of a hash map.
Must be paired withTripStream.leaveHashed()in afinallyblock.voidEnters the contents of an object, array, collection or map while reading.
Must be paired withTripStream.leaveNested()in afinallyblock.longGets the maximum number of bytes allocated ahead of their data per message.Class<?> Gets the class for the given class name.Gets the number of object references per class.
Provided for debugging purposes only!static longGets the allocation limit servers apply to the messages they receive.Gets the deserializer.Gets the dictionary.getDictionaryEntry(int ordinal) Returns the dictionary entry for the given ordinal.getDictionaryEntry(Class<?> clazz) Returns the entry for the given class.Gets the filter deciding which components of an object are serialized.Gets the policy restricting what is read from a client that is not authenticated yet.<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.static booleanReturns whether back-references within hashed values are restricted (seeisHashShareable(Object)).protected booleanisHashShareable(Object object) Returns whether an object received before may be referenced again within a hashed value.
Besides theshareablevalues, these are the objects whose hash code does not depend on other objects: arrays, objects inheritinghashCodeandequalsfromObjectand objects annotated withTripShallowHash.static booleanisShallowHashed(Object object) Returns whether the hash code of an object does not depend on the objects it refers to.
This is the case if its class is annotated withTripShallowHash, or, for a dynamic proxy, the class of its invocation handler.protected booleanisShareable(Object object) Returns whether an object received before may be referenced again by a client that is not authenticated yet.
Only immutable values whose hash code does not depend on other objects may be shared.voidLeaves a value the receiving container will hash while reading.voidLeaves the contents of an object, array, collection or map while reading.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.voidresumeHashed(int hashed) Resumes the hashed values suspended byTripStream.suspendHashed().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.voidsetAllocationLimit(long bytes) Sets the maximum number of bytes allocated ahead of their data per message.
Not reset byTripStream.clear().static voidsetDefaultAllocationLimit(long bytes) Sets the allocation limit servers apply to the messages they receive.
Applies to connections opened afterward.voidsetFilter(TripFilter filter) Sets the filter deciding which components of an object are serialized.
The filter applies to writing only and is not reset byTripStream.clear().static voidsetHashGuardEnabled(boolean enabled) Enables or disables the hash guard for all streams.voidsetReadPolicy(PreAuthenticationPolicy readPolicy) Sets the policy restricting what is read from a client that is not authenticated yet.
The policy applies to reading only and is not reset byTripStream.clear(), which, however, starts a new message as far as the policy's size limit is concerned.intSuspends the hashed values entered so far while reading.
Used while reading the contents of an object whose hash code does not depend on them (seeTripShallowHash).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.
-
Field Details
-
DISABLE_HASH_GUARD_PROPERTY
System property to disable the hash guard.- See Also:
-
ALLOCATION_LIMIT_PROPERTY
System property to change the default allocation limit in bytes, 0 if unlimited.- See Also:
-
DEFAULT_ALLOCATION_LIMIT
public static final long DEFAULT_ALLOCATION_LIMITDefault maximum number of bytes allocated ahead of their data per message (256 MiB).- See Also:
-
-
Constructor Details
-
DefaultTripStream
Creates a new stream.- Parameters:
dictionary- the dictionary to useserializer- the serializer to usedeserializer- the deserializer to use
-
-
Method Details
-
isShallowHashed
Returns whether the hash code of an object does not depend on the objects it refers to.
This is the case if its class is annotated withTripShallowHash, or, for a dynamic proxy, the class of its invocation handler.- Parameters:
object- the object, null if none- Returns:
- true if shallow hashed
-
isHashGuardEnabled
public static boolean isHashGuardEnabled()Returns whether back-references within hashed values are restricted (seeisHashShareable(Object)).- Returns:
- true if enabled (default)
- See Also:
-
setHashGuardEnabled
public static void setHashGuardEnabled(boolean enabled) Enables or disables the hash guard for all streams.Disable it only if the application really shares mutable objects within the elements of hash sets or the keys of hash maps, and only if all peers are trusted.
- Parameters:
enabled- true to enable (default), false to disable- See Also:
-
getDefaultAllocationLimit
public static long getDefaultAllocationLimit()Gets the allocation limit servers apply to the messages they receive.- Returns:
- the maximum number of bytes allocated ahead of their data per message, 0 if unlimited
- See Also:
-
setDefaultAllocationLimit
public static void setDefaultAllocationLimit(long bytes) Sets the allocation limit servers apply to the messages they receive.
Applies to connections opened afterward.The limit must allow for the largest arrays a single remote call of the application legitimately sends. A sparse 1000 x 1000
doublematrix, for example, takes about 8 MiB.- Parameters:
bytes- the maximum number of bytes allocated ahead of their data per message, 0 if unlimited- See Also:
-
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.- 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.- 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- Type Parameters:
T- the object's type- 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.- 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- Type Parameters:
T- the object's type- 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
-
setFilter
Description copied from interface:TripStreamSets the filter deciding which components of an object are serialized.
The filter applies to writing only and is not reset byTripStream.clear(). Since the stream is usually shared, a filter set for a single object or message should be removed afterward, preferably in afinallyblock.- Specified by:
setFilterin interfaceTripStream- Parameters:
filter- the filter, null to serialize all components (default)
-
getFilter
Description copied from interface:TripStreamGets the filter deciding which components of an object are serialized.- Specified by:
getFilterin interfaceTripStream- Returns:
- the filter, null if all components are serialized
-
setReadPolicy
Description copied from interface:TripStreamSets the policy restricting what is read from a client that is not authenticated yet.
The policy applies to reading only and is not reset byTripStream.clear(), which, however, starts a new message as far as the policy's size limit is concerned.- Specified by:
setReadPolicyin interfaceTripStream- Parameters:
readPolicy- the policy, null if no restrictions apply (default)
-
getReadPolicy
Description copied from interface:TripStreamGets the policy restricting what is read from a client that is not authenticated yet.- Specified by:
getReadPolicyin interfaceTripStream- Returns:
- the policy, null if no restrictions apply
-
checkSize
public void checkSize(int size) Description copied from interface:TripStreamChecks the number of elements of an array, collection or map read from the stream.
Invoked before anything is allocated for the elements. Does nothing if there is no read policy.- Specified by:
checkSizein interfaceTripStream- Parameters:
size- the number of elements
-
checkAllocation
public void checkAllocation(long bytes) Description copied from interface:TripStreamAccounts for memory allocated ahead of the data it is filled with, such as an array.
Must be invoked before the memory is allocated. The amounts are summed up per message and reset byTripStream.clear().Arrays are allocated with the length announced by the sender, but filled with what follows. Since default values are skipped, a few bytes on the wire may allocate gigabytes. The budget limits the total per message, whatever the number of arrays.
- Specified by:
checkAllocationin interfaceTripStream- Parameters:
bytes- the estimated number of bytes allocated
-
setAllocationLimit
public void setAllocationLimit(long bytes) Description copied from interface:TripStreamSets the maximum number of bytes allocated ahead of their data per message.
Not reset byTripStream.clear(). The read policy, if any, takes precedence.- Specified by:
setAllocationLimitin interfaceTripStream- Parameters:
bytes- the maximum number of bytes, 0 if unlimited (default)- See Also:
-
getAllocationLimit
public long getAllocationLimit()Description copied from interface:TripStreamGets the maximum number of bytes allocated ahead of their data per message.- Specified by:
getAllocationLimitin interfaceTripStream- Returns:
- the maximum number of bytes, 0 if unlimited
-
enterNested
public void enterNested()Description copied from interface:TripStreamEnters the contents of an object, array, collection or map while reading.
Must be paired withTripStream.leaveNested()in afinallyblock. Checked against the maximum depth of the read policy, if any.- Specified by:
enterNestedin interfaceTripStream
-
leaveNested
public void leaveNested()Description copied from interface:TripStreamLeaves the contents of an object, array, collection or map while reading.- Specified by:
leaveNestedin interfaceTripStream- See Also:
-
enterHashed
public void enterHashed()Description copied from interface:TripStreamEnters a value the receiving container will hash while reading, such as an element of a hash set or the key of a hash map.
Must be paired withTripStream.leaveHashed()in afinallyblock. May be nested.Within a hashed value, back-references are refused unless they refer to an object whose hash code does not depend on other objects, such as a
String, a number, an enum, aUUID, an array, an object inheritinghashCodeandequalsfromObjector an object annotated withTripShallowHash. Otherwise, a few kilobytes of containers sharing each other make computing the hash code take forever, whatever the nesting depth. This applies to authenticated clients as well.- Specified by:
enterHashedin interfaceTripStream
-
leaveHashed
public void leaveHashed()Description copied from interface:TripStreamLeaves a value the receiving container will hash while reading.- Specified by:
leaveHashedin interfaceTripStream- See Also:
-
suspendHashed
public int suspendHashed()Description copied from interface:TripStreamSuspends the hashed values entered so far while reading.
Used while reading the contents of an object whose hash code does not depend on them (seeTripShallowHash). Hashed values entered within are guarded on their own.
Must be paired withTripStream.resumeHashed(int)in afinallyblock.- Specified by:
suspendHashedin interfaceTripStream- Returns:
- the state to pass to
TripStream.resumeHashed(int) - See Also:
-
resumeHashed
public void resumeHashed(int hashed) Description copied from interface:TripStreamResumes the hashed values suspended byTripStream.suspendHashed().- Specified by:
resumeHashedin interfaceTripStream- Parameters:
hashed- the state returned byTripStream.suspendHashed()
-
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.If the OpCode denotes an error, the remote stack trace is read and thrown as a
TripError.- 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
-