java.lang.Object
net.i2p.router.update.ConsoleUpdateManager
All Implemented Interfaces:
ClientApp, RouterApp, UpdateManager

public class ConsoleUpdateManagerextends Object implements UpdateManager, RouterApp
The central resource coordinating updates. This must be registered with the context.

The UpdateManager starts and stops all updates, prevents multiple updates as appropriate, and controls notification to the user.

Version notes: For news and unsigned updates, use Long.toString(long, int)modtime).

  • Field Details

  • Constructor Details

    • ConsoleUpdateManager

      public ConsoleUpdateManager(RouterContext ctx, ClientAppManager listener, String[] args)
      Creates the manager; the update handlers for the router, plugins, news and the desktop GUI are registered and checks scheduled in startup().
      Parameters:
      ctx - router context supplying the update policy, timers and logging
      listener - client app manager to register this with and to notify of state changes
      args - command line arguments, unused
  • Method Details

    • getInstance

      public static ConsoleUpdateManager getInstance()
      Finds the registered console update manager app.
      Returns:
      the running instance, null if the app is not registered
    • start

      public void start()
      UpdateManager interface.
      Specified by:
      start in interface UpdateManager
    • startup

      public void startup()
      ClientApp interface.
      Specified by:
      startup in interface ClientApp
    • shutdown

      public void shutdown()
      UpdateManager interface.
      Specified by:
      shutdown in interface UpdateManager
    • shutdown

      public void shutdown(String[] args)
      ClientApp interface.
      Specified by:
      shutdown in interface ClientApp
      Parameters:
      args - command line arguments, unused
    • getState

      public ClientAppState getState()
      Current state of the updater.
      Specified by:
      getState in interface ClientApp
      Returns:
      the state the updater is in
    • getName

      public String getName()
      Application name.
      Specified by:
      getName in interface ClientApp
      Returns:
      the name the console shows for this plugin
    • getDisplayName

      public String getDisplayName()
      Name shown in the console plugin list.
      Specified by:
      getDisplayName in interface ClientApp
      Returns:
      the human-readable plugin name
    • changeState

      private void changeState(ClientAppState state)
      Record the app state and notify the client app manager of the change.
      Parameters:
      state - the new state of this client app
    • getStatus

      public String getStatus()
      The status on any update current or last finished.
      Specified by:
      getStatus in interface UpdateManager
      Returns:
      status or ""
    • checkAvailable

      public String checkAvailable(UpdateType type)
      Is an update available? Blocking. An available update may still have a constraint or lack sources.
      Specified by:
      checkAvailable in interface UpdateManager
      Parameters:
      type - the UpdateType of this request
      Returns:
      new version or null if nothing newer is available
    • checkAvailable

      public String checkAvailable(UpdateType type, long maxWait)
      Is an update available? Blocking. An available update may still have a constraint or lack sources.
      Specified by:
      checkAvailable in interface UpdateManager
      Parameters:
      type - the UpdateType of this request
      maxWait - max time to block
      Returns:
      new version or null if nothing newer is available
    • checkAvailable

      public String checkAvailable(UpdateType type, String id, long maxWait)
      Is an update available? Blocking. An available update may still have a constraint or lack sources.
      Specified by:
      checkAvailable in interface UpdateManager
      Parameters:
      type - the UpdateType of this request
      id - id of this request
      maxWait - max time to block
      Returns:
      new version or null if nothing newer is available
    • check

      public void check(UpdateType type)
      Fire off a checker task Non-blocking.
      Parameters:
      type - the UpdateType of this request
    • check

      public void check(UpdateType type, String id)
      Fire off a checker task Non-blocking.
      Parameters:
      type - the UpdateType of this request
      id - id of this request
    • getUpdateAvailable

      public String getUpdateAvailable(UpdateType type)
      Is an update available? Non-blocking, returns result of last check or notification from an Updater. An available update may still have a constraint or lack sources.
      Parameters:
      type - the UpdateType of this request
      Returns:
      new version or null if nothing newer is available
    • getUpdateAvailable

      public String getUpdateAvailable(UpdateType type, String id)
      Is an update available? Non-blocking, returns result of last check or notification from an Updater. An available update may still have a constraint or lack sources.
      Parameters:
      type - the UpdateType of this request
      id - id of this request
      Returns:
      new version or null if nothing newer is available
    • getUpdateDownloaded

      public String getUpdateDownloaded(UpdateType type)
      Is an update downloaded? Non-blocking, returns result of last download.
      Parameters:
      type - the UpdateType of this request
      Returns:
      new version or null if nothing was downloaded
    • getUpdateDownloaded

      public String getUpdateDownloaded(UpdateType type, String id)
      Is an update downloaded? Non-blocking, returns result of last download.
      Parameters:
      type - the UpdateType of this request
      id - id of this request
      Returns:
      new version or null if nothing was downloaded
    • getDownloadedOrInstalledVersion

      private String getDownloadedOrInstalledVersion(UpdateType type, String id)
      The highest of the installed or downloaded version.
      Parameters:
      type - the UpdateType to look up
      id - id of the request to look up
      Returns:
      new version or null if nothing was downloaded or installed
    • isUpdateInProgress

      public boolean isUpdateInProgress()
      Is any download in progress? Does not include checks.
      Specified by:
      isUpdateInProgress in interface UpdateManager
      Returns:
      whether update in progress
    • isUpdateInProgress

      public boolean isUpdateInProgress(UpdateType type)
      Is a download in progress?
      Specified by:
      isUpdateInProgress in interface UpdateManager
      Parameters:
      type - the UpdateType of this request
      Returns:
      whether update in progress
    • isUpdateInProgress

      public boolean isUpdateInProgress(UpdateType type, String id)
      Is a download in progress?
      Specified by:
      isUpdateInProgress in interface UpdateManager
      Parameters:
      type - the UpdateType of this request
      id - of this request
      Returns:
      whether update in progress
    • stopUpdates

      public void stopUpdates()
      Stop all downloads in progress.
    • stopUpdate

      public void stopUpdate(UpdateType type)
      Stop this download.
      Parameters:
      type - the UpdateType of this download
    • stopUpdate

      public void stopUpdate(UpdateType type, String id)
      Stop this download.
      Parameters:
      type - the UpdateType of this download
      id - id of this download
    • isCheckInProgress

      public boolean isCheckInProgress()
      Is any check in progress? Does not include updates.
      Returns:
      whether check in progress
    • isCheckInProgress

      public boolean isCheckInProgress(UpdateType type)
      Is a check in progress?
      Parameters:
      type - the UpdateType of this request
      Returns:
      whether check in progress
    • isCheckInProgress

      public boolean isCheckInProgress(UpdateType type, String id)
      Is a check in progress?
      Parameters:
      type - the UpdateType of this request
      id - id of this request
      Returns:
      whether check in progress
    • stopChecks

      public void stopChecks()
      Stop all checks in progress.
    • stopCheck

      public void stopCheck(UpdateType type)
      Stop this check.
      Parameters:
      type - the UpdateType of this check
    • stopCheck

      public void stopCheck(UpdateType type, String id)
      Stop this check.
      Parameters:
      type - the UpdateType of this check
      id - id of this check
    • isExternalRestartPending

      public boolean isExternalRestartPending()
      A router update had been downloaded and handled by an UpdatePostProcessor. It will provide wrapper-like function to install the update and restart after shutdown.
      Returns:
      whether external restart pending
    • installPlugin

      public boolean installPlugin(String name, URI uri)
      Install a plugin. Non-blocking. If returns true, then call isUpdateInProgress() in a loop.
      Parameters:
      name - if null, a new install
      uri - where to fetch the plugin, a file: URI installs without a network fetch
      Returns:
      true if task started
    • update

      public boolean update(UpdateType type)
      Non-blocking. Does not check. If returns true, then call isUpdateInProgress() in a loop Max time 3 hours by default but not honored by all Updaters.
      Specified by:
      update in interface UpdateManager
      Parameters:
      type - the UpdateType of this request
      Returns:
      true if task started
    • update

      public boolean update(UpdateType type, String id)
      Non-blocking. Does not check. Max time 3 hours by default but not honored by all Updaters If returns true, then call isUpdateInProgress() in a loop.
      Specified by:
      update in interface UpdateManager
      Parameters:
      type - the UpdateType of this request
      id - id of this request
      Returns:
      true if task started
    • update

      public boolean update(UpdateType type, long maxTime)
      Non-blocking. Does not check. If returns true, then call isUpdateInProgress() in a loop.
      Specified by:
      update in interface UpdateManager
      Parameters:
      type - the UpdateType of this request
      maxTime - not honored by all Updaters
      Returns:
      true if task started
    • update

      public boolean update(UpdateType type, String id, long maxTime)
      Non-blocking. Does not check. Fails if check or update already in progress. If returns true, then call isUpdateInProgress() in a loop.
      Specified by:
      update in interface UpdateManager
      Parameters:
      type - the UpdateType of this request
      id - id of this request
      maxTime - not honored by all Updaters
      Returns:
      true if task started
    • update_fromCheck

      private boolean update_fromCheck(UpdateType type, String id, long maxTime)
      Non-blocking. Does not check. Fails update already in progress. Use this to call from within a checker task. If returns true, then call isUpdateInProgress() in a loop.
      Parameters:
      type - the UpdateType to update
      id - id of the request to update
      maxTime - not honored by all Updaters
      Returns:
      true if task started
    • retry

      Schedules a retry of a failed update.
      Parameters:
      ui - the update item to retry
      sourceMap - the source URIs, by method
      toTry - the updaters still to try
      maxTime - the maximum time for the whole update
      Returns:
      the scheduled task, null if none could be scheduled
    • register

      public void register(Updater updater, UpdateType type, UpdateMethod method, int priority)
      Add the Updater to the set consulted for each update type and method.
      Specified by:
      register in interface UpdateManager
      Parameters:
      updater - the Updater to be told when a newer version appears
      type - the update type
      method - the update method
      priority - higher values are notified first
    • unregister

      public void unregister(Updater updater, UpdateType type, UpdateMethod method)
      Remove the Updater previously registered for the type and method.
      Specified by:
      unregister in interface UpdateManager
      Parameters:
      updater - the Updater to stop notifying
      type - the update type
      method - the update method
    • register

      public void register(Checker updater, UpdateType type, UpdateMethod method, int priority)
      Add the Checker to the set consulted for each update type and method.
      Specified by:
      register in interface UpdateManager
      Parameters:
      updater - the Checker to poll for a newer version
      type - the update type
      method - the update method
      priority - higher values are polled first
    • unregister

      public void unregister(Checker updater, UpdateType type, UpdateMethod method)
      Remove the Checker previously registered for the type and method.
      Specified by:
      unregister in interface UpdateManager
      Parameters:
      updater - the Checker to stop polling
      type - the update type
      method - the update method
    • register

      public void register(UpdatePostProcessor upp, UpdateType type, int fileType)
      Register a post-processor for this UpdateType and SU3File file type.
      Specified by:
      register in interface UpdateManager
      Parameters:
      upp - update post processor
      type - only ROUTER_SIGNED_SU3 and ROUTER_DEV_SU3 are currently supported
      fileType - a SU3File TYPE_xxx constant, 1-255, TYPE_ZIP not supported.
    • notifyVersionAvailable

      public boolean notifyVersionAvailable(UpdateTask task, URI newsSource, UpdateType type, String id, UpdateMethod method, List<URI> updateSources, String newVersion, String minVersion)
      Called by the Updater, either after check(UpdateType) was called, or it found out on its own. Use this if there is only one UpdateMethod; otherwise use the Map method below.
      Specified by:
      notifyVersionAvailable in interface UpdateManager
      Parameters:
      task - the running check or download this report concerns
      newsSource - who told us
      type - the UpdateType selecting which update is being reported on
      id - plugin name for plugins, ignored otherwise
      method - How to get the new version
      updateSources - Where to get the new version
      newVersion - The new version available
      minVersion - The minimum installed version to be able to update to newVersion
      Returns:
      true if it's newer
    • notifyVersionAvailable

      public boolean notifyVersionAvailable(UpdateTask task, URI newsSource, UpdateType type, String id, Map<UpdateMethod, List<URI>> sourceMap, String newVersion, String minVersion)
      Called by the Checker, either after check() was called, or it found out on its own. Checkers must use this method if there are multiple UpdateMethods discovered simultaneously.
      Specified by:
      notifyVersionAvailable in interface UpdateManager
      Parameters:
      task - the running check or download this report concerns
      newsSource - who told us
      type - the UpdateType selecting which update is being reported on
      id - plugin name for plugins, ignored otherwise
      sourceMap - Mapping of methods to sources
      newVersion - The new version available
      minVersion - The minimum installed version to be able to update to newVersion
      Returns:
      true if we didn't know already
    • notifyVersionConstraint

      public void notifyVersionConstraint(UpdateTask task, URI newsSource, UpdateType type, String id, String newVersion, String message)
      A new version is available but cannot be downloaded or installed due to some constraint. The manager should notify the user. Called by the Checker, either after check() was called, or it found out on its own.
      Specified by:
      notifyVersionConstraint in interface UpdateManager
      Parameters:
      task - the running check or download this report concerns
      newsSource - who told us
      type - the UpdateType selecting which update is being reported on
      id - plugin name for plugins, ignored otherwise
      newVersion - The new version available
      message - A translated message to be displayed to the user, non-null
    • notifyCheckComplete

      public void notifyCheckComplete(UpdateTask task, boolean newer, boolean success)
      Called by the Updater after check() was called and all notifyVersionAvailable(UpdateTask, URI, UpdateType, String, UpdateMethod, List, String, String) callbacks are finished.
      Specified by:
      notifyCheckComplete in interface UpdateManager
      Parameters:
      task - the running check or download this report concerns
      newer - notifyVersionAvailable was called
      success - check succeeded (newer or not)
    • notifyProgress

      public void notifyProgress(UpdateTask task, String status, long downloaded, long totalSize)
      Publish the download status text and progress bar to the console.
      Specified by:
      notifyProgress in interface UpdateManager
      Parameters:
      task - the running check or download this report concerns
      status - translated message to show to the user, may contain HTML
      downloaded - bytes received so far, for the progress bar
      totalSize - expected total bytes, for the progress bar
    • notifyProgress

      public void notifyProgress(UpdateTask task, String status)
      May be null.
      Specified by:
      notifyProgress in interface UpdateManager
      Parameters:
      task - may be null
      status - translated message to show to the user, may contain HTML
    • notifyComplete

      public void notifyComplete(UpdateTask task, String status)
      An expiring status.
      Parameters:
      task - may be null
      status - the message to show, cleared when it expires
    • notifyAttemptFailed

      public void notifyAttemptFailed(UpdateTask task, String reason, Throwable t)
      Not necessarily the end if there are more URIs to try.
      Specified by:
      notifyAttemptFailed in interface UpdateManager
      Parameters:
      task - checker or updater
      reason - a short translated description of what went wrong
      t - may be null
    • notifyTaskFailed

      public void notifyTaskFailed(UpdateTask task, String reason, Throwable t)
      The task has finished and failed.
      Specified by:
      notifyTaskFailed in interface UpdateManager
      Parameters:
      task - checker or updater
      reason - a short translated description of what went wrong
      t - may be null
    • notifyComplete

      public boolean notifyComplete(UpdateTask task, String actualVersion, File file)
      An update has been downloaded but not verified. The manager will verify it. Caller should delete the file upon return, unless it will share it with others, e.g. on a torrent. If the return value is false, caller must call notifyTaskFailed(UpdateTask, String, Throwable) or notifyComplete() again.
      Specified by:
      notifyComplete in interface UpdateManager
      Parameters:
      task - must be an Updater, not a Checker
      actualVersion - may be higher (or lower?) than the version requested
      file - a valid format for the task's UpdateType, or null if it did the installation itself
      Returns:
      true if valid, false if corrupt
    • notifyInstalled

      public void notifyInstalled(UpdateType type, String id, String version)
      Adds to installed, removes from downloaded and available.
      Specified by:
      notifyInstalled in interface UpdateManager
      Parameters:
      type - the UpdateType whose installed version is being recorded or cleared
      id - subtype for plugins, or ""
      version - null to remove from installed
    • notifyDownloaded

      private void notifyDownloaded(UpdateType type, String id, String version)
      Adds to downloaded, removes from available.
      Parameters:
      type - the UpdateType that was downloaded
      id - id of the downloaded item
      version - the version string that was downloaded
    • shouldInstall

      boolean shouldInstall()
      From NewsFetcher.
      Returns:
      false if the update policy is notify-only or news has suppressed installs, or a downloaded router update zip is still pending
    • getUpdateURLs

      public List<URI> getUpdateURLs(UpdateType type, String id, UpdateMethod method)
      Where to find various resources.
      Parameters:
      type - the UpdateType of this request
      id - id of this request
      method - the transport to list sources for
      Returns:
      non-null may be empty
    • getUpdateConstraint

      public String getUpdateConstraint(UpdateType type, String id)
      Is there a reason we can't download the update?
      Parameters:
      type - the UpdateType of this request
      id - id of this request
      Returns:
      translated constraint or null
    • handleRouterFile

      private boolean handleRouterFile(URI uri, String actualVersion, File f, UpdateType updateType)
      Process sud, su2, or su3. Only for router updates.
      Parameters:
      uri - the download source, recorded in the update status
      actualVersion - the version string reported by the download
      f - the downloaded file to verify and stage
      updateType - router update type selecting the verification path
      Returns:
      true if the file verified and was staged for install at next restart, false if verification or the copy failed
    • handleUnsignedFile

      private boolean handleUnsignedFile(URI uri, String lastmod, File updFile)
      Only for router updates.
      Parameters:
      uri - download URI, used only for the status text and to tell an I2P+ (skank) build from a stock one
      lastmod - server-supplied last-modified timestamp in ms, as a string; the current time is used when null or unparseable
      updFile - the downloaded file, verified as a zip and then copied into the router directory
      Returns:
      true if the verified file was copied for install at next restart
    • restart

      private void restart()
      Restarts the router console.
    • linkify

      static String linkify(String url)
      Convert a URL to an HTML clickable link.
      Parameters:
      url - the URL, truncated in the label if longer than 28 characters
      Returns:
      an anchor tag opening in a new window
    • _t

      public String _t(String s)
      Translate a string.
      Parameters:
      s - the message key to look up
      Returns:
      the translated text, or the key itself if there is no translation
    • _t

      public String _t(String s, Object o)
      Translate a string with a parameter.
      Parameters:
      s - the message key to look up
      o - substituted for {0} in the message
      Returns:
      the translated text, or the key itself if there is no translation
    • _t

      public String _t(String s, Object o, Object o2)
      Translate a string with parameters.
      Parameters:
      s - the message key to look up
      o - substituted for {0} in the message
      o2 - substituted for {1} in the message
      Returns:
      the translated text, or the key itself if there is no translation
    • updateStatus

      private void updateStatus(String s)
      Sets the status message shown in the console.
      Parameters:
      s - the message to show
    • finishStatus

      private void finishStatus(String msg)
      Shows the final status message and schedules its cleanup.
      Parameters:
      msg - the message to show
    • getDebugCounts

      public int[] getDebugCounts()
      Debug overview counts as {installed, downloaded, available}, where available counts items with an update available from any source, including the internal placeholder.
      Returns:
      the counts, never null
    • renderStatusHTML

      public void renderStatusHTML(Writer out) throws IOException
      Render update manager status HTML for the debug page as one table with two sections - update items, then registered checkers, updaters and post-processors - sharing a column grid so the content aligns. Name, type and method lead, then role, and status is a colored dot with the state as a tooltip.
      Specified by:
      renderStatusHTML in interface UpdateManager
      Parameters:
      out - writer to output to
      Throws:
      IOException - if writing fails
    • buildUpdateTable

      static String buildUpdateTable(List<String[]> items, List<String[]> workers)
      Build the two-section table from the rows of buildItemRows {status, type, name, installed, available, methods} and buildWorkerRows {active, role, class, type, method, version}. Item-only and worker-only columns carry an em-dash in the other section. Package-visible for testing.
      Parameters:
      items - one row per update item
      workers - one row per registered worker
      Returns:
      the table html, never null
    • buildItemRows

      Merge the installed, downloaded and available maps into one row set of {status, type, name, installed, available, methods}, sorted by name. Methods lists the update methods registered as checkers for the item's type. An item present in more than one map yields a single row; the status reflects the most advanced stage: Downloaded, then Available, then Installed. Items with no id (non-plugin updates) get a display name derived from their type. Null maps are treated as empty. Pure, for unit tests.
      Parameters:
      installed - downloaded and installed versions by item
      downloaded - downloaded but not installed versions by item
      available - versions reported by checkers by item
      checkMethods - update methods registered as checkers, by type
      Returns:
      one row per item, never null
    • itemDisplayName

      static String itemDisplayName(UpdateType type, String id)
      Display name for an update item. Plugin items are named by their plugin id; blocklist items combine their id with the type ("Sybil Blocklist"); geoip ids are already readable; the remaining unnamed per-type items get a readable name from their type instead of a dash. Package-visible for testing.
      Parameters:
      type - the update type
      id - the item id, empty for non-plugin items
      Returns:
      a display name, never null
    • versionFor

      private static String versionFor(Map<UpdateType, String> typeVersions, UpdateType type)
      The installed version string registered for a worker's type, or a dash when that type has no installed version.
      Parameters:
      typeVersions - installed version strings by type, may be null
      type - the worker's update type
      Returns:
      the version or an em-dash
    • capitalize

      private static String capitalize(String s)
      Upper-case the first character, leaving the rest as-is.
      Parameters:
      s - the string to capitalize, not null
      Returns:
      the capitalized string
    • buildWorkerRows

      Merge the registered checkers, updaters and post-processors into one row set of {active, role, class, type, method, version}, sorted by role then class. A checker is active when a running task matches its type and method; an updater when it is in a running task's remaining list; post-processors have no running state. Null collections are treated as empty. Pure, for unit tests.
      Parameters:
      checkers - the registered checkers, null treated as empty
      updaters - the registered updaters, null treated as empty
      postProcessors - registered post-processors by type id
      activeCheckers - currently running check tasks
      downloaders - currently running update tasks to their remaining updaters
      typeVersions - installed version strings by type, for the worker's Installed/Version cell
      Returns:
      one row per registration, never null
    • fmtVersion

      static String fmtVersion(String versionStr)
      Format a 13-digit version string as a date, otherwise as-is.
      Parameters:
      versionStr - the raw version string
      Returns:
      a display string, never null
    • dotClass

      static String dotClass(String status)
      Map an item or worker status to its status dot colour: green for installed or active, yellow for an available update, blue for a downloaded update awaiting install, grey otherwise.
      Parameters:
      status - the row status
      Returns:
      a shared.css .appdot colour class, never null