Archive for the ‘life’ Category

Coding Tip: Programming as a Game

Tuesday, August 12th, 2008

I recently read a Wired article where people achieved success losing weight because Weight Watchers is basically an RPG. One game/exercise that I often do is to see how many lines of code or components I can write without any errors. It’s hard to quantify but my record so far as about 200 lines of Javascript/AJAX code. It was kind of cheating because I was basically re-using Javascript and backend PHP code that had been thoroughly tested. Overall, this is a pretty good method to increase the amount of code you can hold in your head and attempts to reduce the relying of debugging tools in favor of self/mental debugging.

Consistent Memcache Hashing and Failover with PHP

Monday, August 11th, 2008

I’ve written about Memcache before because it’s one of the best pieces of software written. It’s power lies in it’s simplicity and how easily you can plug it into any application. One of the things I really wanted was to implement consistent hashing and failover.

After checking out the PHP/Memcache documentation, the code to achieve this is fairly simple:

ini_set('memcache.allow_failover', true); // default is usually true
ini_set('memcache.hash_strategy', 'consistent');

Somewhere in your configuration/initialization script, make sure these memcache settings are in place. Allow failover tells the memcache client to try another server if it cannot connect to a particular memcache daemon. The hash strategy is pretty self explanatory. Now onto the actual server code:

$failCount = 0;
$realInstance = new Memcache;
$testInstance = new Memcache;
 
$servers = array("server1", "server2", "server3");
$defautlPort = '1';
foreach($servers as $host) {
    if($testInstance->connect($host)) {
        $realInstance->addServer($host);
        $testInstance->close(); // only close if connection was success
    } else {
        $realInstance->addServer($host, defautlPort, true, 1, 1, -1, false);
        $failCount++;
    }
}
$isConnected = true;
if($failCount == count($servers)) {
    // set false if every server is marked as failed
    $isConnected = false;
}

I use two instances of Memcache (might not be most optimal solution) to check the availability of that server and if it is available, add it to the pool with all of the default options. If it is not available, set up the connection to automatically failover but also maintain its position in the server pool. These options are set using this paragraph from the PHP docs:

bool Memcache::addServer ( string $host [, int $port [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback ]]]]]]] )

retry_interval: Controls how often a failed server will be retried, the default value is 15 seconds. Setting this parameter to -1 disables automatic retry.

status: Controls if the server should be flagged as online. Setting this parameter to FALSE and retry_interval to -1 allows a failed server to be kept in the pool so as not to affect the key distribution algoritm. Requests for this server will then failover or fail immediatly depending on the memcache.allow_failover setting.

There’s also error checking to ensure that at least one server is online to be considered connected to a memcache pool.

Working Hard for the Man

Saturday, April 5th, 2008

It’s 1.40AM on a Friday night and I’m still at work. Something is very wrong with that picture.

Over at Grooveshark, LOTS of changes are being that is going to make a pretty good music site an even better one. Better organized data, more online songs and a better user experience are all on the horizon. For all those impatient folks out there, which I am one of, just hold on and this major update will knock you out of your boots, or socks… or feet so make sure you wear some kind of footwear!

I Want MY Music: The History and Future of Music, Downloads and Mixtapes

Wednesday, March 26th, 2008

I want MY music. I want it fast, organized, high-quality and now. This is not too much to ask, and I, a consumer, have been begging for it for years.

Before BitTorrent, Napster or CDs, there was the mixtape. I would spend hours listening to the radio and when a song I liked came on, I recorded it. It was MY music. I choose to keep the notes, the voice, the rhythms with me for all time. I would mix and match songs according to my mood and the occasion. I would swap tapes with friends with similar tastes. And if they were lucky, they might have received one as a gift. Of course being a kid, I didn’t have a lot of money. But nothing says I care more than a well crafted mixtape (and no, I’m not ripping off Nick Hornby).

Then came the CD with its shiny new cover and it’s obvious similarity to the venerable vinyl record. But this was the 90’s. A new time, a new era, full of ones and zeros. Anything digital was all the rage. So I paid $20 a pop for a piece of plastic that did sound better than a tape, but was still inferior to a vinyl record. Granted, the CD came with a small booklet full of useful information: more pictures, sometimes lyrics, and even the name of the sound engineer from that song I never listened to. With a mixture of mass marketing and an era of economic prosperity, YOU, the labels, reaped record profits and I was left with piles of CDs containing just a few songs of enjoyable music.

In the end, I was left unfulfilled. I returned to the trusty mixtape to create the album I really wanted. Sure it was lesser quality and I couldn’t magically skip to the next song, but I at least had the songs I wanted in the order I wanted. It was my music my way. At the end of the 90’s, the party was over. The floodgates were open and a sea of unlimited free music was unleashed upon us. But this wasn’t a revolution. People didn’t storm the castle or burn down the walls. It’s the same evolutionary process that brought about the transistors, computers and the internet.

As the technology advanced with cheaper disk space and faster web connections, I wanted more. First there was Usenet, full of silly chatter and annoying trolls, but I could download free music. Then there was IRC with better file downloading but added viruses. Each successive iteration provided faster downloads and more content. The tide of progress couldn’t be stopped so technology finally reached a tipping point where the will, getting the music I want, found a way, Napster.

In truth, Napster changed everything for internet users. Before, advanced internet users had to download from large, clunky servers that might not have the data you wanted, or use a command-line interface to connect directly to files you wanted. What Napster did correctly was provide an adequate interface to content that was searchable and downloadable. What Napster neglected to do was filter out all the junk that people tossed in the system. Despite their drawbacks, what made Napster and other P2P networks even more worthwhile was recordable CDs. Now you had the best of both worlds: the crisp, clean digital sound of a CD along with the malleable, personal nature of a mixtape.

During this era of free music, a connection was broken. I was no longer a customer. I became a common theif and had to be dealt with immediately. You tried suing me and that did not work. You tried suing my little siblings and grandmother and you lost my respect. You did not ask me what I wanted or what it would take to change my ways. You could have created a new and better system with higher quality files and organized information. You could have created a faster system with special content like videos or live concerts. You could have done a lot of things, but you chose to ignore me. That was your decision, but now I have made mine: I WILL have my music.

There is Bittorrent which lets me download massive amount of music with a click of a button. There are decentralized P2P networks which let me find the exact song I want without being sued. There are social music sites where random strangers and I can share our individual tastes with each other and the world. There are programs out there that let me organize, tag and correct the gigabytes of music I have on my hard drive, and portable music players that can carry my entire library and fit in my pocket. I do this because I still love music and listen to it everyday. Now I download music and create playlists instead of buying CDs and burning mixed CDs. I do this because you drove me away and made me an outcast. Now a new era is upon us and you have to decide what your next step will be.

It’s starting out with a trickle: Radiohead, Trent Reznor and Barenaked Ladies. But that’s only the beginning of your troubles. When I finally build a system that let’s me listen to my music when, where and how I want it, I will need you less. When I can easily share and broadcast my music with friends or the world, you will see my influence. When I can discover the music I like or find music I never knew I would like, I will be empowered. And when I can finally pay the people directly who create, perform and produce the music I love, you will be nothing.

There are some who claim that they can do these things already. That they can provide a solution to my problems. I will be the judge of that. In the meantime, I refuse to wait for a solution because I am the solution. I choose to build the very system I pleaded for you to build. I have built Wikipedia, Flickr, Digg, Last.fm and Grooveshark. These are just the beginning and more is coming ahead. You can be part of this future if you really want. Just remember, I will have my music, but will YOU be part of the system?

Gullah Gullah Island!

Tuesday, March 4th, 2008

I found this article about the silence of Justice Clarence Thomas:
“In the past, the Georgia-born Thomas has chalked up his silence to his struggle as a teenager to master standard English after having grown up speaking geechee, a dialect that thrived among descendants of former slaves on the islands off the South Carolina, Georgia and Florida coasts.”

One word instantly caught my attention and I had to figure out what it meant: geechee. I hopped on over to wikipedia to figure it out.

From the wikipedia article:
“The Gullah people are also called Geechee, especially in Georgia.”

That’s right, Supreme Court Justice Clarence Thomas is basically from Gullah Island… sort of.

GULLAH! GULLAH! ISLAND!!!

If you have never heard of Gullah Gullah Island, it’s a fairly old Nickelodeon tv show from “back in the day” set in the unique community of Gullah Island.

By the way, what does “Binya Binya!” mean anyway. It seems that television taught kids to trust giant, costumed creatures, regardless of what they said. See Barney and Big Bird.

Becoming a Programmer

Sunday, March 2nd, 2008

I haven’t always been a programmer. In the beginning, I actually wanted to be a hacker. After a couple of well placed news stories, I decided that the fame and glory of hacking might not be worth going to “federal pound me in the ass prison”.

It all started at the Auburn University Library. Most of my friend’s parents worked at the university and being too big for a baby sitter, we usually spent lots of our time on the public computers. The internet is a funny place because it can take you anywhere. Sure enough after all the hours wasted going to chatrooms, sports forums, and game sites, I accidentally stumbled upon The Hacker’s Manifesto. It might have been because I was extremely bored, or I was young and impressionable, but no matter what, I was going to be a hacker.

I delved into all things hacking and consumed everything I could find on the subject. Phrack and Hack This Site were favorites of mine but the article that left the largest impression on me was How to Become a Hacker (pretty obvious how I found it).

After reading the article, I went ahead put it into practice. I started off, learning HTML (no Javascript) in the 6th grade. The only major project from that era was a Dragonball Z site that got 200 hits a week. After HTML, I learned C/C++. By this time, my lust for hacking died down and I was just enthralled with programming. The ability to put your thoughts in code and have a machine run it exactly was really thrilling.

By this point, hacking was non-existent in my mind and all I wanted to do was create games. John Carmack was a personal hero of mine for his legendary 3d rendering engines for Doom and Quake series. Starcraft is the best game ever made and really made me want to join the industry. I ran through most basic games like Pong and Breakout (never did make a Tetris clone). 3d game programming was the next progression and my parents bought me this book: Tricks of the Windows Game Programming Gurus by Andre Lamothe. This book was extremely helpful and pretty much gave you a great framework for Windows game programming. Using GarageGames, I worked on a couple of game projects with other kids who lived and died for coding.

Unfortunately, I lost most of my drive for programming in junior year in high school. I can’t really pinpoint the reason why, but programming lost some of the thrill and excitement it held earlier. I continued to program in internships but the companies I worked for didn’t really want a high school kid coding on one of their live projects. I was mainly a technician for email support, or desktops/laptops.

When everything was looking down and college wasn’t really doing it for me, I thought that I should just dive back into programming. I interviewed with two companies and decided that this really small start-up would be a great place to learn some new skills and do something that’s never been done before. A year and a half later, I am still with Grooveshark and loving it. Currently, I do web-development writing some of the HTML/CSS and Javascript that gives Grooveshark its web2.0 feel. Every now and then I dabble with some server-side PHP.

So that’s basically it. Programming isn’t really hard, but becoming a good one is. Like anything worthwhile, lots of practice and a good breakfast is all you really need.