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 class
TCPIPLink.CreateRxStream
The process which creates rxStream.private class
TCPIPLink.CreateTxStream
The process which creates txStream.private class
TCPIPLink.RxId
The process which recieves the remote Id.private class
TCPIPLink.RxLoop
The process which recieves from a socket.private class
TCPIPLink.TxId
The process which sends our Id.private class
TCPIPLink.TxLoop
The process which does transmission to a socket.private class
TCPIPLink.TxLoopPoison
An object of this type is used by RxLoop to poison TxLoop.
-
Field Summary
Fields Modifier and Type Field Description private static int
BUFFER_SIZE
Size of Java buffers.private static java.lang.String
PROTOCOL_IDENTIFIER
Handshaking string.private TCPIPAddressID
remoteTCPIPAddress
The other computer's IP address and port.private java.io.ObjectInputStream
rxStream
The stream for reading from the socket.private java.net.Socket
socket
The actual socket.private java.io.ObjectOutputStream
txStream
The 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 void
closeRx()
Closes the Rx stream and the socket, if needed.private void
closeSocket()
Closes the socket, if needed.private void
closeTx()
Closes the Tx stream and the socket, if needed.protected boolean
connect()
Connect to remote host.protected boolean
createResources()
Create the object streams used to communicate with the peer system.protected void
destroyResources()
Closes the streams and the socket, if needed.protected boolean
exchangeNodeIDs()
Performs the node exchange part of the handshaking process.protected boolean
readLinkDecision()
Reads a boolean link decision from the input stream.protected java.lang.Object
readTestObject()
Reads a test object from the input stream.protected void
runTxRxLoop()
Run the send and receive threads to marshall and unmarshall objects.protected void
start(boolean newProcess)
Start this Link but allow the caller to continue in parallel.protected void
waitForReplies(int numRepliesOutstanding)
Waits fornumRepliesOutstanding
instances ofLinkLost
to arrive from thetxChannel
.protected void
writeLinkDecision(boolean use)
Writes a boolean link decision value to the output stream.protected void
writeTestObject(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:
runTxRxLoop
in classLink
-
waitForReplies
protected void waitForReplies(int numRepliesOutstanding)
Waits fornumRepliesOutstanding
instances ofLinkLost
to arrive from thetxChannel
.- Overrides:
waitForReplies
in classLink
- Parameters:
numRepliesOutstanding
-LinkLost
instances 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:
createResources
in 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 thisNodeID
and a process to receive the peer node ID. Called internally by the superclass.- Overrides:
exchangeNodeIDs
in classLink
- Returns:
- true on success, false on failure
-
writeTestObject
protected void writeTestObject(java.lang.Object obj) throws java.lang.Exception
Writes 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 thereadTestObject
method at the peer node.- Overrides:
writeTestObject
in classLink
- Parameters:
obj
- Object to be sent- Throws:
java.lang.Exception
-
readTestObject
protected java.lang.Object readTestObject() throws java.lang.Exception
Reads a test object from the input stream. Called internally by the superclass during the testing stage to determine link speed. Returns the parameter passed towriteTestObject
at the peer node.- Overrides:
readTestObject
in classLink
- Returns:
- the object received.
- Throws:
java.lang.Exception
-
writeLinkDecision
protected void writeLinkDecision(boolean use) throws java.lang.Exception
Writes 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 thereadLinkDecision
method at the peer node.- Overrides:
writeLinkDecision
in classLink
- Parameters:
use
- boolean decision value to send.- Throws:
java.lang.Exception
-
readLinkDecision
protected boolean readLinkDecision() throws java.lang.Exception
Reads a boolean link decision from the input stream. Called internally during the handshaking sequence by the superclass. Returns the parameter passed towriteLinkDecision
at the peer node.- Overrides:
readLinkDecision
in classLink
- Returns:
- the boolean decision.
- Throws:
java.lang.Exception
-
destroyResources
protected void destroyResources()
Closes the streams and the socket, if needed. Surpresses errors.- Overrides:
destroyResources
in 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.
-
-