Class 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 a ThreadPoolExecutor. The logic that is used to send messages to multiple clients is handled within ServerOutputStream, and this is passed to each TCPClientInstance 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 with System.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 a ServerSocket 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 the min and max 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 is null, or the port is outside the given bounds.
      • TCPServer

        public TCPServer​(int port)
        Constructs a server with System.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 a ServerSocket 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 interface Initializable
        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 new Socket that is created, it will add a TCPClientInstance 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 the run method). As to the output, the ServerOutputStream is passed to the TCPClientInstance, and that class takes care of the output and input.
        Specified by:
        run in interface Runnable
        See Also:
        ServerOutputStream, TCPClientInstance.run()
      • 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()