Archive for December, 2009

Amazon’s Elastic Compute Cloud for Low-Use Game Hosting

Thursday, December 31st, 2009

My friend Dan and I enjoy playing first-person shooters, where it’s just the two of us going head-to-head in a game.  It’s something we’ve been doing off and on since the days of serial cables connecting two Commodore Amiga’s in one room.  Dan has since moved across the country from me, but the growth of the Internet and gaming technology has allowed us to keep the gaming going, even if it involves a bit more complexity now.  A few years ago, I was looking for an inexpensive way to host a game server that would have a reasonably low and equivalent latency for both of us.  At the time, the virtual hosting services I looked at were running about $25/mo, which isn’t bad but seemed like overkill, given that we’d only be using the server once a week or so (late-night Friday sessions are the norm for us).

As luck would have it, about this time Amazon introduced their virtual hosting/cloud computing service called the Elastic Compute Cloud (EC2).  It allows users to create an image for a virtual server that can be activated and shut down easily, with service charges at $0.10/hour for a running instance plus a bit for data transmission.  When the instance is not running, the user is only charged for data storage of the image on Amazon’s Simple Storage Service (S3), at $0.15/GB per month (charges listed here may vary in the future or over different configurations).  So with several gigs of storage and running a few hours each week, this is a much better deal than paying for an always-running virtual server.

One annoying thing when EC2 first became available was that you couldn’t make changes to data stored with the instance.  Building and provisioning an instance would take a few hours.  After that, it could be started and stopped within minutes, but any changes would be lost between boots.  If you wanted to upload a new map for your game, you’d have to rebuild the instance.  Within the last year or so, Amazon introduced Elastic Block Storage (EBS).  This allows a user to create a volume within S3 that can be mounted by an instance at boot time.  Any changes on that volume are permanent.  Snapshots of the volume state can also be made, allowing for recovery if the volume data becomes corrupted or you need to return to a previous state of the volume.  Of course, you do have to pay for the data space used by these volumes and snapshots, but the pricing, as I indicated above, is pretty reasonable.  Even when Dan and I have done quite a few hours a month of gaming, I seldom go over $3/mo in total charges.

If you do the math, using EC2 for a game server that is up and running constantly is no bargain.  If this is your need, it’s definitely better to look at a virtual hosting service rather than EC2.  But, if you only need the game server running a few times a month, EC2 is pretty hard to beat.

Half-Life 2 Deathmatch (HL2DM) is the game of choice for me and my friend right now.  It’s been ideal under EC2, since the standalone Steam server runs under Linux and is easily configured and controlled using the command line and scripts.  At some point, I’d like to post more details on my installation.  I have some cool features going—including using a Linux service that communicates the dynamic IP of the EC2 instance to and associates it with a hostname so that when we fire up Half-Life 2, we don’t have to point the game to a new IP each time and instead just reference a static hostname.

Here are a couple handy URL’s if you’re interested in giving EC2 and a Half-Life 2 game server a try:

Amazon Web Services (AWS) Management Console

Source Dedicated Server (srcds)

itunes2android—Perl Script for Moving Music from iTunes to an Android microSD Card

Sunday, December 27th, 2009

To use the Just Playlists program on an Android phone, you need to create M3U playlists and move them and your music to your phone’s microSD card.  If you use iTunes to manage your music library on your computer—as I do—one way to accomplish this is to use the iTunes playlists as the starting point.  The Perl script I created called itunes2android does just this.  You can download it here.

The script is meant to be run under Microsoft Windows—though adapting it to the Mac or Linux wouldn’t be difficult.  Here are the steps for using it:

  • Download the script and save it to a folder on your computer—let’s call it C:\scripts as an example.  It’s in a ZIP archive, so be sure to unzip it.
  • Install a Perl interpreter on your computer.  The one I use is called Strawberry Perl and can be found here.  ActiveState makes another Perl distribution for Windows.  It should work with it, as well, though I haven’t tried it.
  • Use iTunes to create a playlist of some of your music.  Once this is done, right-click on the playlist name.  One of the choices in the menu is “Export…”.  Choose this, and choose to export it as a text file into a folder of your choice.  Let’s use C:\playlists as an example directory and favorites.txt as the playlist export file name.
  • Now, open a DOS command prompt and change directory to where you put the exported playlist.  Using my example directory, the command would be

cd c:\playlists

  • Connect your Android phone to your computer with its USB cable.  Once connected, pull down the notification bar on the phone and choose to mount your microSD card.
  • Open up “My Computer” on your computer and note the drive letter assigned to your phone.  Let’s assume it’s D:
  • Go back to your DOS command prompt window and use the itunes2android script.  Using my example set-up, the command would be

perl c:\scripts\ favorites.txt d

  • The script will run and tell you what it’s doing.  When it’s done, you can open up the microSD card on your computer and look around for the new files it copied over.  When you’re done, unmount your phone and you’ll be ready to go.

The script will create a directory tree on your microSD card of D:\Music\ipod\… for your music tracks, based on the structure you’ve used to store them on your computer.  The “ipod” directory isn’t special but just happens to be the way I do it, to keep music moved over by itunes2android separate from any other files in the “Music” folder.   The “Music” folder is probably already on your microSD card, since it’s where Android puts music by default.  There will also be a D:\Music\Playlists folder that will contain the file favorites.m3u.  This is the playlist that you can use with Just Playlists.

A few more details.  First, I choose to manage my music files manually and thus they aren’t stored in the default iTunes directory structure.  I store my music as C:\ipod\<Artist>\<Album>\<track>.  If you let iTunes decide where to keep your music, the above should still work, but I don’t know exactly what directory structure you’ll end up with on your microSD card.  Feel free to comment here and let me know what you get or if it gives you problems.  Also, I live in the USA and have all my data on a Windows computer using character sets for English.  My script might have problems if you are using character sets for other languages.

If you use iTunes to add album art to your music, it doesn’t actually put the artwork in each music file.  I manually tell iTunes to write the artwork to each music file.  This way, other music players like Just Playlists and the default Android music player can find the artwork.  I’d be glad to post on how to do this in the future, if anyone is interested.

Finally, you can use the M3U playlists you create in the default music player on your Android phone.  They will show up in the “Playlists” list, once Android gets around to re-scanning your microSD card.  Your music will also show up under the other play categories in the default music player.  I should make the disclaimer that this is true with a Google Experience phone like the G1.  I’d guess it should also be true for the music players on phones like the Hero and Droid, but I haven’t used them to be sure.

Just Playlists—Android Music Player for M3U Playlists

Friday, December 25th, 2009

There are quite a few nice general-purpose music players for the Android operating system.  But I couldn’t find one that was lightweight and implemented playlist shuffling the way I wanted it to.  A “proper” playlist shuffle for me is shuffling the list of tracks as found in the playlist and then playing the shuffled list from first track to last track.  This means that no track will be played again until all tracks in the shuffled list have been played.  Most the Android players I’ve tried simply do random play—that is, they randomly pick tracks from the playlist and thus will end up with repeats before all tracks are played.

That’s why I ended up writing the “Just Playlists” music player for Android.  It’s not meant to be a replacement for the other music players and doesn’t have the sophisticated features they do.  But it does shuffle playlists as described above.  Here’s a complete list of the important features of the program:

  • User choice of loading the playlists in given order or as randomly shuffled.
  • A shuffled playlist is read from first to last track and thus doesn’t repeat any tracks before all tracks have been played.  Upon getting to the end of the playlist, play continues at the beginning of the list.
  • The current track, current position in the track, and track order are saved for each loaded playlist.  Thus, the user can switch between playlists and always return to the same spot in each playlist.
  • The music player interface is very simple, with maximal space being given to the album artwork.
  • Android’s media database is queried for tag and artwork information.  Thus, any media that Android can scan and store data for will return information for the program to display.
  • The user can use the “Touch” menu option to for the currently-loaded playlist, which causes it to be reloaded from the microSD card, with the choice of a new shuffle or unshuffled.
  • If a playlist file is changed by the user, the program detects this and reloads it as if it’s a new playlist.
  • The user creates playlists outside of the program in M3U format and then loads them to the microSD card.  An M3U playlist is a text file with one track file name per line.  Track file names can be complete Unix-style file paths to the location of music files anywhere on the microSD card.
  • If a phone call comes in, the player will pause during the call and then resume afterward.

    This program is not for everyone.  To use it, you’ll need to be comfortable with creating M3U playlists on your computer and then loading them and your music files to your phone’s microSD card.   I do this using Perl and DOS scripts to create the playlists from iTunes playlist exports, copying both the music and the playlists to my microSD card from my PC.  I hope to make these suitable for public consumption and make them available at some point.

    Here’s a list of improvements I hope to make to “Just Playlists” in the future:

    • Better use of higher screen resolutions.  The program should work fine at all resolutions, but it looks best under 320×480 and doesn’t take full advantage of higher resolutions and their increased screen areas—such as the 854×480 screen of the Motorola Droid.
    • Bluetooth headset control.
    • Ability to view and manipulate playlists from within the program.
    • Ability to specify either repeat play when the end of a playlist is reached or that the playback should just stop.

      If you try the program out and find that it has any problems, feel free to leave a comment here and I’ll see if I can fix it.  Please include information on your phone, including its screen resolution and the version of Android its running.  I own a T-Mobile G1 and thus have only used it on this physical hardware, which is running Cyanogen’s modded version of Android Donut (v. 1.6r2) at 320×480.  I have tested it via emulators under most screen resolutions and on Android v. 1.5r3 through 2.0.1.

      Thinkpad T61p Battery Failure and Replacement

      Saturday, December 12th, 2009

      My main computer system right now is a Lenovo Thinkpad T61p—type and model 6459-CTO.  I have the Advanced Mini-Dock with dual monitors, external 5.1 USB sound, parallel port printer, etc.  So when I dock, I don’t really yearn for a more powerful desktop system.  I’ve had this set-up for about a year and a half.  I also purchased the extended warranty, making for a full three years of generally well-regarded hardware protection.

      A few weeks ago, I sit down to work at my docked laptop and notice a flashing orange battery light.  I’d never seen this before.  A quick check of the power manager shows an error message—also sent to my Lenovo message center.  It reads “Battery error is detected:  A battery error has occurred.  The battery cannot be charged.  Replace the battery”.  This doesn’t give me a warm, fuzzy feeling.  The battery charge level icon is showing 100% and the battery isn’t all that old and has been holding a charge just fine.  It sounds like something beyond normal battery wear and tear.  So, I decide to pop the thing out of the dock and see what happens—after all, I have 100% charge, right?  Well, I should have hibernated.  As soon as I undock, it immediately shuts down, acting like there is no battery present.  I get the idea now that the 100% indicator means my laptop isn’t talking to the battery and doesn’t know how much charge is left.  You’d think it would show 0%, instead.  Go figure.

      Some quick web searches show that others have experienced the same problem.  Actually, Lenovo has acknowledged the problem for a few battery models and done a recall.  But, upon running their recall check tool, I find that mine isn’t one of the batteries that it is part of the recall.  From the Lenovo parts inventory of my original purchase—found at the Lenovo warranty site—the part number is 92P1132 and the FRU number is 42T4619.  The description is a “SANYO 9 CELL BATTERY FRU”.  FRU is short for Field-Replaceable Unit, I believe.  Not only that, but the extended warranty doesn’t cover the battery—it’s only warranted for one year.  Lovely.

      I thought about calling Lenovo tech support and trying to get a replacement, regardless, but decided after reading other comments on-line that it probably wouldn’t have do any good, since others hadn’t succeeded in prying a new battery out of Lenovo who were in the same situation.  Instead, I bit the bullet and bought a new Lenovo battery at a decent price off of eBay.  There are cheaper no-name clones, but I don’t like to risk clones with such large-capacity batteries as are in laptops.  They cost a lot and I don’t want to have a fire—that may be just paranoid, but there it is.

      I did a parts search through the Lenovo store and found that the current recommended battery for my laptop is model number 40Y6797.  There are several on-line vendors that offer the original Lenovo battery for much less than it costs when ordered directly from Lenovo.  But the price I found on eBay was better still, and the seller had received much positive feedback on this specific item.  It arrived promptly and is working great.  The diagnostic information shown by the battery status program I use says it was made by Panasonic.  This is good to know, since I read some buzz saying that the Panasonic batteries are better than the Sanyo ones—though I’d say take this with a grain of salt.  But considering my old one was made by Sanyo, I’m happier this way.  Also, the name of the battery shows as 42T4620, which looks like a FRU number and is a one-notch increment from the FRU of my old battery.  Some web searching indicates that the Lenovo part number for this battery is 92P1134.  So there you have all the numbers for both the old and new batteries.  This should be useful if you run into similar issues with your Thinkpad.

      Translating Character Codes from iTunes Playlist Exports for Android

      Friday, December 11th, 2009

      I have a Perl script I use to take an iTunes playlist export text file and turn it into a DOS batch script for copying music to my Android phone’s microSDHC card, as well as creating an M3U playlist.  This may sound like a strange way to do things, but I have a VMware Ubuntu instance installed under Windows Vista and run my Perl scripts from there.  But, it’s easier to copy my music files from Vista when my Android phone is mounted, which makes the auto-generated DOS script a simple way to do things.

      In order to make sure that files with greater than 7-bit character codes in their names get copied and played correctly, I had to do some interesting things.  There may be easier or smarter ways to do this, but at least this seems to work according to the 80/20 rule of time spent investigating versus results. =)

      It looks like the iTunes playlist export files use the UCS-2 character set.  The upside of this is that each character has a two-byte representation that shows up as an extra 0x00 byte for each character.  I strip the nulls and end up with characters in ASCII representation.  For the M3U playlist, I convert the characters with more than seven bits to their corresponding UTF-8 two-byte representation.  For example, a lower-case “e” with an acute over it is 0xE9 coming from the iTunes file, after having its null stripped.  The UTF-8 representation is 0xC3A9.  After this conversion, programs under Android that read the file names in the M3U file and compare them to file names in the file system work just fine.

      Now, to copy the files from my hard drive to the microSDHC card via a DOS batch script, I had to convert the ASCII character codes to what I have found is the CP437 character set—also known as Extended ASCII.  That’s because the DOS shell uses it for the file names.  So, in order to get comparisons and file moves to work via the batch script, the 8-bit character codes in file names had to be converted to it.  In the case of our lower-case “e” with an acute, 0xE9 becomes 0x82.  The file names for the files as they were copied to the microSDHC card were left in this format.  The files then are seen just fine by the Android OS and programs running under it.

      I’m writing an Android program that uses these M3U playlist files to access music files I’ve transferred as described above.  As I investigated the character sets and representations, I found that the Android SDK’s Java presents file names as UTF-8 strings.  When my M3U playlist contained ASCII 8-bit characters, my file names would not match the file names as retrieved by Java.  Using the Eclipse debugger, 8-bit characters from the M3U file were shown as a small rectangle, meaning Java didn’t recognize them.  Using the getBytes() String method to dump a string to a byte array showed the character represented as 0xEF 0xBF 0xBD, which I’ve found is the UTF-8 representation of “I have no idea what this character is.” =)  Using the debugger in this way also led me to learn that Java doesn’t store the character bytes as unsigned short ints like C, which is what I’m most familiar with.  Java doesn’t have the concept of an unsigned int at all.  Thus, 0xEF 0xBF 0xBD showed up as three consecutive bytes with decimal displays of -17 -65 -67.  The bytes are in 2’s-complement, so subtracting their absolute values from 256 results in decimal 239 191 189, which in hex are the codes described above.