50
EVENT AND SIGNAL DRIVEN PROGRAMMING TECHNIQUES The wheel already exists…

Event and signal driven programming techniques

Embed Size (px)

DESCRIPTION

Given at Zendcon 2011 this is a computer theory heavy, code absent look at event driven programming design patterns and techniques in the programming community

Citation preview

Page 1: Event and signal driven programming techniques

EVENT AND SIGNAL DRIVEN PROGRAMMING TECHNIQUES

The wheel already exists…

Page 2: Event and signal driven programming techniques

FEEDBACK IS PRECIOUS

CS LessonLectureRantTutorialTalk

Page 3: Event and signal driven programming techniques

Patterns and Concepts – oh my

IN THE BEGINNING – CS 201

Page 4: Event and signal driven programming techniques

STRUCTURED PROGRAMMING

simple, hierarchical program flow structures sequence selection repetition

procedural adds sub-routines or functions

object oriented modularizes components of code

Page 5: Event and signal driven programming techniques

EVENT DRIVEN PROGRAMMING

Wait for Events

Accept Event

Dispatch Event

Handlers are

Called

Page 6: Event and signal driven programming techniques

TRANSITIONING TO EVENTS

code flow is controlled by the user, not the program

software can sit “idle” until an event occurs

allows software to react why?

Page 7: Event and signal driven programming techniques

PUBLISH/SUBSCRIBE

type of event programming this is NOT observer publisher and subscriber are decoupled subscribers express interest in a state

change/event/message/signal each subscriber receives a copy

Page 8: Event and signal driven programming techniques

SUBJECT/OBSERVER

this is a subset of publish/subscribe NOT the other way around

in this case, the subject maintains the observers and notifies them of state change/event/message/signal

more tightly coupled then publish/subscribe

Page 9: Event and signal driven programming techniques

EVENT/HANDLER

events are spawned by the system events are dispatched to handlers

which are attached to them handler can go by many names –

callback, closure, function events might have priority, might

interrupt depending on the dispatch method used

Page 10: Event and signal driven programming techniques

SIGNAL/SLOT

signal – something you know is going to happen that is attached to a class

slot – method in a class that can be connected to a signal

connect a signal to a slot (wire it up) emit the signal requires oo

Page 11: Event and signal driven programming techniques

ASYNCRONOUS VS. SYNCHRONOUS

event-driven !== asynchronous structured !== synchronous

concurrency through Async I/O (epoll, IOCP, kqueue, et al) threads forking (multi-process)

Page 12: Event and signal driven programming techniques

(MAIN) EVENT LOOP

poll event provider blocks until event arrives calls the event handler (dispatches) main is added if it is the highest level

of control in a program

Page 13: Event and signal driven programming techniques

DISPATCHER

gets event determines which handlers get called

either attached determined by type stored in some manner

calls the handlers

Page 14: Event and signal driven programming techniques

MESSAGE QUEUE

Buffers the input stream of events FIFO usually, unless priority or other

features are present in the queue while(events_pending)

Page 15: Event and signal driven programming techniques

EVENT DRIVEN STATE MACHINE

A number of behavioral nodes waiting for a trigger to execute the transition

The trigger in this case is an event

Page 16: Event and signal driven programming techniques

REACTOR

Synchronous Request Handlers

Demultiplexer and Dispatcher

Service Request

Page 17: Event and signal driven programming techniques

INTERRUPTS

asynchronous signal/event indicating need for change in execution or need for attention

“The ship is going to explode” A signal is a software interrupt

delivered to a process

Page 18: Event and signal driven programming techniques

The Wheels keep on Turning

WE’VE BEEN DOING THIS ALL ALONG

Page 19: Event and signal driven programming techniques

KERNEL SIGNALS“Get off my Lawn!”

Page 20: Event and signal driven programming techniques

USING IT WITH PHP

PCNTL very simplistic most signals (other then SIGUSR1 and

SIGUSR2) have predefined meanings

Page 21: Event and signal driven programming techniques

XLIB SIGNALS1985 called and they want their shoes.

Page 22: Event and signal driven programming techniques

XLIB AND XNEXTEVENT

blocks until event appears in the queue application process it only handles window system events calling xlib functions from a signal

handler “POOF”

Page 23: Event and signal driven programming techniques

GOBJECT (FORMERLY GTK+)Gimp wanted a nice toolkit and had a gnome

Page 24: Event and signal driven programming techniques

GOBJECT IN PHP

PHP-GTK PHP-GTK (next)

Extend gobject class define your signals attach to your signals emit your signals

Page 25: Event and signal driven programming techniques

FEATURES

Register Signal Connect Signal to Closure Emit Signal User Data Accumulators Details

Page 26: Event and signal driven programming techniques

GOBJECT EVENT LOOP

Manages all sources of events Event type can be added Gmaincontext for each thread to

manage sources Priorities for event sources and idle

functions

Page 27: Event and signal driven programming techniques

QTFor those who like a little ++ with their C

Page 28: Event and signal driven programming techniques

QT – “EVENTS” AND “SIGNAL/SLOT”

event – virtual function in a class you reimplement to handle the event

signal – wired to a callback (slot) by a metaobject class, no loop necessarily required

Page 29: Event and signal driven programming techniques

Click icon to add picture

WIN APIPump that message baby

Page 30: Event and signal driven programming techniques
Page 31: Event and signal driven programming techniques

Click icon to add picture

.NETThe windows api, memory managed and less sucky

Page 32: Event and signal driven programming techniques
Page 33: Event and signal driven programming techniques

Click icon to add picture

LIBEVENT (NODE.JS, TWISTED, EVENTMACHINE…)It’s all in the family!

Page 34: Event and signal driven programming techniques
Page 35: Event and signal driven programming techniques

Deep Thoughts on Good Code

BEST PRACTICES

Page 36: Event and signal driven programming techniques

THE LEGACY OF HTTP

Page 37: Event and signal driven programming techniques

WHY EVENTS OR SIGNALS?

Page 38: Event and signal driven programming techniques

IMPORTANCE OF CLOSURES AND TRAITS

Page 39: Event and signal driven programming techniques

NAME IT PROPERLY

Publish / Subscribe Decoupled

Subject / Observer stateless

Event / Handler event loop

Signal / Slot Tightly coupled

objects Traits

Dispatcher (Main) Event Loop State Machine Message Pump Reactor

Page 40: Event and signal driven programming techniques

STANDARD INTERFACES

Observer? Use SplSubject and SplObserver

Pub/Sub? Event/Handler? Signal/Slot?

Page 41: Event and signal driven programming techniques

BORROW FROM OTHER LANGUAGES

Any C based language translates REALLY well to PHP

If you can’t depend on an extension, but want to “drop it in” – do the same API in a different namespace and class_alias as appropriate

Upgrading from stone wheels to vulcanized rubber is great, but don’t reinvent

Page 42: Event and signal driven programming techniques

Event Driven Code in Action

IN THE WILD

Page 43: Event and signal driven programming techniques

ZF2

EventManager (aggregator, trigger events )

Listener (callback) Event (action modeled as object)

Page 44: Event and signal driven programming techniques

SYMFONY COMPONENTS – EVENT DISPATCHER

Register of listeners Event object notify, notifyUntil, filter

Page 45: Event and signal driven programming techniques

ZETA COMPONENTS

Component – SignalSlot signal as event, slot as callable priorities static connection (hook)

Page 46: Event and signal driven programming techniques

ALMOST BUT NOT QUITE

Lithium Prado Wordpress Symfony 1

Page 47: Event and signal driven programming techniques

THE FUTURE?

Best new hope for clean, easy eventing implementations? Traits

Some good wrappers around existing C libraries (libevent, dbus, glib/gobject, qt, winapi, .NET)

Pulling in good ideas from other languages, PHP evolves

Page 49: Event and signal driven programming techniques

BRING IT ON

Good Libraries Blog posts Articles Extensions

Page 50: Event and signal driven programming techniques

CONTACT ME

http://emsmith.net http://joind.in/3756 [email protected] IRC – freenode – auroraeosrose #php-gtk #coapp and others