diff --git a/os2-tcp-test.rkt b/os2-tcp-test-chat-service.rkt similarity index 100% rename from os2-tcp-test.rkt rename to os2-tcp-test-chat-service.rkt diff --git a/os2-tcp-test-echo-service.rkt b/os2-tcp-test-echo-service.rkt new file mode 100644 index 0000000..a7c46b7 --- /dev/null +++ b/os2-tcp-test-echo-service.rkt @@ -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)