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
readUTF
method, and outputs the data it receives via thewriteUTF
method. This is all done within therun
method, which will automaticallyclose
the 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
init
method. This is called within therun
method.- Since:
- JTalker 0.0.1
- Version:
- 0.0.2
- Author:
- C. William Oswald
-
-
Field Summary
Fields Modifier and Type Field Description protected DataInputStream
input
The input stream used for socket input.protected ServerOutputStream
output
The output stream (where we send all the data we receive).protected Socket
socket
The 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 void
close()
Removes theDataOutputStream
associated with this instance from theServerOutputStream
, writes a goodbye message to theServerOUtputStream
, closes the socket and the socket input.String
getID()
Returns the unique identifier of the client instance.void
init()
Initializes the client instance.void
run()
Runs the client instance by calling theinit
method, 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 therun
method. This method will create aDataInputStream
form the socket's input stream, aDataOutputStream
from the socket's output stream, and add that output stream to theServerOutputStream
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 therun
method.- Specified by:
init
in interfaceInitializable
-
run
public void run()
Runs the client instance by calling theinit
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 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 theclose
method. 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:
run
in interfaceRunnable
- See Also:
init()
,close()
,ServerOutputStream
-
close
public void close() throws IOException
Removes theDataOutputStream
associated with this instance from theServerOutputStream
, writes a goodbye message to theServerOUtputStream
, closes the socket and the socket input.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in 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.
-
-