event and signal driven programming

44
Event and Signal Driven Programming The wheel already exists…

Upload: elizabeth-smith

Post on 08-May-2015

7.571 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Event and signal driven programming

Event and Signal Driven ProgrammingThe wheel already exists…

Page 2: Event and signal driven programming

Feedback is preciousNo real code was written or harmed for this talk

Page 3: Event and signal driven programming

Computer Science 201Event based programming …. ??? …. PROFIT!

Page 4: Event and signal driven programming

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

Event Driven Programming

Wait for Events

Accept Event

Dispatch Event

Handlers are Called

Page 6: Event and signal driven programming

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

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

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

Design PatternsThe Evolution of Event based programming

Page 10: Event and signal driven programming

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

Publisher Broker

Subscriber

Subscriber

Subscriber

Page 12: Event and signal driven programming

(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

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

Sub

jectObserver

1Observer

2Observer

3

Page 15: Event and signal driven programming

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

Object

Send Event

Send Event2

Dispatcher

Handler1(Event1)

Handler2(Event1)

Handler3(Event1)

Handler4(Event 2)

Page 17: Event and signal driven programming

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

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
Page 20: Event and signal driven programming

Reactor Resources Synchronous Event Demultiplexer Dispatcher Request Handler

Page 21: Event and signal driven programming

Reactor

Various Handlers

Dispatcher

Demultiplexer

Service Request

Service Request

Service Request

Page 22: Event and signal driven programming

Copy the GiantsUse examples of existing event driven code

http://sdrv.ms/LIJUlAhttp://emsmith.net/slides/dpc2012/event-code.zip

Page 23: Event and signal driven programming

Windows APIMessage Pump of Doom

Page 24: Event and signal driven programming

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

GobjectEvent Handler madness

Page 26: Event and signal driven programming

On to the Code Concepts

Dispatcher Register event by name Attach a handler Emit event

Page 27: Event and signal driven programming

QTFor those who like a little ++ with their C

Page 28: Event and signal driven programming

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

Best PracticesWhat to do in your own code

Page 30: Event and signal driven programming

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

Standard interfaces Observer? Use SplSubject and

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

Page 32: Event and signal driven programming

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

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

The Wild Wild WestExperiments are coming

Page 36: Event and signal driven programming

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

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

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
Page 40: Event and signal driven programming

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
Page 42: Event and signal driven programming

Bring it on Good Libraries Blog posts Articles Extensions

Page 43: Event and signal driven programming

DPC12

I HAZ BETA CODE 4 U

mojolive.com

Page 44: Event and signal driven programming

Contact Me http://emsmith.net http://joind.in/6249 [email protected] IRC – freenode – auroraeosrose #coapp and others