Class IiziGoInstances

java.lang.Object
com.iizix.jetty.IiziGoInstances

public final class IiziGoInstances extends Object
The registry of the iiziGo instances of the user that run now, and the port offsets their workspaces claim (CC-FIX-PORT-RANGE-A, R-D-5 and R-D-6 of CC-SURVEY-PORT-RANGE). Two iiziGo side by side - two workspaces, or a UI harness beside the owner's iiziGo - share four fixed things of the user: the web server ports, the QUIC work directory, the Derby server on 1527 and the WebView2 cache folder. This registry is what each of them asks.
  • ~/.iizi/com.iizigo/instances/<pid>.properties: one file per live iiziGo, written once its web server has bound (workspace, workspace ID, ports, build, start time), deleted at its exit; a file whose process is gone is pruned by the next reader. The process start time is recorded too, so that a PID the operating system reused is not taken for the iiziGo that wrote the file.
  • ~/.iizi/com.iizigo/ports.properties: the offset of the port range each workspace bound last (its claim), so that a workspace falling back from the first ports avoids the offsets other workspaces use. A workspace is one claim: a claim that names the same workspace directory under another ID (an ID an earlier iiziGo generated and lost before it was saved) is replaced, never left to count as another workspace (CC-FIX-PWA-PAGE-PORTS, ruling PW-5). The first ports of the ranges, the ones of Preferences > IIZI > Web Server, are tried first whatever the claims say: they are taken whenever they are free.

No socket and no new dependency: plain files, written to a temporary name and renamed onto the real one; the claims file is changed under a file lock. The class lives in com.iizix.jetty, beside the port range it serves, so that its decisions are tested headless in com.iizix.jetty.tests; the iiziGo plug-ins call it.

Author:
Christopher Mindus
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    What a starting iiziGo does with the WebView2 cache folder (R-D-17).
    static enum 
    What an exiting iiziGo does with the Derby network server (R-D-16).
    static final record 
    One live iiziGo.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the default directory.
    IiziGoInstances(File dir, long ownPid)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    cacheRotation(boolean buildChanged, boolean othersLive)
    Decides what a starting iiziGo does with the WebView2 cache folder (R-D-17).
    static int[]
    candidateOffsets(int count, int sticky, Collection<Integer> claimedByOthers)
    Orders the offsets of a port range for a start: first offset 0, the ports of Preferences > IIZI > Web Server, whatever the claims and the sticky offset say - they are taken whenever they are free (ruling PW-5 of CC-FIX-PWA-PAGE-PORTS, which supersedes the sticky-first order of R-D-4/R-D-5); then the workspace's own sticky offset when it is inside the range; then every offset no other workspace claims; then the offsets other workspaces claim - a port is taken from another workspace only when nothing else is free.
    void
    claim(String workspaceId, String workspace, int offset)
    Records the offset a workspace bound.
    claimedByOthers(String workspaceId)
    Gets the offsets other workspaces claim.
    claimedWorkspace(String workspaceId)
    Gets the workspace directory recorded with a claim.
    Gets the claims: the offset each workspace bound last.
    static File
    Gets the default iiziGo user directory, ~/.iizi/com.iizigo.
    derbyExit(boolean autostop, boolean startedHere, boolean leftRunningByIiziGo, boolean othersLive)
    Decides what an exiting iiziGo does with the Derby network server (R-D-16): the last iiziGo out stops it, and no iiziGo stops a Derby another live iiziGo uses.
    void
    forget(String workspaceId)
    Removes the claim of a workspace ("Forget" on the Preferences page).
    holderOf(int port)
    Gets the live iiziGo, other than this one, that holds a port.
    Gets the other iiziGo instances that run now, pruning the files of those that are gone.
    void
    Registers this iiziGo, replacing an earlier registration of it (a restart of its web server).
    Gets the registration of this iiziGo.
    void
    Removes the registration of this iiziGo.
  • Constructor Details

    • IiziGoInstances

      public IiziGoInstances()
      Constructor for the default directory.
    • IiziGoInstances

      public IiziGoInstances(File dir, long ownPid)
      Constructor.
      Parameters:
      dir - The iiziGo user directory: the registry lives in instances and ports.properties under it.
      ownPid - The process ID that counts as "this iiziGo".
  • Method Details

    • defaultDirectory

      public static File defaultDirectory()
      Gets the default iiziGo user directory, ~/.iizi/com.iizigo.
      Returns:
      The directory (it may not exist yet).
    • register

      public void register(IiziGoInstances.Instance instance)
      Registers this iiziGo, replacing an earlier registration of it (a restart of its web server).
      Parameters:
      instance - The instance; its PID must be this process's.
    • unregister

      public void unregister()
      Removes the registration of this iiziGo.
    • self

      public IiziGoInstances.Instance self()
      Gets the registration of this iiziGo.
      Returns:
      The instance, null when not registered.
    • liveOthers

      public List<IiziGoInstances.Instance> liveOthers()
      Gets the other iiziGo instances that run now, pruning the files of those that are gone.
      Returns:
      The live instances other than this one, ordered by PID.
    • holderOf

      public IiziGoInstances.Instance holderOf(int port)
      Gets the live iiziGo, other than this one, that holds a port.
      Parameters:
      port - The TCP or UDP port.
      Returns:
      The instance, null when no other live iiziGo holds it.
    • claims

      public Map<String,Integer> claims()
      Gets the claims: the offset each workspace bound last.
      Returns:
      The offsets by workspace ID.
    • claimedWorkspace

      public String claimedWorkspace(String workspaceId)
      Gets the workspace directory recorded with a claim.
      Parameters:
      workspaceId - The workspace ID.
      Returns:
      The directory, null for none.
    • claimedByOthers

      public Set<Integer> claimedByOthers(String workspaceId)
      Gets the offsets other workspaces claim.
      Parameters:
      workspaceId - This workspace's ID.
      Returns:
      The offsets claimed by the others.
    • claim

      public void claim(String workspaceId, String workspace, int offset)
      Records the offset a workspace bound. A claim of the same workspace directory under another ID is removed: it is this workspace's, left by an iiziGo that generated its ID and never saved it (killed, or ended before its preferences were written), and it must not count as another workspace's claim (ruling PW-5: three such claims moved the owner's iiziGo to 2801 and then 2802 after two kills).
      Parameters:
      workspaceId - The workspace ID.
      workspace - The workspace directory, for messages and for the match above.
      offset - The offset.
    • forget

      public void forget(String workspaceId)
      Removes the claim of a workspace ("Forget" on the Preferences page).
      Parameters:
      workspaceId - The workspace ID.
    • candidateOffsets

      public static int[] candidateOffsets(int count, int sticky, Collection<Integer> claimedByOthers)
      Orders the offsets of a port range for a start: first offset 0, the ports of Preferences > IIZI > Web Server, whatever the claims and the sticky offset say - they are taken whenever they are free (ruling PW-5 of CC-FIX-PWA-PAGE-PORTS, which supersedes the sticky-first order of R-D-4/R-D-5); then the workspace's own sticky offset when it is inside the range; then every offset no other workspace claims; then the offsets other workspaces claim - a port is taken from another workspace only when nothing else is free.
      Parameters:
      count - The number of offsets in the range, at least 1.
      sticky - The workspace's sticky offset, -1 for none.
      claimedByOthers - The offsets other workspaces claim.
      Returns:
      The offsets in the order to try, each once.
    • derbyExit

      public static IiziGoInstances.DerbyExit derbyExit(boolean autostop, boolean startedHere, boolean leftRunningByIiziGo, boolean othersLive)
      Decides what an exiting iiziGo does with the Derby network server (R-D-16): the last iiziGo out stops it, and no iiziGo stops a Derby another live iiziGo uses. A Derby this process did not start is stopped only when an iiziGo started it (its derbyserver.pid names a live process) - never one the user started by hand.
      Parameters:
      autostop - The preference "auto-stop" of the database server.
      startedHere - true when this process started the Derby that runs.
      leftRunningByIiziGo - true when a Derby started by an earlier iiziGo still runs.
      othersLive - true when another iiziGo runs now.
      Returns:
      The action.
    • cacheRotation

      public static IiziGoInstances.CacheRotation cacheRotation(boolean buildChanged, boolean othersLive)
      Decides what a starting iiziGo does with the WebView2 cache folder (R-D-17).
      Parameters:
      buildChanged - true when the folder was last set up by another build.
      othersLive - true when another iiziGo runs now.
      Returns:
      The action.