#lang racket/base ;; (Temporary) example client and server (require racket/tcp) (require racket/pretty) (require racket/match) (require racket/class) (require racket/port) (require racket/sandbox) (require "ssh-service.rkt") (require "standard-thread.rkt") (require "conversation.rkt") #;(define (t-client) (let-values (((i o) (tcp-connect "localhost" 2323 ;;22 ))) (let ((api (ssh-session 'client i o))) (printf "Got API ~v\n" api) (semaphore-wait (make-semaphore 0))))) (define *shells* (make-hash)) (define *interaction* (make-room 'interaction)) (define *prompt* "RacketSSH> ") (define (->string/safe bs) (cond ((string? bs) bs) ((bytes? bs) (with-handlers ((exn? (lambda (e) (bytes->string/latin-1 bs)))) (bytes->string/utf-8 bs))) (else (call-with-output-string (lambda (p) (write bs p)))))) (define (dump-interactions handle) (display *prompt*) (let retry-without-prompt () (sync (handle-evt (send handle listen-evt) (lambda (message) (display "\033[1G\033[2K") ;; clear current line (let loop ((message message)) (when message (match message ((arrived who) (printf "*** ~a arrived\n" (->string/safe who))) ((departed who why) (printf "*** ~a departed (~a)\n" (->string/safe who) (->string/safe why))) ((says who what #f) (printf " ~a: ~a\n" (->string/safe who) (->string/safe what))) ((says who what topic) (printf " ~a (~a): ~a\n" (->string/safe who) (->string/safe topic) (->string/safe what)))) (loop (send handle try-listen)))) (dump-interactions handle))) (handle-evt (peek-string-evt 1 0 #f (current-input-port)) (lambda (s) (cond ((eof-object? s)) ((char-whitespace? (string-ref s 0)) (read-string 1 (current-input-port)) (retry-without-prompt)) (else 'ready-to-read-something-real))))))) (define (call-with-interaction-prompt-read handle thunk) (parameterize ((current-prompt-read (lambda () (dump-interactions handle) (read-syntax "" (current-input-port))))) (thunk))) (define (get-user-evaluator username) (when (not (hash-has-key? *shells* username)) (hash-set! *shells* username (make-evaluator 'racket/base))) (hash-ref *shells* username)) (define (repl-shell username in out) (fprintf out "Hello, ~a.\n" username) (define handle (join-room *interaction* username)) (parameterize ((current-input-port in) (current-output-port out) (current-error-port out) (sandbox-input in) (sandbox-output out) (sandbox-error-output out) (current-namespace (make-empty-namespace))) (parameterize ((current-eval (get-user-evaluator username))) (eval `(define say ,(lambda (utterance) (printf " You: ~a\n" (->string/safe utterance)) (send handle say utterance) (void)))) (call-with-interaction-prompt-read handle read-eval-print-loop)) (fprintf out "\nGoodbye!\n") (close-input-port in) (close-output-port out))) (define (t-server) (define s (tcp-listen 2322)) (printf "Accepting...\n") (tcp-pty-ssh-server s repl-shell #:prompt *prompt*)) (if (getenv "clientmode") (void) #;(t-client) (t-server))