Thursday, September 20, 2007

Pocket Web Service

In A Future Without Keyboards and Asterisk, WiFi, and a Nokia N800 Internet Tablet, I mentioned how I was playing with the Nokia N800 Internet Tablet, a pocket-sized, Linux-based, WiFi-capable, internet appliance. I also mentoned that I had installed a C Virtual Machine (CVM) on it, a version of the JVM configured for the Java Connected Device Configuration (CDC).

After just a few hours of futzing around, I can now serve the entire Digital Aggregates Corporation web site off the N800 using some Java code I slung together based on the prior work of Jon Berg of turtlemeat.com. Sure, it's not the fastest or even the smallest web server on the planet. I'm not about to expose it outside the company firewall. And of course I'm sure I'm not the first to do this. But I can sit at my laptop and browse pages on a web server that I'm carrying around in my shirt pocket.

Here is the source zip and the jar file (served off the production web server) if you want to have this kind of joy yourself. I dropped the jar file into the Foundation directory that comes with the CVM distribution, placed a copy of the entire web site in a www directory, then fired the server up with this shell script.

#!/bin/sh
cd Foundation*
./bin/cvm -cp chapparal-0.3.jar \
com.diag.chapparal.http.Server \
/home/user/www 300000 8080

I then pointed my laptop's web browser at http://192.168.1.106/index.html and watched wackiness ensue! (The IP address is DHCP served from behind my NAT firewall, so your mileage will vary.) The three arguments to the Server main are the root path prefixed to every file request, the number of milliseconds the server stays up, and the HTTP listen port to use.

I developed the whole thing on my laptop using Eclipse, tested it using junit, built it for the target with ant using the 1.6 compiler in 1.4 source and target mode, and then downloaded the jar to the N800 via its own web browser.

Wicked mad fun!

Update (2008-01-03)

Sun Microsystems offers a Java SE 5.0 implementation for Linux on an embedded PowerPC that you can use for a ninety-day free trial. (If you want to ship it as a product, you have to pay a royalty.) As an experiment I recently installed this JVM on such a system, and as a demo ran my Java-based pocket web server on it. Took almost no effort, worked like a charm. It makes a nice proof of concept.

2 comments:

Chip Overclock said...

I just updated the jar and zip links in this article to point to the Chapparal 0.1 distribution. The new code understands more mime types, and keeps the mappings from file name suffix to mime type in a resource bundle.

You can still access the original 0.0 distribution via http://www.diag.com/ftp/chapparal-0.0.jar and http://www.diag.com/ftp/chapparal-0.0-src.zip .

Chip Overclock said...

The Chapparal distribution is up to 0.3 now, and seems stable enough that I've removed the prior distributions. The Buckaroo package (which conforms to Java 1.6 and hence can't be used under Java ME/CVM without back-porting to 1.4) has an equivalent version of the simple Chapparal web server. Downloads of both Chapparal and Buckaroo can be found here.