Class DefaultTripStream

java.lang.Object
org.tentackle.trip.impl.DefaultTripStream
All Implemented Interfaces:
TripStream

public class DefaultTripStream extends Object implements 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.

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

    Fields
    Modifier and Type
    Field
    Description
    static final String
    System property to change the default allocation limit in bytes, 0 if unlimited.
    static final long
    Default maximum number of bytes allocated ahead of their data per message (256 MiB).
    static final String
    System property to disable the hash guard.
  • Constructor Summary

    Constructors
    Constructor
    Description
    DefaultTripStream(Dictionary dictionary, Serializer serializer, Deserializer deserializer)
    Creates a new stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> void
    addReference(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.
    void
    checkAllocation(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.
    void
    checkSize(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.
    void
    Clears the stream's state.
    The sender and receiver should invoke this method to mark the boundary between messages.
    void
    Clears 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.
    Derives the concrete type from the given OpCode.
    Same as TripStream.deriveType(OpCode) but reads the 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.
    void
    Enters 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 with TripStream.leaveHashed() in a finally block.
    void
    Enters the contents of an object, array, collection or map while reading.
    Must be paired with TripStream.leaveNested() in a finally block.
    long
    Gets the maximum number of bytes allocated ahead of their data per message.
    getClass(String className)
    Gets the class for the given class name.
    Gets the number of object references per class.
    Provided for debugging purposes only!
    static long
    Gets 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.
    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>
    getType(Class<? extends T> clazz)
    Gets the TripType for the given class.
    If there is a predefined or application-specific TripType, that is returned.
    static boolean
    Returns whether back-references within hashed values are restricted (see isHashShareable(Object)).
    protected boolean
    Returns whether an object received before may be referenced again within a hashed value.
    Besides the shareable values, these are the objects whose hash code does not depend on other objects: arrays, objects inheriting hashCode and equals from Object and objects annotated with TripShallowHash.
    static boolean
    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 with TripShallowHash, or, for a dynamic proxy, the class of its invocation handler.
    protected boolean
    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.
    void
    Leaves a value the receiving container will hash while reading.
    void
    Leaves the contents of an object, array, collection or map while reading.
    int
    Reads a component ordinal from the stream.
    int
    Reads a dictionary ordinal from the stream.
    int
    readNextComponentOrdinal(int lastOrdinal, OpCode opCode)
    Reads the next ordinal from the stream, using optimization.
    Receives the dictionary entry and adds it to the dictionary.
    Same as TripStream.receiveDictionary(OpCode) but reads the opcode.
    Receives the dictionary entry and adds it to the dictionary.
    Receives the next OpCode from the stream.
    void
    resumeHashed(int hashed)
    Resumes the hashed values suspended by TripStream.suspendHashed().
    void
    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.
    void
    sendError(OpCode errorOpCode, Throwable t)
    Sends a TRIP error.
    The error opcode is sent first, followed by the throwable's stack trace.
    void
    setAllocationLimit(long bytes)
    Sets the maximum number of bytes allocated ahead of their data per message.
    Not reset by TripStream.clear().
    static void
    Sets the allocation limit servers apply to the messages they receive.
    Applies to connections opened afterward.
    void
    Sets the filter deciding which components of an object are serialized.
    The filter applies to writing only and is not reset by TripStream.clear().
    static void
    setHashGuardEnabled(boolean enabled)
    Enables or disables the hash guard for all streams.
    void
    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 by TripStream.clear(), which, however, starts a new message as far as the policy's size limit is concerned.
    int
    Suspends the hashed values entered so far while reading.
    Used while reading the contents of an object whose hash code does not depend on them (see TripShallowHash).
    void
    writeComponentOrdinal(int ordinal)
    Writes a component ordinal to the stream.
    void
    Writes a dictionary ordinal to the stream.
    int
    writeNextComponentOrdinal(int lastOrdinal, TripComponent<?,?> component)
    Writes the next ordinal to the stream, using optimization.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DISABLE_HASH_GUARD_PROPERTY

      public static final String DISABLE_HASH_GUARD_PROPERTY
      System property to disable the hash guard.
      See Also:
    • ALLOCATION_LIMIT_PROPERTY

      public static final String 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_LIMIT
      Default maximum number of bytes allocated ahead of their data per message (256 MiB).
      See Also:
  • Constructor Details

    • DefaultTripStream

      public DefaultTripStream(Dictionary dictionary, Serializer serializer, Deserializer deserializer)
      Creates a new stream.
      Parameters:
      dictionary - the dictionary to use
      serializer - the serializer to use
      deserializer - the deserializer to use
  • Method Details

    • isShallowHashed

      public static boolean isShallowHashed(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 with TripShallowHash, 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 (see isHashShareable(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 double matrix, 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

      public Dictionary getDictionary()
      Description copied from interface: TripStream
      Gets the dictionary.
      Specified by:
      getDictionary in interface TripStream
      Returns:
      the dictionary
    • getDictionaryEntry

      public DictionaryEntry getDictionaryEntry(int ordinal)
      Description copied from interface: TripStream
      Returns the dictionary entry for the given ordinal.
      Specified by:
      getDictionaryEntry in interface TripStream
      Parameters:
      ordinal - the ordinal of the entry
      Returns:
      the entry
    • getDictionaryEntry

      public DictionaryEntry getDictionaryEntry(Class<?> clazz)
      Description copied from interface: TripStream
      Returns the entry for the given class.
      Specified by:
      getDictionaryEntry in interface TripStream
      Parameters:
      clazz - the class of the entry
      Returns:
      the entry, null if no such entry exists
    • getType

      public <T> TripType<T> getType(Class<? extends T> clazz)
      Description copied from interface: TripStream
      Gets 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:
      getType in interface TripStream
      Type Parameters:
      T - the data type
      Parameters:
      clazz - the class of the data type
      Returns:
      the trip type, never null
    • getSerializer

      public Serializer getSerializer()
      Description copied from interface: TripStream
      Gets the serializer.
      Specified by:
      getSerializer in interface TripStream
      Returns:
      the serializer
    • getDeserializer

      public Deserializer getDeserializer()
      Description copied from interface: TripStream
      Gets the deserializer.
      Specified by:
      getDeserializer in interface TripStream
      Returns:
      the deserializer
    • getReference

      public <T> Reference<T> getReference(T object, boolean deduplicate)
      Description copied from interface: TripStream
      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. Otherwise, the reference will be mapped by an IdentityHashMap.
      Specified by:
      getReference in interface TripStream
      Type Parameters:
      T - the object's type
      Parameters:
      object - the object to check
      deduplicate - true if the object is de-duplicatable, false otherwise
      Returns:
      the reference, never null
      See Also:
    • getReference

      public <T> Reference<T> getReference(Class<T> clazz, int referenceId)
      Description copied from interface: TripStream
      Gets the reference for the given reference id.
      Specified by:
      getReference in interface TripStream
      Type Parameters:
      T - the reference type
      Parameters:
      clazz - the referenced object's class
      referenceId - the reference id
      Returns:
      the reference, never null
    • addReference

      public <T> void addReference(Reference<T> reference, T object, boolean deduplicate)
      Description copied from interface: TripStream
      Adds a reference to the stream.
      The method is invoked by the receiver to add an object received from the sender.
      Specified by:
      addReference in interface TripStream
      Type Parameters:
      T - the object's type
      Parameters:
      reference - the reference to add
      object - the object the reference belongs to
      deduplicate - true if the object is de-duplicatable, false otherwise
    • getClass

      public Class<?> getClass(String className)
      Description copied from interface: TripStream
      Gets the class for the given class name.
      Specified by:
      getClass in interface TripStream
      Parameters:
      className - the class name
      Returns:
      the class, never null
    • clear

      public void clear()
      Description copied from interface: TripStream
      Clears the stream's state.
      The sender and receiver should invoke this method to mark the boundary between messages.
      Specified by:
      clear in interface TripStream
    • clearModifiable

      public void clearModifiable()
      Description copied from interface: TripStream
      Clears 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:
      clearModifiable in interface TripStream
    • setFilter

      public void setFilter(TripFilter filter)
      Description copied from interface: TripStream
      Sets the filter deciding which components of an object are serialized.
      The filter applies to writing only and is not reset by TripStream.clear(). Since the stream is usually shared, a filter set for a single object or message should be removed afterward, preferably in a finally block.
      Specified by:
      setFilter in interface TripStream
      Parameters:
      filter - the filter, null to serialize all components (default)
    • getFilter

      public TripFilter getFilter()
      Description copied from interface: TripStream
      Gets the filter deciding which components of an object are serialized.
      Specified by:
      getFilter in interface TripStream
      Returns:
      the filter, null if all components are serialized
    • setReadPolicy

      public void setReadPolicy(PreAuthenticationPolicy readPolicy)
      Description copied from interface: TripStream
      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 by TripStream.clear(), which, however, starts a new message as far as the policy's size limit is concerned.
      Specified by:
      setReadPolicy in interface TripStream
      Parameters:
      readPolicy - the policy, null if no restrictions apply (default)
    • getReadPolicy

      public PreAuthenticationPolicy getReadPolicy()
      Description copied from interface: TripStream
      Gets the policy restricting what is read from a client that is not authenticated yet.
      Specified by:
      getReadPolicy in interface TripStream
      Returns:
      the policy, null if no restrictions apply
    • checkSize

      public void checkSize(int size)
      Description copied from interface: TripStream
      Checks 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:
      checkSize in interface TripStream
      Parameters:
      size - the number of elements
    • checkAllocation

      public void checkAllocation(long bytes)
      Description copied from interface: TripStream
      Accounts 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 by TripStream.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:
      checkAllocation in interface TripStream
      Parameters:
      bytes - the estimated number of bytes allocated
    • setAllocationLimit

      public void setAllocationLimit(long bytes)
      Description copied from interface: TripStream
      Sets the maximum number of bytes allocated ahead of their data per message.
      Not reset by TripStream.clear(). The read policy, if any, takes precedence.
      Specified by:
      setAllocationLimit in interface TripStream
      Parameters:
      bytes - the maximum number of bytes, 0 if unlimited (default)
      See Also:
    • getAllocationLimit

      public long getAllocationLimit()
      Description copied from interface: TripStream
      Gets the maximum number of bytes allocated ahead of their data per message.
      Specified by:
      getAllocationLimit in interface TripStream
      Returns:
      the maximum number of bytes, 0 if unlimited
    • enterNested

      public void enterNested()
      Description copied from interface: TripStream
      Enters the contents of an object, array, collection or map while reading.
      Must be paired with TripStream.leaveNested() in a finally block. Checked against the maximum depth of the read policy, if any.
      Specified by:
      enterNested in interface TripStream
    • leaveNested

      public void leaveNested()
      Description copied from interface: TripStream
      Leaves the contents of an object, array, collection or map while reading.
      Specified by:
      leaveNested in interface TripStream
      See Also:
    • enterHashed

      public void enterHashed()
      Description copied from interface: TripStream
      Enters 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 with TripStream.leaveHashed() in a finally block. 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, a UUID, an array, an object inheriting hashCode and equals from Object or an object annotated with TripShallowHash. 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:
      enterHashed in interface TripStream
    • leaveHashed

      public void leaveHashed()
      Description copied from interface: TripStream
      Leaves a value the receiving container will hash while reading.
      Specified by:
      leaveHashed in interface TripStream
      See Also:
    • suspendHashed

      public int suspendHashed()
      Description copied from interface: TripStream
      Suspends the hashed values entered so far while reading.
      Used while reading the contents of an object whose hash code does not depend on them (see TripShallowHash). Hashed values entered within are guarded on their own.
      Must be paired with TripStream.resumeHashed(int) in a finally block.
      Specified by:
      suspendHashed in interface TripStream
      Returns:
      the state to pass to TripStream.resumeHashed(int)
      See Also:
    • resumeHashed

      public void resumeHashed(int hashed)
      Description copied from interface: TripStream
      Resumes the hashed values suspended by TripStream.suspendHashed().
      Specified by:
      resumeHashed in interface TripStream
      Parameters:
      hashed - the state returned by TripStream.suspendHashed()
    • writeComponentOrdinal

      public void writeComponentOrdinal(int ordinal)
      Description copied from interface: TripStream
      Writes a component ordinal to the stream.
      Specified by:
      writeComponentOrdinal in interface TripStream
      Parameters:
      ordinal - the ordinal to write
    • readComponentOrdinal

      public int readComponentOrdinal(OpCode opCode)
      Description copied from interface: TripStream
      Reads a component ordinal from the stream.
      Specified by:
      readComponentOrdinal in interface TripStream
      Parameters:
      opCode - the ordinal OpCode
      Returns:
      the ordinal
    • writeNextComponentOrdinal

      public int writeNextComponentOrdinal(int lastOrdinal, TripComponent<?,?> component)
      Description copied from interface: TripStream
      Writes the next ordinal to the stream, using optimization.
      Specified by:
      writeNextComponentOrdinal in interface TripStream
      Parameters:
      lastOrdinal - the last ordinal written, -1 for none
      component - the component to write the ordinal for
      Returns:
      the component ordinal
    • readNextComponentOrdinal

      public int readNextComponentOrdinal(int lastOrdinal, OpCode opCode)
      Description copied from interface: TripStream
      Reads the next ordinal from the stream, using optimization.
      Specified by:
      readNextComponentOrdinal in interface TripStream
      Parameters:
      lastOrdinal - the last ordinal read, -1 for none
      opCode - the ordinal OpCode
      Returns:
      the ordinal
    • writeDictionaryOrdinal

      public void writeDictionaryOrdinal(int ordinal)
      Description copied from interface: TripStream
      Writes a dictionary ordinal to the stream.
      Specified by:
      writeDictionaryOrdinal in interface TripStream
      Parameters:
      ordinal - the ordinal to write
    • readDictionaryOrdinal

      public int readDictionaryOrdinal(OpCode opCode)
      Description copied from interface: TripStream
      Reads a dictionary ordinal from the stream.
      Specified by:
      readDictionaryOrdinal in interface TripStream
      Parameters:
      opCode - the ordinal OpCode
      Returns:
      the ordinal
    • sendDictionary

      public void sendDictionary(Class<?> clazz)
      Description copied from interface: TripStream
      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.
      Specified by:
      sendDictionary in interface TripStream
      Parameters:
      clazz - the object's class
    • receiveDictionary

      public DictionaryEntry receiveDictionary(OpCode opCode)
      Description copied from interface: TripStream
      Receives the dictionary entry and adds it to the dictionary.
      Specified by:
      receiveDictionary in interface TripStream
      Parameters:
      opCode - the opcode to read (already read from the stream)
      Returns:
      the dictionary entry
    • receiveDictionary

      public DictionaryEntry receiveDictionary()
      Description copied from interface: TripStream
      Receives the dictionary entry and adds it to the dictionary.
      Same as TripStream.receiveDictionary(OpCode) but reads the opcode.
      Specified by:
      receiveDictionary in interface TripStream
      Returns:
      the dictionary entry
    • deriveType

      public TripType<?> deriveType(OpCode opCode)
      Description copied from interface: TripStream
      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.
      Specified by:
      deriveType in interface TripStream
      Parameters:
      opCode - the opcode (already read from the stream)
      Returns:
      the type
    • deriveType

      public TripType<?> deriveType()
      Description copied from interface: TripStream
      Derives the concrete type from the given OpCode.
      Same as TripStream.deriveType(OpCode) but reads the opcode.
      Specified by:
      deriveType in interface TripStream
      Returns:
      the type
    • receiveOpCode

      public OpCode receiveOpCode()
      Description copied from interface: TripStream
      Receives 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:
      receiveOpCode in interface TripStream
      Returns:
      the OpCode
    • sendError

      public void sendError(OpCode errorOpCode, Throwable t)
      Description copied from interface: TripStream
      Sends a TRIP error.
      The error opcode is sent first, followed by the throwable's stack trace.
      Specified by:
      sendError in interface TripStream
      Parameters:
      errorOpCode - the error opcode
      t - the throwable
    • getClassCount

      public Map<Class<?>, Holder<Integer>> getClassCount()
      Gets the number of object references per class.
      Provided for debugging purposes only!
      Returns:
      the map of class to reference count
    • isShareable

      protected boolean isShareable(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.
      Parameters:
      object - the referenced object, null if not completely read yet
      Returns:
      true if allowed
    • isHashShareable

      protected boolean isHashShareable(Object object)
      Returns whether an object received before may be referenced again within a hashed value.
      Besides the shareable values, these are the objects whose hash code does not depend on other objects: arrays, objects inheriting hashCode and equals from Object and objects annotated with TripShallowHash. Dynamic proxies are not, unless their invocation handler is annotated with TripShallowHash, since the handler decides.
      Parameters:
      object - the referenced object, null if not completely read yet
      Returns:
      true if allowed
      See Also: