net.i2p.router.transport.udp

## Class PeerState

- java.lang.Object
- - net.i2p.router.transport.udp.PeerState

-      class PeerState
      extends Object

  Contain all of the state about a UDP connection to a peer.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `(package private) static int` | `IP_HEADER_SIZE`
    `(package private) static long` | `RETRANSMISSION_PERIOD_WIDTH` how many packets will be considered within the retransmission rate calculation
    `(package private) static int` | `UDP_HEADER_SIZE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PeerState(RouterContext ctx, UDPTransport transport)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `acked(ACKBitfield bitfield)`
    `int` | `acked(long messageId)`
    `int` | `add(OutboundMessageState state)`
    `void` | `adjustClockSkew(long skew)` Update the moving-average clock skew based on the current difference.
    `OutboundMessageState` | `allocateSend()` Pick a message we want to send and allocate it out of our window
    `boolean` | `allocateSendingBytes(int size, boolean isForACK)`
    `boolean` | `allocateSendingBytes(int size, boolean isForACK, int messagePushCount)`
    `boolean` | `allocateSendingBytes(int size, int messagePushCount)` Decrement the remaining bytes in the current period's window, returning true if the full size can be decremented, false if it cannot.
    `void` | `dataReceived()`
    `void` | `dropOutbound()` drop all outbound messages
    `void` | `ECNReceived()` we received a backoff request, so cut our send window
    `int` | `expireInboundMessages()` Expire partially received inbound messages, returning how many are still pending.
    `(package private) void` | `fetchPartialACKs(List<ACKBitfield> rv)`
    `int` | `finishMessages()` Expire / complete any outbound messages
    `long` | `getClockSkew()` How far off is the remote peer from our clock, in milliseconds? A positive number means our clock is ahead of theirs.
    `int` | `getConcurrentSends()`
    `int` | `getConcurrentSendWindow()`
    `int` | `getConsecutiveFailedSends()` how many seconds have we sent packets without any ACKs received?
    `int` | `getConsecutiveSendRejections()`
    `SessionKey` | `getCurrentCipherKey()` The AES key used to encrypt/decrypt packets, set only after the connection is established.
    `List<Long>` | `getCurrentFullACKs()` Grab a list of message ids (Long) that we want to send to the remote peer, regardless of the packet size, but don't remove it from our "want to send" list.
    `SessionKey` | `getCurrentMACKey()` The AES key used to verify packets, set only after the connection is established.
    `long` | `getCurrentReceiveSecond()` what is the current receive second, for congestion control?
    `boolean` | `getCurrentSecondECNReceived()` have we received a packet with the ECN bit set in the current second?
    `long` | `getInactivityTime()`
    `Map<Long,InboundMessageState>` | `getInboundMessages()` Fetch the internal id (Long) to InboundMessageState for incomplete inbound messages.
    `long` | `getIntroducerTime()`
    `long` | `getKeyEstablishedTime()` when were the current cipher and MAC keys established/rekeyed?
    `long` | `getLastACKSend()` when did we last send an ACK to the peer?
    `long` | `getLastReceiveTime()` when did we last receive a packet from them?
    `long` | `getLastSendFullyTime()` when did we last send them a message that was ACKed?
    `long` | `getLastSendTime()` when did we last send them a packet?
    `long` | `getMessagesReceived()`
    `long` | `getMessagesSent()`
    `int` | `getMTU()` what is the largest packet we can send to the peer?
    `long` | `getMTUDecreases()`
    `long` | `getMTUIncreases()`
    `long` | `getMTULastChecked()` when did we last check the MTU?
    `SessionKey` | `getNextCipherKey()` The pending AES key for encrypting/decrypting packets if we are rekeying the connection, or null if we are not in the process of rekeying.
    `int` | `getNextDelay()` return how long to wait before sending, or -1 if we have nothing to send
    `byte[]` | `getNextKeyingMaterial()` The keying material used for the rekeying, or null if we are not in the process of rekeying.
    `SessionKey` | `getNextMACKey()` The pending AES key for verifying packets if we are rekeying the connection, or null if we are not in the process of rekeying.
    `int` | `getOutboundMessageCount()`
    `long` | `getPacketRetransmissionRate()` avg number of packets retransmitted for every 100 packets
    `int` | `getPacketsPeriodRetransmitted()`
    `long` | `getPacketsPeriodTransmitted()`
    `long` | `getPacketsReceived()`
    `long` | `getPacketsReceivedDuplicate()`
    `long` | `getPacketsRetransmitted()`
    `long` | `getPacketsTransmitted()`
    `int` | `getReceiveACKBps()`
    `int` | `getReceiveBps()`
    `int` | `getReceiveMTU()` estimate how large the other side is sending packets
    `boolean` | `getRekeyBeganLocally()` true if we began the current rekeying, false otherwise
    `(package private) RemoteHostId` | `getRemoteHostId()`
    `byte[]` | `getRemoteIP()` what IP is the peer sending and receiving packets on?
    `InetAddress` | `getRemoteIPAddress()`
    `Hash` | `getRemotePeer()` The peer are we talking to.
    `int` | `getRemotePort()` what port is the peer sending and receiving packets on?
    `boolean` | `getRemoteRequiresIntroduction()` if we need to contact them, do we need to talk to an introducer?
    `boolean` | `getRemoteWantsPreviousACKs()` have all of the packets received in the current second requested that the previous second's ACKs be sent?
    `int` | `getRTO()` how soon should we retransmit an unacked packet?
    `int` | `getRTT()` how long does it usually take to get a message ACKed?
    `long` | `getRTTDeviation()` how skewed are the measured RTTs?
    `int` | `getSendACKBps()` how fast we are sending *ack* packets
    `int` | `getSendBps()` return the smoothed send transfer rate
    `int` | `getSendWindowBytes()` how many bytes should we send to the peer in a second
    `int` | `getSendWindowBytesRemaining()` how many bytes can we send to the peer in the current second
    `int` | `getSlowStartThreshold()`
    `long` | `getTheyRelayToUsAs()` If they have offered to serve as an introducer to us, this is the tag we can use to publish that fact.
    `long` | `getWantedACKSendSince()`
    `long` | `getWeRelayToThemAs()` if we are serving as an introducer to them, this is the the tag that they can publish that, when presented to us, will cause us to send a relay introduction to the current peer
    `int` | `incrementConsecutiveFailedSends()`
    `boolean` | `isInbound()`
    `void` | `loadFrom(PeerState oldPeer)` Transfer the basic activity/state from the old peer to the current peer
    `void` | `messageACKed(int bytesACKed, long lifetime, int numSends)` we sent a message which was ACKed containing the given # of bytes
    `void` | `messageFullyReceived(Long messageId, int bytes)` we received the message specified completely
    `void` | `messageFullyReceived(Long messageId, int bytes, boolean isForACK)`
    `void` | `messagePartiallyReceived()`
    `void` | `messageRetransmitted(int packets)` we are resending a packet, so lets jack up the rto
    `void` | `packetReceived(int size)`
    `void` | `packetsTransmitted(int packets)`
    `void` | `remoteDoesNotWantPreviousACKs()` have all of the packets received in the current second requested that the previous second's ACKs be sent?
    `void` | `removeACKMessage(Long messageId)`
    `List<ACKBitfield>` | `retrieveACKBitfields()` grab a list of ACKBitfield instances, some of which may fully ACK a message while others may only partially ACK a message.
    `List<ACKBitfield>` | `retrieveACKBitfields(boolean alwaysIncludeRetransmissions)`
    `void` | `setCurrentCipherKey(SessionKey key)` The AES key used to encrypt/decrypt packets, set only after the connection is established.
    `void` | `setCurrentMACKey(SessionKey key)` The AES key used to verify packets, set only after the connection is established.
    `void` | `setCurrentReceiveSecond(long sec)` what is the current receive second, for congestion control?
    `void` | `setInbound()`
    `void` | `setIntroducerTime()`
    `void` | `setKeyEstablishedTime(long when)` when were the current cipher and MAC keys established/rekeyed?
    `void` | `setLastACKSend(long when)`
    `void` | `setLastReceiveTime(long when)` when did we last receive a packet from them?
    `void` | `setLastSendTime(long when)` when did we last send them a packet?
    `void` | `setMTU(int mtu)` what is the largest packet we can send to the peer?
    `void` | `setNextCipherKey(SessionKey key)` The pending AES key for encrypting/decrypting packets if we are rekeying the connection, or null if we are not in the process of rekeying.
    `void` | `setNextKeyingMaterial(byte[] data)` The keying material used for the rekeying, or null if we are not in the process of rekeying.
    `void` | `setNextMACKey(SessionKey key)` The pending AES key for verifying packets if we are rekeying the connection, or null if we are not in the process of rekeying.
    `void` | `setRekeyBeganLocally(boolean local)` true if we began the current rekeying, false otherwise
    `void` | `setRemoteAddress(byte[] ip, int port)` what IP+port is the peer sending and receiving packets on?
    `void` | `setRemotePeer(Hash peer)` The peer are we talking to.
    `void` | `setRemoteRequiresIntroduction(boolean required)` if we need to contact them, do we need to talk to an introducer?
    `void` | `setTheyRelayToUsAs(long tag)` If they have offered to serve as an introducer to us, this is the tag we can use to publish that fact.
    `void` | `setWeRelayToThemAs(long tag)` if we are serving as an introducer to them, this is the the tag that they can publish that, when presented to us, will cause us to send a relay introduction to the current peer
    `String` | `toString()`
    `boolean` | `unsentACKThresholdReached()`

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

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

- - ### Field Detail

    - #### RETRANSMISSION_PERIOD_WIDTH

          static final long RETRANSMISSION_PERIOD_WIDTH

      how many packets will be considered within the retransmission rate calculation
      See Also:
        Constant Field Values

    - #### UDP_HEADER_SIZE

          static final int UDP_HEADER_SIZE

      See Also:
        Constant Field Values

    - #### IP_HEADER_SIZE

          static final int IP_HEADER_SIZE

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### PeerState

          public PeerState(RouterContext ctx,
                   UDPTransport transport)

  - ### Method Detail

    - #### getRemotePeer

          public Hash getRemotePeer()

      The peer are we talking to. This should be set as soon as this state is created if we are initiating a connection, but if we are receiving the connection this will be set only after the connection is established.

    - #### getCurrentMACKey

          public SessionKey getCurrentMACKey()

      The AES key used to verify packets, set only after the connection is established.

    - #### getCurrentCipherKey

          public SessionKey getCurrentCipherKey()

      The AES key used to encrypt/decrypt packets, set only after the connection is established.

    - #### getNextMACKey

          public SessionKey getNextMACKey()

      The pending AES key for verifying packets if we are rekeying the connection, or null if we are not in the process of rekeying.

    - #### getNextCipherKey

          public SessionKey getNextCipherKey()

      The pending AES key for encrypting/decrypting packets if we are rekeying the connection, or null if we are not in the process of rekeying.

    - #### getNextKeyingMaterial

          public byte[] getNextKeyingMaterial()

      The keying material used for the rekeying, or null if we are not in the process of rekeying.

    - #### getRekeyBeganLocally

          public boolean getRekeyBeganLocally()

      true if we began the current rekeying, false otherwise

    - #### getKeyEstablishedTime

          public long getKeyEstablishedTime()

      when were the current cipher and MAC keys established/rekeyed?

    - #### getClockSkew

          public long getClockSkew()

      How far off is the remote peer from our clock, in milliseconds? A positive number means our clock is ahead of theirs.

    - #### getCurrentReceiveSecond

          public long getCurrentReceiveSecond()

      what is the current receive second, for congestion control?

    - #### getLastSendTime

          public long getLastSendTime()

      when did we last send them a packet?

    - #### getLastSendFullyTime

          public long getLastSendFullyTime()

      when did we last send them a message that was ACKed?

    - #### getLastReceiveTime

          public long getLastReceiveTime()

      when did we last receive a packet from them?

    - #### getConsecutiveFailedSends

          public int getConsecutiveFailedSends()

      how many seconds have we sent packets without any ACKs received?

    - #### getCurrentSecondECNReceived

          public boolean getCurrentSecondECNReceived()

      have we received a packet with the ECN bit set in the current second?

    - #### getRemoteWantsPreviousACKs

          public boolean getRemoteWantsPreviousACKs()

      have all of the packets received in the current second requested that the previous second's ACKs be sent?

    - #### getSendWindowBytes

          public int getSendWindowBytes()

      how many bytes should we send to the peer in a second

    - #### getSendWindowBytesRemaining

          public int getSendWindowBytesRemaining()

      how many bytes can we send to the peer in the current second

    - #### getRemoteIP

          public byte[] getRemoteIP()

      what IP is the peer sending and receiving packets on?

    - #### getRemoteIPAddress

          public InetAddress getRemoteIPAddress()

    - #### getRemotePort

          public int getRemotePort()

      what port is the peer sending and receiving packets on?

    - #### getRemoteRequiresIntroduction

          public boolean getRemoteRequiresIntroduction()

      if we need to contact them, do we need to talk to an introducer?

    - #### getWeRelayToThemAs

          public long getWeRelayToThemAs()

      if we are serving as an introducer to them, this is the the tag that they can publish that, when presented to us, will cause us to send a relay introduction to the current peer

    - #### getTheyRelayToUsAs

          public long getTheyRelayToUsAs()

      If they have offered to serve as an introducer to us, this is the tag we can use to publish that fact.

    - #### getMTU

          public int getMTU()

      what is the largest packet we can send to the peer?

    - #### getReceiveMTU

          public int getReceiveMTU()

      estimate how large the other side is sending packets

    - #### getMTULastChecked

          public long getMTULastChecked()

      when did we last check the MTU?

    - #### getMTUIncreases

          public long getMTUIncreases()

    - #### getMTUDecreases

          public long getMTUDecreases()

    - #### setRemotePeer

          public void setRemotePeer(Hash peer)

      The peer are we talking to. This should be set as soon as this state is created if we are initiating a connection, but if we are receiving the connection this will be set only after the connection is established.

    - #### setCurrentMACKey

          public void setCurrentMACKey(SessionKey key)

      The AES key used to verify packets, set only after the connection is established.

    - #### setCurrentCipherKey

          public void setCurrentCipherKey(SessionKey key)

      The AES key used to encrypt/decrypt packets, set only after the connection is established.

    - #### setNextMACKey

          public void setNextMACKey(SessionKey key)

      The pending AES key for verifying packets if we are rekeying the connection, or null if we are not in the process of rekeying.

    - #### setNextCipherKey

          public void setNextCipherKey(SessionKey key)

      The pending AES key for encrypting/decrypting packets if we are rekeying the connection, or null if we are not in the process of rekeying.

    - #### setNextKeyingMaterial

          public void setNextKeyingMaterial(byte[] data)

      The keying material used for the rekeying, or null if we are not in the process of rekeying.

    - #### setRekeyBeganLocally

          public void setRekeyBeganLocally(boolean local)

      true if we began the current rekeying, false otherwise

    - #### setKeyEstablishedTime

          public void setKeyEstablishedTime(long when)

      when were the current cipher and MAC keys established/rekeyed?

    - #### adjustClockSkew

          public void adjustClockSkew(long skew)

      Update the moving-average clock skew based on the current difference. The raw skew will be adjusted for RTT/2 here.
      Parameters:
        `skew` - milliseconds, NOT adjusted for RTT. A positive number means our clock is ahead of theirs.

    - #### setCurrentReceiveSecond

          public void setCurrentReceiveSecond(long sec)

      what is the current receive second, for congestion control?

    - #### setLastSendTime

          public void setLastSendTime(long when)

      when did we last send them a packet?

    - #### setLastReceiveTime

          public void setLastReceiveTime(long when)

      when did we last receive a packet from them?

    - #### getSendBps

          public int getSendBps()

      return the smoothed send transfer rate

    - #### getReceiveBps

          public int getReceiveBps()

    - #### incrementConsecutiveFailedSends

          public int incrementConsecutiveFailedSends()

    - #### getInactivityTime

          public long getInactivityTime()

    - #### getSendACKBps

          public int getSendACKBps()

      how fast we are sending *ack* packets

    - #### getReceiveACKBps

          public int getReceiveACKBps()

    - #### remoteDoesNotWantPreviousACKs

          public void remoteDoesNotWantPreviousACKs()

      have all of the packets received in the current second requested that the previous second's ACKs be sent?

    - #### allocateSendingBytes

          public boolean allocateSendingBytes(int size,
                                     int messagePushCount)

      Decrement the remaining bytes in the current period's window, returning true if the full size can be decremented, false if it cannot. If it is not decremented, the window size remaining is not adjusted at all.

    - #### allocateSendingBytes

          public boolean allocateSendingBytes(int size,
                                     boolean isForACK)

    - #### allocateSendingBytes

          public boolean allocateSendingBytes(int size,
                                     boolean isForACK,
                                     int messagePushCount)

    - #### setRemoteAddress

          public void setRemoteAddress(byte[] ip,
                              int port)

      what IP+port is the peer sending and receiving packets on?

    - #### setRemoteRequiresIntroduction

          public void setRemoteRequiresIntroduction(boolean required)

      if we need to contact them, do we need to talk to an introducer?

    - #### setWeRelayToThemAs

          public void setWeRelayToThemAs(long tag)

      if we are serving as an introducer to them, this is the the tag that they can publish that, when presented to us, will cause us to send a relay introduction to the current peer

    - #### setTheyRelayToUsAs

          public void setTheyRelayToUsAs(long tag)

      If they have offered to serve as an introducer to us, this is the tag we can use to publish that fact.

    - #### setMTU

          public void setMTU(int mtu)

      what is the largest packet we can send to the peer?

    - #### getSlowStartThreshold

          public int getSlowStartThreshold()

    - #### getConcurrentSends

          public int getConcurrentSends()

    - #### getConcurrentSendWindow

          public int getConcurrentSendWindow()

    - #### getConsecutiveSendRejections

          public int getConsecutiveSendRejections()

    - #### isInbound

          public boolean isInbound()

    - #### setInbound

          public void setInbound()

    - #### getIntroducerTime

          public long getIntroducerTime()

    - #### setIntroducerTime

          public void setIntroducerTime()

    - #### messageFullyReceived

          public void messageFullyReceived(Long messageId,
                                  int bytes)

      we received the message specified completely

    - #### messageFullyReceived

          public void messageFullyReceived(Long messageId,
                                  int bytes,
                                  boolean isForACK)

    - #### messagePartiallyReceived

          public void messagePartiallyReceived()

    - #### getInboundMessages

          public Map<Long,InboundMessageState> getInboundMessages()

      Fetch the internal id (Long) to InboundMessageState for incomplete inbound messages. Access to this map must be synchronized explicitly!

    - #### expireInboundMessages

          public int expireInboundMessages()

      Expire partially received inbound messages, returning how many are still pending. This should probably be fired periodically, in case a peer goes silent and we don't try to send them any messages (and don't receive any messages from them either)

    - #### getCurrentFullACKs

          public List<Long> getCurrentFullACKs()

      Grab a list of message ids (Long) that we want to send to the remote peer, regardless of the packet size, but don't remove it from our "want to send" list. If the message id is transmitted to the peer, removeACKMessage(Long) should be called.

    - #### removeACKMessage

          public void removeACKMessage(Long messageId)

    - #### retrieveACKBitfields

          public List<ACKBitfield> retrieveACKBitfields()

      grab a list of ACKBitfield instances, some of which may fully ACK a message while others may only partially ACK a message. the values returned are limited in size so that they will fit within the peer's current MTU as an ACK - as such, not all messages may be ACKed with this call. Be sure to check getWantedACKSendSince() which will be unchanged if there are ACKs remaining.

    - #### retrieveACKBitfields

          public List<ACKBitfield> retrieveACKBitfields(boolean alwaysIncludeRetransmissions)

    - #### fetchPartialACKs

          void fetchPartialACKs(List<ACKBitfield> rv)

    - #### messageACKed

          public void messageACKed(int bytesACKed,
                          long lifetime,
                          int numSends)

      we sent a message which was ACKed containing the given # of bytes

    - #### messageRetransmitted

          public void messageRetransmitted(int packets)

      we are resending a packet, so lets jack up the rto

    - #### packetsTransmitted

          public void packetsTransmitted(int packets)

    - #### getRTT

          public int getRTT()

      how long does it usually take to get a message ACKed?

    - #### getRTO

          public int getRTO()

      how soon should we retransmit an unacked packet?

    - #### getRTTDeviation

          public long getRTTDeviation()

      how skewed are the measured RTTs?

    - #### getMessagesSent

          public long getMessagesSent()

    - #### getMessagesReceived

          public long getMessagesReceived()

    - #### getPacketsTransmitted

          public long getPacketsTransmitted()

    - #### getPacketsRetransmitted

          public long getPacketsRetransmitted()

    - #### getPacketsPeriodTransmitted

          public long getPacketsPeriodTransmitted()

    - #### getPacketsPeriodRetransmitted

          public int getPacketsPeriodRetransmitted()

    - #### getPacketRetransmissionRate

          public long getPacketRetransmissionRate()

      avg number of packets retransmitted for every 100 packets

    - #### getPacketsReceived

          public long getPacketsReceived()

    - #### getPacketsReceivedDuplicate

          public long getPacketsReceivedDuplicate()

    - #### packetReceived

          public void packetReceived(int size)

    - #### ECNReceived

          public void ECNReceived()

      we received a backoff request, so cut our send window

    - #### dataReceived

          public void dataReceived()

    - #### getLastACKSend

          public long getLastACKSend()

      when did we last send an ACK to the peer?

    - #### setLastACKSend

          public void setLastACKSend(long when)

    - #### getWantedACKSendSince

          public long getWantedACKSendSince()

    - #### unsentACKThresholdReached

          public boolean unsentACKThresholdReached()

    - #### getRemoteHostId

          RemoteHostId getRemoteHostId()

    - #### add

          public int add(OutboundMessageState state)

    - #### dropOutbound

          public void dropOutbound()

      drop all outbound messages

    - #### getOutboundMessageCount

          public int getOutboundMessageCount()

    - #### finishMessages

          public int finishMessages()

      Expire / complete any outbound messages
      Returns:
        number of active outbound messages remaining

    - #### allocateSend

          public OutboundMessageState allocateSend()

      Pick a message we want to send and allocate it out of our window
      Returns:
        allocated message to send, or null if no messages or no resources

    - #### getNextDelay

          public int getNextDelay()

      return how long to wait before sending, or -1 if we have nothing to send

    - #### acked

          public int acked(long messageId)

    - #### acked

          public void acked(ACKBitfield bitfield)

    - #### loadFrom

          public void loadFrom(PeerState oldPeer)

      Transfer the basic activity/state from the old peer to the current peer

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`