41 lines
1.3 KiB
Racket
41 lines
1.3 KiB
Racket
|
#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)
|