Module OpenNetLib

Class Client

  • Direct Known Subclasses:
    SSLClient

    public abstract class Client
    extends java.lang.Object
    Represents an abstract implementation of the client-side connection.
    The connection process can be initiated using the client objects connect() method, which will return true, if the connection process has been successful.
    Closing the connection can be done using the client objects close() method, which will return true, if the disconnection process has been completed successfully.
    Incoming data from the server will be preprocessed by preprocessReceivedData(byte[]) and then forwarded to processReceivedData(byte[]).
    Data to the server can be sent using the clients IOHandler.
    Some events trigger the notification of registered ClientListeners.
    See Also:
    IOHandler, ClientListener
    • Constructor Summary

      Constructors 
      Constructor Description
      Client​(java.lang.String host, int port)
      Creates a Client object and sets the endpoint on which on startup the client will try to connect to
      Client​(java.net.URL url)
      Creates a Client object and sets the endpoint to which the client will connect on startup.
    • Field Detail

      • HOST

        public final java.lang.String HOST
      • PORT

        public final int PORT
      • socket

        protected java.net.Socket socket
    • Constructor Detail

      • Client

        public Client​(java.lang.String host,
                      int port)
        Creates a Client object and sets the endpoint on which on startup the client will try to connect to
        Parameters:
        host - the domain or IP-adress without protocoll information or port
        port - to connect to
      • Client

        public Client​(java.net.URL url)
        Creates a Client object and sets the endpoint to which the client will connect on startup.
        Parameters:
        url - the URL object that specifies the endpoint adress and port.
    • Method Detail

      • connect

        public boolean connect()
        Initiates the connection process.
        The IOHandler will be created. Listeners will be called before the client tries to connect to the server, when the connection was successful, and when it wasn't.
        Returns:
        true if the connection process as been completed successfully
      • openSocket

        protected void openSocket()
                           throws java.net.UnknownHostException,
                                  java.io.IOException
        Opens the Socket connection. Moved to a different function to make it easier to replace the Socket implementation.
        Throws:
        java.net.UnknownHostException - if the specified Host is unknown
        java.io.IOException - when the process of opening the Socket fails.
      • close

        public boolean close()
        Closes the connection.
        Listeners will be called before the disconnection process is started and when it succeeded or failed.
        It will also call the close method of the client objects IOHandler
        Returns:
        true if the connection has been closed successfully
        See Also:
        IOHandler.close()
      • send

        public void send​(byte[] data)
        Sends data to the Server
        Parameters:
        data - to send
      • preprocessReceivedData

        protected void preprocessReceivedData​(byte[] data)
        Preprocesses incomming data by sending it to the DataPreProcessor. The processed data is then forwarded to processReceivedData(byte[]);
        Parameters:
        data - to process
      • processReceivedData

        protected abstract void processReceivedData​(byte[] data)
        This function will be called once the client receives data from the server.
        Parameters:
        data - sent by the server
      • registerListener

        public void registerListener​(ClientListener listener)
        Registers a ClientListener
        Parameters:
        listener - to add
      • registerListener

        public void registerListener​(IOHandlerListener listener)
        Registers a IOHandlerListener
        Parameters:
        listener - to add
      • removeListener

        public void removeListener​(ClientListener listener)
        Removes ClientListener from the listeners
        Parameters:
        listener - to remove
      • removeListener

        public void removeListener​(IOHandlerListener listener)
        Removes IOHandlerListener from the listeners
        Parameters:
        listener - to remove
      • isConnected

        public boolean isConnected()
        Indicates whether the current client object has an active connection to the server
        Returns:
        true if there is an active connection to the server
      • isClosed

        public boolean isClosed()
      • setClosed

        public void setClosed​(boolean closed)
      • getIOHandler

        public IOHandler getIOHandler()
        Returns the IOHandler associated with the client object
        Returns:
        the IOHandler
      • getDataPreProcessor

        public DataPreProcessor getDataPreProcessor()
        Returns the DataPreProcessor associated with the client object
        Returns:
        the DataPreProcessor