Class SharedObject

java.lang.Object
org.red5.server.AttributeStore
org.red5.server.so.SharedObject
All Implemented Interfaces:
IAttributeStore, ICastingAttributeStore, IPersistable, ISharedObjectStatistics, IStatisticsBase, AttributeStoreMXBean, Constants
Direct Known Subclasses:
ClientSharedObject

public class SharedObject extends AttributeStore implements ISharedObjectStatistics, IPersistable, Constants
Represents a remote shared object on server-side. RSO's are shared by multiple clients and synchronized on each data change. A shared object can be persistent or transient; the difference being saved to the disk for later access vs transient objects that are not persisted to storage. Shared objects have name identifiers and a path. In this implementation we use IPersistenceStore to delegate all (de)serialization work. SOs store data as a "name-value" store. Each value in can be a complex object or map. All access to methods that change properties in the SO must be properly synchronized for multithreaded access.
Author:
mondain
  • Field Details

    • log

      protected static org.slf4j.Logger log
      Constant log
    • name

      protected String name
      Shared Object name (identifier)
    • path

      protected String path
      SO path
    • persistent

      protected boolean persistent
      true if the SharedObject was stored by the persistence framework and can be used later on reconnection
    • storage

      protected IPersistenceStore storage
      Object that is delegated with all storage work for persistent SOs
    • version

      protected volatile AtomicInteger version
      Version. Used on synchronization purposes.
    • updateCounter

      protected volatile AtomicInteger updateCounter
      Number of pending update operations (beginUpdate / endUpdate)
    • lastModified

      protected volatile long lastModified
      Last modified timestamp
    • ownerMessage

      protected SharedObjectMessage ownerMessage
      Owner event
    • syncEvents

      protected transient volatile ConcurrentSkipListSet<ISharedObjectEvent> syncEvents
      Synchronization events
    • listeners

      protected transient volatile CopyOnWriteArraySet<IEventListener> listeners
      Listeners
    • source

      protected IEventListener source
      Event listener, actually RTMP connection
    • acquireCount

      protected volatile AtomicInteger acquireCount
      Number of times the SO has been acquired
    • listenerStats

      protected transient StatisticsCounter listenerStats
      Manages listener statistics.
    • changeStats

      protected AtomicInteger changeStats
      Counts number of "change" events.
    • deleteStats

      protected AtomicInteger deleteStats
      Counts number of "delete" events.
    • sendStats

      protected AtomicInteger sendStats
      Counts number of "send message" events.
    • closed

      protected volatile AtomicBoolean closed
      Whether or not this shared object is closed
  • Constructor Details

    • SharedObject

      public SharedObject()
      Constructs a new SharedObject.
    • SharedObject

      public SharedObject(Input input) throws IOException
      Constructs new SO from Input object
      Parameters:
      input - Input source
      Throws:
      IOException - I/O exception
      See Also:
    • SharedObject

      public SharedObject(String name, String path, boolean persistent)
      Creates new SO from given data map, name, path and persistence option
      Parameters:
      name - SO name
      path - SO path
      persistent - SO persistence
    • SharedObject

      public SharedObject(String name, String path, boolean persistent, IPersistenceStore storage)
      Creates new SO from given data map, name, path, storage object and persistence option
      Parameters:
      name - SO name
      path - SO path
      persistent - SO persistence
      storage - Persistence storage
    • SharedObject

      public SharedObject(Map<String,Object> data, String name, String path, boolean persistent)
      Creates new SO from given data map, name, path and persistence option
      Parameters:
      data - Data
      name - SO name
      path - SO path
      persistent - SO persistence
    • SharedObject

      public SharedObject(Map<String,Object> data, String name, String path, boolean persistent, IPersistenceStore storage)
      Creates new SO from given data map, name, path, storage object and persistence option
      Parameters:
      data - Data
      name - SO name
      path - SO path
      persistent - SO persistence
      storage - Persistence storage
  • Method Details

    • getName

      public String getName()
      Return the name of the shared object.
      Specified by:
      getName in interface IPersistable
      Specified by:
      getName in interface ISharedObjectStatistics
      Returns:
      a String object
    • setName

      public void setName(String name)
      Set the name of the persistent object.
      Specified by:
      setName in interface IPersistable
      Parameters:
      name - New object name
    • getPath

      public String getPath()
      Returns the path of the persistent object.
      Specified by:
      getPath in interface IPersistable
      Returns:
      a String object
    • setPath

      public void setPath(String path)
      Set the path of the persistent object.
      Specified by:
      setPath in interface IPersistable
      Parameters:
      path - New persisted object path
    • getType

      public String getType()
      Returns the type of the persistent object.
      Specified by:
      getType in interface IPersistable
      Returns:
      a String object
    • getLastModified

      public long getLastModified()
      Returns the timestamp when the object was last modified.
      Specified by:
      getLastModified in interface IPersistable
      Returns:
      a long
    • isPersistent

      public boolean isPersistent()
      Check if the shared object is persistent.
      Specified by:
      isPersistent in interface IPersistable
      Specified by:
      isPersistent in interface ISharedObjectStatistics
      Returns:
      a boolean
    • setPersistent

      public void setPersistent(boolean persistent)
      Set the persistent flag of the object.
      Specified by:
      setPersistent in interface IPersistable
      Parameters:
      persistent -
       true
       
      if object is persistent,
       false
       
      otherwise
    • sendUpdates

      protected void sendUpdates()
      Send update notification over data channel of RTMP connection
    • notifyModified

      protected void notifyModified()
      Send notification about modification of SO
    • returnError

      protected void returnError(String message)
      Return an error message to the client.
      Parameters:
      message - message
    • getAttribute

      public Object getAttribute(String name)
      Return the value for a given attribute.
      Specified by:
      getAttribute in interface IAttributeStore
      Overrides:
      getAttribute in class AttributeStore
      Parameters:
      name - a String object
      Returns:
      a Object object
    • getAttribute

      public Object getAttribute(String name, Object def)
      Return the value for a given attribute and set it if it doesn't exist. This is a utility function that internally performs the following code:
       if (!hasAttribute(name))
           setAttribute(name, defaultValue);
       return getAttribute(name);
       
      Specified by:
      getAttribute in interface IAttributeStore
      Overrides:
      getAttribute in class AttributeStore
      Parameters:
      name - the name of the attribute to get
      def - the value of the attribute to set if the attribute doesn't exist
      Returns:
      the attribute value
    • setAttribute

      public boolean setAttribute(String name, Object value)
      Set an attribute on this object. Set an attribute on this object.
      Specified by:
      setAttribute in interface IAttributeStore
      Overrides:
      setAttribute in class AttributeStore
      Parameters:
      name - the name of the attribute to change
      value - the new value of the attribute
      Returns:
      true if the attribute value changed otherwise false
    • setAttributes

      public boolean setAttributes(Map<String,Object> values)
      Set multiple attributes on this object.
      Specified by:
      setAttributes in interface IAttributeStore
      Overrides:
      setAttributes in class AttributeStore
      Parameters:
      values - a Map object
      Returns:
      a boolean
    • setAttributes

      public boolean setAttributes(IAttributeStore values)
      Set multiple attributes on this object.
      Specified by:
      setAttributes in interface IAttributeStore
      Overrides:
      setAttributes in class AttributeStore
      Parameters:
      values - the attributes to set
      Returns:
      true if the attribute values changed otherwise false
    • removeAttribute

      public boolean removeAttribute(String name)

      removeAttribute.

      Remove an attribute.
      Specified by:
      removeAttribute in interface AttributeStoreMXBean
      Specified by:
      removeAttribute in interface IAttributeStore
      Overrides:
      removeAttribute in class AttributeStore
      Parameters:
      name - a String object
      Returns:
      a boolean
    • removeAttributes

      public void removeAttributes()
      Remove all attributes.
      Specified by:
      removeAttributes in interface AttributeStoreMXBean
      Specified by:
      removeAttributes in interface IAttributeStore
      Overrides:
      removeAttributes in class AttributeStore
    • sendMessage

      protected void sendMessage(String handler, List<?> arguments)
      Broadcast event to event handler
      Parameters:
      handler - Event handler
      arguments - Arguments
    • getData

      public Map<String,Object> getData()
      Getter for data.
      Returns:
      SO data as unmodifiable map
    • getVersion

      public int getVersion()
      Getter for version.
      Specified by:
      getVersion in interface ISharedObjectStatistics
      Returns:
      SO version.
    • register

      protected boolean register(IEventListener listener)
      Register event listener
      Parameters:
      listener - Event listener
      Returns:
      true if listener was added
    • unregister

      protected void unregister(IEventListener listener)
      Unregister event listener
      Parameters:
      listener - Event listener
    • checkRelease

      protected void checkRelease()
      Check if shared object must be released.
    • getListeners

      public Set<IEventListener> getListeners()
      Get event listeners.
      Returns:
      Value for property 'listeners'.
    • beginUpdate

      public void beginUpdate()
      Begin update of this Shared Object. Increases number of pending update operations
    • beginUpdate

      public void beginUpdate(IEventListener listener)
      Begin update of this Shared Object and setting listener
      Parameters:
      listener - Update with listener
    • endUpdate

      public void endUpdate()
      End update of this Shared Object. Decreases number of pending update operations and broadcasts modified event if it is equal to zero (i.e. no more pending update operations).
    • serialize

      public void serialize(Output output) throws IOException
      Write the object to the passed output stream.
      Specified by:
      serialize in interface IPersistable
      Parameters:
      output - Output stream to write to
      Throws:
      IOException - Any I/O exception
    • deserialize

      public void deserialize(Input input) throws IOException
      Load the object from the passed input stream.
      Specified by:
      deserialize in interface IPersistable
      Parameters:
      input - Input stream to load from
      Throws:
      IOException - Any I/O exception
    • setStore

      public void setStore(IPersistenceStore store)
      Store a reference to the persistence store in the object.
      Specified by:
      setStore in interface IPersistable
      Parameters:
      store - Store the object is saved in
    • getStore

      public IPersistenceStore getStore()
      Returns the persistence store this object is stored in
      Specified by:
      getStore in interface IPersistable
      Returns:
      a IPersistenceStore object
    • clear

      protected boolean clear()
      Deletes all the attributes and sends a clear event to all listeners. The persistent data object is also removed from a persistent shared object.
      Returns:
      true on success, false otherwise
    • close

      protected void close()
      Detaches a reference from this shared object, reset it's state, this will destroy the reference immediately. This is useful when you don't want to proxy a shared object any longer.
    • acquire

      public void acquire()
      Prevent shared object from being released. Each call to acquire must be paired with a call to release so the SO isn't held forever. This is only valid for non-persistent SOs.
    • isAcquired

      public boolean isAcquired()
      Check if shared object currently is acquired.
      Returns:
      true if the SO is acquired, false otherwise
    • release

      public void release()
      Release previously acquired shared object. If the SO is non-persistent, no more clients are connected the SO isn't acquired any more, the data is released.
    • isClosed

      public boolean isClosed()

      isClosed.

      Returns:
      a boolean
    • getCreationTime

      public long getCreationTime()
      Return the timestamp the object was created.
      Specified by:
      getCreationTime in interface IStatisticsBase
      Returns:
      a long
    • getTotalListeners

      public int getTotalListeners()
      Return total number of subscribed listeners.
      Specified by:
      getTotalListeners in interface ISharedObjectStatistics
      Returns:
      a int
    • getMaxListeners

      @Deprecated public int getMaxListeners()
      Deprecated.
      Return maximum number of concurrent subscribed listenes.
      Specified by:
      getMaxListeners in interface ISharedObjectStatistics
      Returns:
      a int
    • getActiveListeners

      public int getActiveListeners()
      Return current number of subscribed listeners.
      Specified by:
      getActiveListeners in interface ISharedObjectStatistics
      Returns:
      a int
    • getTotalChanges

      public int getTotalChanges()
      Return number of attribute changes.
      Specified by:
      getTotalChanges in interface ISharedObjectStatistics
      Returns:
      a int
    • getTotalDeletes

      public int getTotalDeletes()
      Return number of attribute deletes.
      Specified by:
      getTotalDeletes in interface ISharedObjectStatistics
      Returns:
      a int
    • getTotalSends

      public int getTotalSends()
      Return number of times a message was sent.
      Specified by:
      getTotalSends in interface ISharedObjectStatistics
      Returns:
      a int
    • setDirty

      public void setDirty(boolean dirty)
      Parameters:
      dirty - a boolean
    • setDirty

      public void setDirty(String name)
      Parameters:
      name - a String object