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

public class RouterContextextends 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 Details

    • _router

      private final Router _router
      The router object that created this context.
    • _clientManagerFacade

      private ClientManagerFacade _clientManagerFacade
      Manager of the I2CP client connections.
    • _internalClientManager

      private InternalClientManager _internalClientManager
      Manager of the router's internal clients.
    • _clientMessagePool

      private ClientMessagePool _clientMessagePool
      Pool of reusable client messages.
    • _jobQueue

      private JobQueue _jobQueue
      Queue of scheduled router jobs.
    • _inNetMessagePool

      private InNetMessagePool _inNetMessagePool
      Pool of inbound network messages.
    • _outNetMessagePool

      private OutNetMessagePool _outNetMessagePool
      Pool of outbound network messages.
    • _messageHistory

      private MessageHistory _messageHistory
      Recent message ids for duplicate detection.
    • _messageRegistry

      private OutboundMessageRegistry _messageRegistry
      Registry of outbound message handlers.
    • _netDb

      The router's network database.
    • _keyManager

      private KeyManager _keyManager
      Manager of our session keys.
    • _commSystem

      private CommSystemFacade _commSystem
      The transport and message comm system.
    • _profileOrganizer

      private ProfileOrganizer _profileOrganizer
      Organizer of the peer profiles.
    • _peerManagerFacade

      private PeerManagerFacade _peerManagerFacade
      Manager of peer connections and tests.
    • _profileManager

      private ProfileManager _profileManager
      Manager of the peer profiles.
    • _bandwidthLimiter

      private FIFOBandwidthLimiter _bandwidthLimiter
      The bandwidth limiter for transports.
    • _tunnelManager

      private TunnelManagerFacade _tunnelManager
      Manager of the tunnel pools.
    • _tunnelDispatcher

      private TunnelDispatcher _tunnelDispatcher
      Dispatcher of messages through tunnels.
    • _statPublisher

      private StatisticsManager _statPublisher
      Publisher of the router statistics.
    • _banlist

      private Banlist _banlist
      The banlist of bad routers.
    • _blocklist

      private Blocklist _blocklist
      The blocklist of bad networks.
    • _messageValidator

      private MessageValidator _messageValidator
      Validator of inbound messages.
    • _throttle

      private RouterThrottle _throttle
      The router's connection throttle.
    • _appManager

      private RouterAppManager _appManager
      Manager of the router's internal apps.
    • _routingKeyGenerator

      private RouterKeyGenerator _routingKeyGenerator
      Generator of our routing keys.
    • _garlicMessageParser

      private GarlicMessageParser _garlicMessageParser
      Parser of garlic messages.
    • _elGamalAESEngine

      private ElGamalAESEngine _elGamalAESEngine
      Engine for the legacy ElGamal-AES encryption.
    • _eciesEngine

      private ECIESAEADEngine _eciesEngine
      Engine for the ratchet encryption types.
    • _finalShutdownTasks

      private final Set<Runnable> _finalShutdownTasks
      Tasks run at the end of final shutdown.
    • _initialized

      private volatile boolean _initialized
      True once the context components are built.
    • _lock1

      private final Object _lock1
      Lock for the first group of components.
    • _lock2

      private final Object _lock2
      Lock for the second group of components.
    • _lock3

      private final Object _lock3
      Lock for the third group of components.
    • _contexts

      private static final List<RouterContext> _contexts
      All live router contexts.
  • Constructor Details

    • 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
      envProps - configuration properties to override the defaults, or null to use none
    • 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
      envProps - configuration properties to override the defaults, or null to use none
      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(RouterContext) afterwards.
  • Method Details

    • setGlobalContext

      static boolean setGlobalContext(RouterContext ctx)
      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)
    • filterProps

      private static final Properties filterProps(Properties envProps)
      Properties where the defaults must be different from those in I2PAppContext.

      Unless we are explicitly disabling the timestamper, we want to use it. We need this now as the new timestamper default is disabled (so we don't have each I2PAppContext creating their own SNTP queries all the time)

      Set more PRNG buffers, as the default is now small for the I2PAppContext.

      Parameters:
      envProps - the properties to filter, may be null
      Returns:
      the same properties with the router defaults added, never null
    • 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.
    • 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
    • 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)
      
    • 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.
    • 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.
      Returns:
      the modifiable list of router contexts registered in this JVM
    • 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.
    • router

      public Router router()
      What router is this context working for?
      Returns:
      the Router instance, or null when constructed for unit tests
    • 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
    • clientManager

      public ClientManagerFacade clientManager()
      How are we coordinating clients for the router?
      Returns:
      the ClientManagerFacade instance
    • clientMessagePool

      public ClientMessagePool clientMessagePool()
      Where do we toss messages for the clients (and where do we get client messages to forward on from)?
      Returns:
      the ClientMessagePool instance
    • inNetMessagePool

      public InNetMessagePool inNetMessagePool()
      Where do we get network messages from (aka where does the comm system dump what it reads)?
      Returns:
      the InNetMessagePool instance
    • outNetMessagePool

      public OutNetMessagePool outNetMessagePool()
      Where do we put messages that the router wants to forwards onto the network?
      Returns:
      the OutNetMessagePool instance
    • 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.
      Returns:
      the MessageHistory instance
    • messageRegistry

      public OutboundMessageRegistry messageRegistry()
      The registry is used by outbound messages to wait for replies.
      Returns:
      the OutboundMessageRegistry instance
    • netDbSegmentor

      public SegmentedNetworkDatabaseFacade netDbSegmentor()
      The network database segmentor for advanced database access.
      Returns:
      the SegmentedNetworkDatabaseFacade instance
    • netDb

      public NetworkDatabaseFacade netDb()
      The main network database facade for standard database operations.
      Returns:
      the NetworkDatabaseFacade for the main network
    • clientNetDb

      public NetworkDatabaseFacade clientNetDb(Hash id)
      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
    • jobQueue

      public JobQueue jobQueue()
      The actual driver of the router, where all jobs are enqueued and processed.
      Returns:
      the JobQueue instance
    • 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.
      Returns:
      the KeyManager instance
    • commSystem

      public CommSystemFacade commSystem()
      How do we pass messages from our outNetMessagePool to another router.
      Returns:
      the CommSystemFacade instance
    • profileOrganizer

      public ProfileOrganizer profileOrganizer()
      Organize the peers we know about into various tiers, profiling their performance and sorting them accordingly.
      Returns:
      the ProfileOrganizer instance
    • 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).
      Returns:
      the PeerManagerFacade instance
    • 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).
      Returns:
      the ProfileManager instance
    • bandwidthLimiter

      public FIFOBandwidthLimiter bandwidthLimiter()
      Coordinate this router's bandwidth limits.
      Returns:
      the FIFOBandwidthLimiter instance
    • 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.
      Returns:
      the TunnelManagerFacade instance
    • tunnelDispatcher

      public TunnelDispatcher tunnelDispatcher()
      Handle tunnel messages, as well as coordinate the gateways.
      Returns:
      the TunnelDispatcher instance
    • 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.
      Returns:
      the StatisticsManager instance
    • banlist

      public Banlist banlist()
      The banlist for managing banned peers.
      Returns:
      the Banlist instance
    • blocklist

      public Blocklist blocklist()
      The blocklist for managing blocked peers.
      Returns:
      the Blocklist instance
    • messageValidator

      public MessageValidator messageValidator()
      The router keeps track of messages it receives to prevent duplicates, as well as other criteria for "validity".
      Returns:
      the MessageValidator instance
    • throttle

      public RouterThrottle throttle()
      The router throttle controller for managing load.
      Returns:
      the RouterThrottle instance
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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
      Parameters:
      propName - the property name
      Returns:
      the value of the named property, taken from router.config when set and otherwise from the app context, or null if neither defines it
    • 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
      Parameters:
      propName - the property name
      defaultVal - the default value
      Returns:
      the value of the named property, taken from router.config when set and otherwise from the app context, or defaultVal if neither defines it
    • getProperty

      public int getProperty(String propName, int defaultVal)
      Return an int with an int default.
      Overrides:
      getProperty in class I2PAppContext
      Parameters:
      propName - the property name
      defaultVal - the default value
      Returns:
      the value of the named property parsed as an int, or defaultVal if it is unset or does not parse as an integer
    • getProperty

      public long getProperty(String propName, long defaultVal)
      Return a long with a long default.
      Overrides:
      getProperty in class I2PAppContext
      Parameters:
      propName - the property name
      defaultVal - the default value
      Returns:
      the value of the named property parsed as a long, or defaultVal if it is unset or does not parse as a long
    • getProperties

      public Properties getProperties()
      New Properties with system and context properties.
      Overrides:
      getProperties in class I2PAppContext
      Returns:
      a fresh copy, never null
    • initializeClock

      protected void initializeClock()
      Description copied from class: I2PAppContext
      Initialize the clock.
      Overrides:
      initializeClock in class I2PAppContext
    • keyRing

      public KeyRing keyRing()
      Override to support storage in router.config.
      Overrides:
      keyRing in class I2PAppContext
      Returns:
      the key ring
    • initializeKeyRing

      protected void initializeKeyRing()
      Description copied from class: I2PAppContext
      Initialize the key ring.
      Overrides:
      initializeKeyRing in class I2PAppContext
    • removeShutdownTasks

      void removeShutdownTasks()
      Remove all shutdown tasks from the context. Used during router shutdown to clear pending tasks.
    • 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
    • getFinalShutdownTasks

      Set<Runnable> getFinalShutdownTasks()
      The set of final shutdown tasks.
      Returns:
      the Set of Runnable tasks to execute during final shutdown
    • isRouterContext

      public boolean isRouterContext()
      Use this instead of context instanceof RouterContext.
      Overrides:
      isRouterContext in class I2PAppContext
      Returns:
      true always, as this class is the router's own context
    • internalClientManager

      public InternalClientManager internalClientManager()
      Use this to connect to the router in the same JVM.
      Overrides:
      internalClientManager in class I2PAppContext
      Returns:
      the client manager
    • clientAppManager

      public ClientAppManager clientAppManager()
      The RouterAppManager.
      Overrides:
      clientAppManager in class I2PAppContext
      Returns:
      the RouterAppManager holding this router's client applications
    • routerAppManager

      public RouterAppManager routerAppManager()
      The RouterAppManager. For convenience, same as clientAppManager(), no cast required.
      Returns:
      the same RouterAppManager that clientAppManager() returns, already typed as the router-specific subclass
    • initializeSessionKeyManager

      protected void initializeSessionKeyManager()
      Create the session key manager for the router, picking the implementation from the router's configured encryption type: a RatchetSKM for an ECIES_X25519 key, otherwise a TransientSessionKeyManager. Overridden here because I2PAppContext returns a dummy SessionKeyManager.
      Overrides:
      initializeSessionKeyManager in class I2PAppContext
    • 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 I2PAppContext, returns non-null in RouterContext and null in I2PAppContext.

      Overrides:
      routingKeyGenerator in class I2PAppContext
      Returns:
      non-null
    • routerKeyGenerator

      public RouterKeyGenerator routerKeyGenerator()
      The routing key generator for this context.
      Returns:
      the RouterKeyGenerator instance
    • garlicMessageParser

      public GarlicMessageParser garlicMessageParser()
      The garlic message parser for this context. Since we only need one parser instance, this is lazily initialized.
      Returns:
      the GarlicMessageParser instance
    • 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).
      Returns:
      the ElGamalAESEngine instance
    • 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.
      Returns:
      the ECIESAEADEngine instance
    • 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