Module 06A Event Driven Programming - Event Driven Architecture (1)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Module 06 Event driven

programming
BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

Event driven architecture


Window-based programming
• Most modern desktop systems are window-based.

What location do I use to set this pixel?

Window based environment


Non-window based environment

CSIS Dept. BITS Pilani, Pilani Campus


Event-based Programming
• Window-based GUI’s are typically comprised of many
independent processes.
• These processes sit and wait for the user to do something,
to which it can respond.
• Moreover, a simple application may be waiting for any of a
number of things.

CSIS Dept. BITS Pilani, Pilani Campus


Programming forms
statement 1
 Procedural programming statement 2
statement 3
 code is executed in sequential order. --------
--------
statement n

 Event-driven programming
 code is executed upon activation of events.

method 1
method 2 Do
Do method 3 method 2
then
method 1
then
-------- method 1
method 3 -------- then
method 3
method n

CSIS Dept. BITS Pilani, Pilani Campus


Procedural programming
• Up to now, our control flow model has been pretty straight-
forward.
• Execution starts at main() and executes statement
sequentially branching with if,for,and while statement, and
occasional method calls.
• When we need user input we call read() on the console
stream which waits (blocks) until the user types something,
then returns.
• One problem with this model is: How do we wait for and
respond to input from more than one source (eg keyboard
and mouse). If we block on either waiting for input, we may
miss input from the other.

CSIS Dept. BITS Pilani, Pilani Campus


Event-driven programming
• The system waits for user input events, and these events
trigger program methods
• Event-based programming addresses the two problems:
o How to wait on multiple input sources (input events)
o How to decide what to do on each type of input event

CSIS Dept. BITS Pilani, Pilani Campus


General form of event-driven programming

• A data structure describing the event is produced


and placed on an event queue. Events are added
to one end of the queue.
• Events are removed from the queue and
processed by the program. These event data
structures hold information including:
o Which of the program's windows this event belongs to.
o The type of event (mouse click, key press, etc.)
o Any event-specific data (mouse position, key code, etc.)

CSIS Dept. BITS Pilani, Pilani Campus


Event Driven Architectures
• Event-driven architecture (EDA) is a software design
pattern that emphasizes the production, detection,
consumption, and reaction to events
o EDA promotes loose coupling between components, enabling them
to interact asynchronously through events. Events are messages
that encapsulate information about occurrences within the system.

CSIS Dept. BITS Pilani, Pilani Campus


Core Concepts of EDA
• Events
o The fundamental building blocks of EDA, representing occurrences
that trigger actions.
• Producers
o Components that generate events and publish them to the system.
• Channels
o Mechanisms for conveying events from producers to consumers.
• Consumers
o Components that subscribe to channels and react to incoming
events.
• EDA revolves around events, which serve as the catalyst for system
interactions. Producers generate events, channels facilitate their
transmission, and consumers process them to trigger appropriate
responses

CSIS Dept. BITS Pilani, Pilani Campus


Applications of EDA
• Scalable Server design
o Nginx, Node.js
• GUI Programming
• Real-time data processing
o financial trading, fraud detection, and sensor monitoring
• Microservices communication
• IoT applications
o real-time data collection, analysis, and decision making.
• Workflow orchestration
o triggers and manages workflows, automating tasks and streamlining
business processes.

CSIS Dept. BITS Pilani, Pilani Campus


BITS Pilani
Pilani Campus

Event-driven approach for server


design
Event Driven Webserver Architectures

• A single threaded event loop consumes event after event


from the queue and sequentially executes associated event
handler code.
• New events are emitted by external sources such as socket
or •
file I/O notifications.
• Event handlers trigger I/O actions that eventually result in
new events later.

CSIS Dept. BITS Pilani, Pilani Campus


Mapping EDA to Web server
• Events
o Data availability on different file descriptors
• Producers
o Clients through operating system (OS)
• Channels
o Message queue
• Consumers
o Server threads

CSIS Dept. BITS Pilani, Pilani Campus


Mapping EDA to Web server
• How to wait on multiple input sources (input events)?

CSIS Dept. BITS Pilani, Pilani Campus


IO Models
• While doing I/O there are two phases
o Waiting for the data
o Copying the data
• Each I/O model differs how it deals with these two phases.
• There are five I/O models
o blocking I/O
o nonblocking I/O
o I/O multiplexing (select and poll)
o signal driven I/O (SIGIO)
o asynchronous I/O (the POSIX aio_functions)

CSIS Dept. BITS Pilani, Pilani Campus


Blocking I/O Model
• Most prevalent model

CSIS Dept. BITS Pilani, Pilani Campus


Nonblocking I/O Model
• When the socket is set to be non-blocking,
o We tell the kernel that do not put the process to sleep if IO can’t be
completed.

CSIS Dept. BITS Pilani, Pilani Campus


I/O Multiplexing Model
• Block in select() or poll() instead of blocking in actual I/O
system call.

CSIS Dept. BITS Pilani, Pilani Campus


I/O Multiplexing Model
• Blocking and I/O multiplexing seem to be non-different and
actually calling two sys calls in I/O multiplexing.
• Advantage with I/O multiplexing is that it can wait for I/O on
multiple fds.

CSIS Dept. BITS Pilani, Pilani Campus


Signal-Driven I/O Model
• Tell the kernel to notify us with the SIGIO signal when the
descriptor is ready.

CSIS Dept. BITS Pilani, Pilani Campus


Signal-Driven I/O Model
• To use signal-driven I/O with a socket (SIGIO) requires the
process to perform the following three steps:
o A signal handler must be established for the SIGIO signal.
o The socket owner must be set, normally with the F_SETOWN
command of fcntl.
o Signal-driven I/O must be enabled for the socket, normally with the
F_SETFL command of fcntl to turn on the O_ASYNC flag.

CSIS Dept. BITS Pilani, Pilani Campus


Asynchronous I/O Model
• The main difference between this model and the signal-
driven I/O models that
o with signal-driven I/O, the kernel tells us when an I/O operation can
be initiated,
o but with asynchronous I/O, the kernel tells us when an I/O operation
is complete.

CSIS Dept. BITS Pilani, Pilani Campus


Asynchronous I/O Model
• POSIX API for asynchronous IO is implemented in a very
few systems.
• aiocb structure

CSIS Dept. BITS Pilani, Pilani Campus


Comparison of I/O Models
• First four models differ only in first phase.

CSIS Dept. BITS Pilani, Pilani Campus


Using threads vs using EDA
Source: SEDA paper (Welsh et al., SOSP 2001)

• No throughput degradation under load


• Peak throughput is higher

CSIS Dept. BITS Pilani, Pilani Campus


I/O Multiplexing
• Commonly used I/O multiplexing functions:
• select: The select function is a POSIX function that allows a
single thread to monitor multiple file descriptors for I/O
events.
• poll: The poll function is another POSIX function that is
similar to select, but it is more efficient for monitoring a set
of file descriptors sparsely distributed.
• epoll: The epoll function is a Linux-specific function that is
even more efficient than select or poll. It is the preferred I/O
multiplexing function for most Linux applications.

CSIS Dept. BITS Pilani, Pilani Campus


Node.js
• Node.js is an open-source, cross-platform JavaScript
runtime environment that executes JavaScript code outside
of a web browser
o It is built on Chrome's V8 JavaScript engine and uses an event-
driven, non-blocking I/O model that makes it lightweight and efficient
o Node.js is commonly used for developing server-side applications,
but it can also be used for developing desktop applications,
command-line tools, and network applications.

CSIS Dept. BITS Pilani, Pilani Campus


Common Use Cases for Node.js
• Web applications
o web applications, particularly real-time applications and APIs.
• Microservices
o well-suited for developing microservices, which are small,
independent services that communicate with each other through
APIs.
• Command-line tools
o used to develop command-line tools and scripts for automating
tasks and managing system resources.
• Network applications
o Node.js can be used to develop network applications, such as chat
servers, games, and network monitoring tools.

CSIS Dept. BITS Pilani, Pilani Campus


A Server Code in Node.js

• This code defines an HTTP server that listens on port 3000 and responds with
the request method and URL of any incoming request. This is the basic
implementation of the echo protocol, which simply reflects back the information
received from the client.
• To run this code, save it as a file named echo_server.js and then run the
following command in your terminal: node echo_server.js

CSIS Dept. BITS Pilani, Pilani Campus


References

CSIS Dept. BITS Pilani, Pilani Campus


BITS Pilani
Pilani Campus

Thank You

You might also like