[plt-scheme] Implementing web services

From: Matt Jadud (jadudm at gmail.com)
Date: Tue Aug 14 08:01:44 EDT 2007

Hi Iasaac,

I'll see if I can help.

On 8/14/07, Isaac Raway <isaac.raway at gmail.com> wrote:

> /collects/web-server/default-default-web-root/servlets/math.ss
>
> > (module math mzscheme
> >   (require (planet "xmlrpc-module-servlet.ss"
> >                    ("schematics" "xmlrpc.plt" 2)))
> >   (provide interface-version manager timeout start)
> >   (define (add x y) (+ x y))
> >   (add-handler 'math.add add)
> >   )

This looks correct.

>
> ~/Documents/Scheme/xmlrpc/add_client.scm
>
> > (module add_client mzscheme
> >   (require (planet "xmlrpc.ss" ("schematics" "xmlrpc.plt" 2 1)))
> >   (provide (all-defined))
> >   (define math (xmlrpc-server "localhost:8080/servlets/math.ss"
> > 8080 "RPC2"))
> >   (define add (math "math.add"))
> >   (add 1 1)
> >   )

As you comment, 'xmlrpc-server' seems strange. It's there because that
was how CapnRPC did it, and I used Pete's interface (years ago now) so
that my code would have the same interfaces as his. It might be better
to say "define-server" instead.

As you see later in your code, you have a contract violation here. Try
these two things:

(define math (xmlrpc-server (string->url
"http://localhost:8080/servlets/math.ss")))

You'll need to '(require (lib "url.ss" "net"))' at the top of the
client code for this form. Or, if you want to use the form you have,
try

(define math (xmlrpc-server "localhost" 8080 "/servlets/math.ss"))

I usually prefer the first form.

The rest of your client code looks like it should work.

For testing, I really recommend the Instaweb package from PLaneT.
Create a file called "test-server.scm" in the same directory as your
module, and put this in that module:

(require
 (planet "instaweb.ss" ("schematics" "instaweb.plt" 1)))
(instaweb "add.ss" 5678 "127.0.0.1")

Here, "add.ss" is the name of the server-side servlet. Now, either run
this in DrScheme, or on the commandline do

mzscheme -f test-server.scm

Either way, it will pull down instaweb, create a webserver instance,
and load the servlet, as well as give you the URL that you can use as
an argument to "string->url". Very handy.

>
> The xmlrpc-server call seems very wrong to me, but it's the only way
> I could get even close. By 'close' I mean that it causes an error on
> the server, which returns a HTTP 500 and prints this on the terminal
> running plt-web-server as a background process:

Yep. That's because it did its best to make use of what you handed to
it. I'll take your example and see if we can't provided a better
error. I don't know how many people use the library, but eliminating
the choice from the interface might be a Good Thing.

> If I define math as (define math (xmlrpc-server "http://localhost:
> 8080/servlets/math.ss")) which seems to be valid, I get the following
> error in DrScheme from the client:

This is what I suggested a fix for up above.

> I feel like I need a bit too much hand holding, but any tips would be
> appreciated.

Nah. Perhaps its just bad interface design to the library, and perhaps
not being familiar with the errors that MzScheme generates. For the
future, when you see a contract error come from a library, it means
you have the types wrong for a call, and the error is telling you what
types are expected instead.

Good questions.

Cheers,
M


Posted on the users mailing list.