event and signal driven programming techniques
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 communityTRANSCRIPT
![Page 1: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/1.jpg)
EVENT AND SIGNAL DRIVEN PROGRAMMING TECHNIQUES
The wheel already exists…
![Page 2: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/2.jpg)
FEEDBACK IS PRECIOUS
CS LessonLectureRantTutorialTalk
![Page 3: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/3.jpg)
Patterns and Concepts – oh my
IN THE BEGINNING – CS 201
![Page 4: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/5.jpg)
EVENT DRIVEN PROGRAMMING
Wait for Events
Accept Event
Dispatch Event
Handlers are
Called
![Page 6: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/6.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/7.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/8.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/10.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/11.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/12.jpg)
(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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/14.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/15.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/16.jpg)
REACTOR
Synchronous Request Handlers
Demultiplexer and Dispatcher
Service Request
![Page 17: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/17.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/18.jpg)
The Wheels keep on Turning
WE’VE BEEN DOING THIS ALL ALONG
![Page 19: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/19.jpg)
KERNEL SIGNALS“Get off my Lawn!”
![Page 20: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/20.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/21.jpg)
XLIB SIGNALS1985 called and they want their shoes.
![Page 22: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/22.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/23.jpg)
GOBJECT (FORMERLY GTK+)Gimp wanted a nice toolkit and had a gnome
![Page 24: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/24.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/25.jpg)
FEATURES
Register Signal Connect Signal to Closure Emit Signal User Data Accumulators Details
![Page 26: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/26.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/27.jpg)
QTFor those who like a little ++ with their C
![Page 28: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/28.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/29.jpg)
Click icon to add picture
WIN APIPump that message baby
![Page 30: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/30.jpg)
![Page 31: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/31.jpg)
Click icon to add picture
.NETThe windows api, memory managed and less sucky
![Page 32: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/32.jpg)
![Page 33: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/33.jpg)
Click icon to add picture
LIBEVENT (NODE.JS, TWISTED, EVENTMACHINE…)It’s all in the family!
![Page 34: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/34.jpg)
![Page 35: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/35.jpg)
Deep Thoughts on Good Code
BEST PRACTICES
![Page 36: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/36.jpg)
THE LEGACY OF HTTP
![Page 37: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/37.jpg)
WHY EVENTS OR SIGNALS?
![Page 38: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/38.jpg)
IMPORTANCE OF CLOSURES AND TRAITS
![Page 39: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/39.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/40.jpg)
STANDARD INTERFACES
Observer? Use SplSubject and SplObserver
Pub/Sub? Event/Handler? Signal/Slot?
![Page 41: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/41.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/42.jpg)
Event Driven Code in Action
IN THE WILD
![Page 43: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/43.jpg)
ZF2
EventManager (aggregator, trigger events )
Listener (callback) Event (action modeled as object)
![Page 44: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/44.jpg)
SYMFONY COMPONENTS – EVENT DISPATCHER
Register of listeners Event object notify, notifyUntil, filter
![Page 45: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/45.jpg)
ZETA COMPONENTS
Component – SignalSlot signal as event, slot as callable priorities static connection (hook)
![Page 46: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/46.jpg)
ALMOST BUT NOT QUITE
Lithium Prado Wordpress Symfony 1
![Page 47: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/47.jpg)
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 48: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/48.jpg)
RESOURCES
http://components.symfony-project.org/event-dispatcher/
http://incubator.apache.org/zetacomponents/documentation/trunk/SignalSlot/tutorial.html
http://weierophinney.net/matthew/archives/266-Using-the-ZF2-EventManager.html
http://c2.com/cgi/wiki?EventDrivenProgramming http://eventdrivenpgm.sourceforge.net/ http://www.gtk.org/ http://qt.nokia.com
![Page 49: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/49.jpg)
BRING IT ON
Good Libraries Blog posts Articles Extensions
![Page 50: Event and signal driven programming techniques](https://reader035.vdocument.in/reader035/viewer/2022062319/554bcb8cb4c9058f6c8b46db/html5/thumbnails/50.jpg)
CONTACT ME
http://emsmith.net http://joind.in/3756 [email protected] IRC – freenode – auroraeosrose #php-gtk #coapp and others