net.vattp.data
Class VatTPConnection

java.lang.Object
  |
  +--net.vattp.data.VatTPConnection

public final class VatTPConnection
extends Object

Untamed:


Field Summary
private  long bytesReceived
          The total bytes of messages received over this VatTPConnection
private  long bytesSent
          The total bytes of messages sent over this VatTPConnection
private static byte DEAD
           
private static byte DIEING
           
private  int maxReceivedMessageSize
          The size of the largest message received over this VatTPConnection
private  int maxSentMessageSize
          The size of the largest message sent over this VatTPConnection
private  long messagesReceived
          The total number of messages received over this VatTPConnection
private  long messagesSent
          The total number of messages sent over this VatTPConnection
private  Hashtable myAddressesTried
          A Hashtable of the InetAddresses tried which failed due to host related problems.
private  VatTPMgr myConnMgr
          The VatTPMgr we report status changes to.
private  DataPath myDataPath
          The DataPath object we are using.
private  String myFirstAddressToTry
          The first place to look for the remote vat when resuming the connection.
private  String myFlattenedRemoteSearchPath
          The places to search for the remote vat as a semicolon separated list.
private  KeyPair myIdentityKeys
          The key pair which defines the identity of this vat
private  DataPath myIncomingDataPath
          During crossed connection resolution, the incoming DataPath.
private  boolean myIsIncoming
          Whether we are initiating the connection or responding to a remotely initiated connection.
private  String myLocalFlattenedSearchPath
          The semicolon separated search path for this vat
private  NetAddr myLocalNetAddr
          The InetAddress of the local end, or null if no connection has been made
private  byte[] myLocalSuspendID
          The suspend ID we asked the other end to present to resume the connection, or null if the connection is not suspended.
private  String myLocalVatID
           
private  MsgHandler[] myMsgHandlers
          Array of MsgHandlers indexed by message type
private  byte[] myOutgoingSuspendID
          The suspend ID to present to the other end for a resume, or null for a new connection
private  Vector myPendingOutput
          Place to queue outgoing messages while myState != RUNNING
private  Vector myProblemAccumulator
          Place to accumulate the results of our search attempts
private  AuthSecrets myProtocolParms
          The protocol versions and authorization parameters
private  String myRemoteAddr
          The location the current DataPath are (to) (have been) speaking to.
private  NetAddr myRemoteNetAddr
          The InetAddress of the remote end, or null if no connection has been made
private  String myRemoteVatID
          The VatID of the other end of the connectiion.
private  Throwable myShutdownReason
          The reason shutDownConnection has been called
private  DynamicMapEnumeration mySiteSearch
          An enumeration of the places to search for the remote vat
private  byte myState
          The current state of this VatTPConnection
private  Vat myVat
          The Vat to have the SendThread and RecvThread synchronize on when calling methods in this object.
private static byte RESUMING
           
private static byte RUNNING
           
private static byte STARTING
           
private static byte SUSPENDED
           
private static byte SUSPENDING
           
 
Constructor Summary
(package private) VatTPConnection(VatTPMgr connMgr, KeyPair identityKeys, String localVatID, Vat vat, String remoteVatID, DataPath path, String localFlattenedSearchPath, boolean isIncoming)
          Make a new VatTPConnection for an incoming connection
(package private) VatTPConnection(VatTPMgr connMgr, String remoteVatID, String flattenedRemoteSearchPath, KeyPair identityKeys, String localVatID, Vat vat, String localFlattenedSearchPath)
          Make a new VatTPConnection
 
Method Summary
(package private)  void close()
          Close the TCP link on this connection.
(package private)  int connectPath(DataPath incomingPath, String remoteVatID, boolean isIncoming)
          Connect a DataPath object for an incoming connection to an existing VatTPConnection object.
(package private)  void duplicatePath(String reason, DataPath path)
          Duplicate crossed connection DataPath exiting.
private  void enqueue(Object message)
          Enqueue a message for output.
(package private)  void extendSearchPath(ConstList addresses)
          Add an address to the list of addresses to try.
 ConditionLock getEmbargoLock()
          Enabled:
 NetAddr getLocalNetAddr()
          Enabled: Get the NetAddr of the local end, or null if no connection has been made.
 String getLocalVatID()
          Enabled: Get the vatID of the local end.
(package private)  byte[] getOutgoingSuspendID()
          Get the outgoing suspend ID for resuming a connection.
 NetAddr getRemoteNetAddr()
          Enabled: Get the NetAddr of the remote end, or null if no connection has been made.
 ConstList getRemoteSearchPath()
          Enabled: Get the search path to the remote end
 String getRemoteVatID()
          Enabled: Get the vatID of the remote end.
(package private)  int getState()
          Get the state of this VatTPConnection.
private  void handleConnectionDeath(Throwable reason)
          Handle the death of the connection
(package private)  void handleSuspend(byte[] suspendID)
          Suspend this connection in response to a request from the other end.
(package private)  void identifiedPathDied(String reason, DataPath path)
          Notice that an identified incoming DataPath has failed to complete the startup protocol.
(package private)  boolean isChoiceIncoming()
          Decide whether to keep incoming connection or outgoing when other end asks us to chose on an outgoing connection.
private  String makeConnectionStatusReport()
           
(package private)  void newIncomingMsg(byte[] message)
          Handle a new incoming message from the remote end.
(package private)  void noticeProblem(Throwable problem)
          Notice problem on the connection.
(package private)  boolean noticeRemoteResume(byte[] suspendID)
          Check if a connection should be resumed.
(package private)  void recordConnectionFailure(Object msg)
          Record why this try failed.
 void registerMsgHandler(byte msgType, MsgHandler handler)
          Enabled: Register a MsgHandler for a particular message type.
(package private)  void sendFinished(int count, int length, StreamMessage msg)
          sendFinished called when a message has been completely passed to TCP
 void sendMsg(byte[] message)
          Enabled: Send a message to the remote vat.
 void sendMsg(byte[] message, Runnable notification, Vat placeToRun)
          Enabled: Send a message to the remote vat with notification that it has been queued for processing by the TCP/IP stack on the local machine.
 void shutDownConnection()
          Enabled: Causes a clean shutdown of the connection.
 void shutDownConnection(Throwable reason)
          Enabled: Causes a clean shutdown of the connection.
(package private)  void shutDownFinished(Throwable reason)
          shutDownFinished called when all queued messages have been sent and the socket closed.
private  void startResume()
          Start trying to resume the connection.
(package private)  void startupSuccessful(DataPath path, String remoteVatID, NetAddr remoteNetAddr, NetAddr localNetAddr, int remotePort, AuthSecrets authParms)
          Called by StartUpProtocol when a connection to a remote vat has been successfully made.
 void suspend()
          Enabled: Suspend the connection.
 String toString()
          Suppressed: Describe this connection object.
private  void tryNextAddress()
          Start an attempt to connect to the next (first) address in the list of addresses to try.
(package private)  void unRegisterMsgHandler(byte msgType, MsgHandler handler)
          Remove the registration for a handler for a particular message type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

myConnMgr

private final VatTPMgr myConnMgr
The VatTPMgr we report status changes to.


myDataPath

private DataPath myDataPath
The DataPath object we are using. During crossed connection resolution, the outbound path.


myIncomingDataPath

private DataPath myIncomingDataPath
During crossed connection resolution, the incoming DataPath. Otherwise null.


myMsgHandlers

private final MsgHandler[] myMsgHandlers
Array of MsgHandlers indexed by message type


mySiteSearch

private DynamicMapEnumeration mySiteSearch
An enumeration of the places to search for the remote vat


myAddressesTried

private Hashtable myAddressesTried
A Hashtable of the InetAddresses tried which failed due to host related problems. They are not to be tried again in this attempt to connect to the vat. The key is InetAddress, data is InetAddress.


myFirstAddressToTry

private String myFirstAddressToTry
The first place to look for the remote vat when resuming the connection. This value is "" when there is no better guesses than come from a VLS lookup.


myFlattenedRemoteSearchPath

private String myFlattenedRemoteSearchPath
The places to search for the remote vat as a semicolon separated list.


myRemoteAddr

private String myRemoteAddr
The location the current DataPath are (to) (have been) speaking to.


myRemoteNetAddr

private NetAddr myRemoteNetAddr
The InetAddress of the remote end, or null if no connection has been made


myLocalNetAddr

private NetAddr myLocalNetAddr
The InetAddress of the local end, or null if no connection has been made


myRemoteVatID

private final String myRemoteVatID
The VatID of the other end of the connectiion. May be null.


myVat

private final Vat myVat
The Vat to have the SendThread and RecvThread synchronize on when calling methods in this object.


myIdentityKeys

private final KeyPair myIdentityKeys
The key pair which defines the identity of this vat


myLocalVatID

private final String myLocalVatID

myLocalFlattenedSearchPath

private final String myLocalFlattenedSearchPath
The semicolon separated search path for this vat


myShutdownReason

private Throwable myShutdownReason
The reason shutDownConnection has been called


STARTING

private static final byte STARTING

RUNNING

private static final byte RUNNING

SUSPENDING

private static final byte SUSPENDING

SUSPENDED

private static final byte SUSPENDED

RESUMING

private static final byte RESUMING

DIEING

private static final byte DIEING

DEAD

private static final byte DEAD

myState

private byte myState
The current state of this VatTPConnection


myPendingOutput

private Vector myPendingOutput
Place to queue outgoing messages while myState != RUNNING


myProblemAccumulator

private Vector myProblemAccumulator
Place to accumulate the results of our search attempts


myIsIncoming

private boolean myIsIncoming
Whether we are initiating the connection or responding to a remotely initiated connection.


myOutgoingSuspendID

private byte[] myOutgoingSuspendID
The suspend ID to present to the other end for a resume, or null for a new connection


myLocalSuspendID

private byte[] myLocalSuspendID
The suspend ID we asked the other end to present to resume the connection, or null if the connection is not suspended.


myProtocolParms

private AuthSecrets myProtocolParms
The protocol versions and authorization parameters


bytesReceived

private long bytesReceived
The total bytes of messages received over this VatTPConnection


messagesReceived

private long messagesReceived
The total number of messages received over this VatTPConnection


maxReceivedMessageSize

private int maxReceivedMessageSize
The size of the largest message received over this VatTPConnection


bytesSent

private long bytesSent
The total bytes of messages sent over this VatTPConnection


messagesSent

private long messagesSent
The total number of messages sent over this VatTPConnection


maxSentMessageSize

private int maxSentMessageSize
The size of the largest message sent over this VatTPConnection

Constructor Detail

VatTPConnection

VatTPConnection(VatTPMgr connMgr,
                String remoteVatID,
                String flattenedRemoteSearchPath,
                KeyPair identityKeys,
                String localVatID,
                Vat vat,
                String localFlattenedSearchPath)
Make a new VatTPConnection

Parameters:
connMgr - The VatTPMgr to notify of status changes.
remoteVatID - the vatID of the remote vat
flattenedRemoteSearchPath - is a pathlist of Strings each of which is the IP address and port number of a place to look for the remote vat. The IP address can be a DNS name or a dot repsentation of the 32 bit IP number. Most commonly, the Strings will be the location of the Vat Location Servers (VLSs) with which the remote vat is believed to register.
identityKeys - is the KeyPair which defines the identity of this vat.
vat - is the Vat whose thread we use to synchronize calls into the VatTPConnection object from the SendThread and ReceiveThread
localFlattenedSearchPath - Is the search path we publish for references to us, flattened into a string.

VatTPConnection

VatTPConnection(VatTPMgr connMgr,
                KeyPair identityKeys,
                String localVatID,
                Vat vat,
                String remoteVatID,
                DataPath path,
                String localFlattenedSearchPath,
                boolean isIncoming)
          throws IOException
Make a new VatTPConnection for an incoming connection

Parameters:
connMgr - The VatTPMgr to notify of status changes.
identityKeys - is the KeyPair which defines the identity of this vat.
localVatID - is the vatID of the local vat.
vat - is the Vat whose thread we use to synchronize calls into the VatTPConnection object from the SendThread and ReceiveThread
remoteVatID - is the vatID of the remote vat.
path - The DataPath object controlling the TCP connection.
localFlattenedSearchPath - Is the search path we publish for references to us, flattened into a string.
isIncoming - is true if this connection is an incoming connection, otherwise false.
Method Detail

getEmbargoLock

public ConditionLock getEmbargoLock()
Enabled:


close

void close()
Close the TCP link on this connection.


connectPath

int connectPath(DataPath incomingPath,
                String remoteVatID,
                boolean isIncoming)
Connect a DataPath object for an incoming connection to an existing VatTPConnection object. This call is where we first know that there is a crossed connection problem.

Parameters:
incomingPath - is the DataPath object for the incoming connection.
remoteVatID - is the vatID of the remote vat.
isIncoming - is true if the DataPath represents an incoming TCP connection, is false if it is an outgoing TCP connection.
Returns:
LIVES_CONTINUE - Continue setting up this connection
LIVES_DUP - This connection is a duplicate, discard it.
LIVES_NOTIFY - Notify the other end of a duplicate connection. The other end must decide which connection to keep.

duplicatePath

void duplicatePath(String reason,
                   DataPath path)
Duplicate crossed connection DataPath exiting.

Parameters:
reason - is the reason this is a duplicate.
path - is the path which has been declared duplicate.

enqueue

private void enqueue(Object message)
              throws IOException
Enqueue a message for output.

Parameters:
message - the message to queue
Throws:
IOException - if state is DEAD or DIEING

extendSearchPath

void extendSearchPath(ConstList addresses)
Add an address to the list of addresses to try.

Parameters:
addresses - An array of Strings of the addresses to add.

getLocalNetAddr

public NetAddr getLocalNetAddr()
Enabled: Get the NetAddr of the local end, or null if no connection has been made. If the connection is suspended or dead, this address will be the last address used when there was a live connection.

Returns:
the NetAddr of the local end or null if no connection has been made.

getLocalVatID

public String getLocalVatID()
Enabled: Get the vatID of the local end.

Returns:
the vatID of the local identity as a string.

getOutgoingSuspendID

byte[] getOutgoingSuspendID()
Get the outgoing suspend ID for resuming a connection.

Returns:
is the suspend ID or null if the connection is not suspended.

getRemoteNetAddr

public NetAddr getRemoteNetAddr()
Enabled: Get the NetAddr of the remote end, or null if no connection has been made. If the connection is suspended or dead, this address will be the last address used when there was a live connection.

Returns:
the NetAddr of the remote end or null if no connection has been made.

getRemoteSearchPath

public ConstList getRemoteSearchPath()
Enabled: Get the search path to the remote end


getRemoteVatID

public String getRemoteVatID()
Enabled: Get the vatID of the remote end.

Returns:
the vatID of the remote end as a string.

getState

int getState()
Get the state of this VatTPConnection.

Returns:
is the current state (STARTING, RUNNING, DIEING etc.) of this VatTPConnection object.

handleConnectionDeath

private void handleConnectionDeath(Throwable reason)
Handle the death of the connection


handleSuspend

void handleSuspend(byte[] suspendID)
             throws IOException
Suspend this connection in response to a request from the other end.

Parameters:
suspendID - is the ID we will need to present to resume the connection.
IOException

identifiedPathDied

void identifiedPathDied(String reason,
                        DataPath path)
Notice that an identified incoming DataPath has failed to complete the startup protocol.

Parameters:
path - is the DataPath object which failed.

isChoiceIncoming

boolean isChoiceIncoming()
Decide whether to keep incoming connection or outgoing when other end asks us to chose on an outgoing connection.

Returns:
true - Keep the incoming connection of the pair.
false - Keep the outgoing connection of the pair.

makeConnectionStatusReport

private String makeConnectionStatusReport()

newIncomingMsg

void newIncomingMsg(byte[] message)
Handle a new incoming message from the remote end.

Parameters:
message - the incoming message as a byte array. The first character is the message type as defined in class Msg.

noticeProblem

void noticeProblem(Throwable problem)
Notice problem on the connection. This method is called by the Send and Recv threads when they encounter a fatal error and give up.

Parameters:
problem - The exception which describes the problem

noticeRemoteResume

boolean noticeRemoteResume(byte[] suspendID)
Check if a connection should be resumed.

Parameters:
suspendID - The secret that the other side must know to resume a connection or null if this is a new connection.
Returns:
true if the connection should be completed.

false if the connection should be abandoned.


recordConnectionFailure

void recordConnectionFailure(Object msg)
Record why this try failed. When the path is shutdown, the next location in the search order will be tried.

Parameters:
msg - A String or Throwable describing the reason for abandoning this try.

registerMsgHandler

public void registerMsgHandler(byte msgType,
                               MsgHandler handler)
                        throws IOException
Enabled: Register a MsgHandler for a particular message type. It will be called whenever a new message of the specified type arrives. It will also be called when the connection dies.

Parameters:
msgType - is the message type. It must be chosen from the types defined in the definition class Msg. An IOException is thrown if an attempt is made to register more than one handler for a message type.

An assertion is made that the msgType is not out of range. An assertion is made that no attempts are made to register a handler after the reactToNewConnection method of the object registered with the VatTPMgr has returned.

handler - is an object which implements the MsgHandler interface. It will be called with processMessage when a message of msgType is received from the remote vat. It will be called with connectionDead when this connection dies.
Throws:
IOException - is thrown if there is already a different handler registered for this message type.
See Also:
Msg, MsgHandler, VatTPMgr

sendFinished

void sendFinished(int count,
                  int length,
                  StreamMessage msg)
sendFinished called when a message has been completely passed to TCP

Parameters:
length - The size of the message passed to TCP.

sendMsg

public void sendMsg(byte[] message)
             throws IOException
Enabled: Send a message to the remote vat.

Parameters:
message - is the message to be sent. It must not be altered after the call to sendMsg. The first byte of message is the message type and must be chosen from the types defined in the definition class Msg. (These two restrictions allow sendMsg to avoid copying the message into a private buffer.)
Throws:
IOException - is thrown if the message can not be queued to be sent because the connection has broken, if the message is longer than, Msg.MAX_OUTBOUND_MSG_LENGTH, or if there is no listener registered to process messages of this message's message type.
See Also:
Msg

sendMsg

public void sendMsg(byte[] message,
                    Runnable notification,
                    Vat placeToRun)
             throws IOException
Enabled: Send a message to the remote vat with notification that it has been queued for processing by the TCP/IP stack on the local machine. This call is designed to allow messaging with flow control. By only sending short message segments, and waiting for notification before sending the next segment, a caller can limit the buffering required by the communication system.

Parameters:
message - is the message to be sent. It must not be altered after the call to sendMsg. The first byte of message is the message type and must be chosen from the types defined in the definition class Msg. (These two restrictions allow sendMsg to avoid copying the message into a private buffer.)
notification - is the Runnable which will be enqueued when the message has been passed to the platform's TCP stack.
placeToRun - is the Vat where the notification will be enqueued.
Throws:
IOException - is thrown if the message can not be queued to be sent because the connection has broken, if the message is longer than, Msg.MAX_OUTBOUND_MSG_LENGTH, or if there is no listener registered to process messages of this message's message type.
See Also:
Msg

shutDownConnection

public void shutDownConnection()
Enabled: Causes a clean shutdown of the connection. All messages sent (XXX markm asks: "All messages sent" or "All messages queued to be sent"?) previously will be sent. When the shut down is complete, the connectionDead method will be called on all registered MsgHandlers.


shutDownConnection

public void shutDownConnection(Throwable reason)
Enabled: Causes a clean shutdown of the connection. All messages sent (XXX markm asks: "All messages sent" or "All messages queued to be sent"?) previously will be sent. When the shut down is complete, the connectionDead method will be called on all registered MsgHandlers.

XXX markm: I made this public, and changed ProxyConnection to call this with a non-ConnectionShutDownException. As far as I can tell, this shouldn't break anything. But Bill, it would be good for you to double check this.

Parameters:
reason - indicates why the connection is shutting down

shutDownFinished

void shutDownFinished(Throwable reason)
shutDownFinished called when all queued messages have been sent and the socket closed.


startResume

private void startResume()
Start trying to resume the connection.


startupSuccessful

void startupSuccessful(DataPath path,
                       String remoteVatID,
                       NetAddr remoteNetAddr,
                       NetAddr localNetAddr,
                       int remotePort,
                       AuthSecrets authParms)
Called by StartUpProtocol when a connection to a remote vat has been successfully made. The StartUpProtocol object has unregistered its MsgHandlers and is ready to become garbage.

Parameters:
path - is the DataPath object which made the connection.
remoteVatID - is the vatID of the remote vat.
remoteNetAddr - is the remote end's network address.
localNetAddr - is the local end's network address.
remotePort - is the remote port number we tried to connect to, or 0 if this is an inbound connection (or the port number is unavailable for some other reason). This port number will be used if we need to resume the connection later.
authParms - is the AuthSecrets object which contains the protocol versions and authorization parameters for the connection.

suspend

public void suspend()
             throws IOException
Enabled: Suspend the connection. Note that this method is NOT part of the published API. It is public to enable testing of the suspend/resume logic. (Popular demand could cause it to remain public.)

Suspended connections will be automatically resumed when data is sent to them with sendMsg().

Throws:
IOException - if the connection isn't in the RUNNING state.

toString

public String toString()
Suppressed: Describe this connection object.

Overrides:
toString in class Object
Returns:
a string representation of the object.

tryNextAddress

private void tryNextAddress()
Start an attempt to connect to the next (first) address in the list of addresses to try.


unRegisterMsgHandler

void unRegisterMsgHandler(byte msgType,
                          MsgHandler handler)
                    throws IOException
Remove the registration for a handler for a particular message type.

Parameters:
msgType - is the message type. It must be chosen from the types defined in the definition class Msg. It is a fatal error if the msgType is out of range.
handler - is the object currently registered as the handler.
Throws:
IOException - is thrown if the handler passed is not the handler registered for this message type.
See Also:
Msg, MsgHandler


comments?