net.i2p.router.transport.tcp
Class TCPConnection

java.lang.Object
  extended by net.i2p.router.transport.tcp.TCPConnection

public class TCPConnection
extends java.lang.Object

Central choke point for a single TCP connection to a single peer.


Field Summary
static double DEFAULT_QUEUE_FREE_FACTOR
          by default, try to keep the queue completely full, but this can be overridden with the property 'tcp.queueFreeFactor'
 
Constructor Summary
TCPConnection(RouterContext ctx)
           
 
Method Summary
 void addMessage(OutNetMessage msg)
          Add the given message to the outbound queue, notifying our runners that we want to send it.
 java.util.List clearPendingMessages()
          Pull off any unsent OutNetMessages from the queue
 void closeConnection()
          Disconnect from the peer immediately.
 void closeConnection(boolean wasError)
           
 Hash getAttemptedPeer()
          Who we initially were trying to contact
(package private)  boolean getIsActive()
           
(package private)  boolean getIsClosed()
          Have we been closed already?
 boolean getIsOutbound()
           
 long getLifetime()
          How long has this connection been active for?
(package private)  OutNetMessage getNextMessage()
          Blocking call to retrieve the next pending message.
 long getOffsetReceived()
          skew that the other peer has from our clock
(package private)  java.io.OutputStream getOutputStream()
          Where this connection should write its data to.
 TCPAddress getRemoteAddress()
          What is the peer's TCP address (using the IP address not hostname)
 RouterIdentity getRemoteRouterIdentity()
          Who are we talking with (or null if not identified)
(package private)  RouterContext getRouterContext()
           
 long getSendRate()
          how many Bps we are sending data to the peer (or 2KBps if we don't know)
 java.lang.String getShownAddress()
          What address the peer said we are reachable on
 TCPTransport getTransport()
           
(package private)  void messageReceived()
           
 void runConnection()
          Actually start processing the messages on the connection (and reading from the peer, of course).
(package private)  void sent(OutNetMessage msg, boolean ok, long time)
          The message was sent.
 void setAttemptedPeer(Hash peer)
          Who we initially were trying to contact
(package private)  void setInputStream(java.io.InputStream in)
          Configure where this connection should read its data from.
 void setIsOutbound(boolean outbound)
           
 void setOffsetReceived(long ms)
           
(package private)  void setOutputStream(java.io.OutputStream out)
          Configure where this connection should write its data to.
 void setRemoteAddress(TCPAddress addr)
          What is the peer's TCP address (using the IP address not hostname)
 void setRemoteRouterIdentity(RouterIdentity ident)
          Who are we talking with (or null if not identified)
 void setShownAddress(java.lang.String ip)
          What address the peer said we are reachable on
(package private)  void setSocket(java.net.Socket socket)
          Configure what underlying socket this connection uses.
(package private)  void setTransport(TCPTransport transport)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_QUEUE_FREE_FACTOR

public static final double DEFAULT_QUEUE_FREE_FACTOR
by default, try to keep the queue completely full, but this can be overridden with the property 'tcp.queueFreeFactor'

See Also:
Constant Field Values
Constructor Detail

TCPConnection

public TCPConnection(RouterContext ctx)
Method Detail

getRemoteRouterIdentity

public RouterIdentity getRemoteRouterIdentity()
Who are we talking with (or null if not identified)


getRemoteAddress

public TCPAddress getRemoteAddress()
What is the peer's TCP address (using the IP address not hostname)


getAttemptedPeer

public Hash getAttemptedPeer()
Who we initially were trying to contact


setRemoteRouterIdentity

public void setRemoteRouterIdentity(RouterIdentity ident)
Who are we talking with (or null if not identified)


setRemoteAddress

public void setRemoteAddress(TCPAddress addr)
What is the peer's TCP address (using the IP address not hostname)


setAttemptedPeer

public void setAttemptedPeer(Hash peer)
Who we initially were trying to contact


setShownAddress

public void setShownAddress(java.lang.String ip)
What address the peer said we are reachable on


getShownAddress

public java.lang.String getShownAddress()
What address the peer said we are reachable on


getOffsetReceived

public long getOffsetReceived()
skew that the other peer has from our clock


setOffsetReceived

public void setOffsetReceived(long ms)

getTransport

public TCPTransport getTransport()

getIsOutbound

public boolean getIsOutbound()

setIsOutbound

public void setIsOutbound(boolean outbound)

runConnection

public void runConnection()
Actually start processing the messages on the connection (and reading from the peer, of course). This call should not block.


closeConnection

public void closeConnection()
Disconnect from the peer immediately. This stops any related helper threads, closes all streams, and fails all pending messages. This can be called multiple times safely.


closeConnection

public void closeConnection(boolean wasError)

clearPendingMessages

public java.util.List clearPendingMessages()
Pull off any unsent OutNetMessages from the queue


addMessage

public void addMessage(OutNetMessage msg)
Add the given message to the outbound queue, notifying our runners that we want to send it.


getSendRate

public long getSendRate()
how many Bps we are sending data to the peer (or 2KBps if we don't know)


getNextMessage

OutNetMessage getNextMessage()
Blocking call to retrieve the next pending message. As a side effect, this fails messages on the queue that have expired, and in turn never returns an expired message.

Returns:
next message or null if the connection has been closed.

getLifetime

public long getLifetime()
How long has this connection been active for?


setTransport

void setTransport(TCPTransport transport)

setInputStream

void setInputStream(java.io.InputStream in)
Configure where this connection should read its data from. This should have any necessary bandwidth limiting and encryption filters already wrapped in it.


setOutputStream

void setOutputStream(java.io.OutputStream out)
Configure where this connection should write its data to. This should have any necessary bandwidth limiting and encryption filters already wrapped in it.


setSocket

void setSocket(java.net.Socket socket)
Configure what underlying socket this connection uses. This is only referenced when closing the connection, and only if it was set.


getOutputStream

java.io.OutputStream getOutputStream()
Where this connection should write its data to.


getIsClosed

boolean getIsClosed()
Have we been closed already?


getRouterContext

RouterContext getRouterContext()

getIsActive

boolean getIsActive()

messageReceived

void messageReceived()

sent

void sent(OutNetMessage msg,
          boolean ok,
          long time)
The message was sent.

Parameters:
msg - message in question
ok - was the message sent ok?
time - how long did it take to write the message?