org.red5.server
Class Scope

java.lang.Object
  extended by org.red5.server.AttributeStore
      extended by org.red5.server.PersistableAttributeStore
          extended by org.red5.server.BasicScope
              extended by org.red5.server.Scope
All Implemented Interfaces:
Iterable<IBasicScope>, AttributeStoreMBean, IEventDispatcher, IEventHandler, IEventListener, IEventObservable, IAttributeStore, IBasicScope, ICastingAttributeStore, ICoreObject, IScope, IPersistable, IServiceHandlerProvider, IScopeStatistics, IStatisticsBase, ScopeMBean, ResourceLoader, ResourcePatternResolver
Direct Known Subclasses:
GlobalScope, WebScope

public class Scope
extends BasicScope
implements IScope, IScopeStatistics, ScopeMBean

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 its possible for a scope to have a parent. If a client is connect to a scope then they are also connected to its parent scope. The scope object is used to access resources, shared object, streams, etc. The following are all names for scopes: application, room, place, lobby.

Author:
The Red5 Project (red5@osflash.org), Paul Gregoire (mondain@gmail.com), Nathan Smith (nathgs@gmail.com)

Nested Class Summary
static class Scope.Builder
          Builder pattern
 
Nested classes/interfaces inherited from class org.red5.server.BasicScope
BasicScope.EmptyBasicScopeIterator
 
Field Summary
protected  StatisticsCounter clientStats
          Statistics about clients connected to the scope.
protected  StatisticsCounter connectionStats
          Statistics about connections to the scope.
protected  Lock lock
          Lock for critical sections, to prevent concurrent modification.
protected static Logger log
          Logger
protected  ObjectName oName
          Mbean object name.
protected  StatisticsCounter subscopeStats
          Statistics about sub-scopes.
 
Fields inherited from class org.red5.server.BasicScope
keepDelay, keepOnDisconnect, listeners, parent, persistenceClass
 
Fields inherited from class org.red5.server.PersistableAttributeStore
lastModified, name, path, persistent, store, type
 
Fields inherited from class org.red5.server.AttributeStore
attributes
 
Fields inherited from interface org.red5.server.api.IScope
ID, SEPARATOR
 
Fields inherited from interface org.red5.server.api.persistence.IPersistable
TRANSIENT_PREFIX
 
Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_ALL_URL_PREFIX
 
Fields inherited from interface org.springframework.core.io.ResourceLoader
CLASSPATH_URL_PREFIX
 
Constructor Summary
Scope()
          Creates unnamed scope
Scope(Scope.Builder builder)
          Creates scope using a Builder
Scope(String name)
          Creates scope with given name
 
Method Summary
 boolean addChildScope(IBasicScope scope)
          Add child scope to this scope
 boolean connect(IConnection conn)
          Connect to scope
 boolean connect(IConnection conn, Object[] params)
          Connect to scope with parameters.
 boolean createChildScope(String name)
          Create child scope with given name
 void destroy()
          Destroys scope
 void disconnect(IConnection conn)
          Disconnect connection from scope
 void dispatchEvent(IEvent event)
          Dispatches event (notifies all listeners)
 void dump()
           
 int getActiveClients()
          Return current number of clients connected to the scope.
 int getActiveConnections()
          Return current number of connections to the scope.
 int getActiveSubscopes()
          Return number of currently existing subscopes.
 IBasicScope getBasicScope(String type, String name)
          Return base scope of given type with given name
 Iterator<String> getBasicScopeNames(String type)
          Return basic scope names iterator
 ClassLoader getClassLoader()
          Return current thread context classloader
 Set<IClient> getClients()
          Return set of clients
 Collection<Set<IConnection>> getConnections()
          Return connection iterator
 IContext getContext()
          Return scope context.
 String getContextPath()
          Return scope context path
 long getCreationTime()
          Return the timestamp the object was created.
 int getDepth()
          return scope depth
 boolean getEnabled()
          Here for JMX only, uses isEnabled()
 IScopeHandler getHandler()
          Return scope handler or parent's scope handler if this scope doesn't have one
 int getMaxClients()
          Return maximum number of clients concurrently connected to the scope.
 int getMaxConnections()
          Return maximum number of concurrent connections to the scope.
 int getMaxSubscopes()
          Return maximum number of concurrently existing subscopes.
 IScope getParent()
          Return parent scope
 String getPath()
          Return scope path calculated from parent path and parent scope name
 Resource getResource(String path)
          Return resource located at given path
 Resource[] getResources(String path)
          Return array of resources from path string, usually used with pattern path
 boolean getRunning()
          Here for JMX only, uses isEnabled()
 IScope getScope(String name)
          Return child scope by name
 Iterator<String> getScopeNames()
          Return child scope names iterator
 IServer getServer()
          Return the server instance connected to this scope.
 Object getServiceHandler(String name)
          Return service handler by name
 Set<String> getServiceHandlerNames()
          Return set of service handler names.
protected  Map<String,Object> getServiceHandlers()
          Return map of service handlers.
protected  Map<String,Object> getServiceHandlers(boolean allowCreate)
          Return map of service handlers and optionally created it if it doesn't exist.
 IScopeStatistics getStatistics()
          Return statistics informations about the scope.
 int getTotalClients()
          Return total number of clients connected to the scope.
 int getTotalConnections()
          Return total number of connections to the scope.
 int getTotalSubscopes()
          Return total number of subscopes created.
 boolean handleEvent(IEvent event)
          Handles event.
 boolean hasChildScope(String name)
          Check whether scope has child scope with given name
 boolean hasChildScope(String type, String name)
          Check whether scope has child scope with given name and type
 boolean hasContext()
          Check if scope has a context
 boolean hasHandler()
          Check if scope or it's parent has handler
 boolean hasParent()
          Check if scope has parent scope
 void init()
          Initialization actions, start if autostart is set to true
 boolean isEnabled()
          Check if scope is enabled
 boolean isRunning()
          Check if scope is in running state
 Iterator<IBasicScope> iterator()
          Child scopes iterator
 void lock()
          Lock handling - attempts to obtain a lock
 Set<IConnection> lookupConnections(IClient client)
          Looks up connections for client
 void registerServiceHandler(String name, Object handler)
          Register service handler by name
 void removeChildScope(IBasicScope scope)
          Removes child scope
 void setAutoStart(boolean autoStart)
          Setter for autostart flag
 void setChildLoadPath(String pattern)
          Setter for child load path.
 void setContext(IContext context)
          Setter for context
 void setDepth(int depth)
          Set scope depth
 void setEnabled(boolean enabled)
          Enable or disable scope by setting enable flag
 void setHandler(IScopeHandler handler)
          Setter for scope event handler
 void setName(String name)
          Setter for scope name
 void setParent(IScope parent)
          Setter for parent scope
 void setPersistenceClass(String persistenceClass)
          Set scope persistence class
 boolean start()
          Starts scope
 void stop()
          Stops scope
 String toString()
          
 void uninit()
          Uninitialize scope and unregister from parent.
 void unlock()
          Lock handling - attempts to unlock a lock
 void unregisterServiceHandler(String name)
          Unregisters service handler by name
 
Methods inherited from class org.red5.server.BasicScope
addEventListener, getEventListeners, hasEventListeners, notifyEvent, removeEventListener, setKeepDelay
 
Methods inherited from class org.red5.server.PersistableAttributeStore
deserialize, getAttribute, getLastModified, getName, getStore, getType, isPersistent, modified, removeAttribute, removeAttributes, serialize, setAttribute, setAttributes, setAttributes, setPath, setPersistent, setStore
 
Methods inherited from class org.red5.server.AttributeStore
filterNull, getAttribute, getAttributeNames, getAttributes, getBoolAttribute, getByteAttribute, getDoubleAttribute, getIntAttribute, getListAttribute, getLongAttribute, getMapAttribute, getSetAttribute, getShortAttribute, getStringAttribute, hasAttribute
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.red5.server.api.IBasicScope
getName, getType, setKeepDelay
 
Methods inherited from interface org.red5.server.api.ICastingAttributeStore
getBoolAttribute, getByteAttribute, getDoubleAttribute, getIntAttribute, getListAttribute, getLongAttribute, getMapAttribute, getSetAttribute, getShortAttribute, getStringAttribute
 
Methods inherited from interface org.red5.server.api.IAttributeStore
getAttribute, getAttribute, getAttributeNames, getAttributes, hasAttribute, removeAttribute, removeAttributes, setAttribute, setAttributes, setAttributes
 
Methods inherited from interface org.red5.server.api.event.IEventListener
notifyEvent
 
Methods inherited from interface org.red5.server.api.event.IEventObservable
addEventListener, getEventListeners, removeEventListener
 
Methods inherited from interface org.red5.server.api.persistence.IPersistable
deserialize, getLastModified, getStore, isPersistent, serialize, setPath, setPersistent, setStore
 
Methods inherited from interface org.red5.server.api.statistics.IScopeStatistics
getName
 

Field Detail

log

protected static Logger log
Logger


clientStats

protected final StatisticsCounter clientStats
Statistics about clients connected to the scope.


connectionStats

protected final StatisticsCounter connectionStats
Statistics about connections to the scope.


subscopeStats

protected final StatisticsCounter subscopeStats
Statistics about sub-scopes.


lock

protected Lock lock
Lock for critical sections, to prevent concurrent modification. A "fairness" policy is used wherein the longest waiting thread will be granted access before others.


oName

protected ObjectName oName
Mbean object name.

Constructor Detail

Scope

public Scope()
Creates unnamed scope


Scope

public Scope(String name)
Creates scope with given name

Parameters:
name - Scope name

Scope

public Scope(Scope.Builder builder)
Creates scope using a Builder

Parameters:
builder -
Method Detail

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 with given name

Specified by:
createChildScope in interface IScope
Specified by:
createChildScope in interface ScopeMBean
Parameters:
name - Child scope name
Returns:
true on success, false otherwise

destroy

public void destroy()
Destroys scope

Specified by:
destroy in interface ScopeMBean

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

getActiveClients

public int getActiveClients()
Return current number of clients connected to the scope.

Specified by:
getActiveClients in interface IScopeStatistics
Specified by:
getActiveClients in interface ScopeMBean
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 ScopeMBean
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 ScopeMBean
Returns:
number of subscopes

getBasicScope

public IBasicScope getBasicScope(String type,
                                 String name)
Return base scope of given type with given name

Specified by:
getBasicScope in interface IScope
Specified by:
getBasicScope in interface ScopeMBean
Parameters:
type - Scope type
name - Scope name
Returns:
Basic scope object

getBasicScopeNames

public Iterator<String> getBasicScopeNames(String type)
Return basic scope names iterator

Specified by:
getBasicScopeNames in interface IScope
Specified by:
getBasicScopeNames in interface ScopeMBean
Parameters:
type - Scope type
Returns:
Iterator

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
Specified by:
getClients in interface ScopeMBean
Returns:
Set of clients bound to scope
See Also:
IClient.getConnections(IScope)

getConnections

public Collection<Set<IConnection>> getConnections()
Return connection iterator

Specified by:
getConnections in interface IScope
Specified by:
getConnections in interface ScopeMBean
Returns:
Connections iterator

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
Specified by:
getContext in interface ScopeMBean
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 ScopeMBean
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 ScopeMBean
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
Specified by:
getHandler in interface ScopeMBean
Returns:
Scope handler (or parent's one)

getMaxClients

public int getMaxClients()
Return maximum number of clients concurrently connected to the scope.

Specified by:
getMaxClients in interface IScopeStatistics
Specified by:
getMaxClients in interface ScopeMBean
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 ScopeMBean
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 ScopeMBean
Returns:
number of subscopes

getParent

public IScope getParent()
Return parent scope

Specified by:
getParent in interface IBasicScope
Specified by:
getParent in interface ScopeMBean
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 IPersistable
Specified by:
getPath in interface IScopeStatistics
Specified by:
getPath in interface ScopeMBean
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 ScopeMBean
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 ScopeMBean
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
Specified by:
getScope in interface ScopeMBean
Parameters:
name - Scope name
Returns:
Child scope with given name

getScopeNames

public Iterator<String> getScopeNames()
Return child scope names iterator

Specified by:
getScopeNames in interface IScope
Specified by:
getScopeNames in interface ScopeMBean
Returns:
Child scope names iterator

getServiceHandler

public Object getServiceHandler(String name)
Return service handler by name

Specified by:
getServiceHandler in interface IServiceHandlerProvider
Specified by:
getServiceHandler in interface ScopeMBean
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 ScopeMBean
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:
the statistics

getTotalClients

public int getTotalClients()
Return total number of clients connected to the scope.

Specified by:
getTotalClients in interface IScopeStatistics
Specified by:
getTotalClients in interface ScopeMBean
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 ScopeMBean
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 ScopeMBean
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 ScopeMBean
Parameters:
name - Child scope name
Returns:
true if scope has child node with given name, false otherwise

hasChildScope

public boolean hasChildScope(String 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 ScopeMBean
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 ScopeMBean
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 ScopeMBean
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 ScopeMBean
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 ScopeMBean

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 ScopeMBean
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 ScopeMBean
Returns:
true if scope is in running state, false otherwise

iterator

public Iterator<IBasicScope> iterator()
Child scopes iterator

Specified by:
iterator in interface Iterable<IBasicScope>
Overrides:
iterator in class BasicScope
Returns:
Child scopes iterator

lookupConnections

public Set<IConnection> lookupConnections(IClient client)
Looks up connections for client

Specified by:
lookupConnections in interface IScope
Parameters:
client - Client
Returns:
Connection

registerServiceHandler

public void registerServiceHandler(String name,
                                   Object handler)
Register service handler by name

Specified by:
registerServiceHandler in interface IServiceHandlerProvider
Specified by:
registerServiceHandler in interface ScopeMBean
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

setAutoStart

public void setAutoStart(boolean autoStart)
Setter for autostart flag

Specified by:
setAutoStart in interface ScopeMBean
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 ScopeMBean
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 ScopeMBean
Parameters:
depth - Scope depth

setEnabled

public void setEnabled(boolean enabled)
Enable or disable scope by setting enable flag

Specified by:
setEnabled in interface ScopeMBean
Parameters:
enabled - Enable flag value

setHandler

public void setHandler(IScopeHandler handler)
Setter for scope event handler

Parameters:
handler - Event handler

setName

public void setName(String name)
Setter for scope name

Specified by:
setName in interface IPersistable
Specified by:
setName in interface ScopeMBean
Overrides:
setName in class PersistableAttributeStore
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 ScopeMBean
Parameters:
persistenceClass - Scope's persistence class
Throws:
Exception - Exception

start

public boolean start()
Starts scope

Specified by:
start in interface ScopeMBean
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 ScopeMBean

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 ScopeMBean
Parameters:
name - Service handler name

getServer

public IServer getServer()
Return the server instance connected to this scope.

Returns:
the server instance

lock

public void lock()
Description copied from interface: IBasicScope
Lock handling - attempts to obtain a lock

Specified by:
lock in interface IBasicScope

unlock

public void unlock()
Description copied from interface: IBasicScope
Lock handling - attempts to unlock a lock

Specified by:
unlock in interface IBasicScope

dump

public void dump()


Copyright © 2006-2010 The Red5 Project