Dataflow Network Programming with Neuron Eric Griffis RacketCon 2018 St. Louis, MO 1
I. Meet Nick 2
Nick cares about cancer 3
Curing cancer is expensive 4
5
6
II. Enter Neuron 7
The Neuron Framework concurrency model messaging API process construction library 8
Lightweight Processes (process ( λ () (forever (emit (f (take)))))) 9
Synchronous and Asymmetric Simple Exchange (give π v) → (take) (recv π ) ← (emit v) 10-11
Synchronous and Asymmetric Mediated Exchange (give π fwd v) → (forward-to π ) → (take) (recv π fwd ) ← (forward-from π ) ← (emit v) 12
Serial Messaging Endpoints Codecs Sockets 13-14
Sockets • input-output port • simplified messaging API • prevent half-open connections 15
Codecs • printer and parser functions • encoder and decoder functions 16
Parsers & Printers parser :: (-> input-port? any/c) read read-json printer :: (-> any/c output-port? void?) writeln write-json 17
Codecs decoder :: (-> parser (-> socket process)) encoder :: (-> printer (-> socket process)) codec :: (-> parser printer (-> socket process)) 18
UDP Endpoints (define π udp-src (udp-source read "::" 5000)) (define π udp-snk (udp-sink write "somehost" 5000)) 19
TCP Endpoints (define π tcp-cli (tcp-client "somehost" 1234)) (define π tcp-srv (tcp-server 1234)) (define π tcp-svc (tcp-service sexp-codec 1234)) 20
III. Putting it all Together 21
Multiplayer Game Server TCP service register clients UDP sinks broadcast world state world simulator refresh world state (default 10 Hz) 22-23
Game Server: TCP Service (define π svc (tcp-service sexp-codec 3000)) (forever (define-values (key msg) (recv π svc )) (match msg [`(SET ,host ,port) (set-client key host port)] [`(DROP ,host ,port) (drop-client key)] [`(MOVE , Δ x , Δ y) (move-client key Δ x Δ y)])) 24-25
Game Server: UDP Sinks (udp-sink writeln host port) 26
Game Server: World Simulator (define π sim (simulator update-world)) (for ([ π pub (all-clients)]) (give π pub the-world)) 27-28
Complete Game Server (define-values (world clients) (values (make-hash) (make-hash))) (define π svc (tcp-service sexp-codec 3000)) (define (set-client key host port) ... (hash-set! clients key (udp-sink writeln host port))) (define (update-world Δ t) ... (define the-world (hash->list world)) (for ([ π pub (hash-values clients)]) (give π pub the-world))) (define π sim (simulator update-world)) (forever (define-values (key msg) (recv π svc )) (match msg [`(SET ,host ,port) (set-client key host port)] [`(DROP ,host ,port) (drop-client key)] [`(MOVE , Δ x , Δ y) (move-client key Δ x Δ y)])) 29
IV. Summary & Conclusion 30
Summary • Neuron is a compositional framework • Making network software development easy (and fun!) 31
What Happened to Nick? 32-33
Thank You Eric Griffis https://dedbox.github.io/ dedbox@gmail.com @dedbox on twitter and github 34
Recommend
More recommend