Untitled

Alternative Model for Structuring Server Applications

Event Driven Model

  • Definition: An event driven application is characterized by the following elements:

    • Implemented in a single address space.

    • Operates with a single process and a single thread of control.

Structure of Event Driven Model

  • Event Dispatcher:

    • The central component of the process.

    • Continuously operates in a loop to:

    • Look for incoming events.

    • Based on the events, invoke one or more registered handlers.

Types of Events

  • Events correspond to various triggers in the system, including:

    • Receipt of a request from client browsers.

    • Message received from the network (considered an event).

    • Completion of a send operation (the send completion is treated as an event).

    • Completion of a discrete operation, which denotes other important occurrences.

  • The dispatcher processes these notifications, allowing it to invoke the appropriate handler.

Functioning of the Dispatcher

  • The dispatcher operates similarly to a state machine due to its handling of event notifications.

  • Handler Invocation:

    • Invoking a handler involves jumping to the corresponding location in the process’s address space where the handler is implemented.

    • This allows execution of the handler to commence immediately once an event is detected.

Example of Event Handling
  1. Pending Connection Request:

    • If notified of a pending connection on the network port:

      • The dispatcher passes the event to the accept connection handler.

  2. Incoming Data Message:

    • If the event is the receipt of data from an existing connection:

      • The dispatcher passes this to the read request handler.

    • Once a file name is extracted from the request and verified:

      • Chunks of the file are sent in response.

    • After confirmation that a chunk has been successfully sent:

      • The process continues to invoke the handler managing the send operation.

    • If the file is not present:

      • An error message is generated and sent back to the client.

Handlers and Execution

  • Handlers are sequences of code that execute upon the occurrence of events and are characterized by:

    • Running to completion: Once a handler starts executing, it will process until completion.

    • Blocking Operations: If a handler requires a blocking operation:

    • It initiates the operation and then passes control back to the event dispatcher.

    • Control is returned so that the dispatcher can manage other events or invoke other handlers, thereby maintaining system efficiency.