Another example program

This commit is contained in:
Tony Garnock-Jones 2012-05-11 15:28:48 -04:00
parent 0d23202e54
commit 51960662d4
2 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,46 @@
#lang racket/base
;; A more different trivial example program demonstrating os2-tcp.rkt.
(require racket/string)
(require racket/set)
(require racket/match)
(require "os2.rkt")
(require "os2-tcp.rkt")
(define ((connection-handler local-addr remote-addr) self-pid)
(transition 'no-state
(send-tcp-mode remote-addr local-addr 'lines)
(send-tcp-credit remote-addr local-addr 1)
(role 'echoer (topic-subscriber (tcp-channel remote-addr local-addr (wild)))
#:state state
[(tcp-channel remote _ (? bytes? line))
(transition state
(send-tcp-credit remote-addr local-addr 1)
(send-message
(tcp-channel local-addr remote-addr (bytes-append #"You said: " line #"\n"))))]
[(tcp-channel remote _ (? eof-object?))
(transition state
(kill))])))
(define (listener local-addr)
(transition 'no-state
(role 'inbound-handler (topic-subscriber (tcp-channel (wild) local-addr (wild)) #:virtual? #t)
#:state state
#:topic t
#:on-presence (match t
[(topic 'publisher (tcp-channel remote-addr (== local-addr) _) #t)
;; Ignore virtual flows. They just mean there's
;; someone willing to supply connections to us
;; at some point in the future.
state]
[(topic 'publisher (tcp-channel remote-addr (== local-addr) _) #f)
(transition state (spawn (connection-handler local-addr remote-addr)))]))))
(define (main port)
(ground-vm
(transition 'none
(spawn tcp-spy)
(spawn tcp-driver)
(spawn (listener (tcp-listener port))))))
(main 5999)