Class Scope

All Implemented Interfaces:
Comparable<BasicScope>, IEventDispatcher, IEventHandler, IEventListener, IEventObservable, ICoreObject, IBasicScope, IScope, IServiceHandlerProvider, IScopeStatistics, IStatisticsBase, ScopeMXBean, ResourceLoader, ResourcePatternResolver
Direct Known Subclasses:
GlobalScope, RoomScope, WebScope

@ManagedResource(objectName="org.red5.server:type=Scope", description="Scope") public class Scope extends BasicScope implements IScope, IScopeStatistics, ScopeMXBean
The scope object.
A stateful object shared between a group of clients connected to the same context path. Scopes are arranged in a hierarchical way, so a scope always has a parent unless its a "global" scope. If a client is connected to a scope then they are also connected to its parent scope. The scope object is used to access resources, shared object, streams, etc.
Scope layout:
  /Global scope - Contains application scopes
      /Application scope - Contains room, shared object, and stream scopes
          /Room scope - Contains other room, shared object, and / or stream scopes
              /Shared object scope - Contains shared object
              /Broadcast stream scope - Contains a broadcast stream
 
Author:
The Red5 Project, Paul Gregoire (mondain@gmail.com), Nathan Smith (nathgs@gmail.com)
  • Field Details

    • log

      protected static org.slf4j.Logger log
    • UNSET

      private static final int UNSET
      Unset flag constant
      See Also:
    • creationTime

      private long creationTime
      Timestamp the scope was created.
    • depth

      private int depth
      Scope nesting depth, unset by default
    • enabled

      private boolean enabled
      Whether scope is enabled
    • running

      private boolean running
      Whether scope is running
    • autoStart

      private boolean autoStart
      Auto-start flag
    • context

      private transient IContext context
      Scope context
    • handler

      private transient IScopeHandler handler
      Scope handler
    • serviceHandlers

      private transient volatile ConcurrentMap<String,Object> serviceHandlers
      Registered service handlers for this scope. The map is created on-demand only if it's accessed for writing.
    • children

      private final transient Scope.ConcurrentScopeSet children
      Child scopes
    • clients

      private final transient CopyOnWriteArraySet<IClient> clients
      Connected clients map
    • connectionStats

      protected final transient StatisticsCounter connectionStats
      Statistics about connections to the scope.
    • subscopeStats

      protected final transient StatisticsCounter subscopeStats
      Statistics about sub-scopes.
    • attributes

      protected final AttributeStore attributes
      Storage for scope attributes
    • oName

      protected ObjectName oName
      Mbean object name.
  • Constructor Details

  • Method Details

    • addChildScope

      public boolean addChildScope(IBasicScope scope)
      Add child scope to this scope
      Specified by:
      addChildScope in interface IScope
      Parameters:
      scope - Child scope
      Returns:
      true on success (if scope has handler and it accepts child scope addition), false otherwise
    • connect

      public boolean connect(IConnection conn)
      Connect to scope
      Specified by:
      connect in interface IScope
      Parameters:
      conn - Connection object
      Returns:
      true on success, false otherwise
    • connect

      public boolean connect(IConnection conn, Object[] params)
      Connect to scope with parameters. To successfully connect to scope it must have handler that will accept this connection with given set of parameters. Client associated with connection is added to scope clients set, connection is registered as scope event listener.
      Specified by:
      connect in interface IScope
      Parameters:
      conn - Connection object
      params - Parameters passed with connection
      Returns:
      true on success, false otherwise
    • createChildScope

      public boolean createChildScope(String name)
      Create child scope of room type, with the given name.
      Specified by:
      createChildScope in interface IScope
      Specified by:
      createChildScope in interface ScopeMXBean
      Parameters:
      name - child scope name
      Returns:
      true on success, false otherwise
    • destroy

      public void destroy() throws Exception
      Destroys scope
      Specified by:
      destroy in interface ScopeMXBean
      Throws:
      Exception - on error
    • disconnect

      public void disconnect(IConnection conn)
      Disconnect connection from scope
      Specified by:
      disconnect in interface IScope
      Parameters:
      conn - Connection object
    • dispatchEvent

      public void dispatchEvent(IEvent event)
      Dispatches event (notifies all listeners)
      Specified by:
      dispatchEvent in interface IEventDispatcher
      Overrides:
      dispatchEvent in class BasicScope
      Parameters:
      event - Event to dispatch
    • getAttribute

      public Object getAttribute(String name)
      Get attribute by name
      Specified by:
      getAttribute in interface IScope
      Parameters:
      name - name
      Returns:
      value for the given name in the attributes or null if not found
    • setAttribute

      public boolean setAttribute(String name, Object value)
      Set attribute by name
      Specified by:
      setAttribute in interface IScope
      Parameters:
      name - name
      value - value
      Returns:
      true if added, false if not added
    • hasAttribute

      public boolean hasAttribute(String name)
      Whether or not an attribute exists, keyed by the given name
      Specified by:
      hasAttribute in interface IScope
      Parameters:
      name - name
      Returns:
      true if it exists, false otherwise
    • removeAttribute

      public boolean removeAttribute(String name)
      Remove attribute by name
      Specified by:
      removeAttribute in interface IScope
      Parameters:
      name - name
      Returns:
      true if removed, false otherwise
    • getAttributeNames

      public Set<String> getAttributeNames()
      Return attribute names
      Specified by:
      getAttributeNames in interface IScope
      Returns:
      attribute names
    • getAttributes

      public Map<String,Object> getAttributes()
      Return scope attributes
      Specified by:
      getAttributes in interface IScope
      Returns:
      attributes
    • getActiveClients

      public int getActiveClients()
      Return current number of clients connected to the scope.
      Specified by:
      getActiveClients in interface IScopeStatistics
      Specified by:
      getActiveClients in interface ScopeMXBean
      Returns:
      number of clients
    • getActiveConnections

      public int getActiveConnections()
      Return current number of connections to the scope.
      Specified by:
      getActiveConnections in interface IScopeStatistics
      Specified by:
      getActiveConnections in interface ScopeMXBean
      Returns:
      number of connections
    • getActiveSubscopes

      public int getActiveSubscopes()
      Return number of currently existing subscopes.
      Specified by:
      getActiveSubscopes in interface IScopeStatistics
      Specified by:
      getActiveSubscopes in interface ScopeMXBean
      Returns:
      number of subscopes
    • getBroadcastScope

      public IBroadcastScope getBroadcastScope(String name)
      Return the broadcast scope for a given name
      Specified by:
      getBroadcastScope in interface IScope
      Parameters:
      name - name
      Returns:
      broadcast scope or null if not found
    • getBasicScope

      public IBasicScope getBasicScope(ScopeType type, String name)
      Return base scope of given type with given name
      Specified by:
      getBasicScope in interface IScope
      Parameters:
      type - Scope type
      name - Scope name
      Returns:
      Basic scope object
    • getBasicScopeNames

      public Set<String> getBasicScopeNames(ScopeType type)
      Return basic scope names matching given type
      Specified by:
      getBasicScopeNames in interface IScope
      Parameters:
      type - Scope type
      Returns:
      set of scope names
    • getClassLoader

      public ClassLoader getClassLoader()
      Return current thread context classloader
      Specified by:
      getClassLoader in interface ResourceLoader
      Returns:
      Current thread context classloader
    • getClients

      public Set<IClient> getClients()
      Return set of clients
      Specified by:
      getClients in interface IScope
      Returns:
      Set of clients bound to scope
      See Also:
    • getConnections

      @Deprecated public Collection<Set<IConnection>> getConnections()
      Deprecated.
      Get a connection iterator. You can call remove, and the connection will be closed.
      Specified by:
      getConnections in interface IScope
      Returns:
      Iterator holding all connections
    • getClientConnections

      public Set<IConnection> getClientConnections()
      Get all current connections. You can call remove, and the connection will be closed.
      Specified by:
      getClientConnections in interface IScope
      Returns:
      Set containing all connections
    • lookupConnections

      @Deprecated public Set<IConnection> lookupConnections(IClient client)
      Deprecated.
      Lookup connections.
      Specified by:
      lookupConnections in interface IScope
      Parameters:
      client - object
      Returns:
      Set of connection objects (read-only)
    • lookupConnection

      public IConnection lookupConnection(IClient client)
      Lookup connection for a given client.
      Specified by:
      lookupConnection in interface IScope
      Parameters:
      client - object
      Returns:
      connection object
    • getContext

      public IContext getContext()
      Return scope context. If scope doesn't have context, parent's context is returns, and so forth.
      Specified by:
      getContext in interface IScope
      Returns:
      Scope context or parent context
    • getContextPath

      public String getContextPath()
      Return scope context path
      Specified by:
      getContextPath in interface IScope
      Specified by:
      getContextPath in interface ScopeMXBean
      Returns:
      Scope context path
    • getCreationTime

      public long getCreationTime()
      Return the timestamp the object was created.
      Specified by:
      getCreationTime in interface IStatisticsBase
      Returns:
      the timestamp in milliseconds since midnight, January 1, 1970 UTC.
    • getDepth

      public int getDepth()
      return scope depth
      Specified by:
      getDepth in interface IBasicScope
      Specified by:
      getDepth in interface IScopeStatistics
      Specified by:
      getDepth in interface ScopeMXBean
      Overrides:
      getDepth in class BasicScope
      Returns:
      Scope depth
    • getHandler

      public IScopeHandler getHandler()
      Return scope handler or parent's scope handler if this scope doesn't have one.
      Specified by:
      getHandler in interface IScope
      Returns:
      Scope handler (or parent's one)
    • getMaxClients

      @Deprecated public int getMaxClients()
      Deprecated.
      Return maximum number of clients concurrently connected to the scope.
      Specified by:
      getMaxClients in interface IScopeStatistics
      Specified by:
      getMaxClients in interface ScopeMXBean
      Returns:
      number of clients
    • getMaxConnections

      public int getMaxConnections()
      Return maximum number of concurrent connections to the scope.
      Specified by:
      getMaxConnections in interface IScopeStatistics
      Specified by:
      getMaxConnections in interface ScopeMXBean
      Returns:
      number of connections
    • getMaxSubscopes

      public int getMaxSubscopes()
      Return maximum number of concurrently existing subscopes.
      Specified by:
      getMaxSubscopes in interface IScopeStatistics
      Specified by:
      getMaxSubscopes in interface ScopeMXBean
      Returns:
      number of subscopes
    • getParent

      public IScope getParent()
      Return parent scope
      Specified by:
      getParent in interface IBasicScope
      Overrides:
      getParent in class BasicScope
      Returns:
      Parent scope
    • getPath

      public String getPath()
      Return scope path calculated from parent path and parent scope name
      Specified by:
      getPath in interface IBasicScope
      Specified by:
      getPath in interface IScopeStatistics
      Specified by:
      getPath in interface ScopeMXBean
      Overrides:
      getPath in class BasicScope
      Returns:
      Scope path
    • getResource

      public Resource getResource(String path)
      Return resource located at given path
      Specified by:
      getResource in interface ResourceLoader
      Parameters:
      path - Resource path
      Returns:
      Resource
    • getResources

      public Resource[] getResources(String path) throws IOException
      Return array of resources from path string, usually used with pattern path
      Specified by:
      getResources in interface ResourcePatternResolver
      Parameters:
      path - Resources path
      Returns:
      Resources
      Throws:
      IOException - I/O exception
    • getScope

      public IScope getScope(String name)
      Return child scope by name
      Specified by:
      getScope in interface IScope
      Parameters:
      name - Scope name
      Returns:
      Child scope with given name
    • getScopeNames

      public Set<String> getScopeNames()
      Return child scope names iterator
      Specified by:
      getScopeNames in interface IScope
      Returns:
      Child scope names iterator
    • getServiceHandler

      public Object getServiceHandler(String name)
      Return service handler by name
      Specified by:
      getServiceHandler in interface IServiceHandlerProvider
      Parameters:
      name - Handler name
      Returns:
      Service handler with given name
    • getServiceHandlerNames

      public Set<String> getServiceHandlerNames()
      Return set of service handler names. Removing entries from the set unregisters the corresponding service handler.
      Specified by:
      getServiceHandlerNames in interface IServiceHandlerProvider
      Specified by:
      getServiceHandlerNames in interface ScopeMXBean
      Returns:
      Set of service handler names
    • getServiceHandlers

      protected Map<String,Object> getServiceHandlers()
      Return map of service handlers. The map is created if it doesn't exist yet.
      Returns:
      Map of service handlers
    • getServiceHandlers

      protected Map<String,Object> getServiceHandlers(boolean allowCreate)
      Return map of service handlers and optionally created it if it doesn't exist.
      Parameters:
      allowCreate - Should the map be created if it doesn't exist?
      Returns:
      Map of service handlers
    • getStatistics

      public IScopeStatistics getStatistics()
      Return statistics informations about the scope.
      Specified by:
      getStatistics in interface IScope
      Returns:
      statistics
    • getTotalClients

      @Deprecated public int getTotalClients()
      Deprecated.
      Return total number of clients connected to the scope.
      Specified by:
      getTotalClients in interface IScopeStatistics
      Specified by:
      getTotalClients in interface ScopeMXBean
      Returns:
      number of clients
    • getTotalConnections

      public int getTotalConnections()
      Return total number of connections to the scope.
      Specified by:
      getTotalConnections in interface IScopeStatistics
      Specified by:
      getTotalConnections in interface ScopeMXBean
      Returns:
      number of connections
    • getTotalSubscopes

      public int getTotalSubscopes()
      Return total number of subscopes created.
      Specified by:
      getTotalSubscopes in interface IScopeStatistics
      Specified by:
      getTotalSubscopes in interface ScopeMXBean
      Returns:
      number of subscopes created
    • handleEvent

      public boolean handleEvent(IEvent event)
      Handles event. To be implemented in subclasses.
      Specified by:
      handleEvent in interface IEventHandler
      Overrides:
      handleEvent in class BasicScope
      Parameters:
      event - Event to handle
      Returns:
      true on success, false otherwise
    • hasChildScope

      public boolean hasChildScope(String name)
      Check whether scope has child scope with given name
      Specified by:
      hasChildScope in interface IScope
      Specified by:
      hasChildScope in interface ScopeMXBean
      Parameters:
      name - Child scope name
      Returns:
      true if scope has child node with given name, false otherwise
    • hasChildScope

      public boolean hasChildScope(ScopeType type, String name)
      Check whether scope has child scope with given name and type
      Specified by:
      hasChildScope in interface IScope
      Specified by:
      hasChildScope in interface ScopeMXBean
      Parameters:
      type - Child scope type
      name - Child scope name
      Returns:
      true if scope has child node with given name and type, false otherwise
    • hasContext

      public boolean hasContext()
      Check if scope has a context
      Specified by:
      hasContext in interface ScopeMXBean
      Returns:
      true if scope has context, false otherwise
    • hasHandler

      public boolean hasHandler()
      Check if scope or it's parent has handler
      Specified by:
      hasHandler in interface IScope
      Specified by:
      hasHandler in interface ScopeMXBean
      Returns:
      true if scope or it's parent scope has a handler, false otherwise
    • hasParent

      public boolean hasParent()
      Check if scope has parent scope
      Specified by:
      hasParent in interface IBasicScope
      Specified by:
      hasParent in interface ScopeMXBean
      Overrides:
      hasParent in class BasicScope
      Returns:
      true if scope has parent scope, false otherwise`
    • init

      public void init()
      Initialization actions, start if autostart is set to true.
      Specified by:
      init in interface ScopeMXBean
    • uninit

      public void uninit()
      Uninitialize scope and unregister from parent.
    • isEnabled

      public boolean isEnabled()
      Check if scope is enabled
      Returns:
      true if scope is enabled, false otherwise
    • getEnabled

      public boolean getEnabled()
      Here for JMX only, uses isEnabled()
      Specified by:
      getEnabled in interface ScopeMXBean
      Returns:
       true
       
      if scope is enabled,
       false
       
      otherwise
    • isRunning

      public boolean isRunning()
      Check if scope is in running state
      Returns:
      true if scope is in running state, false otherwise
    • getRunning

      public boolean getRunning()
      Here for JMX only, uses isEnabled()
      Specified by:
      getRunning in interface ScopeMXBean
      Returns:
       true
       
      if scope is in running state,
       false
       
      otherwise
    • registerServiceHandler

      public void registerServiceHandler(String name, Object handler)
      Register service handler by name
      Specified by:
      registerServiceHandler in interface IServiceHandlerProvider
      Parameters:
      name - Service handler name
      handler - Service handler
    • removeChildScope

      public void removeChildScope(IBasicScope scope)
      Removes child scope
      Specified by:
      removeChildScope in interface IScope
      Parameters:
      scope - Child scope to remove
    • removeChildren

      public void removeChildren()
      Removes all the child scopes
      Specified by:
      removeChildren in interface IScope
    • setAutoStart

      public void setAutoStart(boolean autoStart)
      Setter for autostart flag
      Specified by:
      setAutoStart in interface ScopeMXBean
      Parameters:
      autoStart - Autostart flag value
    • setChildLoadPath

      public void setChildLoadPath(String pattern)
      Setter for child load path. Should be implemented in subclasses?
      Specified by:
      setChildLoadPath in interface ScopeMXBean
      Parameters:
      pattern - Load path pattern
    • setContext

      public void setContext(IContext context)
      Setter for context
      Parameters:
      context - Context object
    • setDepth

      public void setDepth(int depth)
      Set scope depth
      Specified by:
      setDepth in interface ScopeMXBean
      Parameters:
      depth - Scope depth
    • setEnabled

      public void setEnabled(boolean enabled)
      Enable or disable scope by setting enable flag
      Specified by:
      setEnabled in interface ScopeMXBean
      Parameters:
      enabled - Enable flag value
    • setHandler

      public void setHandler(IScopeHandler handler)
      Setter for scope event handler
      Parameters:
      handler - Event handler
    • setName

      public final void setName(String name)
      Setter for scope name
      Specified by:
      setName in interface ScopeMXBean
      Parameters:
      name - Scope name
    • setParent

      public void setParent(IScope parent)
      Setter for parent scope
      Parameters:
      parent - Parent scope
    • setPersistenceClass

      public void setPersistenceClass(String persistenceClass) throws Exception
      Set scope persistence class
      Specified by:
      setPersistenceClass in interface ScopeMXBean
      Parameters:
      persistenceClass - Scope's persistence class
      Throws:
      Exception - Exception
    • start

      public boolean start()
      Starts scope
      Specified by:
      start in interface ScopeMXBean
      Returns:
      true if scope has handler and it's start method returned true, false otherwise
    • stop

      public void stop()
      Stops scope
      Specified by:
      stop in interface ScopeMXBean
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • unregisterServiceHandler

      public void unregisterServiceHandler(String name)
      Unregisters service handler by name
      Specified by:
      unregisterServiceHandler in interface IServiceHandlerProvider
      Specified by:
      unregisterServiceHandler in interface ScopeMXBean
      Parameters:
      name - Service handler name
    • getServer

      public IServer getServer()
      Return the server instance connected to this scope.
      Returns:
      the server instance
    • dump

      public void dump()
    • registerJMX

      protected void registerJMX()
    • unregisterJMX

      protected void unregisterJMX()
    • from

      public static Scope from(CompositeData cd)
      Allows for reconstruction via CompositeData.
      Parameters:
      cd - composite data
      Returns:
      Scope class instance
    • hashCode

      public int hashCode()
      Description copied from class: BasicScope
      Hash code is based on the scope's name and type
      Overrides:
      hashCode in class BasicScope
      Returns:
      hash code
    • equals

      public boolean equals(Object obj)
      Description copied from class: BasicScope
      Equality is based on the scope's name and type.
      Overrides:
      equals in class BasicScope
      Parameters:
      obj - object