Class ReactorImpl

    • Constructor Detail

      • ReactorImpl

        protected ReactorImpl​(IO io)
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • ReactorImpl

        protected ReactorImpl​(IO io,
                              ReactorOptions options)
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • ReactorImpl

        public ReactorImpl()
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • ReactorImpl

        public ReactorImpl​(ReactorOptions options)
                    throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • mark

        public long mark()
        Description copied from interface: Reactor
        Updates the last time that the reactor's state has changed, potentially resulting in events being generated.
        Specified by:
        mark in interface Reactor
        Returns:
        the current time in milliseconds System.currentTimeMillis().
      • now

        public long now()
        Specified by:
        now in interface Reactor
        Returns:
        the last time that Reactor.mark() was called.
      • free

        public void free()
        Description copied from interface: Reactor
        Frees any resources (such as sockets and selectors) held by the reactor or its children.
        Specified by:
        free in interface Reactor
      • attachments

        public Record attachments()
        Specified by:
        attachments in interface Extendable
        Specified by:
        attachments in interface Reactor
        Returns:
        an instance of Record that can be used to associate other objects (attachments) with this instance of the Reactor class.
      • getOptions

        public ReactorOptions getOptions()
        Description copied from interface: Reactor
        Gets the reactor options.
        Specified by:
        getOptions in interface Reactor
        Returns:
        the reactor options
      • setTimeout

        public void setTimeout​(long timeout)
        Description copied from interface: Reactor
        The value the reactor will use for Selector.select(long) that is called as part of Reactor.process().
        Specified by:
        setTimeout in interface Reactor
        Parameters:
        timeout - a timeout value in milliseconds, to associate with this instance of the reactor. This can be retrieved using the Reactor.getTimeout() method
      • getGlobalHandler

        public Handler getGlobalHandler()
        Specified by:
        getGlobalHandler in interface Reactor
        Returns:
        the global handler for this reactor. Every event the reactor sees is dispatched to the global handler. To receive every event generated by the reactor, associate a child handler with the global handler. For example:
                    getGlobalHandler().add(yourHandler);
                 
      • setGlobalHandler

        public void setGlobalHandler​(Handler handler)
        Description copied from interface: Reactor
        Sets a new global handler. You probably don't want to do this and would be better adding a handler to the value returned by the {Reactor.getGlobalHandler() method.
        Specified by:
        setGlobalHandler in interface Reactor
        Parameters:
        handler - the new global handler.
      • getHandler

        public Handler getHandler()
        Specified by:
        getHandler in interface Reactor
        Returns:
        the handler for this reactor. Every event the reactor sees, which is not handled by a child of the reactor (such as a timer, connection, acceptor, or selector) is passed to this handler. To receive these events, it is recommend that you associate a child handler with the handler returned by this method. For example:
                   getHandler().add(yourHandler);
                 
      • setHandler

        public void setHandler​(Handler handler)
        Description copied from interface: Reactor
        Sets a new handler, that will receive any events not handled by a child of the reactor. Note that setting a handler via this method replaces the previous handler, and will result in no further events being dispatched to the child handlers associated with the previous handler. For this reason it is recommended that you do not use this method and instead add child handlers to the value returned by the Reactor.getHandler() method.
        Specified by:
        setHandler in interface Reactor
        Parameters:
        handler - the new handler for this reactor.
      • collector

        public Collector collector()
        Specified by:
        collector in interface Reactor
        Returns:
        the Collector used to gather events generated by this reactor.
      • selectable

        public Selectable selectable()
        Description copied from interface: Reactor
        Creates a new Selectable as a child of this reactor.
        Specified by:
        selectable in interface Reactor
        Returns:
        the newly created Selectable.
      • yield

        public void yield()
        Description copied from interface: Reactor
        Yields, causing the next call to Reactor.process() to return successfully - without processing any events. If multiple calls can be made to yield and only the next invocation of Reactor.process() will be affected.
        Specified by:
        yield in interface Reactor
      • quiesced

        public boolean quiesced()
        Specified by:
        quiesced in interface Reactor
        Returns:
        true if the reactor is in quiesced state (e.g. has no events to process). false is returned otherwise.
      • process

        public boolean process()
                        throws HandlerException
        Description copied from interface: Reactor
        Process any events pending for this reactor. Events are dispatched to the handlers registered with the reactor, or child objects associated with the reactor. This method blocks until the reactor has no more work to do (and no more work pending, in terms of scheduled tasks or open selectors to process).
        Specified by:
        process in interface Reactor
        Returns:
        true if the reactor may have more events in the future. For example: if there are scheduled tasks, or open selectors. false is returned if the reactor has (and will have) no more events to process.
        Throws:
        HandlerException - if an unchecked exception is thrown by one of the handlers - it will be re-thrown attached to an instance of HandlerException.
      • wakeup

        public void wakeup()
        Description copied from interface: Reactor
        Wakes up the thread (if any) blocked in the Reactor.process() method. This is the only method of this class that is thread safe, in that it can be used at the same time as another thread is using the reactor.
        Specified by:
        wakeup in interface Reactor
      • start

        public void start()
        Description copied from interface: Reactor
        Starts the reactor. This method should be invoked before the first call to Reactor.process().
        Specified by:
        start in interface Reactor
      • run

        public void run()
                 throws HandlerException
        Description copied from interface: Reactor
        Simplifies the use of the reactor by wrapping the use of start, run, and stop method calls.

        Logically the implementation of this method is:

           start();
           while(process()) {}
           stop();
         
        Specified by:
        run in interface Reactor
        Throws:
        HandlerException - if an unchecked exception is thrown by one of the handlers - it will be re-thrown attached to an instance of HandlerException.
      • schedule

        public Task schedule​(int delay,
                             Handler handler)
        Description copied from interface: Reactor
        Schedules execution of a task to take place at some point in the future.
        Specified by:
        schedule in interface Reactor
        Parameters:
        delay - the number of milliseconds, in the future, to schedule the task for.
        handler - a handler to associate with the task. This is notified when the deadline for the task is reached.
        Returns:
        an object representing the task that has been scheduled.
      • getSelector

        protected Selector getSelector()
      • setSelector

        protected void setSelector​(Selector selector)
      • connectionToHost

        public Connection connectionToHost​(java.lang.String host,
                                           int port,
                                           Handler handler)
        Description copied from interface: Reactor
        Creates a new out-bound connection to the given host and port.

        This method will cause Reactor to set up a network connection to the host and create a Connection for it.

        Specified by:
        connectionToHost in interface Reactor
        Parameters:
        host - the host to connect to (e.g. "localhost")
        port - the port used for the connection.
        handler - a handler that is notified when events occur for the connection.
        Returns:
        the newly created connection object.
      • getConnectionAddress

        public java.lang.String getConnectionAddress​(Connection connection)
        Description copied from interface: Reactor
        Get the address used by the connection

        This may be used to retrieve the remote peer address. Note that the returned address may be in numeric IP format.

        Specified by:
        getConnectionAddress in interface Reactor
        Parameters:
        connection - the Connection
        Returns:
        a string containing the address in the following format:
           host[:port]
         
      • setConnectionHost

        public void setConnectionHost​(Connection connection,
                                      java.lang.String host,
                                      int port)
        Description copied from interface: Reactor
        Set the host address used by the connection

        This method will set/change the host address used by the Reactor to create an outbound network connection for the given Connection

        Specified by:
        setConnectionHost in interface Reactor
        Parameters:
        connection - the Connection to assign the address to
        host - the address of the host to connect to (e.g. "localhost")
        port - the port to use for the connection.
      • acceptor

        public Acceptor acceptor​(java.lang.String host,
                                 int port)
                          throws java.io.IOException
        Description copied from interface: Reactor
        Creates a new acceptor. This is equivalent to calling:
           acceptor(host, port, null);
         
        Specified by:
        acceptor in interface Reactor
        Returns:
        the newly created acceptor object.
        Throws:
        java.io.IOException
      • acceptor

        public Acceptor acceptor​(java.lang.String host,
                                 int port,
                                 Handler handler)
                          throws java.io.IOException
        Description copied from interface: Reactor
        Creates a new acceptor. This acceptor listens for in-bound connections.
        Specified by:
        acceptor in interface Reactor
        Parameters:
        host - the host name or address of the NIC to listen on.
        port - the port number to listen on.
        handler - if non-null this handler is registered with each new connection accepted by the acceptor.
        Returns:
        the newly created acceptor object.
        Throws:
        java.io.IOException
      • getIO

        public IO getIO()