Class MainModuleEngineImpl

java.lang.Object
io.codetoil.curved_spacetime.api.Engine
io.codetoil.curved_spacetime.MainModuleEngineImpl

public class MainModuleEngineImpl extends Engine
Engine Implementation
  • Field Details

    • logger

      public final Logger logger
      The logger the engine writes its own diagnostics to.
    • callbackExecutor

      protected final ScheduledExecutorService callbackExecutor
      The single thread every callback runs on.
    • callbackLoopHandler

      protected ScheduledFuture<?> callbackLoopHandler
      Tracks the repeating task that drives every registered callback, cancelled by clean().
    • initialized

      protected boolean initialized
      Is true if the Engine is initialized. Note that this doesn't mean all callbacks are registered, As a callback can be registered at any time and is registered asynchronously.
  • Constructor Details

    • MainModuleEngineImpl

      public MainModuleEngineImpl(CurvedSpacetimeLoader loader)
      Creates the engine and brings the whole module graph up.

      Loads the main configuration, registers the initial scene, runs every main entrypoint, and starts the callback loop at the configured frame rate.

      Parameters:
      loader - the loader that resolves module entrypoints
      Throws:
      RuntimeException - if the main configuration cannot be loaded, or if any module's entrypoint fails
  • Method Details

    • registerScene

      public void registerScene(Scene scene)
      Description copied from class: Engine
      Adds a scene and gives every registered generator a callback for it.
      Specified by:
      registerScene in class Engine
      Parameters:
      scene - the scene to add
    • runEntrypoints

      public void runEntrypoints()
      Description copied from class: Engine
      Prepares the loader and runs every module's main entrypoint.
      Specified by:
      runEntrypoints in class Engine
    • callDependents

      public <E> void callDependents(String name, Class<E> moduleInitializerClass, Consumer<E> onInitialize, Logger logger) throws Throwable
      Description copied from class: Engine
      Invokes every entrypoint registered under the given name, concurrently, and waits for all of them.

      The pool this uses is unbounded, and must remain so. The dependency handshake blocks a consumer in TransferQueue.take while its producer is still to run, so a producer and its consumer have to be runnable at the same time; a bounded pool deadlocks as soon as its threads fill with consumers whose producers are still queued.

      The first entrypoint to fail aborts the whole call rather than leaving a half-initialized module graph running.

      Specified by:
      callDependents in class Engine
      Type Parameters:
      E - the entrypoint type
      Parameters:
      name - the entrypoint name to invoke, such as main
      moduleInitializerClass - the type registered entrypoints must be assignable to
      onInitialize - the action to apply to each entrypoint
      logger - the logger to trace dispatch through
      Throws:
      Throwable - whatever the first failing entrypoint threw
    • getCurvedSpacetimeLoader

      public CurvedSpacetimeLoader getCurvedSpacetimeLoader()
      Description copied from class: Engine
      Returns the loader this engine resolves entrypoints through.
      Overrides:
      getCurvedSpacetimeLoader in class Engine
      Returns:
      the loader
    • registerMainCallback

      public CompletableFuture<Void> registerMainCallback(MainCallback mainCallback)
      Description copied from class: Engine
      Registers scene-independent work and initialises it on the callback thread.
      Specified by:
      registerMainCallback in class Engine
      Parameters:
      mainCallback - the callback to register
      Returns:
      the completable future generated for this task
    • registerSceneCallbackGenerator

      public CompletableFuture<Void> registerSceneCallbackGenerator(Function<Scene, SceneCallback> sceneCallbackGenerator)
      Description copied from class: Engine
      Registers a factory that produces one callback per scene.

      The generator is applied to every scene that exists now, and to each one registered later, so a module never has to track scenes itself.

      Specified by:
      registerSceneCallbackGenerator in class Engine
      Parameters:
      sceneCallbackGenerator - the factory producing a callback for a given scene
      Returns:
      the completable future generated for this task
    • clean

      public void clean()
      Description copied from class: Engine
      Stops the callback loop, releases every callback, and then shuts the callback executor down.
      Specified by:
      clean in class Engine
    • throwError

      public void throwError(Throwable t, String message)
      Description copied from class: Engine
      Logs a failure at SEVERE and stops the engine.
      Specified by:
      throwError in class Engine
      Parameters:
      t - the failure to log
      message - the message to log it with
    • getConsoleHandler

      public ConsoleHandler getConsoleHandler()
      Description copied from class: Engine
      Returns the console handler used for the Curved Spacetime Loggers.
      Specified by:
      getConsoleHandler in class Engine
      Returns:
      the console handler
    • isInitialized

      public boolean isInitialized()
      Description copied from class: Engine
      Returns whether this engine has finished initializing.

      That does not mean every callback is registered: a callback can be registered at any time, and registration is asynchronous.

      Specified by:
      isInitialized in class Engine
      Returns:
      true if the engine is initialized
    • getLoggerLevel

      public Level getLoggerLevel()
      Description copied from class: Engine
      Returns the level every module logger is set to.
      Specified by:
      getLoggerLevel in class Engine
      Returns:
      the logger level from this engine's configuration
    • getFPS

      public int getFPS()
      Description copied from class: Engine
      Returns how many times per second the callback loop runs.
      Specified by:
      getFPS in class Engine
      Returns:
      the configured frame rate, in frames per second