[plt-scheme] How to execute eval from a network udp socket?

From: Todd Rovito (rovitotv at gmail.com)
Date: Wed Jul 1 13:39:34 EDT 2009

Grant,
   Thanks for the links, the Scheme cookbook is nice!

I worked on this yesterday and here is what I have, at this point it
works for my problem.  Thanks to all for the help, I am still learning
but so far I find plt-scheme to be very cool.

; to execute this program with mzscheme "mzschemecgc -e '(load
"SchemeUDPServerEval.scm")'" then
; you can use netcat to send udp traffic to the program with the command
; "nc -u 214.4.1.147 51515" then simply enter a valid scheme
expression and watch it
; magically get evaluated. If the expression is invalid, the
application will terminate.
(require (lib "string.ss"))
(define repl-string (make-string 1024))
(define repl-buffer (make-bytes 1024 ))
(define sleep-secs 1)
(define port 51515)
(define the-socket (udp-open-socket))

(define (doeval StringLength)
  	; this code converts byte string to a locale string and removes the '\n'
	(set! repl-string (bytes->string/locale repl-buffer #f 0 (- StringLength 1)))

	; print out the string to eval then the eval result
	(display "string to eval:")
	(display repl-string)
	(display " ")
	(display (eval-string repl-string))
	(display " \n")
)

; this function sleeps for 1 second then listens for udp traffic on
the ip and port number
; it is a non-blocking receive.
; this function is an example of a scheme "for" loop
(define (doit x x-max dx)
  (when (<= x x-max)
    (begin
      (sleep sleep-secs)
      (printf "for loop:~a\n" x)
      (let-values (((n ip port)
                      (udp-receive!* the-socket repl-buffer)))
  	(if n (doeval n)
	       (printf "No data read on udp socket\n") ) )

      (doit (+ x dx) x-max dx)
      )))

(define (start)
  (printf "Scheme UDP Server with eval\n")

  ; bind to the socket and port
  (udp-bind! the-socket #f port)
  (doit 0 50 1) ; execute loop from 0 to 50 in steps of 1
  (udp-close the-socket)
)

(start)


On Tue, Jun 30, 2009 at 4:22 AM, Grant Rettke<grettke at acm.org> wrote:
> Hi Todd,
>
> On Tue, Jun 30, 2009 at 4:14 AM, Todd Rovito<rovitotv at gmail.com> wrote:
>>   I am new to scheme trying to learn.  Currently I have a closed
>> network where I am passing s-expressions around with UDP sockets.  The
>> UDP sockets work well in scheme now I need to go to the next level and
>> actually execute those s-expressions.  Here is an example server:
>
> Here are some links that I have collected regarding this topic:
>
> http://download.plt-scheme.org/doc/html/reference/syntax-model.html#(part._namespace-model)
> http://download.plt-scheme.org/doc/html/reference/Namespaces.html
> http://download.plt-scheme.org/doc/html/reference/Sandboxed_Evaluation.html
>
> http://schemecookbook.org/Cookbook/DynamicEvalCustomNamespace
> http://schemecookbook.org/Cookbook/DynamicUntrustedEval
>


Posted on the users mailing list.