Plain TCP beats RMI

I’ve been working on a class project that involves a cluster of worker machines.  Just to throw something quick together, I thought I’d use RMI.  Since I could ensure that every machine was running byte-for-byte identical programs, I didn’t think I’d run into class loading issues.  However, I’ve never had good luck with RMI, and it failed again.

Even with my class Worker existing on the client and on the server, and being identical, I’d still get ClassNotFoundExceptions for it.  I also had to manually set the java.rmi.server.hostname and java.rmi.server.codebase properties to get RMI to work at all, since it can’t seem to autodetect these settings.

After a few hours of failure, I gave up and decided to give up and use simple TCP connections to send serialized objects.  Worked like a charm.  Screw you, RMI.

Ant death spiral

Apparently, army ants can form death spirals where massive numbers of ants walk in a circle.  Mental floss has an article.

Adventures in Haskell – Error handling

I fell in love with Haskell almost the day I started using it.  It’s not perfect, though, and one of its weaker areas is error/exception handling.  I’m not going to cover all ways to return errors.  For more details, see 8 ways to report errors in Haskell.

Haskell has an Exception class in the Control.Exception module, but this can only be caught within the IO monad, which is an onerous restriction.  In my (un)experience, a more flexible approach is to have your function return Either String a, where a is whatever type it would return normally.  In this case, the function would evaluate to Left "error text" on an error, or Right 2 (if it was going to return 2, for example).   There are two issues:

  1. A lot of boilerplate is necessary to propagate the exception.  This is actually easy to solve with the Error monad.
  2. Haskell has no support whatsoever for stack traces.  These can be hacked in, but it requires manual effort.

To propagate the exceptions, use the Error monad.  You can use pretty much any error type with the Error monad as long as you set up up first.  Either String a is set up already, so it’s easy to use.   Let’s say you have functions f1 and f2 which accept a non-negative integer and return Either String Int.  To sum them, define mysum as follows:

-- This uses the Error monad even though we don't say so anywhere.  Type inference is awesome!
mysum :: Int -> Either String Int
mysum x =
  do
    when (x < 0) (throwError "x is negative")
    x1 <- f1 x
    x2 <- f2 x
    return (x1 + x2)

This is essentially equivalent to:

-- Ugly boilerplate, even for only two function invocations.
mysum :: Int -> Either String Int
mysum x =
  if x < 0
  then Left "x is negative"
  else case f1 x of
         Left e -> Left e
         Right x1 -> case f2 x of
           Left e -> Left e
           Right x2 -> Right (x1 + x2)

As I said, the problem of stack traces is more difficult.  I’m not aware of a standard or convention, but one way to handle it is to manually add prefixes to a list of some sort, like String (which is a list of Char), which means we can still use Either String a.  A helper function I wrote makes this a little easier:

nestError :: (MonadError [p] m) => [p] -> m a -> m a
nestError prefix a = catchError a (\e -> throwError (prefix ++ e))

This would be used like so:

mysum :: Int -> Either String Int
mysum x =
  nestError "Error in mysum: " (do
    when (x < 0) (throwError "x is negative")
    x1 <- f1 x
    x2 <- f2 x
    return (x1 + x2)
  )

If the second argument returns normally, nestError just returns the value.  If there’s an error, it adds the prefix.  For example, if x was negative, the final result would be Left "Error in mysum: x is negative".  If f1 had the error “foo”, the final result would be Left "Error in mysum: foo".

Clearly, this pales compared to Java’s exceptions, which are the best of any language I’ve used.  However, tricks like this make Haskell’s error handling a bit easier to use.

(Disclaimer: I am not a Haskell expert.  There may be (and probably are) better ways to do this.)

Pics of the Vertical Motion Simulator (flight simulator) at NASA Ames Research Center

I got to go on a tour of the facilities at the Vertical Motion Simulator (flight simulator) at the NASA Ames Research Center.  While we were there, a pilot was training in a simulation of a tiltrotor, one that resembles a 737 but can takeoff vertically.

We also had a view into the control room for the simulator.


Here’s the cab used for space shuttle training.  If I heard our guide right, every astronaut that has flown the space shuttle trained in this exact cab.

.

The cab for the lunar lander was right next to the one for the space shuttle.  We couldn’t go inside it because it wasn’t hooked up, so the lights weren’t on.

Here are all the hookups for the shuttle cab.

Here’s the one for the lunar lander so you can see what some of the cables are for.

You can’t parse HTML with regex

The top voted answer for this question on StackOverflow is epic:

You can’t parse [X]HTML with regex. Because HTML can’t be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML. You will never make me crack. HTML is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide. The <center> cannot hold it is too late. The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty. If you parse HTML with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the n​erves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege̿̔̉x-based HTML parsers are the cancer that is killing StackOverflow it is too late it is too late we cannot be saved the trangession of a chi͡ld ensures regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse HTML has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process HTML establishes a breach between this world and the dread realm of c͒ͪo͛ͫrrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of reg​ex parsers for HTML will ins tantly transport a programmer’s consciousness into a world of ceaseless screaming, he comes, the pestilent slithy regex-infection wil​l devour your HT ML parser, application and existence for all time like Visual Basic only worse he comes he comes do not fi ght he com̡e̶s, ̕h̵i s un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment, HTML tags lea͠ki̧n͘g fr̶ǫm ̡yo​͟ur eye͢s̸ ̛l̕ik͏e liq uid pain, the song of re̸gular exp​ression parsing will exti nguish the voices of mor​tal man from the sp here I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful t he final snuffing of the lie​s of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O ​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ

Upgrading to Android 2.1

I recently upgraded my phone from Android 1.5 to 2.1.  From a user standpoint, there isn’t a huge difference in terms of functionality, at least considering what I use and what my phone supports.  It looks different, especially the lock screen.  Voice input can now be used in several places, just about anywhere the on-screen keyboard can be used.  Apps seem to run faster, although I didn’t think to run an objective test before upgrading.

The upgrade process itself, however, was a pain.  I would like to point out that this is a Sprint issue, not an Android issue.

  • First of all, a program must be installed to flash the firmware, and it only runs on Windows.  As a Linux user, this is a very annoying but common problem.  Thankfully, I kept the manufacturer-installed version of Windows on my laptop available under dual-boot for just such an occasion.
  • Reboot, reboot…  I had to uninstall the USB driver and the previous upgrade installer.  Then I had to reboot, install the new upgrade installer, reboot again, then run the upgrade installer.  I actually had to find this out through experimentation because after following their instructions, the software wouldn’t connect to my phone.  Come on Sprint, this is 2010!  I shouldn’t have to uninstall old software and reboot multiple times.  Again, annoying, but common.  Reboots are a fact of life in Windows.  (In fact, my laptop often automatically installs updates and reboots when I boot into Windows, causing it to load Linux, my default in GRUB, if I’m not at the keyboard to catch it.  A few versions back, Windows got better about not rebooting so much for updates, but now it’s bad again in Windows 7.  But I digress…)  It was very telling that the software is listed in Add/Remove Programs under “Your Company Here”.
  • The upgrade erases everything.  All installed apps, all settings, completely wiped out.  Even if they’re not savvy enough to upgrade the OS without nuking everything, the least they could do is record which apps are installed, then afterward search the market and reinstall them automatically.  This is by far my biggest problem with the upgrade process.  The last time I upgraded, I lost one of my favorite apps because it disappeared from the market.  Re-entering all my settings is a pain, too.

In summary, Android 2.1 is good, and Sprint upgrade software is abysmal.

Color Vision v0.3 released

Version 0.3 of my Android app, Color Vision, has been released.  New in this version is the ability to calibrate the target position.

Creating an Android app for the color vision impaired

For those who don’t know, I’m partially colorblind.  It is only occasionally an issue, usually because of something being color coded.  Asking strangers what color things are is embarrassing.  (“Excuse me, is this pen red?”  Good thing I asked, though.  It turned out to be orange.)  It occurred to me that I walk around with a camera in my pocket all day, and I could use that to determine colors for me.

Diving into Android development was surprisingly easy.  Of course, I’m a veteran Java programmer, I’ve worked with embedded devices before, I already had Eclipse set up, etc.  The API needs more documentation, but that’s not a rare deficiency.  There are also a few random warts.  I started with a camera preview demo on Google’s web site.  When I forgot to add the camera permission to the app, I got an out of memory exception.  I’m glad I started with that demo, because this was a common issue and reported all over the web.  Otherwise, I don’t know if I ever would have figured that out.

Speaking of finding things on the web, the Android community seems to be quite healthy.  There are a lot of people, and many of them make contributions such as offering fixes or workarounds.

The SDK supports debugging over USB, but my computer’s USB is flaky.  I ended up copying the app to my website then downloading it on the phone.  I had to change a setting to install apps that aren’t from the market, but it was a simple checkbox.  Now I just open the file and it installs.

There is a particular issue I’ve run into dealing with the camera preview.  The only supported format (currently) is YUV420 (or 422, not sure which), which has a complicated way of packing pixel data into a byte array (compared with RGB, anyway).  The Android standard BitmapFactory is unable to parse this byte array, possibly because it lacks a header of any kind.  There is a bug report filed, but apparently the issue has yet to be addressed.  On the bug report (and elsewhere) there is code for converting YUV to RGB.  It seems to work on the emulator as far as I can tell.  On my phone (a Samsung Moment), though, the byte array doesn’t seem to be long enough.  For better or worse, I’ve resorted to taking a picture and pulling the data from it.

Overall, it’s been a positive experience.  There are certainly improvements that need to be made, but I was amazed at how quick and painless it was to throw something together and get a working prototype on a phone.  It also did not require buying a Mac, paying $99, and going through an approvals process just to be able to prototype an app, as the iPhone does.  You do have to pay $25 to be able to publish apps on the Android Market, but you can distribute your apps for free outside the market.  (Admittedly, your user base would be much smaller.)

I would like to thank Randall Munroe, author of the xkcd webcomic, whose color data is used in the app to convert RGB values to names.

The app (named “Color Vision”) is available for download now from the market and is also an open source project on SourceForge.

Diversions

I found some random cool stuff and thought I’d lump it together into a post.

First off, I have a couple of videos with music made from Windows sounds.  I like the music in this one better, but this one has what could be called choreography.

Betty White in a Metal Bikini Wielding a Flaming Chainsaw While Riding a John Ritter Centaur

Evil clown service

Green Eggs and Hamlet

Not cool, but interesting: health insurance companies invest in fast food and tobacco.

Linux pipelines

I recently had a Subversion working copy with several files that were modified because they should not have been under source control to begin with.  I had to find all the modified *.Plo files and remove the folders that contained them.  I ended up using this, which is the longest pipeline I can remember using:

svn st|grep '^M.*Plo$'|awk '{print $2}'|xargs -n 1 dirname|uniq|xargs svn rm --force

To break it down:

  1. svn st: List all local changes
  2. grep ‘^M.*Plo$’: Find modified *.Plo files
  3. awk ‘{print $2}’: Strip off the “M” (modified) flag
  4. xargs -n 1 dirname: Find the directory name of each file
  5. uniq: Remove duplicates
  6. xargs svn rm –force: Remove the folders

Since the folders containing the *.Plo files were all named .deps, I could’ve accomplished the same thing with this, but I didn’t think of it until after:

find -name .deps|xargs svn rm --force

Anyway, yay pipelines!