Example

Consider a simple logging server application. Its sole purpose is to accept network connections from clients using a server socket, read lines of text from them and write the text to a log file. Such a server can be implemented using a Reactor and two types of event handler with corresponding handles: logging acceptors (which accept new connections) and logging handlers (which accept new lines of text). Execution proceeds like this:

  1. The application creates a server socket, its corresponding logging acceptor and a dispatcher (which in turn sets up a demux).
  2. The application registers the acceptor with the dispatcher.
  3. The application calls handleEvents on the dispatcher, which starts looping through the following:
    1. The dispatcher calls select on the demux, which blocks until an event is received (the first event must be a new connection event on the acceptor; the following may be text events).
    2. select returns, notifying the dispatcher about the message(s) received.
    3. The dispatcher calls handleEvent on the event handler(s) with the received event(s). If the event was from the server socket, the acceptor is called and it sets up a new logging handler and registers it with the dispatcher. If the event was from a connection to a client, it writes the text to the log file if text was sent. In case of a connection being broken, the logging handler deregisters itself.

Jan Lönnberg 2006-10-23