net.i2p.router.peermanager

## Class PeerProfile

- java.lang.Object
- - net.i2p.router.peermanager.PeerProfile

-     public class PeerProfile
      extends Object

  Copied from http://www.i2p2.i2p/how_peerselection.html See also main() below for additional commentary by zzz. Currently, there is no 'ejection' strategy to get rid of the profiles for peers that are no longer active (or when the network consists of thousands of peers, to get rid of peers that are performing poorly). However, the size of each profile is fairly small, and is unrelated to how much data is collected about the peer, so that a router can keep a few thousand active peer profiles before the overhead becomes a serious concern. Once it becomes necessary, we can simply compact the poorly performing profiles (keeping only the most basic data) and maintain hundreds of thousands of profiles in memory. Beyond that size, we can simply eject the peers (e.g. keeping the best 100,000). TODO most of the methods should be synchronized.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `(package private) static boolean` | `ENABLE_TUNNEL_TEST_RESPONSE_TIME`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PeerProfile(RouterContext context, Hash peer)` Caller should call setLastHeardAbout() and setFirstHeardAbout()

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `(package private) void` | `coalesceOnly(boolean shouldDecay)` Caller must next call updateValues()
    `void` | `coalesceStats()` Coalesce all stats and update values
    `(package private) void` | `dataPushed(int size)`
    `(package private) void` | `dataPushed1m(int size)` the tunnel pushed that much data in a 1 minute period
    `boolean` | `equals(Object obj)`
    `void` | `expandDBProfile()` For floodfills
    `void` | `expandProfile()` When the given peer is performing well enough that we want to keep detailed stats on them again, call this to set up the info we dropped during shrinkProfile.
    `int` | `getCapacityBonus()` extra factor added to the capacity ranking - this can be updated in the profile written to disk to affect how the algorithm ranks capacity.
    `float` | `getCapacityValue()` How many tunnels do we think this peer can handle over the next hour?
    `(package private) RouterContext` | `getContext()` Helper for calculators
    `DBHistory` | `getDBHistory()` history of db activity with the peer Warning - may return null if !getIsExpandedDB()
    `RateStat` | `getDbIntroduction()` how many new peers we get from dbSearchReplyMessages or dbStore messages, calculated over a 1 hour, 1 day, and 1 week period Warning - may return null if !getIsExpandedDB()
    `RateStat` | `getDbResponseTime()` how long it takes to get a db response from the peer (in milliseconds), calculated over a 1 minute, 1 hour, and 1 day period Warning - may return null if !getIsExpandedDB()
    `long` | `getFirstHeardAbout()` When did we first hear about this peer?
    `int` | `getIntegrationBonus()` extra factor added to the integration ranking - this can be updated in the profile written to disk to affect how the algorithm ranks integration.
    `float` | `getIntegrationValue()` How well integrated into the network is this peer (as measured by how much they've told us that we didn't already know).
    `boolean` | `getIsActive()` Is this peer active at the moment (sending/receiving messages within the last 10 minutes)
    `boolean` | `getIsActive(long now)` Is this peer active at the moment (sending/receiving messages within the last 10 minutes)
    `boolean` | `getIsActive(long period, long now)` Is this peer active at the moment (sending/receiving messages within the given period?) Also mark active if it is connected, as this will tend to encourage use of already-connected peers.
    `boolean` | `getIsExpanded()` are we keeping an expanded profile on the peer, or just the bare minimum.
    `boolean` | `getIsExpandedDB()`
    `long` | `getLastHeardAbout()` when did we last hear about this peer?
    `long` | `getLastHeardFrom()` when did we last hear from the peer?
    `long` | `getLastSendFailed()` when did we last have a problem sending to this peer?
    `long` | `getLastSendSuccessful()` when did we last send to this peer successfully?
    `float` | `getPeakThroughputKBps()`
    `float` | `getPeakTunnel1mThroughputKBps()` This is the speed value
    `float` | `getPeakTunnelThroughputKBps()`
    `Hash` | `getPeer()` what peer is being profiled, non-null
    `RateStat` | `getPeerTestResponseTime()` how long it takes for a peer to respond to a direct test (ms)
    `float` | `getPeerTestTimeAverage()`
    `int` | `getSpeedBonus()` extra factor added to the speed ranking - this can be updated in the profile written to disk to affect how the algorithm ranks speed.
    `float` | `getSpeedValue()` How fast is the peer, taking into consideration both throughput and latency.
    `RateStat` | `getTunnelCreateResponseTime()` how long it takes to get a tunnel create response from the peer (in milliseconds), calculated over a 1 minute, 1 hour, and 1 day period Warning - may return null if !getIsExpanded()
    `TunnelHistory` | `getTunnelHistory()` history of tunnel activity with the peer Warning - may return null if !getIsExpanded()
    `(package private) int` | `getXORDistance()` For now, just a one-byte comparison
    `int` | `hashCode()`
    `(package private) boolean` | `isEstablished()`
    `(package private) boolean` | `isSameCountry()`
    `void` | `setCapacityBonus(int bonus)`
    `void` | `setDBHistory(DBHistory hist)`
    `(package private) void` | `setFirstHeardAbout(long when)` Set when did we first heard about this peer, only if older.
    `void` | `setIntegrationBonus(int bonus)`
    `void` | `setLastHeardAbout(long when)` Set when did we last hear about this peer, only if unset or newer Also sets FirstHeardAbout if earlier
    `void` | `setLastHeardFrom(long when)`
    `void` | `setLastSendFailed(long when)`
    `void` | `setLastSendSuccessful(long when)`
    `(package private) void` | `setPeakThroughputKBps(float kBps)` Only for restoration from persisted profile.
    `(package private) void` | `setPeakTunnel1mThroughputKBps(float kBps)` Only for restoration from persisted profile.
    `(package private) void` | `setPeakTunnelThroughputKBps(float kBps)` Only for restoration from persisted profile.
    `(package private) void` | `setPeerTestTimeAverage(float testAvg)`
    `void` | `setSpeedBonus(int bonus)`
    `void` | `setTunnelHistory(TunnelHistory history)`
    `String` | `toString()`
    `(package private) void` | `tunnelDataTransferred(long tunnelByteLifetime)` the tunnel pushed that much data in its lifetime
    `(package private) void` | `updatePeerTestTimeAverage(float ms)`
    `void` | `updateValues()`
    `boolean` | `wasUnreachable()`

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

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

- - ### Field Detail

    - #### ENABLE_TUNNEL_TEST_RESPONSE_TIME

          static final boolean ENABLE_TUNNEL_TEST_RESPONSE_TIME

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### PeerProfile

          public PeerProfile(RouterContext context,
                             Hash peer)

      Caller should call setLastHeardAbout() and setFirstHeardAbout()
      Parameters:
        `peer` - non-null

  - ### Method Detail

    - #### coalesceOnly

          void coalesceOnly(boolean shouldDecay)

      Caller must next call updateValues()

    - #### coalesceStats

          public void coalesceStats()

      Coalesce all stats and update values

    - #### dataPushed

          void dataPushed(int size)

    - #### dataPushed1m

          void dataPushed1m(int size)

      the tunnel pushed that much data in a 1 minute period

    - #### equals

          public boolean equals(Object obj)

      Overrides:
        `equals` in class `Object`

    - #### expandDBProfile

          public void expandDBProfile()

      For floodfills

    - #### expandProfile

          public void expandProfile()

      When the given peer is performing well enough that we want to keep detailed stats on them again, call this to set up the info we dropped during shrinkProfile. This will not however overwrite any existing data, so it can be safely called repeatedly

    - #### getCapacityBonus

          public int getCapacityBonus()

      extra factor added to the capacity ranking - this can be updated in the profile written to disk to affect how the algorithm ranks capacity. Negative values are penalties

    - #### getCapacityValue

          public float getCapacityValue()

      How many tunnels do we think this peer can handle over the next hour?

    - #### getContext

          RouterContext getContext()

      Helper for calculators

    - #### getDBHistory

          public DBHistory getDBHistory()

      history of db activity with the peer Warning - may return null if !getIsExpandedDB()

    - #### getDbIntroduction

          public RateStat getDbIntroduction()

      how many new peers we get from dbSearchReplyMessages or dbStore messages, calculated over a 1 hour, 1 day, and 1 week period Warning - may return null if !getIsExpandedDB()

    - #### getDbResponseTime

          public RateStat getDbResponseTime()

      how long it takes to get a db response from the peer (in milliseconds), calculated over a 1 minute, 1 hour, and 1 day period Warning - may return null if !getIsExpandedDB()

    - #### getFirstHeardAbout

          public long getFirstHeardAbout()

      When did we first hear about this peer?
      Returns:
        greater than zero, set to now in constructor

    - #### getIntegrationBonus

          public int getIntegrationBonus()

      extra factor added to the integration ranking - this can be updated in the profile written to disk to affect how the algorithm ranks integration. Negative values are penalties

    - #### getIntegrationValue

          public float getIntegrationValue()

      How well integrated into the network is this peer (as measured by how much they've told us that we didn't already know). Higher numbers means better integrated

    - #### getIsActive

          public boolean getIsActive()

      Is this peer active at the moment (sending/receiving messages within the last 10 minutes)

    - #### getIsActive

          public boolean getIsActive(long now)

      Is this peer active at the moment (sending/receiving messages within the last 10 minutes)

    - #### getIsActive

          public boolean getIsActive(long period,
                                     long now)

      Is this peer active at the moment (sending/receiving messages within the given period?) Also mark active if it is connected, as this will tend to encourage use of already-connected peers. Note: this appears to be the only use for these two RateStats. Update: Rewritten so we can get rid of the two RateStats. This also helps by not having it depend on coalesce boundaries.
      Parameters:
        `period` - must be one of the periods in the RateStat constructors below (5*60*1000 or 60*60*1000)

    - #### getIsExpanded

          public boolean getIsExpanded()

      are we keeping an expanded profile on the peer, or just the bare minimum. If we aren't keeping the expanded profile, all of the rates as well as the TunnelHistory and DBHistory will not be available.

    - #### getIsExpandedDB

          public boolean getIsExpandedDB()

    - #### getLastHeardAbout

          public long getLastHeardAbout()

      when did we last hear about this peer?
      Returns:
        0 if unset

    - #### getLastHeardFrom

          public long getLastHeardFrom()

      when did we last hear from the peer?

    - #### getLastSendFailed

          public long getLastSendFailed()

      when did we last have a problem sending to this peer?

    - #### getLastSendSuccessful

          public long getLastSendSuccessful()

      when did we last send to this peer successfully?

    - #### getPeakThroughputKBps

          public float getPeakThroughputKBps()

    - #### getPeakTunnel1mThroughputKBps

          public float getPeakTunnel1mThroughputKBps()

      This is the speed value
      Returns:
        the average of the three fastest one-minute data transfers, on a per-tunnel basis, through this peer. Ever. Except that the peak values are cut in half periodically by coalesceThroughput().

    - #### getPeakTunnelThroughputKBps

          public float getPeakTunnelThroughputKBps()

    - #### getPeer

          public Hash getPeer()

      what peer is being profiled, non-null

    - #### getPeerTestResponseTime

          public RateStat getPeerTestResponseTime()

      how long it takes for a peer to respond to a direct test (ms)

    - #### getPeerTestTimeAverage

          public float getPeerTestTimeAverage()

    - #### getSpeedBonus

          public int getSpeedBonus()

      extra factor added to the speed ranking - this can be updated in the profile written to disk to affect how the algorithm ranks speed. Negative values are penalties

    - #### getSpeedValue

          public float getSpeedValue()

      How fast is the peer, taking into consideration both throughput and latency. This may even be made to take into consideration current rates vs. estimated (or measured) max rates, allowing this speed to reflect the speed /available/.

    - #### getTunnelCreateResponseTime

          public RateStat getTunnelCreateResponseTime()

      how long it takes to get a tunnel create response from the peer (in milliseconds), calculated over a 1 minute, 1 hour, and 1 day period Warning - may return null if !getIsExpanded()

    - #### getTunnelHistory

          public TunnelHistory getTunnelHistory()

      history of tunnel activity with the peer Warning - may return null if !getIsExpanded()

    - #### getXORDistance

          int getXORDistance()

      For now, just a one-byte comparison
      Returns:
        -127 to +128, lower is closer

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### isEstablished

          boolean isEstablished()

    - #### isSameCountry

          boolean isSameCountry()

    - #### setCapacityBonus

          public void setCapacityBonus(int bonus)

    - #### setDBHistory

          public void setDBHistory(DBHistory hist)

    - #### setFirstHeardAbout

          void setFirstHeardAbout(long when)

      Set when did we first heard about this peer, only if older. Package private, only set by profile management subsystem.

    - #### setIntegrationBonus

          public void setIntegrationBonus(int bonus)

    - #### setLastHeardAbout

          public void setLastHeardAbout(long when)

      Set when did we last hear about this peer, only if unset or newer Also sets FirstHeardAbout if earlier

    - #### setLastHeardFrom

          public void setLastHeardFrom(long when)

    - #### setLastSendFailed

          public void setLastSendFailed(long when)

    - #### setLastSendSuccessful

          public void setLastSendSuccessful(long when)

    - #### setPeakThroughputKBps

          void setPeakThroughputKBps(float kBps)

      Only for restoration from persisted profile.

    - #### setPeakTunnel1mThroughputKBps

          void setPeakTunnel1mThroughputKBps(float kBps)

      Only for restoration from persisted profile.

    - #### setPeakTunnelThroughputKBps

          void setPeakTunnelThroughputKBps(float kBps)

      Only for restoration from persisted profile.

    - #### setPeerTestTimeAverage

          void setPeerTestTimeAverage(float testAvg)

    - #### setSpeedBonus

          public void setSpeedBonus(int bonus)

    - #### setTunnelHistory

          public void setTunnelHistory(TunnelHistory history)

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### tunnelDataTransferred

          void tunnelDataTransferred(long tunnelByteLifetime)

      the tunnel pushed that much data in its lifetime

    - #### updatePeerTestTimeAverage

          void updatePeerTestTimeAverage(float ms)

    - #### updateValues

          public void updateValues()

    - #### wasUnreachable

          public boolean wasUnreachable()