Read on and leave a comment
or return to our portfolio.

Posts in the ‘Code’ category

13 Sep 2006

by Noel

A live hash-table using XML-RPC

I received a question the other day about using our XML-RPC library, and thought I’d put the response here. It’s still a simple example, but it illustrates one or two things. You must be running PLT Scheme 352.5 or greater for this to work, as we’re using some nifty new features that make things more stable.

I’m going to implement a “live hash-table”. By this, I mean I’m going to have a PLT Scheme hash-table that I can access from anywhere via XML-RPC. I created a “testing” directory in in the “servlets” directory of my server. Inside of that “testing” directory, I created a servlet called “live-hash.ss”. It looks like this:

(module live-hash mzscheme

(require
;; For the 'xmlrpc-server' and 'add-handler' functions
   (planet "xmlrpc-module-servlet.ss"
("schematics" "xmlrpc.plt" 1 3))
;; For 'raise-exn:xmlrpc'
   (planet "base.ss"
("schematics" "xmlrpc.plt" 1 3))
)

(provide interface-version manager timeout start)

;; I'm using 'equal here so that strings can be
  ;; used as hashtable keys.
  (define table (make-hash-table 'equal))

;; CONTRACT
  ;; insert : (U string number) any -> bool
  ;; PURPOSE
  ;; The 'insert' method will insert a value into the
  ;; hashtable, and return #t. We return a boolean in all
  ;; casees because XML-RPC has no natural coercion for the
  ;; #<void> value.
  (define (insert key value)
(if (or (string? key)
(number? key))
(begin (hash-table-put! table key value) #t)
#f))

;; CONTRACT
  ;; get : (U string number) -> any
  ;; PURPOSE
  ;; The 'get' method returns a value from the hashtable, or
  ;; raises an XML-RPC fault.
  (define (get key)
(hash-table-get
table key
(lambda ()
(raise-exn:xmlrpc
(format "No value in hash for key '~a'" key))
)))

;; Make 'insert' and 'get' available to the
  ;; outside world.
  (add-handler 'insert insert)
(add-handler 'get get)

)

If you can execute this in the “module” language of DrScheme and not get any errors, there’s a good chance that it will run under the webserver. At least, that’s a first step.

On the client side, I have some code that declares that the PLT webserver running on my local machine (with the “live-hash.ss” servlet) is an XML-RPC endpoint. I then map two local functions, “ins” and “get” to the remote methods provided by the server. Lastly, I do a quick test: I insert some data, and then request it back.

The client-side looks like:

(module live-hash-client mzscheme

(require (planet "xmlrpc.ss"
("schematics" "xmlrpc.plt" 1 3)))

;; Declare where the endpoint for this XML-RPC call is
  (define local (xmlrpc-server
"localhost"
8080
"servlets/testing/live-hash.ss"))

;; Define the mappings from local method calls to remove
  ;; method calls. In this case, the local name "ins" is bound
  ;; to the method "insert" running on the server.
  (define ins (local 'insert))
(define get (local 'get))

;; Try inserting some data
  (for-each (lambda (key val)
(ins key val))
'(1 2 3 4 5)
'(a b c d e))

;; Now, try retrieving it
  (for-each (lambda (key)
(printf "K: ~a V: ~a~n" key (get key)))
'(1 2 3 4 5))

)

Again, I’m working under the “module” language here.

If all goes well, you can run the client-side, and see the data go round-trip to the server. Well, you can’t exactly “see” it, but you can guess that it goes round-trip to the server.

Welcome to DrScheme, version 352.5-svn28aug2006.
Language: (module ...).
K: 1 V: a
K: 2 V: b
K: 3 V: c
K: 4 V: d
K: 5 V: e

That’s perhaps not a lot to be getting on with; there’s more documentation in the “doc.txt” file included in the distribution. And it’s not quite as cool as Tony’s explorations with Erlang, but… well, this library has been around for a while, and occasionally, someone finds a use for it.

(Pretty-printing of code to HTML made possible by Paste Scheme at scheme.dk. Woot.)

Posted in Code | Comments Off on A live hash-table using XML-RPC

23 Aug 2006

by Noel

Unlib and IDCheck Release

Two code releases today: a new version of Unlib, and the first release of our library for interacting with the IDCheck single sign-on system (say that ten times quickly).

Posted in Code | Comments Off on Unlib and IDCheck Release

8 Aug 2006

by Noel

XML-RPC 1.2

Things are rolling along on our XML-RPC library for PLT Scheme. At this point, the client is stable and well tested, and both the servlet and Apache CGI server implementations work and are poorly tested. However, one or two people were asking to make use of the server-side code, so we’ve made it available. Caveat developer.

Implementing an XML-RPC servlet is really quite straight-forward:

(require (planet "xmlrpc-servlet.ss"
("schematics" "xmlrpc.plt" 1 2)))

(define (add x y) (+ x y))
(add-handler 'math.add add)

(handle-xmlrpc-requests)

Dropping this code somewhere under the ‘/servlets’ directory should get you going. I’m still unhappy with the current state of the CGI code:

#!/path/to/mzscheme -gqr
(require (lib "config.ss" "planet"))
(PLANET-DIR "/tmp/PLaneTWeb/dir")
(CACHE-DIR "/tmp/PLaneTWeb/cache")
(LINKAGE-FILE "/tmp/PLaneTWeb/linkage")
(LOG-FILE #f)

(require (planet "xmlrpc-cgi.ss"
("schematics" "xmlrpc.plt" 1 2)))

(add-handler 'add (lambda (a b) (+ a b)))

(output-http-headers)

(handle-xmlrpc-requests)

I imagine we’ll absorb the output-http-headers into the handle-xmlrpc-requests macro, and I really want to do something to improve the state of affairs w.r.t. PLaneT package handling in the CGI environment. As I said above: the server code in the library is in motion, and it will likely change.

As an aside, I expect stress-testing the server-side code will be interesting; Noel suggested using Ethereal to record interactions between clients (in other languages) and our server implementations, and then replay those interactions in SchemeUnit unit tests. A neat idea, and not something I had thought of.

Posted in Code, Web development | Comments Off on XML-RPC 1.2