Film Review : Fantastic Mr. Fox

April 11, 2010 – 12:00 pm

Mr. Fox was a world class chicken thief but gave it all up years ago for a quiet middle-class existence when his child was born. He still yearns for some old-time larceny and naturally jumps at the chance for one last big score. Things do not go entirely to plan and Mr. Fox’s grand schemes may have grave consequences for his family and friends. Is Mr. Fox as fantastic as he believes himself to be?

Fantastic Mr. Fox is on the surface a fairly straight-forward children’s tale of talking animals vs mean humans. But director Wes Anderson infuses the story with his own brand of obsessive observation and the result is a mixed if enjoyable bag. The stop-motion animation is a throwback to the stiffly animated kiddy shows of the past such as the old Wind in the Willows show – it makes Wallace and Gromit look positively realistic. I found this to be charming, but heaven knows what anyone born after 1990 would think of it.

Some of the scenes may be a little talky for young children and a lot of the humour is wry rather than slapstick gags. Also, identifying with the main character is fairly hard, the film does not shy away from showing Mr Fox’s flaws. This makes for an interesting plot, but I doubt we will see Mr Fox’s face showing up on kids lunch boxes.

It is actually quite hard to see who Fantastic Mr. Fox is aimed at – it doesn’t have the universal appeal of Wallace and Gromit, nor can I imagine teenagers really liking it. If I was paranoid I would suspect it was aimed directly at me.

Recommended if you like this sort of thing.

A Better Boost Book

April 2, 2010 – 2:58 pm

Boost is a excellent resource for C++ programming, but suffers from inconsistent documentation and a daunting array of sub-projects. Trying to make sense of it all is a fairly serious undertaking. I tried to get my head around it by writing my occasional series of boost blog posts, but now I see that somebody has done a much better job.

The Boost C++ Libraries is a free book that clearly explains some of the more generally useful boost libraries, with lots of useful examples. It even covers advanced libraries like ASIO in an approachable way. I highly recommended bookmarking it if you do any C++ programming.

TV Theme Quiz

March 30, 2010 – 10:20 pm

I have made a new TV Theme Quiz, so much more elaborate than the last few that it deserves its own page.

Start the TV Theme Quiz

The last quizzes were solved too quickly, so this one is extra difficult although none of the shows are really that obscure. Feel free to leave any questions or comments here.

The HTML5 audio tag

March 30, 2010 – 10:13 pm

I have been mucking around with the tag as part of my quest to understand where HTML5 is going. The <video> tag gets all the press but I think there are many more opportunities to use audio in web apps. HTML5 is closing the gap between plugin-based apps (Flash, Silverlight, Java, etc) and sound support is an important part of that goal.

(Those of you who don’t care how it works should go directly to the TV Themes demo puzzle. It works best in Firefox3.6 and the latest version of Safari, although most browsers should function to some degree.)

The audio tag is pretty flexible, able to handle both long form audio (songs and spoken passages – the theme medley on the demo page for example) and short snippets of background audio (alerts, and confirmations – the demo plays one of two short tones when you type an answer. Video game sound effects are another example.) Optionally, the audio tag can provide a user interface for starting and stopping the audio, useful for playing long streams of audio. Different browsers have different ideas about how this should look, but they all function much the same way.

In theory, the audio tag is as easy as embedding an image into HTML:

1
2
3
4
<audio controls>
	<source src="music.mp3">
	You can put HTML here that will be displayed if the browser does not understand the audio tag
</audio>

However, the devil is in the details. There are two problems with the audio tag that complicate matters. The first is that only the very latest browsers support the audio tag at all. This means that if you want to provide audio that everyone can use, you are going to have a fall-back method available. Before the audio tag, people used to use Flash for this purpose and it still works. A number of sites provide simple Flash-based audio players that you can embed – I ended up using the player provided by Google.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<object codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" height="27" width="400" align="middle" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">
	<param name="_cx" value="10583"><param name="_cy" value="714"><param name="FlashVars" value="">
	<param name="Movie" value="http://www.google.com/reader/ui/3247397568-audio-player.swf?audioUrl=http://full/path/to/music.mp3">
	<param name="Src" value="http://www.google.com/reader/ui/3247397568-audio-player.swf?audioUrl=http://full/path/to/music.mp3">
	<param name="WMode" value="Window"><param name="Play" value="0">
 
	<param name="Loop" value="-1">
	<param name="Quality" value="High">
	<param name="SAlign" value="LT">
	<param name="Menu" value="-1">
	<param name="Base" value="">
	<param name="AllowScriptAccess" value="never">
	<param name="Scale" value="NoScale">
	<param name="DeviceFont" value="0">
	<param name="EmbedMovie" value="0">
 
	<param name="BGColor" value="">
	<param name="SWRemote" value="">
	<param name="MovieData" value="">
	<param name="SeamlessTabbing" value="1">
	<param name="Profile" value="0">
	<param name="ProfileAddress" value="">
	<param name="ProfilePort" value="0">
	<param name="AllowNetworking" value="all">
	<param name="AllowFullScreen" value="false">
 
	<embed type="application/x-shockwave-flash" src="http://www.google.com/reader/ui/3247397568-audio-player.swf?audioUrl=http://full/path/to/music.mp3" allowscriptaccess="never" quality="best" bgcolor="#ffffff" wmode="window" flashvars="playerMode=embedded" pluginspage="http://www.macromedia.com/go/getflashplayer" height="27" width="400" />
</object>

Not exactly elegant, is it? Apart from being uuuuug-ly, the full URI of the sound file must be used (the audio tag can use relative paths). Also, the Flash players are not scriptable in the same way as inbuilt audio tag is, which can make doing tricky stuff like animating other content in response to the audio more difficult.

The second problem with the audio tag is the same codec problem I talked about in a previous rant (The HTML5 Video Tag’s Fatal Flaw) For legal reasons, different browsers play different formats of audio – most notably Firefox will not play mp3s while Safari will not play ogg. There is no single format that will play in all browsers except for uncompressed wavs, which are too fat to be useful except for very short snippets.

To get around this problem the audio tag allows multiple files to be specified. The first file that the browser thinks it can play will be used, but it does mean you have to encode and store multiple versions of each audio file.

1
2
3
4
5
<!-- Only one of these files will be downloaded -->
<audio controls>
	<source src="music.ogg" type="audio/ogg">
	<source src="music.mp3" type="audio/mpeg">
</audio>

The demo page also uses the audio tag to play sound effects in the background, using audio elements that do not have a user interface. For simplicity I used wav files (download from this awesome source of free effects.) Since they have no user interface, Javascript must be used to play them:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<audio id="clicksound" preload="auto">
	<source src="click.wav" type="audio/wav">
</audio>
 
<script type="text/javascript">
function playSound( )
{
	var a = document.getElementById( "clicksound" );
	if ( !a ) return;
	if ( !a.play ) return; // will exit if the browser does not understand the audio tag
 
	a.play();
}
</script>

It is all pretty simple but as always there are problems. I did not find a good way of replicating this using Flash, so browsers that do not understand the audio tag do not play these background noises. Also, Google Chrome (which has otherwise excellent support) contains a weird bug that prevents it playing the first couple of seconds of an audio file, making it useless for short sounds. Apparently Firefox3.5 had the same problem, but it works perfectly in 3.6.

I created the demo to see if the audio tag could replicate the functionality of Flash-based applications for both long-form audio and background sound effects. It does seem to be possible provided you are targeting a modern browser and are prepared to work around certain annoyances. Hopefully the next few years will see an improvement in support for audio, I can see many uses for it especially if the iPad (which does not support Flash) takes off.

Opening Lyrics Quiz II – The Quickening

March 14, 2010 – 12:00 pm

Below are the lyric fragments of 16 opening (or closing) themes to “classic” TV shows for you to guess. Feel free to post your answers in the comments, using Google is cheating…

  1. Baby, if you’ve ever wondered,
    Wondered whatever became of me,
    I’m living on the air in Cincinnati…
  2. Friendly faces everywhere
    Humble folks without temptation.
  3. I won’t go
    I won’t sleep
    I can’t breathe
    Until you’re resting here with me
  4. Yes, no, maybe
    I don’t know
    Can you repeat the question?
  5. The Earth began to cool,
    the autotrophs began to drool,
    Neanderthals developed tools,
    We built a wall – WE BUILT THE PYRAMIDS!
  6. Come and listen to a story about a man named Jed
    A poor mountaineer, barely kept his family fed.
  7. Love, exciting and new
    Come aboard, we’re expecting you.
    Love, life’s sweetest reward.
    Let it flow, it floats back to you.
  8. Here we come
    Walking down the street
    We get the funniest looks from
    Everyone we meet.
  9. Every stop I make, I make a new friend,
    Can’t stay for long, just turn around and I’m gone again
    Maybe tomorrow, I’ll want to settle down,
    Until tomorrow, I’ll just keep moving on.
  10. Making your way in the world today takes everything you’ve got.
    Taking a break from all your worries sure would help a lot.
    Wouldn’t you like to get away?
  11. In West Philadelphia I was born and raised
    On the playground is where I spent most of my days.
    Chillin’ out, maxin’, relaxin all cool,
    And all shootin’ some b-ball outside of the school.
  12. Hey baby, I hear the blues a’calling,
    Tossed salad and scrambled eggs
    And maybe I seem a bit confused,
    Yeah maybe, but I got you pegged!
  13. No matter what the odds are this time,
    Nothing’s going to stand in my way.
    This flame in my heart and a long lost friend
    Gives every dark street a light at the end.
    Standing tall on the wings of my dream.
    Rise and fall on the wings of my dream.
  14. Hold me in your arms
    Don’t let me go
    I want to stay forever
    Closer each day
  15. If you want to, I’ll change the situation
    Right people, right time, just the wrong location
    I’ve got a good idea, just you keep me near
    I’d be so good for you
  16. Out here in the fields,
    I fight for my meals
    I get my back into my living.
    I don’t need to fight
    To prove I’m right
    I don’t need to be forgiven.

Using Exceptions in C++

March 8, 2010 – 11:40 pm

C++ is big – it has been said that any given programmer only ever uses about 40% of the language’s features. The trouble is that it is a different 40% for each person. Exceptions are a great example of this, some people swear by them while many coding standards specifically ban/discourage them (cf: google, mozilla). It is ironic that a feature designed to make code safer is sometimes regarded as being too dangerous to use.

Personally I like exceptions, but even I realise that they have there limitations. This post is an attempt to formalise some guidelines about when exceptions should be used and when they should be avoided beyond the usual language rules. I should mention at this stage that most of my experience is in desktop client/server software. C++ is used in all sorts of places these days, and what works on desktops and beefy servers may not suit the embedded world (for example).

When to Catch

My rule of thumb is “Do not let exceptions escape from a function you didn’t explicitly call yourself“. This includes destructors, callbacks, thread functions, WNDPROCs, and any other miscellaneous way your functions can be entered (it does not include constructors or virtual functions – exceptions are very useful in those). In general, all these things should catch and handle all exceptions.

Your program will die if an exception escapes a thread. If you are lucky your runtime will do something clever and your program will die painlessly but possibly the OS will have to dispatch the process messily. Either way, your users will not be impressed, so you should always wrap thread functions in try{}catch blocks. In the best case you might be able to signal that an operation failed to the main thread, which can restart it if required. In the worst case you can at least log what happened before exiting.

Lots of third part libraries communicate with your code using callbacks that you supply. You should always ensure that any exceptions are caught before returning back into third party code, since you can never be sure if the library does the right thing. C style libraries like LibCURL are right out, they will probably leak handles and memory as the stack is unwound. C++ libraries may (or may not) be better but could do things you do not expect, like swallow the exceptions themselves instead of letting them fall through (boost::iostreams). Also, some libraries actually call you back on a different thread (boost::asio) so the advice in the previous paragraph also applies here.

You should always be prepared to catch any exceptions that are documented by any C++ libraries you use, especially things like boost::filesystem which can throw at any time.

What to Throw

My advice is to create a small hierarchy that is derived from std::runtime_exception unless you are already using a custom exception class. Don’t try to get clever and throw std::string or char*. Design your hierarchy around how the exceptions are to be handled, rather than what can go wrong. For instance, if there are 5 different ways your program throw exceptions, but only 3 different things that can happen in response then you only need 3 types of exceptions.

In my experience, exceptions fall into two categories: recoverable and fatal. Recoverable exceptions will be caught within a layer, or perhaps the next layer up which can then reattempt the operation (or perhaps just log and ignore the problem if the operation was not crucial.) Fatal errors are usually not caught until the outer loop of the program, where they are logged before the program can be shutdown cleanly. In general, the exceptions you expect to recover from should derive from exceptions you expect to be fatal.

I tend to name my exception types based on how they are handled and what circumstance they represent, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
class UnrecoverableFileReadException : public std::runtime_error
{
	// thrown when a file cannot be read correctly, ie: the file exists but is misformatted
public:
	UnrecoverableFileException( const std::string& msg ):std::runtime_error(msg) {};
};
 
class FileReadException : public UnrecoverableFileReadException
{
public:
	// thrown when a file cannot be read but the user can be prompted to select another file
	TemporaryFileException( const std::string& msg ):UnrecoverableFileReadException(msg) {};
};

When throwing, always construct the exception with a sensible message if only for logging and debugging purposes. Normally this message would not be shown to the user since it will be hard to localise. Add additional members to your exception class if you want to include other information with the exception. Just remember that exceptions must be copyable.

1
2
3
ostringstream oss;
oss << "Could not open file " << filename << " the error was: " << errno;
throw TemporaryFileException( oss.str() );

If you really want to go the whole hog consider using boost::exception which builds upon similar ideas.

When to Throw

Exceptions should never be thrown if everything is running perfectly. Only when something goes wrong should throwing an exception be considered an option. I have seen code that threw exceptions to signal the end of an enumeration or to signal that the results of a query were empty – I consider these examples to be a terrible use of the feature. Remember that an exception is not just an error, but something really outside of normal program flow.

I also tend not to use exceptions for logic errors. If an algorithm can fail then the calling code should be prepared to handle a return code signalling an error. Likewise I would not throw if a database query returned no results – an empty result set is inside the bounds of normal program flow. However, I would consider throwing an exception if the query failed due to the database being unavailable.

The best places to use exceptions are in situations where your program is using resources that are not under your control, including anything to do with IO. Both files and network connections exist outside of your program and can become unavailable at any point due to any number of reasons. In many cases the problems are transient and all your program needs to do is try again in a few minutes – a program that quits each time the DNS, directory server, or external database cannot be reached will not survive for very long in any production environment. Exceptions allow you to back out of an operation without too much trouble and handle the problem in a sensible location.

One problem you might encounter is that is often hard to retrofit exceptions into code that wasn’t designed for them. In this case, my advice about not letting exceptions fall through 3rd party code also applies to legacy code that you own. This is not to say that you cannot use exceptions at all, just that you may have to take steps to keep exception handling within the layers of your application.

A Trailer for Every Academy Award Winning Movie Ever

March 6, 2010 – 8:01 pm

It’s Oscar season; presented for your consideration:

Personally I think it is a shame they missed out the female lead’s gay male best friend who gives invaluable relationship advice and it really needs at least one dog.

Mobile Safari Does Not Support Flash (and Never Will)

February 16, 2010 – 10:35 pm

Listening to some people, the lack of Flash on the iPhone/iPad is some sort of crime against nature. There are numerous complaints about it online – all bemoaning the inability to play their favourite Flash games or view video. These complaints miss the point entirely – there are two simple reasons why Flash will never be supported in Mobile Safari.

The first reason is simple – Steve Jobs is a jealous God and thou shall have no other Gods besides Him. Apple created the App store so they would control the single way of getting software onto the device, being able to load a flash file from a browser completely circumvents this control. Simple.

Now we have that out of the way, we can move onto the second, more interesting, reason.

Flash would suck on the iPhone.

Lets talk about Flash video first. Most video sites use a custom Flash wrapper to display video in a sub-frame of the browser, with controls to zoom the video to full screen. The sub-frame is usually of a fixed size (640*360, etc) and surrounded by additional HTML (ads, links to other videos, etc). Straight away you should see the problem – the video is already bigger than the iPhones screen. Mobile Safari does an excellent job of resizing web pages, but that is going to leave you with a postage stamp sized video with even smaller controls. Going fullscreen may be a solution, if you can mange to tap the tiny button, but then you are not really using Flash as part of a web page anymore.

Back in the day (about 5 years ago), Flash video was a step above anything else on the web due to its widely deployed and not-too-bad codec. These days Flash is just a none-too-convenient way of displaying standard h264 files which the iPhone can play natively. Most of the big video sites have realized this and just serve the raw file to iPhones instead of trying to wrap it in a custom player, to the benefit of everyone.

(Drifting slightly off-topic for a moment, I imagine the use of Flash as a video player will start to decline even of desktops now that HTML5 is here with its useful <video> tag.)

Now lets talk about Flash games – Tower Defense, Crayon Physics, room escape puzzles, etc. I love them, you love them, everyone loves them. There is just one problem – none of the thousands of existing games would work on the iPhone even if Mobile Safari supported Flash perfectly!

The iPhone doesn’t have a keyboard, so most arcade-type games are right out. Even games that exclusively use the mouse would have problems since tapping your finger on the screen is much, much less precise than using a pointer. In addition, on the iPhone you effectively have multiple pointing devices – how would current Flash apps handle that?

For a quick demo of why sites like newsgrounds will never work on the iPhone, resize your browser window to 480*320 (or 320*480 since that is more usual) and visit your favourite gaming site. Now set your mouse pointer to a big white blob instead of an arrow to simulate tapping with a large figertip. Remember to stop playing after 45 minutes to replicate the battery drain. See how much fun you have.

UIButton.titleLabel is not as useful as it looks

February 13, 2010 – 12:53 pm

I have been doing some iPhone development lately. Nothing too amazing, just some test apps to get a feel for the system. Now, some people will tell you that Cocoa Touch is an API sent from God and frankly it is pretty good (especially given what passes for UI on other embedded devices), but that doesn’t mean it doesn’t have some annoyances.

Here is something that tripped me up for a while. The UIButton class has a property called titleLabel which (obviously) returns the UILabel that is used to display the text of the button. You can use this property to modify the parameters of the label, like so:

1
2
3
m_addButton.titleLabel.font = [UIFont systemFontOfSize: 7];
m_addButton.titleLabel.textColor = [UIColor blackColor];		 
m_addButton.titleLabel.textAlignment = UITextAlignmentRight;

What you can’t do is this:

1
m_addButton.titleLabel.text = @"Add Stuff";

Although nothing I have found in the documentation says so, the text of the button cannot be set from the titleLabel property. What you have to do is this:

1
[m_addButton setTitle:@"Add Stuff" forState: UIControlStateNormal];

Setting the title this way works, and has the advantage that you can specify different text for different states:

1
2
[m_addButton setTitle:@"Add Stuff" forState: UIControlStateNormal];
[m_addButton setTitle:@"Add Stuff (not now)" forState: UIControlStateDisabled];

This is perhaps not that interesting for text titles, but is an excellent way to control the image the button shows based on whether the button is enabled, highlighted, and/or selected.

The Aliens Rap

February 3, 2010 – 12:25 pm

Following up on the epic 10 minute rap summary of Robocop, the same team has released Aliens:


open at youtube.com

Watching this reminds me what a great film Aliens turned out to be and how Avatar (by the same director) pales in comparison. It’s not that Avatar was terrible but nobody is going to be making 10 minute rap songs about it in 25 years.

quick update: Hey, they’ve done Terminator 2 as well!

Film Review : Avatar

January 3, 2010 – 9:03 pm

James Cameron has always been an interesting film maker. Although on one level most of his films could be classified as pulpy genre-related fare, they usually have a more interesting subtext lurking below the explosions. Previous Cameron films have investigated such themes as mother/daughter relationships, humanity’s fear of the unknown, musings on fate and predestination, and whether it is morally acceptable (and perhaps even admirable) to slum it with a good looking lower class boy for a few weeks before you get married even though an ocean liner might not be the best place to do so. So it is with a heavy heart that I have to say Avatar is a slight disappointment.

avatarThe planet of Pandora (Who names these planets? What were they thinking?) has some stuff that humans want to mine. Unfortunately, the best place to get it is right on top of where the indigenous population (8 foot tall skinny blue people called the Navi) live in harmony with their world. The Navi are distrustful of the humans, so in order to investigate the Navi a bit more, the humans create the titular avatars – mindless Navi bodies that certain individuals can “drive around” remotely. The main character is just such an individual, and he (or his avatar) quickly becomes involved in the local tribe. Although the humans would prefer that the Navi move on without violence, it is clear that a military solution, led by a crazed marine, might be more expedient…

It is almost impossible to spoil anything about Avatar’s plot, no doubt you have already guessed the direction it which it unfolds. It is a shame that for all the risks involved in making what is apparently the most expensive movie ever made (it certainly looks like it), the story is as safe as an after-school special. The film could have made some interesting points about colonialism, or environmentalism, the military, or even feminism, but instead chooses to unspool a conventional yarn where the good guys are selfless and the bad guys are crazy and evil. It is not that is it a bad story per say, just something we have all seen many times before.

I saw Avatar in 3D, it is by far the best 3D experience so far. The lush jungles and mist-shrouded peaks of Pandora look amazing – Avatar is simply the greatest visual treat I have ever seen. The contrast between the sharp grey lines of the human base with the colourful, glowing environment outside is very well rendered. James Cameron has always been interested in portraying technology and Avatar is no exception – a nice touch is that all of the displays that the humans use during the movie are also in 3D. There are a thousand little details like that I loved about Avatar, it is just a shame that the whole thing isn’t as great as the sum of its parts. However, anyone who shares Cameron’s love for helicopters and giant robots and things being blown up by helicopters and giant robots will be thrilled.

Highly recommended if you can see it on the big screen in 3D. Otherwise only recommended if you like this sort of thing (but who doesn’t?)

The Phantom Menace Was Not a Very Good Movie

December 17, 2009 – 5:40 pm

I own all of the Star Wars DVDs except for one – The Phantom Menace. Even the weakest of the others have a certain charm, but TPM was stupid through and through. Even the title is stupid! I have yet to work out exactly what the titular menace actually was. Although the story includes several menaces, none of them seem particularly phantomastical. Unless the menace was supposed to be Palpatine’s amazingly convoluted plan, but that plot point doesn’t really bear fruit until the second film.

Anyway, I haven’t given The Phantom Menace much thought since it first came out but this guy certainly has:




Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7

Even if you ignore the affectations of the reviewer, he has some pretty insightful points about how TPM fails as a movie.

The C++ Boost Libraries Part 6 – boost::any

December 6, 2009 – 3:41 pm

In C++ if you have a variable that you say is of type “Person” (for instance), you can be fairly certain (more or less) that it always actually contains a Person (or perhaps a subclass of Person. If you have a container of Persons, then you know (more or less) that every member is also a Person (or a subclass).

This is all very good, prevents a lot of runtime errors, and generally makes C++ a great language if you care about correctness. But sometimes, very rarely, you actually want to store a whole bunch of messy, unrelated types in a container without trying to ram them into some sort of class hierarchy. Parsers are a good example of this. It is often convenient just to chuck tokens of various types into a data structure for later processing without worrying too much about the specific type (string, int, float, etc).

boost::any is a small class that can hold values from almost any type, designed for just such messy applications. Using boost::any is very simple:

1
2
boost::any a1 = std::string("Moose");
boost::any a2 = 6;

Of course, getting the values back again is a little harder.

1
2
3
4
5
6
7
8
9
try
{
	std::string v1 = boost::any_cast< std::string >(a1); // this works, a1 is a string
	std::string v2 = boost::any_cast< std::string >(a2); // nope, will throw an exception at runtime
}
catch ( const boost::bad_any_cast& e )
{
	// tried to any_cast into something that wouldn't go
}

Of course, you can query a boost::any for the typeid of the stored object. Just don’t do it when Scott Meyers is in the vicinity.

1
2
3
4
5
std::string v;
if ( a1.type() == typeid(std::string) )
{
	v = boost::any_cast< std::string >( a1 ); // this should never throw, since we checked first
}

A single boost::any is perhaps not that useful, but a container of them can store almost anything we want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
typedef std::vector< boost::any > AnyVector;
AnyVector values;
 
values.push_back( 5 );
values.push_back( std::string("Hello") );
values.push_back( 5.3 );
 
try 
{
	for ( AnyVector::const_iterator p = values.begin();
			p != values.end();
			++p )
	{
		if ( p->type() == typeid(int) )
			cout << "Int = " << any_cast<int>(*p) << endl;
		else if ( p->type() == typeid(std::string) )
			cout << "String = " << any_cast<string>(*p) << endl;
		else 
		{
			cout << "Unhandled type: " << p->type().name() << endl;
		}
	}
} 
catch ( const boost::bad_any_cast &e )
{
	cout << "Bad any_cast<>" << e.what() << endl;
}

Any type that you put into a boost::any must be copy constructable (the any makes a copy, not a reference). You also have to make sure that its destructor doesn’t throw (but of course you do that anyway!)

Although I wouldn’t recommend boost::any for everyday use, it does come into its own when the only alternative is a huge class structure or (even worse) void *s.

Python and The Very Slow Server

November 28, 2009 – 2:23 pm

I don’t usually do a lot of Python programming, but I always enjoy it when the opportunity arises. Python is in no way a “clean” language, it has all sorts of warts and limitations that mean that it tends to not get used for big projects. Despite this (or maybe because of it), Python remains my go-to language for Getting Small Things Done Quickly. It is impossible to overstate the utility of just being able to start coding a function by bashing away at the python console – nothing else has given me the same sense of instant gratification since I started programming in BASIC back in the 80s.

The other big advantage of Python is the useful utility libraries that come with it as standard. Want to send twenty thousand emails? Just import smtplib. Want to generate code based on data from a spreadsheet? Import csv and away you go. Need a file that is exactly 32Mb is size? No problem. These are real examples from my job where Python has saved me many hours.

The most recent use I have put Python to is a slow server. For various murky and uninteresting reasons I need a rate-limiting HTTP server, one that I can easily control the speed at which it sends data. Enter Python’s very handy BaseHTTPServer module, which allows you to create custom HTTP servers with only a few lines of code by subclassing a request handler. Although the BaseHTTPServer is fairly useless for serving real files, it is perfect for this type of thing since it does all the boring work of parsing headers and returning status codes.

I don’t care about the contents of the data, just its size and how long it takes to serve. Since I will be varying these parameters a lot, I decided to make them part of each request so that each request could take a different amount of time – this means I don’t have to restart the server between each test run. Modifying the code to serve actual file data would be very simple.

I enjoyed writing this server so much that I regret that it didn’t take longer. Now I actually have to use it for its intended purpose, which I can assure you is not going to be as pleasant.

Here is the complete Python source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# A very simple HTTP server designed to for testing situations where the data returned
# is not important but the rate at which it comes down is. This server can be started
# using the command: python delayserver.py
#
# Once started, it will listen for requests on port 8000
# Requests should be of the form http://<address>:8000/size=<bytes>,duration=<seconds>
# where: <bytes> is the size of the response data
# and    <seconds> is how long you want it to take (at minimum, it may take longer)
#
# Notes:
# * The timing is pretty inaccurate for small byte sizes, this isn't a problem for
#   what I need it for
# * Press ctrl-c to stop serving
 
 
import time
import BaseHTTPServer
 
class MyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
	def do_GET(self):
		request = self.path.strip("/")
		duration = 1
		size = 1024
 
        	validRequest = False
		params = request.split(",")
		for p in params:
                    temp = p.partition("=")
                    if (temp[0] == "size"):
                        size = int(temp[2])
                        validRequest = True
                    elif (temp[0] == "duration"):
                        duration = int(temp[2])
                        validRequest = True
 
                if (validRequest == False):
                   self.send_error(404)
                   return
 
                self.send_response( 200 )
                self.send_header( "Content-Length", str(size) )
                self.send_header( "Pragma", "no-cache" )
                self.end_headers()
                self.slowWrite( self.wfile, size, duration )
 
 
        def slowWrite(self, output, size, duration):
                bytesWritten = 0
                startTime = time.time()
                while ( bytesWritten < size ):
                        now = time.time()
                        if (duration != 0):
                                desiredBytes = ( (now - startTime) / duration ) * size
                        else:
                                desiredBytes = size
                        desiredBytes = min( size, desiredBytes )
                        if (desiredBytes < bytesWritten ):
                                time.sleep(0.2)
                        else:
                                while (bytesWritten < desiredBytes):
                                        output.write('A')
                                        bytesWritten = bytesWritten + 1
                                output.flush()
                now = time.time()
                self.log_message( "Request took %f seconds",   now - startTime  )	
 
if __name__ == "__main__":
    http = BaseHTTPServer.HTTPServer( ('', 8000), MyHTTPRequestHandler )
    print "Listening on 8000 - press ctrl-c to stop"
    http.serve_forever()

I should point out that I am by no means an expert at Python, so take this code with a pinch of salt.

Pacman

November 27, 2009 – 11:00 am

Remember kids – winners don’t do drugs.