Package org.jcsp.net.tcpip
Class TCPIPLink
- java.lang.Object
-
- org.jcsp.net.Link
-
- org.jcsp.net.tcpip.TCPIPLink
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classTCPIPLink.CreateRxStreamThe process which creates rxStream.private classTCPIPLink.CreateTxStreamThe process which creates txStream.private classTCPIPLink.RxIdThe process which recieves the remote Id.private classTCPIPLink.RxLoopThe process which recieves from a socket.private classTCPIPLink.TxIdThe process which sends our Id.private classTCPIPLink.TxLoopThe process which does transmission to a socket.private classTCPIPLink.TxLoopPoisonAn object of this type is used by RxLoop to poison TxLoop.
-
Field Summary
Fields Modifier and Type Field Description private static intBUFFER_SIZESize of Java buffers.private static java.lang.StringPROTOCOL_IDENTIFIERHandshaking string.private TCPIPAddressIDremoteTCPIPAddressThe other computer's IP address and port.private java.io.ObjectInputStreamrxStreamThe stream for reading from the socket.private java.net.SocketsocketThe actual socket.private java.io.ObjectOutputStreamtxStreamThe stream for reading from the socket.-
Fields inherited from class org.jcsp.net.Link
remoteNodeID, sendNodeID, txChannel
-
-
Constructor Summary
Constructors Constructor Description TCPIPLink(java.net.Socket socket)Constructor for using an existing socket.TCPIPLink(java.net.Socket socket, boolean client)Constructor for using an existing socket.TCPIPLink(TCPIPAddressID remoteTCPIPAddress)Constructor for connecting to a remote computer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcloseRx()Closes the Rx stream and the socket, if needed.private voidcloseSocket()Closes the socket, if needed.private voidcloseTx()Closes the Tx stream and the socket, if needed.protected booleanconnect()Connect to remote host.protected booleancreateResources()Create the object streams used to communicate with the peer system.protected voiddestroyResources()Closes the streams and the socket, if needed.protected booleanexchangeNodeIDs()Performs the node exchange part of the handshaking process.protected booleanreadLinkDecision()Reads a boolean link decision from the input stream.protected java.lang.ObjectreadTestObject()Reads a test object from the input stream.protected voidrunTxRxLoop()Run the send and receive threads to marshall and unmarshall objects.protected voidstart(boolean newProcess)Start this Link but allow the caller to continue in parallel.protected voidwaitForReplies(int numRepliesOutstanding)Waits fornumRepliesOutstandinginstances ofLinkLostto arrive from thetxChannel.protected voidwriteLinkDecision(boolean use)Writes a boolean link decision value to the output stream.protected voidwriteTestObject(java.lang.Object obj)Writes a test object to the output stream, flushing and resetting the stream afterwards.-
Methods inherited from class org.jcsp.net.Link
deliverReceivedObject, equals, getPingTime, getProtocolID, getReadSequence, getRemoteNodeID, getTxChannel, hashCode, obtainNodeID, performedPingTest, ping, run
-
-
-
-
Field Detail
-
remoteTCPIPAddress
private TCPIPAddressID remoteTCPIPAddress
The other computer's IP address and port.
-
socket
private java.net.Socket socket
The actual socket.
-
rxStream
private java.io.ObjectInputStream rxStream
The stream for reading from the socket.
-
txStream
private java.io.ObjectOutputStream txStream
The stream for reading from the socket.
-
PROTOCOL_IDENTIFIER
private static java.lang.String PROTOCOL_IDENTIFIER
Handshaking string.
-
BUFFER_SIZE
private static final int BUFFER_SIZE
Size of Java buffers. Note that there are also OS buffers, set usingSocket.setReceiveBufferSize(int)andSocket.setSendBufferSize(int).- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TCPIPLink
TCPIPLink(java.net.Socket socket)
Constructor for using an existing socket.- Parameters:
socket- The socket to the remote computer, which must already be open and active. You must not have transmitted anything over the socket.
-
TCPIPLink
TCPIPLink(java.net.Socket socket, boolean client)Constructor for using an existing socket.- Parameters:
socket- The socket to the remote computer, which must already be open and active. You must not have transmitted anything over the socket.client- Tells the link whether to act as a client or a server during the handshake process
-
TCPIPLink
TCPIPLink(TCPIPAddressID remoteTCPIPAddress)
Constructor for connecting to a remote computer. The actual connect happens when you run the process using start().- Parameters:
remoteTCPIPAddress- The remote computer to connect to.
-
-
Method Detail
-
runTxRxLoop
protected void runTxRxLoop()
Run the send and receive threads to marshall and unmarshall objects.- Overrides:
runTxRxLoopin classLink
-
waitForReplies
protected void waitForReplies(int numRepliesOutstanding)
Waits fornumRepliesOutstandinginstances ofLinkLostto arrive from thetxChannel.- Overrides:
waitForRepliesin classLink- Parameters:
numRepliesOutstanding-LinkLostinstances to wait for.
-
start
protected void start(boolean newProcess)
Start this Link but allow the caller to continue in parallel. This is the similar to "new ProcessManager(this).start()" - except that Link no longer implements CSProcess, so you can't do that.
-
connect
protected boolean connect()
Connect to remote host. Should only be called for client-side Links which have not yet been connected. (i.e. where socket==null and remoteAddress != null).
-
createResources
protected boolean createResources()
Create the object streams used to communicate with the peer system. Called internally by the superclass.- Overrides:
createResourcesin classLink- Returns:
- true on success, false on failure
-
exchangeNodeIDs
protected boolean exchangeNodeIDs()
Performs the node exchange part of the handshaking process. Creates a process to send thisNodeIDand a process to receive the peer node ID. Called internally by the superclass.- Overrides:
exchangeNodeIDsin classLink- Returns:
- true on success, false on failure
-
writeTestObject
protected void writeTestObject(java.lang.Object obj) throws java.lang.ExceptionWrites a test object to the output stream, flushing and resetting the stream afterwards. Called internally by the superclass during the testing stage to determine link speed. The parameter passed to this method must be returned by thereadTestObjectmethod at the peer node.- Overrides:
writeTestObjectin classLink- Parameters:
obj- Object to be sent- Throws:
java.lang.Exception
-
readTestObject
protected java.lang.Object readTestObject() throws java.lang.ExceptionReads a test object from the input stream. Called internally by the superclass during the testing stage to determine link speed. Returns the parameter passed towriteTestObjectat the peer node.- Overrides:
readTestObjectin classLink- Returns:
- the object received.
- Throws:
java.lang.Exception
-
writeLinkDecision
protected void writeLinkDecision(boolean use) throws java.lang.ExceptionWrites a boolean link decision value to the output stream. Called internally by the superclass during the handshaking sequence. The parameter passed to this method must be returned by thereadLinkDecisionmethod at the peer node.- Overrides:
writeLinkDecisionin classLink- Parameters:
use- boolean decision value to send.- Throws:
java.lang.Exception
-
readLinkDecision
protected boolean readLinkDecision() throws java.lang.ExceptionReads a boolean link decision from the input stream. Called internally during the handshaking sequence by the superclass. Returns the parameter passed towriteLinkDecisionat the peer node.- Overrides:
readLinkDecisionin classLink- Returns:
- the boolean decision.
- Throws:
java.lang.Exception
-
destroyResources
protected void destroyResources()
Closes the streams and the socket, if needed. Surpresses errors.- Overrides:
destroyResourcesin classLink
-
closeRx
private void closeRx()
Closes the Rx stream and the socket, if needed. Surpresses errors.
-
closeTx
private void closeTx()
Closes the Tx stream and the socket, if needed. Surpresses errors.
-
closeSocket
private void closeSocket()
Closes the socket, if needed. Surpresses errors.
-
-