summaryrefslogtreecommitdiff
path: root/ebus-racket/tcp-repl.rkt
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2012-03-05 22:27:22 +0100
committerYves Fischer <yvesf-git@xapek.org>2012-03-05 22:27:22 +0100
commitc6753ed8974da8090037a52d65415143ebf1c015 (patch)
tree6f66a2c4e83a7f9df55e9d9328cbf125818cc379 /ebus-racket/tcp-repl.rkt
parent312fc443d91af010051e278e03830aa5ffaa4c9c (diff)
downloadebus-alt-c6753ed8974da8090037a52d65415143ebf1c015.tar.gz
ebus-alt-c6753ed8974da8090037a52d65415143ebf1c015.zip
ebus-racket: tcp repl (--tcp-repl <port>)
Diffstat (limited to 'ebus-racket/tcp-repl.rkt')
-rw-r--r--ebus-racket/tcp-repl.rkt35
1 files changed, 35 insertions, 0 deletions
diff --git a/ebus-racket/tcp-repl.rkt b/ebus-racket/tcp-repl.rkt
new file mode 100644
index 0000000..4b19d85
--- /dev/null
+++ b/ebus-racket/tcp-repl.rkt
@@ -0,0 +1,35 @@
+(module tcp-repl racket/base
+ (require mzlib/thread
+ racket/tcp)
+ (provide (prefix-out tcp-repl- run)
+ (prefix-out tcp-repl- logger))
+
+ (define logger (make-logger 'tcp-repl (current-logger)))
+
+ (define (run namespace port)
+ (thread
+ (lambda ()
+ (run-server port ; TCP-Port
+ (make-connection-handler namespace) ; connection handler
+ #f; timeout
+ (lambda (tcp-port max-allow-wait reuse?) ; listen handler
+ (log-message logger 'info (format "Listen on ~a:~a" "127.0.0.1" tcp-port) #t)
+ (tcp-listen tcp-port max-allow-wait reuse? "127.0.0.1"))
+ ))))
+
+ (define (tcp-tostring port)
+ (let-values ([(address-from port-from address-to port-to) (tcp-addresses port #t)])
+ (format "~a:~a -> ~a:~a" address-from port-from address-to port-to)))
+
+ (define (make-connection-handler namespace)
+ (lambda (ip op)
+ (let/ec exit
+ (log-message logger 'info (format "New Connection ~a" (tcp-tostring ip)) #t)
+ (parameterize ([current-input-port ip]
+ [current-output-port op]
+ [current-error-port op]
+ [current-namespace namespace])
+ (read-eval-print-loop))
+ (log-message logger 'info (format "End Connection ~a" (tcp-tostring ip)) #t)
+ (close-output-port op))))
+ ) ; end module tcp-repl \ No newline at end of file