Package com.coswald.jtalker.net
Class TCPServer
- java.lang.Object
-
- com.coswald.jtalker.net.TCPServer
-
- All Implemented Interfaces:
Initializable,Closeable,AutoCloseable,Runnable
public class TCPServer extends Object implements Closeable, Initializable, Runnable
A TCP server for the JTalker application. This class defines everything you would need to get a server and a client to talk, including the socket programming and the method that JTalker uses to exit and enter. This class mainly deals with adding
TCPClientInstances to aThreadPoolExecutor. The logic that is used to send messages to multiple clients is handled withinServerOutputStream, and this is passed to eachTCPClientInstanceto make sure that they can send messages to each other.- Since:
- JTalker 0.0.1
- Version:
- 0.0.1
- Author:
- C. William Oswald
- See Also:
TCPClientInstance,ServerOutputStream
-
-
Constructor Summary
Constructors Constructor Description TCPServer(int port)Constructs a server withSystem.outas the default output and the given port number.TCPServer(PrintStream out, int port)Constructs a server with the given output stream and the given port.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Shuts down the threadpool associated with the clients, the server output stream, as well as the server socket.intgetActiveClients()Returns the amount of active clients connected to the server.intgetPort()Returns the port number our server will or has binded to.voidinit()Initializes the server by binding aServerSocketto a port.booleanisRunning()Returns the boolean value that represents whether the server is running.voidrun()Waits for clients to connect and handles them when they do.voidsetPort(int port)Sets the port number our server will or has binded to.
-
-
-
Constructor Detail
-
TCPServer
public TCPServer(PrintStream out, int port)
Constructs a server with the given output stream and the given port. No parameter can be null, and the port must not only be a valid port, but it also must be within the port range specified by theminandmaxport numbers ( 0 and 65535).- Parameters:
out- The output stream to send server messages to.port- The port to bind to.- Throws:
IllegalARgumentException- If any parameter isnull, or the port is outside the given bounds.
-
TCPServer
public TCPServer(int port)
Constructs a server withSystem.outas the default output and the given port number.- Parameters:
port- The port to bind to.
-
-
Method Detail
-
init
public void init()
Initializes the server by binding aServerSocketto a port. This will also send messages to the output stream provided to us. These messages will let the user know that the JTalker server has started.- Specified by:
initin interfaceInitializable- See Also:
ServerSocket
-
run
public void run()
Waits for clients to connect and handles them when they do. This logic will accept client up until the maximum amount of clients has been reached, which is 8. With each newSocketthat is created, it will add aTCPClientInstanceand add it to a fixed thread pool. This thread pool takes care of the logic of switching between multiple clients and also helps with thread management (it will call therunmethod). As to the output, theServerOutputStreamis passed to theTCPClientInstance, and that class takes care of the output and input.- Specified by:
runin interfaceRunnable- See Also:
ServerOutputStream,TCPClientInstance.run()
-
close
public void close() throws IOExceptionShuts down the threadpool associated with the clients, the server output stream, as well as the server socket.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- See Also:
ThreadPoolExecutor.shutdown(),ServerSocket.close(),ServerOutputStream.close()
-
getActiveClients
public int getActiveClients()
Returns the amount of active clients connected to the server.- Returns:
- The amount of active clients.
-
getPort
public int getPort()
Returns the port number our server will or has binded to.- Returns:
- The port number.
-
isRunning
public boolean isRunning()
Returns the boolean value that represents whether the server is running.- Returns:
truewhen the server is running,falseotherwise.
-
setPort
public void setPort(int port)
Sets the port number our server will or has binded to. Note that this method will not change the port if we are currently bound to a port.- Parameters:
port- The port number we are binding the TCP socket to.- See Also:
isRunning()
-
-