Thursday, January 22, 2009

An XBox, A Fried Router, and Girl Scout Cookies



About six months ago I bought a Linksys WRT54G to replace my DLink wireless router. The DLink was a horrible POS, with poor signal strength, inexplicably bad bandwidth and requiring power-supply reboots once every few days or so. It never learned to play nice with the iBook (Crappy network stack? I'd like you to meet broken hardware design...). So while walking down the aisle of Office Much one day I thought "Hey, I'll pick up a linksys and put Open WRT on it! Then I'll be a hero hardware hacker for real and I won't have to hear my sweethart cursing a frozen connection as she's trying to meet a publishing deadline anymore..."

Well, not suprisingly, it sat in the box on my desk for months. Turns out, the new ones are all Version 8 which has only 2MB of flash memory & replacing the firmware is problematic. Ugh. So last weekend I finally decided to give it a go with the original (shipped) software.

10 hours later, I had it working, sorta. It has something called "router" mode, but I was unable to get it to work anything like what I would have thought was a router. It was more like a wireless hub with encryption. But OK, it worked. I had the laptop, the SqueezeBox, the XP box and a linux box all on the subnet. Thought I was home free.

The XBox couldn't see it. No way. Reboot everything, move antennae, wave the bloody chicken. Nothing. My SSID was not on the list. I didn't even get the chance to struggle with the firewall, it just wasn't there.

Now, it's the Sunday before the Martin Luther King holiday. My 13-year-old has the next day off and is expecting his friends to come over to play on XBox live. He's getting worried. "Dad, the XBox connection is still down." With lightning-fast fingers he runs through the XBox menus and diagnostics, pointing out with eerie precision where the problem occurs, that the SSID is not appearing, etc. I've tried everything to no avail. I'm totally disgusted at having wasted half my weekend on this stupid project.

Finally, right about dinnertime I decide to give up. I have another NIC on the server, I can run both routers and dedicate the DLink (which I know worked) to the XBox. I plug the network cable and power supply in and a moment later... PWAF! Theres a loud popping sound and a curl of white smoke is coming from the DLink. I yank the cords and sure enough, I put a 12 volt supply into the 5 volt device. The stench of ozone and melting plastic wafts through the room. I grab the box and walk down the hall towards the front door thinking to toss it outside so at least we don't have to breathe whatever evil fumes are coming off it.

At that moment the doorbell rings. I open it, the smoldering router still in my hand and there stands the neighbor's 9-year-old daughter. "Would you like to buy some Girl Scout Cookies?"

Completely flustered, I answer somewhat irritatedly: "I'm sorry, but this is really not a good time."

Her lower lip trembles. "I'm... sorry..." she says and runs away.

Upstairs I can hear my own kid calling. "Dad, are you coming to dinner? And what's that smell?"

Later, as I explain to kidlet why XBox Live is still not working and it may be a few days I experience the final humiliation: he's all noble about it. "That's OK dad. I spend an awful lot of time on the XBox and you're right, I could read or do something else instead. And you've already spent a lot of time working on this. Just relax."

Now I am truly the World's Lamest Dad. My kid's taking care of me. I officially give up and go to dinner.

Days later (Wednesday, actually) I'm chatting with the network guru at work and tell him the story. He's puzzled, having an XBox himself, and goes off at lunch to do a bit of searching. That afternoon he comes back and shows me this link: http://nowpa2.bravehost.com. In short, Microsoft's wireless adapter for the XBox 360 does not support WPA2. The packaging and manual lie. I go home, knock the encryption down to WPA Personal and presto, up she comes.

All's well that ends well, I suppose. The only problem I have now is what to do with a case of Girl Scout Cookies. Oh, and I'd like my weekend back, please?

Friday, January 16, 2009

Testing Values Returned From ActiveRecord



Be really careful about testing values returned from the db by ActiveRecord. How you fetch it affects the data type. ActiveRecord typecasts the column values returned from the database... sometimes.

This bit me hard today:

If you get a model back from active record e.g. with a MyClazz.find() ActiveRecord::Base will read the dictionary tables and typecast the returned attribute to what you expect. If you use one of the connection class methods, it doesn't do this, it just returns everything as a string.
Comparisons to integer, for example, will FAIL.

Watch this:


# run the console

[chrisa@ibs-chrisa-ux1 lims_m4]$ script/console
Loading development environment (Rails 2.1.1)

# fetch an arbitrary slide

>> s = Slide.find(81247588)
=> #<slide id: 81247588,
slide_group_id: 81246827,
...>

# now, I'm expecting an integer for slide_group_id, so I test it with the == operator:

>> s.slide_group_id == 81246827
=> true

# how nice, principle of least astonishment and all...
# now let's get that same record using the Base.connection method:

>> hash = ActiveRecord::Base.connection.select_one("SELECT * FROM slides WHERE id = 81247588")
=> { "id"=>"81247588",
"slide_group_id"=>"81246827",
...}
>> sgi = hash['slide_group_id']
=> "81246827"

# Whoa. It's a string:

>> sgi == 81246827
=> false
>>