Class TCPIPLink

  • All Implemented Interfaces:
    CSProcess

    class TCPIPLink
    extends Link
    Implements a link running over TCP/IP sockets. See the definition of Link for full details.
    See Also:
    Link
    • 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 for numRepliesOutstanding instances of LinkLost to arrive from the txChannel.
      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 java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • 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 using Socket.setReceiveBufferSize(int) and Socket.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 class Link
      • waitForReplies

        protected void waitForReplies​(int numRepliesOutstanding)
        Waits for numRepliesOutstanding instances of LinkLost to arrive from the txChannel.
        Overrides:
        waitForReplies in class Link
        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).
        Overrides:
        connect in class Link
        Returns:
        true on success, false on failure.
      • createResources

        protected boolean createResources()
        Create the object streams used to communicate with the peer system. Called internally by the superclass.
        Overrides:
        createResources in class Link
        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 this NodeID and a process to receive the peer node ID. Called internally by the superclass.
        Overrides:
        exchangeNodeIDs in class Link
        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 the readTestObject method at the peer node.
        Overrides:
        writeTestObject in class Link
        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 to writeTestObject at the peer node.
        Overrides:
        readTestObject in class Link
        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 the readLinkDecision method at the peer node.
        Overrides:
        writeLinkDecision in class Link
        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 to writeLinkDecision at the peer node.
        Overrides:
        readLinkDecision in class Link
        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 class Link
      • 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.