#lang racket/base (require net/sendurl racket/list web-server/servlet web-server/servlet-env "package.rkt") ;; A hacky way to test the evaluation. (provide evaluate) (define port (+ 8000 (random 8000))) ;; Channel's meant to serialize use of the web server. (define ch (make-channel)) ;; start up the web server ;; The web server responds to two types of requests ;; ?p Inputting a program ;; ?r Getting a response (void (thread (lambda () (define (start req) (cond [(exists-binding? 'p (request-bindings req)) ;; Create a web page whose content contains ;; the script. (let ([program (channel-get ch)] [op (open-output-bytes)]) (fprintf op #<

Running program.

EOF ) (response/full 200 #"Okay" (current-seconds) TEXT/HTML-MIME-TYPE empty (list #"" (get-output-bytes op))))] ;; Normal result came back [(exists-binding? 'r (request-bindings req)) (channel-put ch (list (extract-binding/single 'r (request-bindings req)) (string->number (extract-binding/single 't (request-bindings req))))) `(html (body (p "ok")))] ;; Error occurred [(exists-binding? 'e (request-bindings req)) (channel-put ch (make-error-happened (extract-binding/single 'e (request-bindings req)) (string->number (extract-binding/single 't (request-bindings req))))) `(html (body (p "ok")))] [else `(html (body (p "Loaded")))])) (serve/servlet start #:banner? #f #:launch-browser? #f #:quit? #f #:port port #:servlet-path "/eval")))) (define-struct error-happened (str t) #:transparent) ;; evaluate: sexp -> (values string number) ;; A little driver to test the evalution of expressions, using a browser to help. ;; Returns the captured result of stdout, plus # of milliseconds it took to execute. (define (evaluate e) ;; Send the program to the web browser, and wait for the thread to send back (send-url (format "http://localhost:~a/eval?p=t" port) #f) (channel-put ch e) (let ([output+time (channel-get ch)]) (cond [(error-happened? output+time) (raise output+time)] [else (values (first output+time) (second output+time))])))