Class Server

    • Constructor Detail

      • Server

        public Server​(int port)
        Creates a server object and sets the port the server will try to listen on
        Parameters:
        port - to listen on
    • Method Detail

      • start

        public void start()
        Opens a ServerSocket, initializes the DataProcessingLayers and starts listening on the specified port
      • openServerSocket

        protected void openServerSocket()
                                 throws java.io.IOException
        Opens the ServerSocket. Moved to a different function to make it easier to replace the ServerSocket implementation.
        Throws:
        java.io.IOException - when the process of opening the ServerSocket fails.
      • close

        public void close()
        Initiates the closing process of the Server with closing the ClientManager and disabling the DataProcessingLayers. Finally, it closes the serverSocket
      • processReceivedData

        protected abstract void processReceivedData​(java.lang.String clientAddress,
                                                    byte[] data)
        This function will be called once the server receives data from the client.
        Parameters:
        clientAddress - of the client the data came from
        data - sent by the server
      • send

        public boolean send​(java.lang.String clientAddress,
                            byte[] data)
      • broadcast

        public void broadcast​(byte[] data)
        Sends the data to all connected clients.
        Parameters:
        data - to broadcast
      • registerListener

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

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

        public void removeListener​(ServerListener 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
      • notifyListeners

        protected void notifyListeners​(dev.bitbite.networking.Server.EventType type,
                                       java.lang.Object... args)
        Calls the respective function of each listener depending on the event type.
        Optionally Propagates additional info such as exceptions.
        Parameters:
        type - of event that occured
        args - optional additional data
        Throws:
        java.lang.IllegalArgumentException - if additional arguments are supplied whose types do not match the expected types of the listeners eventfunction
        See Also:
        ServerListener