- java.lang.Object
-
- org.praxislive.base.AbstractRoot.Delegate
-
- Enclosing class:
- AbstractRoot
protected abstract class AbstractRoot.Delegate extends Object
An abstract delegate class that may be attached to this Root to drive it from another source (eg. audio callback or UI event queue). Subclasses should calldoUpdate(long)
regularly to update the clock time and process pending tasks.The delegate should prefer calling
doTimedPoll(long, java.util.concurrent.TimeUnit)
to thread sleeping or other timing mechanisms where possible so that incoming packets and tasks are processed while waiting.If the other source provides a mechanism for invoking tasks asynchronously (eg. EventQueue.invokeLater(Runnable task)) it should override
onQueueReceipt()
to asynchronously triggerdoPollQueue()
to process tasks and packets between updates.If the other source driving this delegate requires a new Thread to run on, it should obtain it from
getThreadFactory()
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Delegate()
Create a Delegate with the default configuration.protected
Delegate(AbstractRoot.DelegateConfiguration config)
Create a Delegate subclass with the provided configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
doPollQueue()
Poll the queue, running any available tasks and dispatching any packets with a timecode before the current Root time.protected void
doTimedPoll(long time, TimeUnit unit)
Wait up to the given time for a queue notification, running any available tasks and dispatching any packets with a timecode before the current Root time.protected boolean
doUpdate(long time)
Update the Root time and process tasks, inbound calls and clock listeners.protected ThreadFactory
getThreadFactory()
Get a thread factory for creating any new threads required by the delegate.protected boolean
isRootThread()
Check whether the currently executing thread is the current thread executing the delegate.protected void
onQueueReceipt()
Called when a new PacketAbstractRoot.Controller.submitPacket(org.praxislive.core.Packet)
or taskAbstractRoot.invokeLater(java.lang.Runnable)
is submitted.
-
-
-
Constructor Detail
-
Delegate
protected Delegate()
Create a Delegate with the default configuration.
-
Delegate
protected Delegate(AbstractRoot.DelegateConfiguration config)
Create a Delegate subclass with the provided configuration.- Parameters:
config
- delegate configuration
-
-
Method Detail
-
doUpdate
protected final boolean doUpdate(long time)
Update the Root time and process tasks, inbound calls and clock listeners.- Parameters:
time
- new clock time (directly from or related toRootHub.getClock()
- Returns:
- false if the Root has been terminated or the delegate detached
-
doPollQueue
protected final void doPollQueue()
Poll the queue, running any available tasks and dispatching any packets with a timecode before the current Root time.
-
doTimedPoll
protected final void doTimedPoll(long time, TimeUnit unit) throws InterruptedException
Wait up to the given time for a queue notification, running any available tasks and dispatching any packets with a timecode before the current Root time.- Parameters:
time
-unit
-- Throws:
InterruptedException
-
getThreadFactory
protected final ThreadFactory getThreadFactory()
Get a thread factory for creating any new threads required by the delegate.- Returns:
- thread factory for all required threads
-
onQueueReceipt
protected void onQueueReceipt()
Called when a new PacketAbstractRoot.Controller.submitPacket(org.praxislive.core.Packet)
or taskAbstractRoot.invokeLater(java.lang.Runnable)
is submitted. By default signals the thread waiting ondoTimedPoll(long, java.util.concurrent.TimeUnit)
. Can be overridden to perform other notification, but if this implementation is not called then timed polling will not work.
-
isRootThread
protected boolean isRootThread()
Check whether the currently executing thread is the current thread executing the delegate. The AbstractRoot ThreadContext implementation will delegate to this method if the current thread is not inside an update. The default implementation checks whether the current thread is the last thread that calleddoUpdate(long)
ordoPollQueue()
. Implementations may want to override this, eg. to check if the current thread is a UI update thread. As a guide, this method should return true if blocking the current thread would block the root from executing.- Returns:
- current thread is root thread
- See Also:
ThreadContext.isRootThread()
-
-