New TCP example

This commit is contained in:
Tony Garnock-Jones 2012-05-11 15:18:44 -04:00
parent e925a963ff
commit 0d23202e54
1 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,40 @@
#lang racket/base
;; Differently 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
(role 'date-sender (topic-publisher (tcp-channel local-addr remote-addr (wild)))
#:state state)
(send-message (tcp-channel local-addr remote-addr
(string->bytes/utf-8
(format "~a\n" (current-inexact-milliseconds)))))
(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)