Class TCPClientInstance
- java.lang.Object
-
- com.coswald.jtalker.net.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
readUTFmethod, and outputs the data it receives via thewriteUTFmethod. This is all done within therunmethod, which will automaticallyclosethe listener. This closing only removes the socket's output stream from theServerOutputStream; it does not close theServerOutputStream. This is good, as there still may be other client instances running besides this one, using the sameServerOutputStream.As an aside, please do not call the
initmethod. This is called within therunmethod.- Since:
- JTalker 0.0.1
- Version:
- 0.0.2
- Author:
- C. William Oswald
-
-
Field Summary
Fields Modifier and Type Field Description protected DataInputStreaminputThe input stream used for socket input.protected ServerOutputStreamoutputThe output stream (where we send all the data we receive).protected SocketsocketThe socket used for data I/O.
-
Constructor Summary
Constructors Constructor Description TCPClientInstance(Socket socket, ServerOutputStream output)Constructs a client instance with the given socket and output stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Removes theDataOutputStreamassociated with this instance from theServerOutputStream, writes a goodbye message to theServerOUtputStream, closes the socket and the socket input.StringgetID()Returns the unique identifier of the client instance.voidinit()Initializes the client instance.voidrun()Runs the client instance by calling theinitmethod, and handles message logic.
-
-
-
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 therunmethod. This method will create aDataInputStreamform the socket's input stream, aDataOutputStreamfrom the socket's output stream, and add that output stream to theServerOutputStreamassociated 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 therunmethod.- Specified by:
initin interfaceInitializable
-
run
public void run()
Runs the client instance by calling theinitmethod, 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 aServerOutputStream(which will send it to the rest of the clients and including this one). Once this client receives the exit message, it will call theclosemethod. When it receives a message, the client instance will output the same message; however, it adds theidentifier, a colon, a space, and then the message. It will then also append a carriage return and a newline.- Specified by:
runin interfaceRunnable- See Also:
init(),close(),ServerOutputStream
-
close
public void close() throws IOExceptionRemoves theDataOutputStreamassociated with this instance from theServerOutputStream, writes a goodbye message to theServerOUtputStream, closes the socket and the socket input.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- If any of theclose()methods threw an error.
-
getID
public String getID()
Returns the unique identifier of the client instance.- Returns:
- The unique identifier.
-
-