- java.lang.Object
-
- org.praxislive.base.AbstractRoot
-
- All Implemented Interfaces:
Lookup.Provider
,Root
- Direct Known Subclasses:
AbstractRootContainer
,BasicCoreRoot
,ScriptServiceImpl
public abstract class AbstractRoot extends Object implements Root
A general purpose base implementation ofRoot
. By default uses aScheduledExecutorService
but allows for attaching customAbstractRoot.Delegate
implementations to drive from a thread of choice.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AbstractRoot.Controller
Implementation of Root.Controller.protected class
AbstractRoot.Delegate
An abstract delegate class that may be attached to this Root to drive it from another source (eg.protected static class
AbstractRoot.DelegateConfiguration
A configuration object used for customizingAbstractRoot.Delegate
behaviour in a subclass.protected static class
AbstractRoot.State
The possible states this implementation may transition through.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractRoot()
Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
activating()
Hook called during activation of this Root, asynchronously after a call toAbstractRoot.Controller.start(java.util.concurrent.ThreadFactory)
.protected void
attachDelegate(AbstractRoot.Delegate delegate)
Attach aAbstractRoot.Delegate
to this Root.protected DefaultExecutionContext
createContext(long initialTime)
Method used to create theDefaultExecutionContext
during initialization.protected AbstractRoot.Controller
createController()
Method used to create theAbstractRoot.Controller
during initialization.protected PacketRouter
createRouter()
Method used to create thePacketRouter
during initialization.protected static AbstractRoot.DelegateConfiguration
delegateConfig()
Create aAbstractRoot.DelegateConfiguration
object for passing up to the constructor when creating aAbstractRoot.Delegate
subclass.protected void
detachDelegate(AbstractRoot.Delegate delegate)
Detach the provide delegate (if it is attached).protected ComponentAddress
findService(Class<? extends Service> service)
Find a Service address in the lookup.protected ComponentAddress
getAddress()
Get the address of this Root.protected ExecutionContext
getExecutionContext()
Get theExecutionContext
for this Root.Lookup
getLookup()
Get the Lookup from this Provider.protected RootHub
getRootHub()
Get theRootHub
this Root is installed in.protected PacketRouter
getRouter()
Get thePacketRouter
for this Root.protected AbstractRoot.State
getState()
Get the State of this Root.AbstractRoot.Controller
initialize(String id, RootHub hub)
Method used by the RootHub to initialize the Root and obtain a Controller.protected void
interrupt()
Interrupt the current update cycle, leaving pending calls or tasks to a subsequent update cycle.protected boolean
invokeLater(Runnable task)
Submit a task to be run asynchronously on the main Root thread.protected abstract void
processCall(Call call, PacketRouter router)
Method called to handle every receivedCall
.protected boolean
setIdle()
Set the Root state toAbstractRoot.State.ACTIVE_IDLE
.protected boolean
setRunning()
Set the Root state toAbstractRoot.State.ACTIVE_RUNNING
.protected void
starting()
Hook called during a call tosetRunning()
if the previous state wasAbstractRoot.State.ACTIVE_IDLE
.protected void
stopping()
Hook called during a call tosetIdle()
if the previous state wasAbstractRoot.State.ACTIVE_RUNNING
.protected void
terminating()
Hook called during termination of this Root, asynchronously after a call toAbstractRoot.Controller.shutdown()
or other cause of termination.protected void
update()
Hook called regularly every time the internal time changes, after allExecutionContext.ClockListener
have been called.
-
-
-
Method Detail
-
initialize
public AbstractRoot.Controller initialize(String id, RootHub hub)
Description copied from interface:Root
Method used by the RootHub to initialize the Root and obtain a Controller. Root implementations will ensure this method can only be invoked once.- Specified by:
initialize
in interfaceRoot
- Parameters:
id
- the unique ID of this Roothub
- the RootHub the Root resides within- Returns:
- Controller for use by the RootHub instance
-
getLookup
public Lookup getLookup()
Description copied from interface:Lookup.Provider
Get the Lookup from this Provider.- Specified by:
getLookup
in interfaceLookup.Provider
- Returns:
- lookup
-
findService
protected ComponentAddress findService(Class<? extends Service> service) throws ServiceUnavailableException
Find a Service address in the lookup.- Parameters:
service
-- Returns:
- service address
- Throws:
ServiceUnavailableException
-
getAddress
protected final ComponentAddress getAddress()
Get the address of this Root. Only valid after initialization.- Returns:
- address
-
getRootHub
protected final RootHub getRootHub()
Get theRootHub
this Root is installed in. Only valid after initialization.- Returns:
- RootHub
-
getRouter
protected final PacketRouter getRouter()
Get thePacketRouter
for this Root. Only valid after initialization.- Returns:
- router
-
getExecutionContext
protected final ExecutionContext getExecutionContext()
Get theExecutionContext
for this Root. Only valid after initialization.- Returns:
- execution context
-
getState
protected AbstractRoot.State getState()
Get the State of this Root.- Returns:
- State
-
createController
protected AbstractRoot.Controller createController()
Method used to create theAbstractRoot.Controller
during initialization. Subclasses may override to customize the Controller.- Returns:
- Controller
-
createContext
protected DefaultExecutionContext createContext(long initialTime)
Method used to create theDefaultExecutionContext
during initialization. Subclasses may override to customize the execution context.- Parameters:
initialTime
- the current clock time of the hub- Returns:
- execution context
-
createRouter
protected PacketRouter createRouter()
Method used to create thePacketRouter
during initialization. The default implementation directly callsRootHub.dispatch(org.praxislive.core.Packet)
. Subclasses may override to provide an alternative that eg. logs all messages.- Returns:
- packet router
-
activating
protected void activating()
Hook called during activation of this Root, asynchronously after a call toAbstractRoot.Controller.start(java.util.concurrent.ThreadFactory)
. The default implementation does nothing.
-
terminating
protected void terminating()
Hook called during termination of this Root, asynchronously after a call toAbstractRoot.Controller.shutdown()
or other cause of termination. The default implementation does nothing.
-
starting
protected void starting()
Hook called during a call tosetRunning()
if the previous state wasAbstractRoot.State.ACTIVE_IDLE
. The default implementation does nothing.
-
stopping
protected void stopping()
Hook called during a call tosetIdle()
if the previous state wasAbstractRoot.State.ACTIVE_RUNNING
. The default implementation does nothing.
-
update
protected void update()
Hook called regularly every time the internal time changes, after allExecutionContext.ClockListener
have been called. The default implementation does nothing.
-
processCall
protected abstract void processCall(Call call, PacketRouter router)
Method called to handle every receivedCall
. The provided router should be used for all ongoing or return calls.- Parameters:
call
-router
-
-
setRunning
protected final boolean setRunning()
Set the Root state toAbstractRoot.State.ACTIVE_RUNNING
. The state will only be changed, andstarting()
called, if the existing state is idle.- Returns:
- true if the state has been set to running
-
setIdle
protected final boolean setIdle()
Set the Root state toAbstractRoot.State.ACTIVE_IDLE
. The state will only be changed, andstopping()
called, if the existing state is running.- Returns:
- true if the state has been set to idle
-
attachDelegate
protected final void attachDelegate(AbstractRoot.Delegate delegate)
Attach aAbstractRoot.Delegate
to this Root. Also callsinterrupt()
.- Parameters:
delegate
-- Throws:
IllegalStateException
- if a delegate is already attached
-
detachDelegate
protected final void detachDelegate(AbstractRoot.Delegate delegate)
Detach the provide delegate (if it is attached). Also callsinterrupt()
.- Parameters:
delegate
-
-
interrupt
protected final void interrupt()
Interrupt the current update cycle, leaving pending calls or tasks to a subsequent update cycle.
-
invokeLater
protected final boolean invokeLater(Runnable task)
Submit a task to be run asynchronously on the main Root thread. The task is added to the same queue as incoming packets.- Parameters:
task
-- Returns:
- true if the task has been successfully submitted
-
delegateConfig
protected static AbstractRoot.DelegateConfiguration delegateConfig()
Create aAbstractRoot.DelegateConfiguration
object for passing up to the constructor when creating aAbstractRoot.Delegate
subclass.- Returns:
- delegate configuration
-
-