Woot-Off Detector StreamReader Bug

So today was a Woot-Off.  The detector sort of worked – when I plugged it in, it quickly detected and turned on the lights.  However, after a new product it completely stopped working and was throwing IndexOutOfRange and OutOfMemory exceptions.  Turned out that there are some weird limitations in the StreamReader class.  Originally the code would continue to ReadLine() until there was nothing left to read.  However, some of the lines in the RSS feed were extremely long and would blow the buffer and throw.  I then rewrote the code to just use Peek() and Read() one char at a time to fill a 256 char array.  This also started to throw an IndexOutofRangeException on subsequent Peek() calls; the problem here was the internal buffer is 512. For some reason that I haven’t figured out yet, that 512th Read() caused the problem. Ok, now what?

After reading the documentation, I found this interesting tidbit:

If the current method throws an OutOfMemoryException, the reader’s position in the underlying Stream object is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded.

Hmmm.  So basically, if I hit an OOM then I should be able to just continue to read from the stream from where ever it blew chunks. And this is fine because the liklihood of this barfing on a line that I care about (i.e. <woot:product> or <woot:wootoff>) is damn near close to nil.  I gave it a shot and it seems to be working fine now.

Too bad I didn’t figure this out earlier.  We could have used this today. 😦

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Woot-Off Detector StreamReader Bug

  1. Dm3281 says:

    Awesome. Can you post some schematics and parts. How did you interfc with usb

  2. nitdoggx says:

    @Dm3281: Sure thing. I’ll have a post with instructions and schematics. But briefly, the Woot lights just use +5v to power on, so there’s no data involved. I simply use digitial Pin0 to switch a MOSFET to ground the USB when a woot-off is detected, thus turning on the lights. While I work on instructions and schematics, you can check out the annotated pictures on http://www.flickr.com/photos/nitdoggx/5197552396/in/set-72157625317518253/.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s