event and signal driven programming

Post on 08-May-2015

7.566 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Event and Signal Driven ProgrammingThe wheel already exists…

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

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

Structured programming simple, hierarchical program flow

structures sequence selection repetition

procedural adds sub-routines or functions

object oriented modularizes components of code

Event Driven Programming

Wait for Events

Accept Event

Dispatch Event

Handlers are Called

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?

Asyncronous vs. Synchronous event-driven !== asynchronous structured !== synchronous

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

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

Design PatternsThe Evolution of Event based 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

Publisher Broker

Subscriber

Subscriber

Subscriber

(Main) (event/message) loop poll blocks until something arrives dispatches main is added if it is the highest level of

control in a program

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

Sub

jectObserver

1Observer

2Observer

3

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

Object

Send Event

Send Event2

Dispatcher

Handler1(Event1)

Handler2(Event1)

Handler3(Event1)

Handler4(Event 2)

Dispatcher gets event determines which handlers get called

either attached determined by type stored in some manner

calls the handlers

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

Reactor Resources Synchronous Event Demultiplexer Dispatcher Request Handler

Reactor

Various Handlers

Dispatcher

Demultiplexer

Service Request

Service Request

Service Request

Copy the GiantsUse examples of existing event driven code

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

Windows APIMessage Pump of Doom

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)

GobjectEvent Handler madness

On to the Code Concepts

Dispatcher Register event by name Attach a handler Emit event

QTFor those who like a little ++ with their C

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

Best PracticesWhat to do in your own code

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

Standard interfaces Observer? Use SplSubject and

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

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

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

The Wild Wild WestExperiments are coming

Framework’s New Thing ZF2 – Event Manager Symfony 2 – Event Manager Kohana Events Aura (Aura.signal) CodeIgnitor events I could go on….

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

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

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

Bring it on Good Libraries Blog posts Articles Extensions

DPC12

I HAZ BETA CODE 4 U

mojolive.com

Contact Me http://emsmith.net http://joind.in/6249 auroraeosrose@gmail.com IRC – freenode – auroraeosrose #coapp and others

top related