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
TCPClientInstance
s to aThreadPoolExecutor
. The logic that is used to send messages to multiple clients is handled withinServerOutputStream
, and this is passed to eachTCPClientInstance
to 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.out
as 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 void
close()
Shuts down the threadpool associated with the clients, the server output stream, as well as the server socket.int
getActiveClients()
Returns the amount of active clients connected to the server.int
getPort()
Returns the port number our server will or has binded to.void
init()
Initializes the server by binding aServerSocket
to a port.boolean
isRunning()
Returns the boolean value that represents whether the server is running.void
run()
Waits for clients to connect and handles them when they do.void
setPort(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 themin
andmax
port 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.out
as 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 aServerSocket
to 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:
init
in 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 newSocket
that is created, it will add aTCPClientInstance
and 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 therun
method). As to the output, theServerOutputStream
is passed to theTCPClientInstance
, and that class takes care of the output and input.- Specified by:
run
in interfaceRunnable
- See Also:
ServerOutputStream
,TCPClientInstance.run()
-
close
public void close() throws IOException
Shuts down the threadpool associated with the clients, the server output stream, as well as the server socket.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in 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:
true
when the server is running,false
otherwise.
-
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()
-
-