diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2012-03-05 22:27:22 +0100 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2012-03-05 22:27:22 +0100 |
commit | c6753ed8974da8090037a52d65415143ebf1c015 (patch) | |
tree | 6f66a2c4e83a7f9df55e9d9328cbf125818cc379 | |
parent | 312fc443d91af010051e278e03830aa5ffaa4c9c (diff) | |
download | ebus-alt-c6753ed8974da8090037a52d65415143ebf1c015.tar.gz ebus-alt-c6753ed8974da8090037a52d65415143ebf1c015.zip |
ebus-racket: tcp repl (--tcp-repl <port>)
-rwxr-xr-x | ebus-racket/inserter.rkt | 14 | ||||
-rw-r--r-- | ebus-racket/net-repl.rkt | 34 | ||||
-rw-r--r-- | ebus-racket/tcp-repl.rkt | 35 |
3 files changed, 45 insertions, 38 deletions
diff --git a/ebus-racket/inserter.rkt b/ebus-racket/inserter.rkt index 9aaca78..a2ee1a0 100755 --- a/ebus-racket/inserter.rkt +++ b/ebus-racket/inserter.rkt @@ -6,7 +6,8 @@ racket/match "db.rkt" "layer7.rkt" - "layer2.rkt") + "layer2.rkt" + "tcp-repl.rkt") (define logger (make-logger 'ebus-inserter (current-logger))) @@ -41,6 +42,8 @@ (flush-output)]) (loop)))))) +(define-namespace-anchor repl-ns-anchor) + (define (main) ;; Parse commandline (command-line @@ -48,6 +51,8 @@ [("-c" "--connect") host port "Connect to server <host> <port>" (connect-host? host) (connect-port? (string->number port))] + ["--tcp-repl" port "Open REPL on TCP <port>" + (tcp-repl-run (namespace-anchor->namespace repl-ns-anchor) (string->number port))] ["--debug-layer2" "Log level for Layer 2 Parser" (loglevel-layer2? 'debug)] ["--debug-db" "Log level for DB" @@ -67,18 +72,19 @@ (start-logger-thread (make-log-receiver logger 'info) (make-log-receiver db-logger (loglevel-db?)) (make-log-receiver layer2-logger (loglevel-layer2?)) - (make-log-receiver layer7-logger 'info)) + (make-log-receiver layer7-logger 'info) + (make-log-receiver tcp-repl-logger 'info)) ;; Test Database Connection (when (not (db-pgc-test)) - (display "Failed to connect database") + (log-message logger 'fatal "Failed to connect to database" #t) (exit 1)) ;; Connect, replacing input with tcp connection (if (or (null? (connect-host?)) (null? (connect-port?))) (log-message (current-logger) 'info "Using stdin" #t) (let-values ([(cin cout) (tcp-connect (connect-host?) (connect-port?))]) - (display (format "Connected to ~s ~s ~n" (connect-host?) (connect-port?))) + (log-message logger 'info (format "Connected to ~s ~s ~n" (connect-host?) (connect-port?)) #t) (current-input-port cin))) ;; Process Ebus Packets diff --git a/ebus-racket/net-repl.rkt b/ebus-racket/net-repl.rkt deleted file mode 100644 index 1d53807..0000000 --- a/ebus-racket/net-repl.rkt +++ /dev/null @@ -1,34 +0,0 @@ -#lang racket -(require mzlib/thread) -(require "db.rkt") - -(define-namespace-anchor a) -(define ns (namespace-anchor->namespace a)) - -(define (my-eval expr) - (eval expr ns)) - - -(define (run-tcp-repl namespace port) - (thread - (lambda () - (run-server port - net-repl-connection - #f - (lambda (port-k max-allow-wait reuse?) - (tcp-listen port-k max-allow-wait reuse? "127.0.0.1")) - )))) - - (define (net-repl-connection i o) - (let/ec exit - (parameterize ([current-input-port i] - [current-output-port o] - [current-error-port o] - [current-namespace ns]) - (read-eval-print-loop)) - (fprintf o "\nBye...\n") - (close-output-port o))) - - -(define-namespace-anchor ns-anchor) -(thread-wait (run-tcp-repl (namespace-anchor->namespace ns-anchor) 8890)) 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 |