chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] replacing the "write" function


From: Shawn Rutledge
Subject: [Chicken-users] replacing the "write" function
Date: Thu, 4 Dec 2008 21:31:05 -0700

I'm working on Display Scheme some more, trying to get the
client/server aspect functional (as opposed to just address@hidden around
with graphics stuff).  So far I have a client which connects via ssh
to a server machine and runs a service.  The service (a conveniently
small test application) enumerates some X10 devices (remote-controlled
light switches) and builds up an s-expression to create a "group", and
inside a button for each switch, then sends this code across to the
client.  The problem is what the client should send back: each button
that it creates is a tinyclos object.  The server needs to be able to
asynchronously update the state of the button when the light switch is
turned on or off from somewhere else.  (This is the reason I have
probably ruled out using the remote-repl egg: it's a very synchronous
implementation, like what they call REST in the web development
world.)  So the client needs to either send back a widget ID of some
sort (which I don't like very much, because a lot of things can come
back, and the widget ID would have to be tested for), or what I'm
leaning towards is for it to send back code which when eval'd, will
create a proxy object on the server side (and the proxy encapsulates
the "widget ID" or whatever, so this concept can evolve without
changing the server-side program).  Well actually... the (send ...)
function will return a promise, which when forced, will yield whatever
it was the client sent back.  This is how I plan to keep the protocol
asynchronous: the result of each evaluation can be pipelined, sent
back lazily, and then used later on if/when it is needed.  There will
have to be request IDs and matching response IDs, I guess.

Anyway, I need to detect that we are about to send a tinyclos object,
and instead send the s-expression which when eval'd, will create the
proxy for it instead.  I wish I didn't have to re-invent the "write"
function to do that; I was hoping I could save off a pointer to it,
create a new-write function, then (environment-set! env 'write
new-write) on an environment in which the "display scheme program"
(either server-side or client-side) is to run, but I'm not having much
luck with that.  It is supposed to be possible, right?

So here's the version of "send" which works, but is a bit tedious (and
missing many more possible object types that will need to be handled
"specially"):

(define (make-send write-fn)
        (lambda (o)
                (let send ([o o])
                        (cond
                                [(instance? o)
                                        (send 1)
                                        ;; todo: code to create the proxy object
                                ]
                                [(string? o)
                                        (write-fn (format "~s" o))]
                                [(pair? o)
                                        (write-fn "(")
                                        (if (list? o)
                                                (for-each (lambda (o) (send o) 
(write-fn " ")) o)
                                                (begin (send (car o)) (write-fn 
" . ") (send (cdr o))))
                                        (write-fn ")")]
                                [(eq? o (void))
                                        (write-fn "(void)")]
                                [else (write-fn o)]
                        ))))

(define send (make-send (lambda (o)
        (logprint (format "- send ~s" o))
        (display o send-port) (flush-output send-port))))

What am I missing here?  Could I actually replace "write" with a
slightly customized version, just in the context of an environment,
without having to re-create it from scratch?




reply via email to

[Prev in Thread] Current Thread [Next in Thread]