event and signal driven programming zendcon 2012
TRANSCRIPT
![Page 1: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/1.jpg)
Event and Signal Driven ProgrammingThe wheel already exists…
![Page 3: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/3.jpg)
Computer Science 201Event based programming …. ??? …. PROFIT!
![Page 4: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/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 Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/5.jpg)
Event Driven Programming
Wait for Events
Accept Event
Dispatch Event
Handlers are Called
![Page 6: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/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 Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/7.jpg)
Asyncronous vs. Synchronous event-driven !== asynchronous structured !== synchronous
concurrency through Async I/O (epoll, IOCP, kqueue, et al) threads forking (multi-process)
![Page 8: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/8.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 9: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/9.jpg)
Design PatternsThe Evolution of Event based programming
![Page 10: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/10.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 11: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/11.jpg)
Publisher Broker
Subscriber
Subscriber
Subscriber
![Page 12: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/12.jpg)
(Main) (event/message) loop poll blocks until something arrives dispatches main is added if it is the highest level of
control in a program
![Page 13: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/13.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 14: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/14.jpg)
Sub
jectObserver
1Observer
2Observer
3
![Page 15: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/15.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 16: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/16.jpg)
Object
Send Event
Send Event2
Dispatcher
Handler1(Event1)
Handler2(Event1)
Handler3(Event1)
Handler4(Event 2)
![Page 17: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/17.jpg)
Dispatcher gets event determines which handlers get called
either attached determined by type stored in some manner
calls the handlers
![Page 18: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/18.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 19: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/19.jpg)
![Page 20: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/20.jpg)
Reactor Resources Synchronous Event Demultiplexer Dispatcher Request Handler
![Page 21: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/21.jpg)
Reactor
Various Handlers
Dispatcher
Demultiplexer
Service Request
Service Request
Service Request
![Page 22: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/22.jpg)
Copy the GiantsUse examples of existing event driven code
https://github.com/auroraeosrose/event-and-signal-exercises
![Page 23: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/23.jpg)
Windows APIMessage Pump of Doom
![Page 24: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/24.jpg)
On to the Code Concepts
Main “Loop” for processing Message Queue holding messages Windows (objects) post to queue Queue passes messages around Each window has a winproc to handle
messages sent to it (switch of doom)
![Page 25: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/25.jpg)
GobjectEvent Handler madness
![Page 26: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/26.jpg)
On to the Code Concepts
Dispatcher Register event by name Attach a handler Emit event
![Page 27: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/27.jpg)
QTFor those who like a little ++ with their C
![Page 28: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/28.jpg)
On to the Code 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
slot – the actual implemented function for an event or a callback attached to a signal
![Page 29: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/29.jpg)
Best PracticesWhat to do in your own code
![Page 30: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/30.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 31: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/31.jpg)
Standard interfaces Observer? Use SplSubject and
SplObserver Pub/Sub? Event/Handler? Signal/Slot?
![Page 32: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/32.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 33: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/33.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 34: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/34.jpg)
The Wild Wild WestExperiments are coming
![Page 35: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/35.jpg)
Eventing Libraries https://
github.com/christopherobin/EventEmitter
https://github.com/Wordi/LooPHP/tree/master/LooPHP
https://github.com/igorw/evenement https://github.com/kirbysayshi/
SignalSlot
![Page 36: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/36.jpg)
Framework’s New Thing ZF2 – Event Manager Symfony 2 – Event Manager Kohana Events Aura (Aura.signal) CodeIgnitor events I could go on….
![Page 37: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/37.jpg)
Extensions galore Php-gtk (gobject)
http://gtk.php.net libevent
http://pecl.php.net/package/libevent eio
http://pecl.php.net/package/eio libev
https://github.com/m4rw3r/php-libev
![Page 38: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/38.jpg)
node.php https://
github.com/JosephMoniz/node.php PHP extension using libuv (joyent based
platform abstraction eventing layer ) and http parser used for node
So essentially it IS node in a php extension
![Page 39: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/39.jpg)
![Page 40: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/40.jpg)
react-php http://nodephp.org/ Originally called nodephp (to great
screams), now is called react-php Is actually passing off to another server Written in php
![Page 41: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/41.jpg)
![Page 42: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/42.jpg)
Bring it on Good Libraries Blog posts Articles Extensions
![Page 43: Event and Signal Driven Programming Zendcon 2012](https://reader034.vdocuments.site/reader034/viewer/2022051400/554bcbd0b4c905ac708b45a8/html5/thumbnails/43.jpg)
Contact Me http://emsmith.net http://joind.in/7027 [email protected] IRC – freenode – auroraeosrose #coapp #phpmentoring and others