net.i2p.router.transport

## Class TransportImpl

- java.lang.Object
- - net.i2p.router.transport.TransportImpl

- All Implemented Interfaces:
    Transport
  Direct Known Subclasses:
    NTCPTransport, UDPTransport

      public abstract class TransportImpl
      extends Object
      implements Transport

  Defines a way to send a message to another peer and start listening for messages

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected RouterContext` | `_context`
    `static boolean` | `ADJUST_COST` Do we increase the advertised cost when approaching conn limits?

    - ### Fields inherited from interface net.i2p.router.transport.Transport

      `SOURCE_CONFIG, SOURCE_INTERFACE, SOURCE_UPNP`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `TransportImpl(RouterContext context)` Initialize the new transport

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `afterSend(OutNetMessage msg, boolean sendSuccessful)` The transport is done sending this message
    `protected void` | `afterSend(OutNetMessage msg, boolean sendSuccessful, boolean allowRequeue)` The transport is done sending this message
    `protected void` | `afterSend(OutNetMessage msg, boolean sendSuccessful, boolean allowRequeue, long msToSend)` The transport is done sending this message.
    `protected void` | `afterSend(OutNetMessage msg, boolean sendSuccessful, long msToSend)` The transport is done sending this message
    `int` | `countActivePeers()` How many peers active in the last few minutes?
    `int` | `countActiveSendPeers()` How many peers are we actively sending messages to (this minute)
    `int` | `countPeers()` How many peers are we connected to? For NTCP, this is the same as active, but SSU actually looks at idle time for countActivePeers()
    `void` | `externalAddressReceived(String source, byte[] ip, int port)` Notify a transport of an external address change.
    `void` | `forwardPortStatus(int port, boolean success, String reason)` Notify a transport of the results of trying to forward a port
    `Vector` | `getClockSkews()` Return our peer clock skews on a transport.
    `RouterContext` | `getContext()`
    `RouterAddress` | `getCurrentAddress()` What addresses are we currently listening to?
    `static byte[]` | `getIP(Hash peer)`
    `int` | `getMaxConnections()` Per-transport connection limit
    `List` | `getMostRecentErrorMessages()`
    `OutNetMessage` | `getNextMessage()` Nonblocking call to pull the next outbound message off the queue.
    `short` | `getReachabilityStatus()`
    `int` | `getRequestedPort()` What port would the transport like to have forwarded by UPnP.
    `boolean` | `haveCapacity()` Can we initiate or accept a connection to another peer, saving some margin
    `boolean` | `haveCapacity(int pct)`
    `boolean` | `isBacklogged(Hash dest)`
    `boolean` | `isEstablished(Hash dest)`
    `static boolean` | `isPubliclyRoutable(byte[] addr)`
    `boolean` | `isUnreachable(Hash peer)`
    `void` | `markReachable(Hash peer, boolean isInbound)` called when we establish a peer connection (outbound or inbound)
    `void` | `markUnreachable(Hash peer)` This isn't very useful since it is cleared when they contact us
    `void` | `markWasUnreachable(Hash peer, boolean yes)` Maintain the WasUnreachable list
    `void` | `messageReceived(I2NPMessage inMsg, RouterIdentity remoteIdent, Hash remoteIdentHash, long msToReceive, int bytesReceived)` Message received from the I2NPMessageReader - send it to the listener
    `protected abstract void` | `outboundMessageReady()` This message is called whenever a new message is added to the send pool, and it should not block
    `void` | `recheckReachability()`
    `void` | `renderStatusHTML(Writer out)` Make this stuff pretty (only used in the old console)
    `void` | `renderStatusHTML(Writer out, String urlBase, int sortFlags)`
    `protected void` | `replaceAddress(RouterAddress address)` Replace any existing addresses for the current transport with the given one.
    `void` | `send(OutNetMessage msg)` Asynchronously send the message as requested in the message and, if the send is successful, queue up any msg.getOnSendJob job, and register it with the OutboundMessageRegistry (if it has a reply selector).
    `void` | `setIP(Hash peer, byte[] ip)`
    `void` | `setListener(TransportEventListener listener)` Who to notify on message availability
    `RouterAddress` | `updateAddress()` Ask the transport to update its address based on current information and return it Transports should override.
    `boolean` | `wasUnreachable(Hash peer)` Was the peer UNreachable (outbound only) the last time we tried it? This is NOT reset if the peer contacts us and it is never expired.

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

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

    - ### Methods inherited from interface net.i2p.router.transport.Transport

      `bid, getStyle, startListening, stopListening`

- - ### Field Detail

    - #### _context

          protected RouterContext _context

    - #### ADJUST_COST

          public static final boolean ADJUST_COST

      Do we increase the advertised cost when approaching conn limits?
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### TransportImpl

          public TransportImpl(RouterContext context)

      Initialize the new transport

  - ### Method Detail

    - #### countPeers

          public int countPeers()

      How many peers are we connected to? For NTCP, this is the same as active, but SSU actually looks at idle time for countActivePeers()
      Specified by:
        `countPeers` in interface `Transport`

    - #### countActivePeers

          public int countActivePeers()

      How many peers active in the last few minutes?
      Specified by:
        `countActivePeers` in interface `Transport`

    - #### countActiveSendPeers

          public int countActiveSendPeers()

      How many peers are we actively sending messages to (this minute)
      Specified by:
        `countActiveSendPeers` in interface `Transport`

    - #### getMaxConnections

          public int getMaxConnections()

      Per-transport connection limit

    - #### haveCapacity

          public boolean haveCapacity()

      Can we initiate or accept a connection to another peer, saving some margin
      Specified by:
        `haveCapacity` in interface `Transport`

    - #### haveCapacity

          public boolean haveCapacity(int pct)

      Specified by:
        `haveCapacity` in interface `Transport`
      Parameters:
        `pct` - are we under x% 0-100

    - #### getClockSkews

          public Vector getClockSkews()

      Return our peer clock skews on a transport. Vector composed of Long, each element representing a peer skew in seconds. Dummy version. Transports override it.
      Specified by:
        `getClockSkews` in interface `Transport`

    - #### getMostRecentErrorMessages

          public List getMostRecentErrorMessages()

      Specified by:
        `getMostRecentErrorMessages` in interface `Transport`

    - #### getNextMessage

          public OutNetMessage getNextMessage()

      Nonblocking call to pull the next outbound message off the queue.
      Returns:
        the next message or null if none are available

    - #### afterSend

          protected void afterSend(OutNetMessage msg,
                       boolean sendSuccessful)

      The transport is done sending this message
      Parameters:
        `msg` - message in question
        `sendSuccessful` - true if the peer received it

    - #### afterSend

          protected void afterSend(OutNetMessage msg,
                       boolean sendSuccessful,
                       boolean allowRequeue)

      The transport is done sending this message
      Parameters:
        `msg` - message in question
        `sendSuccessful` - true if the peer received it
        `allowRequeue` - true if we should try other transports if available

    - #### afterSend

          protected void afterSend(OutNetMessage msg,
                       boolean sendSuccessful,
                       long msToSend)

      The transport is done sending this message
      Parameters:
        `msg` - message in question
        `sendSuccessful` - true if the peer received it
        `msToSend` - how long it took to transfer the data to the peer

    - #### afterSend

          protected void afterSend(OutNetMessage msg,
                       boolean sendSuccessful,
                       boolean allowRequeue,
                       long msToSend)

      The transport is done sending this message. This is the method that actually does all of the cleanup - firing off jobs, requeueing, updating stats, etc.
      Parameters:
        `msg` - message in question
        `sendSuccessful` - true if the peer received it
        `msToSend` - how long it took to transfer the data to the peer
        `allowRequeue` - true if we should try other transports if available

    - #### send

          public void send(OutNetMessage msg)

      Asynchronously send the message as requested in the message and, if the send is successful, queue up any msg.getOnSendJob job, and register it with the OutboundMessageRegistry (if it has a reply selector). If the send fails, queue up any msg.getOnFailedSendJob
      Specified by:
        `send` in interface `Transport`

    - #### outboundMessageReady

          protected abstract void outboundMessageReady()

      This message is called whenever a new message is added to the send pool, and it should not block

    - #### messageReceived

          public void messageReceived(I2NPMessage inMsg,
                             RouterIdentity remoteIdent,
                             Hash remoteIdentHash,
                             long msToReceive,
                             int bytesReceived)

      Message received from the I2NPMessageReader - send it to the listener

    - #### getCurrentAddress

          public RouterAddress getCurrentAddress()

      What addresses are we currently listening to?
      Specified by:
        `getCurrentAddress` in interface `Transport`

    - #### updateAddress

          public RouterAddress updateAddress()

      Ask the transport to update its address based on current information and return it Transports should override.
      Specified by:
        `updateAddress` in interface `Transport`
      Since:
        0.7.12

    - #### replaceAddress

          protected void replaceAddress(RouterAddress address)

      Replace any existing addresses for the current transport with the given one.

    - #### externalAddressReceived

          public void externalAddressReceived(String source,
                                     byte[] ip,
                                     int port)

      Notify a transport of an external address change. This may be from a local interface, UPnP, a config change, etc. This should not be called if the ip didn't change (from that source's point of view), or is a local address, or if the ip is IPv6, but the transport should check anyway. The transport should also do its own checking on whether to accept notifications from this source. This can be called before startListening() to set an initial address, or after the transport is running.
      Specified by:
        `externalAddressReceived` in interface `Transport`
      Parameters:
        `source` - defined in Transport.java
        `ip` - typ. IPv4 non-local
        `port` - 0 for unknown or unchanged

    - #### forwardPortStatus

          public void forwardPortStatus(int port,
                               boolean success,
                               String reason)

      Notify a transport of the results of trying to forward a port
      Specified by:
        `forwardPortStatus` in interface `Transport`

    - #### getRequestedPort

          public int getRequestedPort()

      What port would the transport like to have forwarded by UPnP. This can't be passed via getCurrentAddress(), as we have to open the port before we can publish the address.
      Specified by:
        `getRequestedPort` in interface `Transport`
      Returns:
        port or -1 for none or 0 for any

    - #### setListener

          public void setListener(TransportEventListener listener)

      Who to notify on message availability
      Specified by:
        `setListener` in interface `Transport`

    - #### renderStatusHTML

          public void renderStatusHTML(Writer out)
                                throws IOException

      Make this stuff pretty (only used in the old console)
      Throws:
        `IOException`

    - #### renderStatusHTML

          public void renderStatusHTML(Writer out,
                              String urlBase,
                              int sortFlags)
                                throws IOException

      Specified by:
        `renderStatusHTML` in interface `Transport`
      Throws:
        `IOException`

    - #### getContext

          public RouterContext getContext()

    - #### getReachabilityStatus

          public short getReachabilityStatus()

      Specified by:
        `getReachabilityStatus` in interface `Transport`

    - #### recheckReachability

          public void recheckReachability()

      Specified by:
        `recheckReachability` in interface `Transport`

    - #### isBacklogged

          public boolean isBacklogged(Hash dest)

      Specified by:
        `isBacklogged` in interface `Transport`

    - #### isEstablished

          public boolean isEstablished(Hash dest)

      Specified by:
        `isEstablished` in interface `Transport`

    - #### isUnreachable

          public boolean isUnreachable(Hash peer)

      Specified by:
        `isUnreachable` in interface `Transport`

    - #### markUnreachable

          public void markUnreachable(Hash peer)

      This isn't very useful since it is cleared when they contact us

    - #### markReachable

          public void markReachable(Hash peer,
                           boolean isInbound)

      called when we establish a peer connection (outbound or inbound)

    - #### wasUnreachable

          public boolean wasUnreachable(Hash peer)

      Was the peer UNreachable (outbound only) the last time we tried it? This is NOT reset if the peer contacts us and it is never expired.
      Specified by:
        `wasUnreachable` in interface `Transport`

    - #### markWasUnreachable

          public void markWasUnreachable(Hash peer,
                                boolean yes)

      Maintain the WasUnreachable list

    - #### setIP

          public void setIP(Hash peer,
                   byte[] ip)

    - #### getIP

          public static byte[] getIP(Hash peer)

    - #### isPubliclyRoutable

          public static boolean isPubliclyRoutable(byte[] addr)