Class Service


  • public class Service
    extends java.lang.Object
    This is a wrapper for a server socket. It has an arbitrary number of registered ServiceHandler-s. Whenever a client connects to the service, all registered handlers are notified. Each handler can further communucate with the client.
    Author:
    christian
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  Service.ConnectionHandlerRunnable
      A simple class that enables notification of ServiceHandler-s in a new thread.
    • Constructor Summary

      Constructors 
      Constructor Description
      Service​(int port)
      The only constructor, which simply sets a port to be used by the server socket.
    • Field Detail

      • serverSocket

        protected java.net.ServerSocket serverSocket
    • Constructor Detail

      • Service

        public Service​(int port)
        The only constructor, which simply sets a port to be used by the server socket. Note that the constructor does not open the server socket.
        Parameters:
        port - for the server socket.
    • Method Detail

      • addServiceHandler

        public void addServiceHandler​(ServiceHandler handler)
        Registers a new handler. From this moment, the handler will be notified about new client connections.
        Parameters:
        handler - to be registered.
      • removeServiceHandler

        public void removeServiceHandler​(ServiceHandler handler)
        Unregister the handler. From this moment, the handler will not longer be notified about new client connections.
        Parameters:
        handler - to be unregistered.
      • start

        public void start​(ServiceEnvironment environment,
                          java.util.concurrent.Executor executor)
                   throws java.io.IOException
        Opens the server socked on the given port. Periodically notifies the environment that the service is alive. Notifies all registered handlers about every new client connection. It keeps on listening on the socket until the environment is canceled, upon which the socket is closed.
        Parameters:
        environment - is the environment of this service.
        executor - enables notification of handlers in a new thread.
        Throws:
        java.io.IOException
      • handleConnection

        protected void handleConnection​(java.net.Socket socket,
                                        ServiceEnvironment environment,
                                        java.util.concurrent.Executor executor)
        Notifies all registered handlers about every new client connection.
        Parameters:
        socket - is the client socket.
        environment - is the environment of this service.
        executor - enables notification of handlers in a new thread.
      • getPort

        public int getPort()
        Get the server port.
        Returns:
        the port.