Saturday, October 31, 2009

Back after a long time.

The last few months have been very hectic and I couldn't post anything onto my blog. I worked quite a bit on Google App Engine, did some experimental projects using LAMP, messed around with Amazon EC2 and S3, researched on virtualization and tried to use CouchDB.

While mobile computing is very interesting, I feel it is also important to step out of the silo that mobile programming sometimes creates. Usability and performance are important in mobile computing but scalability is not a big factor. I really wanted to try out something far grander in scale than the iPhone and Android apps that I continuously work on.

Google App Engine projects were really fun. I churned out quite a few apps (which we use internally within our company) in a very short span of time. But I think it still has some way to go before enterprises can really leverage the platform. GAE comes with a lot of limitations. For example, you cannot have a data structure (e.g., variable) larger than 1MB in size or you'll get a memory error. While that is not an issue in most cases, I found it to be severely limiting while working on an enterprise class application. GAE has a lot of other idiosyncrasies which Aral Balkan has really nailed in his blog post on the subject.

I knew of EC2 and S3 for a while now but I never really had an excuse to try it out. The 1MB limitation was a perfect opportunity for me to try out AWS. At first, I found it very intimidating. There were tools that could easily help me get started with GAE. Getting started with EC2 on the other hand was not so easy. But after a couple of days of very intense reading, I had my first EC2 instance up and running. I am still learning the nuts and bolts of EC2, but with the release of RDS, I am positive I will enjoy the experience thoroughly.

I just started with CouchDB and haven't really done anything useful with it yet. While I am not new to a non-relational DB, I find using REST calls for db operations very interesting. I am yet to think of why I would want to use CouchDB over MySQL.

It has been a very interesting three months or so for me. My only regret has been that I could not really use these technologies/platforms earlier. But, like the idiom goes, better late than never.

Friday, June 19, 2009

Twittering with Ubiquity

Although Firefox Ubiquity was released in August last year, it was only a couple of weeks ago that I really had a chance to look at it. It is kinda difficult to explain what Ubiquity is, so I will not. Instead, I recommend you watch this video. Ubiquity can be installed from here.

Mozilla has a lot of great documentation on how to use and extend Ubiquity commands. I would recommend you start from this page.

This post deals with posting 'tweets' with Ubiquity.
  • Posting a regular text twitter: This is pretty straight-forward, you just bring up Ubiquity and type twitter and whatever message you want to post.
  • Posting a selection: Select a section of text, bring up Ubiquity and type twitter. Your selection will make up the twitter content.
But I wanted more. I wanted to tweet about the song I am currently listening to. So, I hacked together a Ubiquity command for it. Paste the following in the command editor window (ubiquity command: command-editor)

/*
Post Current Song To Twitter
*/

const TWITTER_STATUS_MAXLEN = 160;

CmdUtils.CreateCommand({
name:"tweet-song",
_getSong: function() {
var trackData = window.foxytunesGetCurrentTrackData();
return CmdUtils.makeSugg("'" + trackData.title + "' by " + trackData.artist);
},

preview:function(previewBlock) {
var song = this._getSong();
var previewTemplate = "Updates your Twitter status to:
" +
"I am listening to ${status}

" +
"Characters remaining: ${chars}";

var truncateTemplate = "
The last ${truncate} " +
"characters will be truncated.";

var previewData = {
status: song.text,
chars: TWITTER_STATUS_MAXLEN - song.text.length
};

var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData);

if (previewData.chars < 0) {
var truncateData = {
truncate: 0 - previewData.chars
};
previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
}
previewBlock.innerHTML = previewHTML;
},

execute:function() {
var statusText = this._getSong();
if (statusText.chars < 0) {
displayMessage("Twitter requires a status to be entered");
return;
}
var updateUrl = "https://twitter.com/statuses/update.json";
var updateParams = {
source: "ubiquity",
status: "I am listening to " + statusText.text
};

jQuery.ajax({
type:"POST",
url: updateUrl,
data: updateParams,
dataType: "json",
error:function() {
displayMessage("Twitter error - status not updated");
},
success:function() {
displayMessage("Twitter status updated.");
}
});
}
});


You will also need to install FoxyTunes to get this command to work. That is it.

Start up a song in iTunes or any other supported player, bring up Ubiquity and type 'tweet-song' and hit enter to post a tweet that looks like 'I am listening to 'Alex Gopher With Demon Presen' by Jazz Boutique (CD Series)' (Note: You may have to login with your twitter account for this to work).

Credits: The tweet-song code is based on work by Blair McBride (twitter command) and Abimanyu Raja (get-lyrics command).

Wednesday, May 20, 2009

Debugging iPhone Crashes

Sometimes, developing iPhone applications can be very frustrating. Debugging crashes and memory issues can be very time consuming if one does not know how. Unfortunately, most books on iPhone programming either devote less than a chapter on debugging or, in most cases, totally skip it.

This post tries to de-mystify debugging crashes. When I say crashes, I mean straightforward exceptions (and not the memory access errors).

Typically, release builds are stripped of debug symbols to prevent reverse engineering (and other kinds of 'abuse') and to keep the released binary small in size. But, by doing so, you end up with only bunch of cryptic addresses in the log and absolutely no information on where the problem is (unlike in Java stack traces which pin-point the source of the exception).

On iPhone, typically, when the application is built, in the build folder (where the .app file is located), you will also find a file with a '.dSYM' extension, that contains a copy of the symbol information in the executable. This file maintains a one-to-one relationship between the source code and the compiled object code, which could make it possible to correlate an execution address with a line of code in the original source. I found that .dSYM files are generated by default. But it is a good idea to check your project settings in Xcode and ensure that 'Generate Debug Symbols' is checked.



Next, grab a copy of ratos. ratos is a ruby script that symbolicates the entire stack trace using atos (address to symbol) tool. The script is very simple to use and extremely helpful. Here is a sample output:

App Name:AtosDemo
App Path:/Users/ajeya/Documents/workspace/AtosDemo

Paste xcode stack trace to stdin, then type 'sim' or 'arm' on a line by itself.
Type 'exit' or '^D' to quit and 'app' to print the current app.

ratos>Stack: (
2483900683,
2534657595,
2483900139,
2483900202,
2478626815,
2478092680,
10809,
816608399,
10391,
816111650,
816149355,
2478330414,
2483403557,
2483403992,
827745792,
827745989,
816114848,
816160924,
10308,
10162
)
sim

-[AtosDemoViewController viewDidLoad] (in AtosDemo) (AtosDemoViewController.m:36)
-[AtosDemoAppDelegate applicationDidFinishLaunching:] (in AtosDemo) (AtosDemoAppDelegate.m:21)
main (in AtosDemo) (main.m:14)
start (in AtosDemo) + 54


Detailed instructions on how to use the script can be found on the script's homepage.

Of course, you can also choose to use 'atos' directly. But, I found ratos to be much more convenient.

Here are some useful links on debugging on iPhone and Xcode:
  1. ratos source on github
  2. Symbolification
  3. Debugging and Symbolizing Crash Dumps in Xcode
  4. Stacktraces
Reblog this post [with Zemanta]

Monday, May 18, 2009

Wolfram Alpha

The much awaited (and a bit hyped) Wolfram Alpha made its debut last week. There was a lot of talk about it being a Google killer and like everybody else I too wanted to try it out. But I was in for a surprise. Firstly, Wolfram Alpha is NOT a search engine. It is being touted as a 'computational engine' which uses an internal knowledge base and outputs computations. Confused? I was too.

My first query was for 'iPhone' which did not return anything. Next, I tried Apple and I was presented with a lot of information about Apple's stock. Information included the latest stock price, revenue, price history, performance comparisons and a lot more. I was slowly getting a gist of what it does. Next I searched for '1 USD in INR' and got some interesting results.



I, then, tried other searches like 'Hyderabad', my birth date and anything else I could think of and there were some interesting and some not so interesting nuggets of information I gathered.

So, my take on the whole thing is that Wolfram Alpha is a data aggregator. Unlike Google, they don't use the internet as the data source and rely on their own internal knowledge base. Only time will tell how effective this tool is and whether or not regular netizens will adopt this tool. I, for one, need to find a reason to use this.

You can register for application APIs here and can find the FAQ here.

Wednesday, April 29, 2009

Static Code Analysis with 'clang'

On Leopard, all Cocoa objects are garbage collected, the developer has the luxury of not having to worry about memory management and can let the system take care of it. There is no such luxury on iPhone since garbage collection can be a performance intensive process. So, it is possible for memory leaks (and other nasty bugs to creep into the code during development). There are a bunch of tools at the developer's disposal to identify such problems and fix them. One such tool is the 'clang' analyzer.

I have found static code analysis to be an indispensable tool when it comes to discovering some very common bugs early in the development cycle. I first heard of the LLVM/Clang static code analyzer command line tool for Mac/iPhone late last year but started using it only recently.

The best way to understand how this works is to look at an example. The following is a sample code containing some obvious (and some not so obvious) memory leaks and bugs:

- (void)viewDidLoad {
// Uninitialized object
NSString *string1;

if (NO) {
string1 = [NSString stringWithString:@"Foo"];
}

NSUInteger counter = 0;

NSNumber *number = [[NSNumber alloc] initWithInt:0];
if (YES) {
// Memory leak
return;
}
[number release];
}

- (IBAction)createString:(id)sender {
// Memory leak
NSString *foo = [[NSString alloc] initWithString:@"Foo"];
}

- (NSString *)dummy {
NSString *string2;
return string2; // Returning an uninitialized object.
}

The analyzer can now be invoked simply by executing the following from the command line:
scan-build -k -V xcodebuild -configuration Debug -sdk iphonesimulator2.2.1
scan-build is the script for running the analyzer over a project build. The above example assumes that scan-build is in the path. The '-k' flag forces the script to keep going even if there are problems in compiling some source files and '-V' flag runs a small web server and opens up the results file in a browser. Running the analyzer against the above code, detected 5 bugs.







The analyzer works best with 'Debug' builds. The tool is not perfect and does throw up false positives, but in most cases it is bang on target. The tool also does not catch some slightly more difficult to detect leaks, but more about that in another post.

LLVM/Clang Mac binaries are available on the home page.

Saturday, April 25, 2009

Why So Serious?

I decided to start posting all the non-tech related, fun stuff to my new blog. Keep checking it out for some interesting posts :-).

Sunday, April 19, 2009

Tuesday, April 7, 2009

Getting started with iPhone development

I have been doing serious development for iPhone for a little over three months now. I had experience programming Objective-C, but the start to iPhone development was not as easy as I had originally thought. I was never good at UI programming and generally shied away from it.

Until a few months ago, the only source of information for iPhone development was the official SDK documentation and the sample code. Moreover, all developers had to accept an NDA agreement which among other things prohibited discussing or sharing information related to iPhone development. However, in October 2008, Apple lifted the NDA and within a month or so books on iPhone development started hitting the shelves.

The two books that really helped me understand the iPhone SDK are Beginning iPhone Development: Exploring the iPhone SDK by Dave Mark and Jeff LaMarche and
The iPhone Developer's Cookbook: Building Applications with the iPhone SDK (Developer's Library) by Erica Sadun.



"Beginning iPhone Development", as the reviews show, is an excellent introduction to iPhone development. The book is well laid out and the book logically progresses from writing a simple "Hello World" application to taking advantage of the iPhone hardware. I don't think I would have been as comfortable with iPhone development, as I am now, without this book. But (there is always a but), the book does not cover any topics beyond the basics. This is understandable because the book is meant as an introduction, but they do not cover controls like UIScrollView or UIPageControl.




In contrast, "The iPhone Developer's Cookbook" is great at covering some not so basic topics. There are some good examples on animation, custom controls and undocumented APIs like cover flow.

Neither of these books cover advanced topics like memory management, debugging, optimizing with Instruments etc.

I am sure we will see newer editions of these books coming out sometime later this year and I hope they get better. I highly recommend getting both the books if you are considering serious iPhone development.

P.S.: Check this out. I just subscribed to it and I am hoping it covers some advanced topics.

Monday, March 30, 2009

Enabling iPhone 3G Tethering

Apple recently announced iPhone OS 3.0 boasting over 100 new features. However, the feature that really held my attention was tethering.

After installing the new OS, I could not find the tethering option anywhere and enabling it required a little bit tinkering around with the carrier support files. I followed this How-To and had it working with Airtel India after a little bit of trial and error.

If you are in India and got your hands on the iPhone 3.0 SDK, you can download the ipcc files for the official Indian providers below:

Airtel India
Vodafone India

The Airtel configuration file works great. I did not test the Vodafone configuration file. If anyone has got it working, please let me know.

Enjoy!!

Tuesday, March 24, 2009

Google Maps Street View

A lobby group called Privacy International (PI) wants to Google Maps Street View shutdown. I think it is stupid if folks expect privacy on streets. Where else would you expect to see ETs or pretty girls flashing (or rather almost flashing) their wares. Anyway, street view is fun. I too feature on Street View if you happen to be looking for 1444 Charleston Rd, Mountain View, California.


View Larger Map

Here is a screen shot if someday they decide to take down these images.



You can find the top 15 Google Street View Sightings here.

Saturday, March 14, 2009

Sixth Sense

No. I am not talking about "Sixth 'I see dead people' Sense". This is what I am talking about. It looks something straight out of a sci-fi movie (in fact a lot like the one in Minority Report). Watch the video.



Thanks to my friend Abhinav for sharing.

Printing from the command line in Mac OSX

I had to print close to 50 individual PDF files and one way to do that was to open each one of them and then print them. But there is a fundamental issue with that. I am lazy. Opening 50 files and then printing them was not something I wanted to do on a Saturday morning.

So, I fired up the browser and started searching for ways to print from the command line and got lucky with the first hit. 'lp' is a command line tool that submits files for printing or alters a pending job and it is very easy to use. To print a bunch of pdf files in a folder, type the following commands in the terminal:
# Printing to default printer.

lp *.pdf

# Printing a document to the default printer in landscape mode

lp -o landscape foo.pdf

# Printing the first and the second pages of the document

lp -P 1,2 foo.pdf

# Printing pages between 4 and 20 in landscape mode

lp -P 4-20 -o landscape foo.pdf

# Printing to a non-default printer called "bar"

lp -d "bar" foo.pdf
More information is available in the man document for 'lp'.

P.S.: I did not use 'really' in this blog post. Oops!! I think I just did it again.

Tuesday, March 10, 2009

Word Clouds

I came across a tool (or as the website calls it, a toy) called Wordle today which allows you to create word clouds. The size of a word in the visualization is proportional to the word frequency in the input text. On my website, apart from 'Android' and 'iPhone', I realized I use 'Really' a lot :-). Here is the wordle for my website.

This will look great on a t-shirt. You can create one for yourself here.

Tuesday, March 3, 2009

Development on iPhone vs Android

As I mentioned in an earlier post, I have been working on both iPhone and Android platforms simultaneously. While the road has been bumpy, it has also been a lot of fun. I think I know enough of both the platforms to give my opinion on development on iPhone and Android.

Apple officially announced an iPhone SDK sometime in March 2008. The SDK contains an iPhone version of the Cocoa libraries called 'Cocoa Touch'. The SDK itself is a free download, but you will have to cough up $99.00 (registration fee for the iPhone developer program) if you want to run the apps you are developing on an iPhone. That is a lot when you compare it with Android, where you have to pay just $25.00 to be a registered Android developer.

The latest version of the SDK is a whopping 1.7 GB and contains the entire iPhone development toolchain including XCode, Interface Builder, Instruments, the simulator and APIs including Cocoa and Cocoa Touch. The SDK can be used to develop applications for both iPhone and Mac OSX. Android SDK on the other hand is a much more reasonable ~80 MB. Andorid SDK contains the Android API, the Android tool chain, Android simulator and Eclipse plugin called ADT.

Android Development Tools or ADT is an eclipse plugin that makes it really easy to create, build and debug Android applications.

One of the biggest drawbacks in developing for iPhone is that it can only be done on a Mac OS X machine. There is no support for Windows (which is good) or Linux (which is bad). Android SDK on the other hand is multi-platform and gives more or less the same user experience on all the supported platforms.

Disclaimer: I am not an Eclipse or an XCode expert and my comments are based on my experience (or lack thereof).

I love and hate developing on both platforms. There are certain great features which each one offers and there are features which both can improve on.

IDE:

XCode is definitely the winner here. It is very stable and takes up a lot less memory than Eclipse. The 'Interface Builder' is an awesome tool, but more about it later.

Not so easy on ADT/Eclipse. While not really an Android issue, using Eclipse slows down my machine. Once it was taking up more than 75% of the physical memory on my machine. I know, I don't have to use Eclipse. But being inherently lethargic and lazy, I never really bothered to figure out how each of the tools works.

I also find Eclipse 'auto-compile/let me display the squiggly line before you finish typing' feature very distracting and annoying. I am sure there is a way to disable it, but could never find it.

UI development Tools:
Getting the UI right for a mobile application is the key. It makes or breaks the app. I feel it is important to spend a good amount of the development time to get the UI right.

UI design on iPhone is very easy. and lets folks like me who are 'UI Design Challenged' an easy way out. You just drag and drop the components, connect the outlets and actions and you are done.

The UI design tool in ADT is useless. DroidDraw is definitely a better alternative but cannot match 'Interface Builder'. The recommended way for creating Android UI is through what are called 'layout files'. Layouts are XML files where you define the hierarchy for your UI. While the learning curve is very small, getting the elements to be at the right place drove me nuts!! There are attributes like 'weight', 'gravity', 'orientation' ... aaarrrrggggghhhh!!! There were times where I spent 30 mins writing the functionality but 3 hrs trying to get an element where I want it.

Debuggers and other tools:
Debuggers on both environments are pretty decent and I really have no complaints there.

The iPhone SDK also has 'Instruments', an instrumentation tools, that is extremely helpful in analyzing performance of the app.

Android SDK comes with Android Debug Bridge (adb) and Dalvik Debug Monitor Service or DDMS which provides a bunch of services like port-forwarding , screen capture on the device, thread and heap information on the device, logs, process information and some really funky features like spoofing incoming calls, location data and SMS.

I must admit that my knowledge of both Instruments and DDMS is very limited. I just understood enough of each to get out of some tight spots.

The Code:
Android is clearly the winner here. The API is really rich. There are things you can do which you cannot really dream of on iPhone (at least not without jailbreaking). The features I really like the best are Intents (Intents are very difficult to explain, but pretty simple to use/abuse and I recommend having a look a the documentation to really understand it). Android API also allows creating of background services opening up more creative avenues for the developer. iPhone API does not support background services yet and does not allow for more than one third-party app to run in the background.

Android API is in Java which means the learning curve is much less steep (for someone who knows Java). Not so easy for iPhone. iPhone API is in Objective-C which while being extremely feature rich is also very, for a lack of a better word, non-traditional. I found the syntax a bit weird at fast, but got over it very fast. Objective-C uses named arguments. For example, a Java method looks like:
someObject.someMethod(someParameter, someOtherParameter);
An Obj-C method on the other hand looks like:
[someObject someMethod:someParameter withSomeOtherParaMether:someOtherParameter];

This can make method names pretty long. For example:
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
Memory management is another issue. Android supports garbage collection while iPhone does not. So, keep track of object allocations and releasing them when not required is a big task on iPhone.

Conclusion:

My goal here was not to do a feature by feature comparison, I neither have the experience or the expertise to do so. I feel that developing on both platforms have their own issues and I accept that some of these issues are probably because I did not take time to understand a feature or an API before using them.

Having said that, both Android and iPhone APIs make mobile development really a fun experience and I would find it very difficult to choose my favorite platform among the two.

I recommend this book for Android and this one and this one for iPhone.

Friday, February 20, 2009

Consuming Keydown Events

The last few weeks have been very hectic for me. I am working on my first Android and iPhone projects simultaneously and anyone will tell you that is not fun :-).

Anyway, while working on an Android app, I came across a small problem that really annoyed me. I was displaying a modal dialog and I did not want the user to navigate away from the modal dialog unless they clicked an 'OK' button.

But then I saw a problem. All the user had to do was press either the back button, the home button or the call buttons and the dialog disappeared. Now that is not what I intended or wanted and in the interest of self preservation, I started wondering how to solve the problem and thankfully found a very easy solution.

All I had to do was consume the keydown events for those buttons by overriding the onKeyDown method in my dialog class.
@Ovveride
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
return true;
case KeyEvent.KEYCODE_CALL:
return true;
case KeyEvent.KEYCODE_DPAD_CENTER :
return true;
case KeyEvent.KEYCODE_ENDCALL
return true;
case KeyEvent.KEYCODE_HOME: // this does not work.
return true;
default:
break;
}
// pass all unhandled events to the parent class
return super.onKeyDown(keyCode, event);
}
That is it. I could now prevent users from clicking on these buttons and navigating away from the dialog.

This can be used to disable users from pressing any other buttons you don't want them to. The only exception is the 'Home' button. I am not sure if there is any other way, but this approach certainly does not work. You can find a more detailed reference on all available key event constants here.

Tuesday, January 27, 2009

The Android Developer Phone

I started with Android programming couple of weeks ago and was itching to get hold of the actual phone and this week my wish was granted.

Being an iPhone owner, I was very excited with the possibilities and opportunities an open platform brings. While the iPhone provides a great user experience, the lack of support for third-party background services puts a limit to the kind of applications one can write. Anyway, back to Android.

The developer phone, also called ADP1, is a SIM-unlocked and hardware-unlocked. So, a SIM card from any network. I used it with Airtel SIM and it worked without any problems (well not really but more on this later). The other great advantage with ADP1 is that since the bootloader is unlocked, advanced users can build Android from the source and flash the phone with custom or bleeding edge builds. Exciting!!

After I got the Android, I booted it up only to find that there was no way to get past the initial setup wizard. The setup wizard ties your Google account to the phone. This essentially allows for download (and sending) of emails on Gmail and syncing of the calendar app on the phone with Google Calendar and the contacts app on the phone with Google Contacts with the registered account. The only problem is that it requires a data connection (aka. EDGE) to be enabled on the SIM. There is no 'skip' button (atleast not with the stock ADP1 firmware) to skip this step, enable WiFi and register.

But I had the SDK installed on my system. So, I used Android Debug Bridge or 'adb' to skip this screen. I connected the phone to my computer and opened a terminal window and typed:

adb shell

This connected my computer to the shell on the phone. The following commands disabled the app wizard:


$ su
# echo "app.setupwizard.disable=1" > /data/local.prop
# reboot

The 'adb shell' opens a shell on the remote device from the computer. The 'su' was required since the default user on the device did not have the necessary permissions to create the file in '/data'.

This connected my computer to the shell on the phone. The following commands disabled the app wizard:

That did the trick. The home screen appeared after reboot. I played around with the phone for some time before I started itching for 'cupcake'.

'cupcake' had a couple of features I wanted to try out including a soft keyboard and the A2DP bluetooth profile.

So, I downloaded and built the Android source code. What was amazing was that the entire source code built without any issues the first time. Well, almost. The default Makefile target was not set for the device. I read through the documentation and found that there were some additional steps for building Android for the device.

I followed the instructions as is and after about 45 minutes or so, I had the 'cupcake' version built and ready for flashing.

Here are the instructions for flashing the device. Please try it at your own risk :-)
  1. Go to the Android source code root folder on your machine.
  2. Ensure that 'out/target/product/dream' exists. We are interested in 'system.img', 'boot.img' and 'userdata.img'. Make sure all the three files exist. If they don't then follow these instructions exactly and try again.
  3. Locate 'fastboot'. On my system I found it under 'out/host/darwin-x86/bin/fastboot'.
  4. Execute the following commands (I included the outputs I got):
$ ln -s out/host/darwin-x86/bin/fastboot out/target/product/dream/fastboot
$ cd out/target/product/dream/fastboot
$ ./fastboot -w
erasing 'userdata'... OKAY
erasing 'cache'... OKAY
$ ./fastboot flash system system.img
sending 'system' (53946 KB)... OKAY
writing 'system'... OKAY
$ ./fastboot flash boot boot.img
sending 'boot' (1418 KB)... OKAY
writing 'boot'... OKAY
$ ./fastboot flash userdata userdata.img
sending 'userdata' (2 KB)... OKAY
writing 'userdata'... OKAY
$ ./fastboot flash reboot
rebooting...
After reboot I had the latest (and not necessarily the greatest) version of Android :-).

My experience with Android (and the device) has been positive so far. I have been updating the Android source code on my machine daily and am seeing quite a few changes being committed every day which is great.

My next goal is to try out the custom firmware by JesusFreke, an xda developer, which appear to be much better than the stock firmware. I will keep everyone posted on how that experience turns out.

Thursday, January 22, 2009

I finally 'git' it

I started work on several personal projects of mine at home and very soon realized that managing code for these projects was getting out of hand.

After making sure that the code worked, I tarballed the folders and stored them a couple of times. However, once I started coding more regularly, I realized that the tarballing approach will not work. It is difficult to maintain and very impractical.

I wanted to have a revision control system in place that would be easy to use and required very little work from my side. Since, I was working on this alone, I also wanted something that would work on my machine but is easy to set up on another machine with very little effort. I used Perforce extensively for a very long time and was comfortable with it. But setting it and having to back it up regularly would have taken up too much of my time.

I tried CVS and after the first day, I started not really liking it. Since I was working on Android and used 'git', ableit very briefly, I wanted to give it a shot.

I did not realize till I downloaded the source code to build it that it was designed and developed by Linus Torvalds for Linux kernel development. I found this great video of Linus' talk on git when he visited the Google campus.

I setup 'git' on my machine in less than 5 minutes and completed my first checkin soon after that. Within the hour, I also setup 'gitweb' to access git repositories via the browser.

After a week's use I can say that I love 'git'. It is very simple to setup and use and moreover it is distributed making a central repository totally unnecessary. In fact, Linus in his talk said that he considered several alternatives to BitKeeper but anything that was not distributed was discarded immediately.

'git' can be downloaded and built from here and you can find the documentation here and here.

Update: Yet another great document on how to use 'git' and 'repo'.

Saturday, January 17, 2009

It is all 'fsck'ed up

A couple of days ago, my MacBook Pro(MBP) refused to boot up. When booting from the Mac OS X partition, I got the usual spinning logo, then it would shut off completely. I did not realize there was an issue until after about five or so attempts at booting, all of which failed.

One of my options would have been to boot up the MBP in target disk mode, mount the volume on another Mac and run Disk Utility to find out any potential issues. Since, I had no other Mac machine handy, I decided to boot from the Mac OS X install disk and run the Disk Utility app that is in there.

Disk Utility detected a rather cryptic problem called 'invalid sibling link' error while verifying the catalog file. Also, interestingly, the 'Repair Disk' option in Disk Utility was grayed out.

I was desperate now. There was a lot of data in that partition which I needed and had no backup of. Thankfully, I was able to fix the problem and here is how I did it.

But, what is 'invalid sibling link' error?

To understand this, let us first, very briefly, look at how a HFS+ volume is structured.

An HFS+ volume contains five special files:
  1. Catalog file: Describes the folder and file hierarchy of the volume. It is organized as a "balanced tree" for fast and efficient searches.
  2. Extents overflow file: This keeps track of the allocation blocks that are allocated to each file as extent (extent-based file systems allocate disk blocks in large groups at a single time, which forces sequential allocation, of course this is somewhat of an oversimplification).
  3. Allocation file: Specifies whether an allocation block is free. This is stored in a bitmap, specifying a free allocation block with a "clear bit".
  4. Attributes file: Contains attribute information regarding files or folder.
  5. Startup file: Allows computers to boot that do have built in support for HFS+ file systems
Catalog files, Extents overflow files and Attribute files are stored in data structures that are technically known as B-trees (technically the HFS B-Trees are a variant of B+-Trees which Apple's technical documentation calls them B*-Trees.)

The Catalog tree (which describes a file or a folder) contains records which, for efficiency, are grouped into fixed length blocks called nodes. The B-trees are structured in such way that the records are in the leaf nodes. These records are sorted and indexed for fast searching which could physically scatter the nodes around the B-tree file. So, all the nodes are linked together to form a doubly linked list which allows the traversal of leaves in the same directory.

If, for whatever reason, the link between the leaves breaks (technically, becomes an invalid or dangling pointer), then we end up with an error like 'invalid sibling link'.

Now that we understand what the problem really is (through the rather oversimplified write-up that preceded this), here is how to fix the problem.
  1. Start in single user mode by holding down Command-S while booting (in my case the problem occurred on the boot volume so this was required).
  2. Once the text scrolls all the way down you should be at a command prompt
  3. Enter fsck -fy (fsck is a file system consistency and repair tool. the 'f' option is to force checks even on a clean file system and the 'y' is to just blindly say 'yes' to every option that may come up. Read the man document on fsck for more detailed explanations).
  4. If it reports 'FILE SYSTEM WAS MODIFIED' repeat from step 3 again.
  5. Keep repeating until it says 'The volume appears to be OK' (or something to that extent) or you give up ;-).
  6. If successful, type reboot to reboot.
Running fsck multiple times may be necessary. In my case I had to run it 4 times before the problem was fixed.

Googling for 'invalid sibling link' returns thousands of results but none of them really explain what causes the problem. Here is another good solution (and a great discussion) to the same problem.

If neither of these solutions work, then Alsoft's DiskWarrior (which will set up back by $99.95) could be another option although even this tool has varying degrees of success.

The first thing I did, after successfully rebooting, was to setup Time Machine. After this episode, I realized the need for a good backup solution. If things are beyond repair, then I at least know that my data is safe. :-)

Friday, January 16, 2009

Last week Palm announced their much anticipated Palm Pre mobile phone. The Palm Pre features a slide-out QWERTY keypad to complement its 3.1-inch touchscreen display, a 3-megapixel camera with flash, EV-DO Rev. A/HSDPA connectivity, Bluetooth, GPS, WiFi, Bluetooth 2.1 with A2DP and EDR, proximity and light sensor, accelerometer, 8GB internal memory, and runs on their latest mobile operating system, the WebOS. The Pre will be one of the first smartphones to feature wireless charging, achieved using electromagnetic induction and the use of an optional wireless charging dock, dubbed the 'Touchstone'.



While watching the Palm Pre CES launch video, I couldn't help but feel that Pre had features that iPhone could have easily had. The card view makes multi-tasking a cinch and 'Synergy' which brings all your email accounts and contact and calendar information from various sources into one place is not all that difficult for Apple to implement. Maybe in firmware v3.0 along with cut and paste.

WebOS looks very promising. Development for WebOS will be through an yet-to-be-released-to-public RAD framework called Mojo. Support for background apps and 'direct access to the device's capabilities' has been confirmed by Ars.

Slated for release in the first half of 2009 in the US, Palm Pre definitely brings innovation and life back to the struggling company. Here are some positive and not so positive reviews about the Palm Pre.

Personally, I think the CDMA only support (atleast for now) is a bummer. While waiting for Palm to bring out the GSM model, I cannot wait to get hold of the SDK and play around with it.

Update: Good article on Engadget titled 'What Apple could learn from Palm's WebOS'.

Saturday, January 10, 2009

Regex and Prime Numbers

I was looking for articles and examples on advanced regular expressions when I came across this amazing expression that prints prime numbers:


perl -e 'print "PRIME" if (1 x shift) !~ /^(11+)\1+$/' xxxx


I knew regular expressions were powerful but this is pure ninja stuff. The origin of this expression is attributed to Abigail who also maintains the Regex::Common module. Abigail is a frequent poster to the comp.lang.perl.misc newsgroup and this example originated as a one-line program in one of Abigail’s sign-off signatures. Since then, it has been commented on within the group on a few occasions and with a few other variations.

You can find explanations on how and why this works here or here.

Thursday, January 8, 2009

Perl vs Python

Perl vs Python is a classic debate that has flared up on many forums, websites and user groups. Here is my $0.02 on the whole affair.

I was a very devoted Perl user till 2005 and then switched to Python, mainly because everyone else in the project was using it.

Like, so many others before me, I had difficulty getting used to the 'no bracket, indentation only' approach. But after some initial hiccups (mainly strong denial and prejudice against Python), I started writing some useful programs in Python. The indentation stopped bothering me after a while, but I never really bothered mastering Python the way I did Perl.

However, my opinion about Perl (and, indirectly, Python) changed last year. I had to generate some code coverage data for the Mac project I was working on but raw 'gcov' data files were not really helpful. I found that 'lcov' could create well formatted HTML pages showing code coverage data and annoted source code. I was very happy with lcov but going through lcov source code, written in Perl, left my head spinning.

I started losing track of the context within which certain variables were being used. The fact that I hadn't used Perl in while and had forgotten quite a lot of the idiosyncrasies native to the language did not help matters either. But I felt very guilty. I loved Perl before and I did not have any complaints against it earlier, so what happened now?

I went back to my old source code 'repository' (actually it was a bunch of zip files on a CD :-)) and had a look at the Perl code I had written. What was interesting was none of the scripts I wrote were more than 200 or so lines of code. They were effective but never long and I had never re-used any script. That made sense to me. I actually enjoyed using Perl as long as the scripts were small, I was the only one using them and I did not have to re-use them.

Henry Spencer, a well known computer programmer, called Perl a 'Swiss-Army Chainsaw, intending to convey his evaluation of the language as exceedingly powerful but ugly and noisy and prone to belch noxious fumes'. I think his observation is spot-on.

But once I started working with a team, Python seemed better. Python was a lot easier to read and understand and the learning curve was lot less steep. Python has everything that Perl offers but without the eccentricities. Moreover, Python has a better implementation of object orientation and is inherent to the language where as Perl's implementation has a very kludgy feel to it.

I will still use Perl for what it was originally intended for, mostly scripts that munge data, I will keep them small and will probably be the only one using it. I will, probably, use Python for everything else that is more involved and has complex tasks.

Update: I found this article, by Eric Raymond, which presents an argument in Python's favor more articulately.

Sunday, January 4, 2009

Laughter's really the best medicine

Marissa Mayer's laughter compilation from her Stanford speech :-)



But this, by far, is the funniest laugh I have ever heard.

Friday, January 2, 2009

QR Codes

I met up with Srikanth at Cafe Coffee Day last week. While we were sipping some overpriced beverages, our conversation went around to the topic of 'mobile tagging'.

The image below explains what mobile tagging is all about.


You just point your phone's camera to a 2D bar code, take a picture of it and decode it. There are several 2D bar code formats but one of the most popular is QR (or Quick Response) code.

QR codes can contain anything from plain alphanumeric text (or a website address or an SMS message) to airline tickets. The Japanese even use QR codes on graves. Check out this interesting article about potential uses of QR code in India.

There are several QR code generators out there including one from Google. Download a QR code-reader for your phone from BeeTagg and find out where the code below takes you :-).