#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 *environments* (make-hash)) (define *interaction* (make-room 'interaction)) (spy-on *interaction*) (define *interaction-handle* (make-parameter #f)) (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-environment username) (when (not (hash-has-key? *environments* username)) (hash-set! *environments* username (make-base-namespace))) (hash-ref *environments* username)) (define (repl-shell username in out) (define handle (join-room *interaction* username)) (define env (get-user-environment 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 env) (sandbox-namespace-specs (list (lambda () env)))) (parameterize ((current-eval (make-evaluator '(begin)))) (printf "Hello, ~a.\n" username) (eval `(,*interaction-handle* ,handle)) (eval `(define say ,(lambda (utterance) (printf " You: ~a\n" (->string/safe utterance)) (send (*interaction-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 4 #t)) (printf "Accepting...\n") (tcp-pty-ssh-server s repl-shell #:prompt *prompt*)) (if (getenv "clientmode") (void) #;(t-client) (t-server))