Class TCPClientInstance

  • All Implemented Interfaces:
    Initializable, Closeable, AutoCloseable, Runnable

    public class TCPClientInstance
    extends Object
    implements Closeable, Initializable, Runnable

    A listener that broadcasts all of what it "hears" from one client to the rest. This class determines the servers broadcast logic. An instance of this class will listen on one socket (a client) and send whatever it hears back to the other clients. That is why it is called a "client" instance: it is the part of the server that is listening on to one client. How it does this is very simplistic: it listens using the readUTF method, and outputs the data it receives via the writeUTF method. This is all done within the run method, which will automatically close the listener. This closing only removes the socket's output stream from the ServerOutputStream; it does not close the ServerOutputStream. This is good, as there still may be other client instances running besides this one, using the same ServerOutputStream.

    As an aside, please do not call the init method. This is called within the run method.

    Since:
    JTalker 0.0.1
    Version:
    0.0.2
    Author:
    C. William Oswald
    • Field Detail

      • socket

        protected Socket socket
        The socket used for data I/O.
      • input

        protected DataInputStream input
        The input stream used for socket input.
      • output

        protected ServerOutputStream output
        The output stream (where we send all the data we receive).
    • Constructor Detail

      • TCPClientInstance

        public TCPClientInstance​(Socket socket,
                                 ServerOutputStream output)
        Constructs a client instance with the given socket and output stream.
        Parameters:
        socket - The socket to use when listening.
        output - The output to resend all of our input to.
    • Method Detail

      • init

        public final void init()
        Initializes the client instance. Note that this method should not be called by anything other than the run method. This method will create a DataInputStream form the socket's input stream, a DataOutputStream from the socket's output stream, and add that output stream to the ServerOutputStream associated with the server. It will then write a boolean (true) to the output stream and listen for a unique identifier. Once the identifier is received, it is ready for listening within the run method.
        Specified by:
        init in interface Initializable
      • run

        public void run()
        Runs the client instance by calling the init method, and handles message logic. This will wait until the exit message "EXIT" is sent through the socket. Until this happens, the client instance will listen for input from its client. When it receives input, it will send it through a ServerOutputStream (which will send it to the rest of the clients and including this one). Once this client receives the exit message, it will call the close method. When it receives a message, the client instance will output the same message; however, it adds the identifier, a colon, a space, and then the message. It will then also append a carriage return and a newline.
        Specified by:
        run in interface Runnable
        See Also:
        init(), close(), ServerOutputStream
      • close

        public void close()
                   throws IOException
        Removes the DataOutputStream associated with this instance from the ServerOutputStream, writes a goodbye message to the ServerOUtputStream, closes the socket and the socket input.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Throws:
        IOException - If any of the close() methods threw an error.
      • getID

        public String getID()
        Returns the unique identifier of the client instance.
        Returns:
        The unique identifier.