net.i2p.router

## Class RouterContext

- java.lang.Object
- - net.i2p.I2PAppContext
  - - net.i2p.router.RouterContext

-     public class RouterContext
      extends I2PAppContext

  Extended application context that provides centralized resource coordination for router instances. Manages subsystem access, configuration, and resource isolation to enable multiple router instances within the same JVM without conflicts.

- - ### Field Summary

    - ### Fields inherited from class net.i2p.I2PAppContext

      `_clock, _clockInitialized, _globalAppContext, _keyRing, _keyRingInitialized, _overrideProps, _sessionKeyManager, _sessionKeyManagerInitialized, _shutdownTasks`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `RouterContext(Router router)` Caller MUST call initAll() after instantiation.
    `RouterContext(Router router, Properties envProps)` Caller MUST call initAll() after instantiation.
    `RouterContext(Router router, Properties envProps, boolean doInit)` Caller MUST call initAll() after instantiation.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addFinalShutdownTask(Runnable task)` Add a task to be run during final shutdown.
    `void` | `addPropertyCallback(I2PProperties.I2PPropertyCallback callback)` Add a callback for property changes.
    `FIFOBandwidthLimiter` | `bandwidthLimiter()` Coordinate this router's bandwidth limits
    `Banlist` | `banlist()` Get thebanlist for managing banned peers.
    `Blocklist` | `blocklist()` Get the blocklist for managing blocked peers.
    `ClientAppManager` | `clientAppManager()` The RouterAppManager.
    `ClientManagerFacade` | `clientManager()` How are we coordinating clients for the router?
    `ClientMessagePool` | `clientMessagePool()` Where do we toss messages for the clients (and where do we get client messages to forward on from)?
    `NetworkDatabaseFacade` | `clientNetDb(Hash id)` Get the client netDb for the given id.
    `CommSystemFacade` | `commSystem()` How do we pass messages from our outNetMessagePool to another router
    `ECIESAEADEngine` | `eciesEngine()` Access the ECIES/AEAD engine for this context.
    `ElGamalAESEngine` | `elGamalAESEngine()` Access the ElGamal/AES+SessionTag engine for this context.
    `GarlicMessageParser` | `garlicMessageParser()` Get the garlic message parser for this context.
    `(package private) static List<RouterContext>` | `getContexts()` Same as listContexts() but package private and modifiable.
    `long` | `getEstimatedDowntime()` How long this router was down before it started, or 0 if unknown.
    `(package private) Set<Runnable>` | `getFinalShutdownTasks()` Get the set of final shutdown tasks.
    `Properties` | `getProperties()` Access the configuration attributes of this context, listing the properties provided during the context construction, as well as the ones included in System.getProperties.
    `String` | `getProperty(String propName)` Tie in the router's config as properties, as well as whatever the I2PAppContext says.
    `int` | `getProperty(String propName, int defaultVal)` Return an int with an int default
    `long` | `getProperty(String propName, long defaultVal)` Return a long with a long default
    `String` | `getProperty(String propName, String defaultVal)` Tie in the router's config as properties, as well as whatever the I2PAppContext says.
    `void` | `initAll()` The following properties may be used to replace various parts of the context with dummy implementations for testing, by setting the property to "true":
    `protected void` | `initializeClock()`
    `protected void` | `initializeKeyRing()`
    `protected void` | `initializeSessionKeyManager()` As of 0.9.15, this returns a dummy SessionKeyManager in I2PAppContext.
    `InNetMessagePool` | `inNetMessagePool()` Where do we get network messages from (aka where does the comm system dump what it reads)?
    `InternalClientManager` | `internalClientManager()` Use this to connect to the router in the same JVM.
    `boolean` | `isRouterContext()` Use this instead of context instanceof RouterContext
    `JobQueue` | `jobQueue()` The actual driver of the router, where all jobs are enqueued and processed.
    `KeyManager` | `keyManager()` Coordinates the router's ElGamal and DSA keys, as well as any keys given to it by clients as part of a LeaseSet.
    `KeyRing` | `keyRing()` override to support storage in router.config
    `(package private) static void` | `killGlobalContext()` Kill the global I2PAppContext, so it isn't still around when we restart in the same JVM (Android).
    `static List<RouterContext>` | `listContexts()` Retrieve the list of router contexts currently instantiated in this JVM.
    `MessageHistory` | `messageHistory()` Tracker component for monitoring what messages are wrapped in what containers and how they proceed through the network.
    `OutboundMessageRegistry` | `messageRegistry()` The registry is used by outbound messages to wait for replies.
    `MessageValidator` | `messageValidator()` The router keeps track of messages it receives to prevent duplicates, as well as other criteria for "validity".
    `NetworkDatabaseFacade` | `netDb()` Get the main network database facade for standard database operations.
    `SegmentedNetworkDatabaseFacade` | `netDbSegmentor()` Get the network database segmentor for advanced database access.
    `OutNetMessagePool` | `outNetMessagePool()` Where do we put messages that the router wants to forwards onto the network?
    `PeerManagerFacade` | `peerManager()` Minimal interface for selecting peers for various tasks based on given criteria.
    `ProfileManager` | `profileManager()` Expose a simple API for various router components to take note of particular events that a peer enacts (sends us a message, agrees to participate in a tunnel, etc).
    `ProfileOrganizer` | `profileOrganizer()` Organize the peers we know about into various tiers, profiling their performance and sorting them accordingly.
    `(package private) void` | `removeProperty(String propName)` Remove a property provided during the context construction.
    `(package private) void` | `removeShutdownTasks()` Remove all shutdown tasks from the context.
    `Router` | `router()` what router is this context working for?
    `RouterAppManager` | `routerAppManager()` The RouterAppManager.
    `Hash` | `routerHash()` Convenience method for getting the router hash.
    `RouterKeyGenerator` | `routerKeyGenerator()` Get the routing key generator for this context.
    `RoutingKeyGenerator` | `routingKeyGenerator()` Determine how much do we want to mess with the keys to turn them into something we can route.
    `(package private) static boolean` | `setGlobalContext(RouterContext ctx)` Sets the default context, unless there is one already.
    `StatisticsManager` | `statPublisher()` If the router is configured to, gather up some particularly tasty morsels regarding the stats managed and offer to publish them into the routerInfo.
    `RouterThrottle` | `throttle()` Get the router throttle controller for managing load.
    `String` | `toString()`
    `TunnelDispatcher` | `tunnelDispatcher()` Handle tunnel messages, as well as coordinate the gateways
    `TunnelManagerFacade` | `tunnelManager()` Coordinate this router's tunnels (its pools, participation, backup, etc).

    - ### Methods inherited from class net.i2p.I2PAppContext

      `addShutdownTask, aes, clock, deleteTempDir, dsa, elGamalEngine, getAppDir, getBaseDir, getBooleanProperty, getBooleanPropertyDefaultTrue, getConfigDir, getCurrentContext, getGlobalContext, getLibDir, getLogDir, getPIDDir, getProperty, getProperty, getPropertyNames, getRouterDir, getShutdownTasks, getTempDir, hasWrapper, hmac256, keyGenerator, logManager, namingService, portMapper, random, removeShutdownTask, sessionKeyManager, setGlobalContext, setLogManager, sha, simpleTimer2, statManager`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### RouterContext

          public RouterContext(Router router)

      Caller MUST call initAll() after instantiation.
      Parameters:
        `router` - may be null for unit tests if you are careful

    - #### RouterContext

          public RouterContext(Router router,
                               Properties envProps)

      Caller MUST call initAll() after instantiation.
      Parameters:
        `router` - may be null for unit tests if you are careful

    - #### RouterContext

          RouterContext(Router router,
                        Properties envProps,
                        boolean doInit)

      Caller MUST call initAll() after instantiation. NOT a public API, for use by Router only, NOT for external use.
      Parameters:
        `router` - may be null for unit tests if you are careful
        `doInit` - should this context be used as the global one (if necessary)? Will only apply if there is no global context now. If false, caller should call setGlobalContext() afterwards.

  - ### Method Detail

    - #### addFinalShutdownTask

          public void addFinalShutdownTask(Runnable task)

      Add a task to be run during final shutdown. This is called after all context resources have been cleaned up. No logging or context resources will be available during task execution.
      Parameters:
        `task` - the Runnable to execute during final shutdown

    - #### addPropertyCallback

          public void addPropertyCallback(I2PProperties.I2PPropertyCallback callback)

      Add a callback for property changes.
      Overrides:
        `addPropertyCallback` in class `I2PAppContext`
      Parameters:
        `callback` - the callback to be notified of property changes

    - #### bandwidthLimiter

          public FIFOBandwidthLimiter bandwidthLimiter()

      Coordinate this router's bandwidth limits

    - #### banlist

          public Banlist banlist()

      Get thebanlist for managing banned peers.
      Returns:
        the Banlist instance

    - #### blocklist

          public Blocklist blocklist()

      Get the blocklist for managing blocked peers.
      Returns:
        the Blocklist instance

    - #### clientAppManager

          public ClientAppManager clientAppManager()

      The RouterAppManager.
      Overrides:
        `clientAppManager` in class `I2PAppContext`
      Returns:
        the manager

    - #### clientManager

          public ClientManagerFacade clientManager()

      How are we coordinating clients for the router?

    - #### clientMessagePool

          public ClientMessagePool clientMessagePool()

      Where do we toss messages for the clients (and where do we get client messages to forward on from)?

    - #### clientNetDb

          public NetworkDatabaseFacade clientNetDb(Hash id)

      Get the client netDb for the given id. Will return the main netDb if the dbid is null or the client db is not found.
      Parameters:
        `id` - may be null
      Returns:
        non-null

    - #### commSystem

          public CommSystemFacade commSystem()

      How do we pass messages from our outNetMessagePool to another router

    - #### eciesEngine

          public ECIESAEADEngine eciesEngine()

      Access the ECIES/AEAD engine for this context. The algorithm makes use of the sessionKeyManager to coordinate transparent access to the sessionKeys and sessionTags.

    - #### elGamalAESEngine

          public ElGamalAESEngine elGamalAESEngine()

      Access the ElGamal/AES+SessionTag engine for this context. The algorithm makes use of the context's sessionKeyManager to coordinate transparent access to the sessionKeys and sessionTags, as well as the context's elGamal engine (which in turn keeps stats, etc).

    - #### garlicMessageParser

          public GarlicMessageParser garlicMessageParser()

      Get the garlic message parser for this context. Since we only need one parser instance, this is lazily initialized.
      Returns:
        the GarlicMessageParser instance

    - #### getContexts

          static List<RouterContext> getContexts()

      Same as listContexts() but package private and modifiable. The list should only be modified when a new context is created or a router is shut down.

    - #### getEstimatedDowntime

          public long getEstimatedDowntime()

      How long this router was down before it started, or 0 if unknown. This may be used for a determination of whether to regenerate keys, for example. We use the timestamp of the previous ping file left behind on crash, as set by isOnlyRouterRunning(), if present. Otherwise, the last STOPPED entry in the event log. May take a while to run the first time, if it has to go through the event log. Once called, the result is cached.
      Overrides:
        `getEstimatedDowntime` in class `I2PAppContext`
      Returns:
        downtime in ms or 0 if unknown

    - #### getFinalShutdownTasks

          Set<Runnable> getFinalShutdownTasks()

      Get the set of final shutdown tasks.
      Returns:
        the Set of Runnable tasks to execute during final shutdown

    - #### getProperties

          public Properties getProperties()

      Description copied from class: `I2PAppContext`
      Access the configuration attributes of this context, listing the properties provided during the context construction, as well as the ones included in System.getProperties.
      Overrides:
        `getProperties` in class `I2PAppContext`
      Returns:
        new Properties with system and context properties

    - #### getProperty

          public String getProperty(String propName)

      Tie in the router's config as properties, as well as whatever the I2PAppContext says.
      Overrides:
        `getProperty` in class `I2PAppContext`

    - #### getProperty

          public int getProperty(String propName,
                                 int defaultVal)

      Return an int with an int default
      Overrides:
        `getProperty` in class `I2PAppContext`

    - #### getProperty

          public long getProperty(String propName,
                                  long defaultVal)

      Return a long with a long default
      Overrides:
        `getProperty` in class `I2PAppContext`

    - #### getProperty

          public String getProperty(String propName,
                                    String defaultVal)

      Tie in the router's config as properties, as well as whatever the I2PAppContext says.
      Overrides:
        `getProperty` in class `I2PAppContext`

    - #### initAll

          public void initAll()

      The following properties may be used to replace various parts of the context with dummy implementations for testing, by setting the property to "true":

            i2p.dummyClientFacade
            i2p.dummyNetDb
            i2p.dummyPeerManager
            i2p.dummyTunnelManager
            i2p.vmCommSystem (transport)

    - #### initializeClock

          protected void initializeClock()

      Overrides:
        `initializeClock` in class `I2PAppContext`

    - #### initializeKeyRing

          protected void initializeKeyRing()

      Overrides:
        `initializeKeyRing` in class `I2PAppContext`

    - #### initializeSessionKeyManager

          protected void initializeSessionKeyManager()

      As of 0.9.15, this returns a dummy SessionKeyManager in I2PAppContext. Overridden in RouterContext to return the full TransientSessionKeyManager or MuxedSKM, depending on configured router encryption type.
      Overrides:
        `initializeSessionKeyManager` in class `I2PAppContext`

    - #### inNetMessagePool

          public InNetMessagePool inNetMessagePool()

      Where do we get network messages from (aka where does the comm system dump what it reads)?

    - #### internalClientManager

          public InternalClientManager internalClientManager()

      Use this to connect to the router in the same JVM.
      Overrides:
        `internalClientManager` in class `I2PAppContext`
      Returns:
        the client manager

    - #### isRouterContext

          public boolean isRouterContext()

      Use this instead of context instanceof RouterContext
      Overrides:
        `isRouterContext` in class `I2PAppContext`
      Returns:
        true

    - #### jobQueue

          public JobQueue jobQueue()

      The actual driver of the router, where all jobs are enqueued and processed.

    - #### keyManager

          public KeyManager keyManager()

      Coordinates the router's ElGamal and DSA keys, as well as any keys given to it by clients as part of a LeaseSet.

    - #### keyRing

          public KeyRing keyRing()

      override to support storage in router.config
      Overrides:
        `keyRing` in class `I2PAppContext`

    - #### killGlobalContext

          static void killGlobalContext()

      Kill the global I2PAppContext, so it isn't still around when we restart in the same JVM (Android). Only do this if there are no other routers in the JVM.

    - #### listContexts

          public static List<RouterContext> listContexts()

      Retrieve the list of router contexts currently instantiated in this JVM. This will always contain only one item (except when a simulation per the MultiRouter is going on).
      Returns:
        an unmodifiable list (as of 0.8.8). May be empty.

    - #### messageHistory

          public MessageHistory messageHistory()

      Tracker component for monitoring what messages are wrapped in what containers and how they proceed through the network. This is fully for debugging, as when a large portion of the network tracks their messages through this messageHistory and submits their logs, we can correlate them and watch as messages flow from hop to hop.

    - #### messageRegistry

          public OutboundMessageRegistry messageRegistry()

      The registry is used by outbound messages to wait for replies.

    - #### messageValidator

          public MessageValidator messageValidator()

      The router keeps track of messages it receives to prevent duplicates, as well as other criteria for "validity".

    - #### netDb

          public NetworkDatabaseFacade netDb()

      Get the main network database facade for standard database operations.
      Returns:
        the NetworkDatabaseFacade for the main network

    - #### netDbSegmentor

          public SegmentedNetworkDatabaseFacade netDbSegmentor()

      Get the network database segmentor for advanced database access.
      Returns:
        the SegmentedNetworkDatabaseFacade instance

    - #### outNetMessagePool

          public OutNetMessagePool outNetMessagePool()

      Where do we put messages that the router wants to forwards onto the network?

    - #### peerManager

          public PeerManagerFacade peerManager()

      Minimal interface for selecting peers for various tasks based on given criteria. This is kept seperate from the profile organizer since this logic is independent of how the peers are organized (or profiled even).

    - #### profileManager

          public ProfileManager profileManager()

      Expose a simple API for various router components to take note of particular events that a peer enacts (sends us a message, agrees to participate in a tunnel, etc).

    - #### profileOrganizer

          public ProfileOrganizer profileOrganizer()

      Organize the peers we know about into various tiers, profiling their performance and sorting them accordingly.

    - #### removeProperty

          void removeProperty(String propName)

      Remove a property provided during the context construction. Only for use by the router. Others use Router.saveConfig()
      Parameters:
        `propName` - The name of the property.

    - #### removeShutdownTasks

          void removeShutdownTasks()

      Remove all shutdown tasks from the context. Used during router shutdown to clear pending tasks.

    - #### router

          public Router router()

      what router is this context working for?

    - #### routerAppManager

          public RouterAppManager routerAppManager()

      The RouterAppManager. For convenience, same as clientAppManager(), no cast required
      Returns:
        the manager

    - #### routerHash

          public Hash routerHash()

      Convenience method for getting the router hash. Equivalent to context.router().getRouterInfo().getIdentity().getHash()
      Returns:
        may be null if called very early

    - #### routerKeyGenerator

          public RouterKeyGenerator routerKeyGenerator()

      Get the routing key generator for this context.
      Returns:
        the RouterKeyGenerator instance

    - #### routingKeyGenerator

          public RoutingKeyGenerator routingKeyGenerator()

      Determine how much do we want to mess with the keys to turn them into something we can route. This is context specific because we may want to test out how things react when peers don't agree on how to skew. Returns same thing as routerKeyGenerator()
      Overrides:
        `routingKeyGenerator` in class `I2PAppContext`
      Returns:
        non-null

    - #### setGlobalContext

          static boolean setGlobalContext(RouterContext ctx)

      Sets the default context, unless there is one already. NOT a public API, for use by Router only, NOT for external use.
      Parameters:
        `ctx` - context constructed with doInit = false
      Returns:
        success (false if previously set)

    - #### statPublisher

          public StatisticsManager statPublisher()

      If the router is configured to, gather up some particularly tasty morsels regarding the stats managed and offer to publish them into the routerInfo.

    - #### throttle

          public RouterThrottle throttle()

      Get the router throttle controller for managing load.
      Returns:
        the RouterThrottle instance

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### tunnelDispatcher

          public TunnelDispatcher tunnelDispatcher()

      Handle tunnel messages, as well as coordinate the gateways

    - #### tunnelManager

          public TunnelManagerFacade tunnelManager()

      Coordinate this router's tunnels (its pools, participation, backup, etc). Any configuration for the tunnels is rooted from the context's properties