Class CodeContext<D extends CodeDelegate>

java.lang.Object
org.praxislive.code.CodeContext<D>
Type Parameters:
D - supported delegate type
Direct Known Subclasses:
AudioCodeContext, CodeContainer.Context, CodeRoot.Context, P2DCodeContext, P3DCodeContext, VideoCodeContext

public abstract class CodeContext<D extends CodeDelegate> extends Object
A CodeContext wraps each CodeDelegate, managing state and the transition from one iteration of delegate to the next on behalf of a CodeComponent.
  • Constructor Details

    • CodeContext

      protected CodeContext(CodeConnector<D> connector)
      Create a CodeContext by processing the provided CodeConnector (containing CodeDelegate).
      Parameters:
      connector - code connector with delegate
    • CodeContext

      protected CodeContext(CodeConnector<D> connector, boolean requireClock)
      Create a CodeContext by processing the provided CodeConnector (containing CodeDelegate). This constructor takes a boolean to force connecting the context to the execution clock, should the subtype always require clock signals.
      Parameters:
      connector - code connector with delegate
      requireClock - true to force clock connection
  • Method Details

    • configure

      protected void configure(CodeComponent<D> cmp, CodeContext<D> oldCtxt)
      A hook method that will be called when the CodeContext is configured on a component. It is called after controls, ports and refs have been configured. Subclasses may override this to do additional configuration. The default implementation does nothing.
      Parameters:
      cmp - component being attached to
      oldCtxt - previous context, or null if there was none
    • hierarchyChanged

      protected void hierarchyChanged()
      Called when the hierarchy changes, which might be because the component hierarchy has changed (see Component.hierarchyChanged()), the context has been added or is being removed from the component, or for any other reason that cached information should be invalidated (eg. anything retrieved from the lookup). Subclasses may override this to handle such events / invalidate lookup results. The default implementation does nothing.
    • onInit

      protected void onInit()
      Hook called when the code context becomes active, either as a result of the execution context becoming active or the code context being attached to a component within an active execution context.
    • onStart

      protected void onStart()
      Hook called when the execution context becomes active. The onInit() hook will always have been called before this hook.
    • onStop

      protected void onStop()
      Hook called when the execution context is stopping.
    • onReset

      protected void onReset()
      Hook called when the context is being reset.
    • tick

      protected void tick(ExecutionContext source)
      Hook called by the clock listener on the execution context. The default implementation does nothing.
      Parameters:
      source - execution context
    • resetAndInitialize

      protected final void resetAndInitialize()
      Reset and (if active) reinitialize all control, port and reference descriptors. This does not call the onReset() or onInit() hooks.
    • dispose

      protected void dispose()
      Hook called during disposal of code context. The default implementation does nothing.
    • getComponent

      public CodeComponent<D> getComponent()
      Get the code component this code context is attached to, if there is one.
      Returns:
      code component, or null
    • getDelegate

      public D getDelegate()
      Get the delegate this context wraps.
      Returns:
      delegate
    • getControl

      protected Control getControl(String id)
      Get the control to handle the specified ID, or null if there isn't one.
      Parameters:
      id - control ID
      Returns:
      control or null
    • getControlDescriptor

      protected ControlDescriptor getControlDescriptor(String id)
      Get the control descriptor for the specified ID, or null if there isn't one.
      Parameters:
      id - control ID
      Returns:
      control descriptor or null
    • controlIDs

      protected Stream<String> controlIDs()
      Get all the available control IDs.
      Returns:
      control IDs
    • getPort

      protected Port getPort(String id)
      Get the port with the specified ID, or null if there isn't one.
      Parameters:
      id - port ID
      Returns:
      port or null
    • getPortDescriptor

      protected PortDescriptor getPortDescriptor(String id)
      Get the port descriptor for the specified ID, or null if there isn't one.
      Parameters:
      id - port ID
      Returns:
      port descriptor or null
    • portIDs

      protected Stream<String> portIDs()
      Get the available port IDs.
      Returns:
      port IDs
    • getInfo

      protected ComponentInfo getInfo()
      Get component info.
      Returns:
      component info
    • getAddress

      protected ControlAddress getAddress(Control control)
      Find the address of the passed in control, or null if it does not have one.
      Parameters:
      control - control to find address for
      Returns:
      control address or null
    • getLookup

      public Lookup getLookup()
      Get lookup.
      Returns:
      lookup
    • locateService

      public Optional<ComponentAddress> locateService(Class<? extends Service> type)
      Locate the provided service type, if available.
      Parameters:
      type - service to lookup
      Returns:
      optional service address
    • getTime

      public long getTime()
      Get current time in nanoseconds.
      Returns:
      time in nanoseconds
    • addClockListener

      public void addClockListener(CodeContext.ClockListener listener)
      Add a clock listener. Resources used inside code delegates should add a clock listener rather than listen directly on the execution context.
      Parameters:
      listener - clock listener
    • removeClockListener

      public void removeClockListener(CodeContext.ClockListener listener)
      Remove a clock listener.
      Parameters:
      listener - to remove
    • getExecutionContext

      protected ExecutionContext getExecutionContext()
      Get the execution context, or null if not attached.
      Returns:
      execution context, or null
    • checkActive

      protected boolean checkActive()
      Check whether the CodeContext is running inside an ExecutionContext with active state. If the execution context is active, but a transition to active has not yet been handled in this code context, the state transition will be triggered.
      Returns:
      true if active
    • update

      protected void update(long time)
      Update the time in this context to the specified time. A value the same or behind the current value will be ignored. This method will call all clock listeners.
      Parameters:
      time - updated time
    • invoke

      public void invoke(long time, Runnable task)
      Invoke the provided task, if the context is active, and after updating the clock to the specified time (if later). Any exception will be caught and logged, and the context will be flushed.
      Parameters:
      time - new clock time
      task - runnable task to execute
    • invokeCallable

      public <V> V invokeCallable(long time, Callable<V> task) throws Exception
      Invoke the provided task and return the result, if the context is active, and after updating the clock to the specified time (if later). Any exception will be logged and rethrown, and the context flushed. Throws an IllegalStateException if checkActive() returns false.
      Type Parameters:
      V - the result type of method call
      Parameters:
      time - new clock time
      task - runnable task to execute
      Returns:
      result
      Throws:
      Exception - if unable to compute a result
    • flush

      protected void flush()
      Flush the code context. By default this message checks for pending log messages and delivers to the log.
    • getLog

      public LogBuilder getLog()
      Get the log builder for writing log messages.
      Returns:
      log builder
    • getLogLevel

      protected LogLevel getLogLevel()
      Get the active log level.
      Returns:
      active log level
    • log

      protected void log(LogBuilder log)
      Process and send messages from an external log builder.
      Parameters:
      log - external log builder