Sweetening Your Xgrid with Cocoaby Drew McCormack
Last time, I took you through the changes that Xgrid has seen in Tiger, and showed you how to setup a private Xgrid controller and run simple jobs with the
xgrid command-line tool. This time we'll take a look at XGridFoundation, a framework that allows you to integrate Xgrid into your Cocoa applications.
In the last installment, I made some grandiose promises about not only covering the Xgrid Cocoa API, but also incorporating some Automator and Core Image. When I got down to business, I realized the complexity of XGridFoundation meant that including these technologies would simply cloud the issues. So I'm going to keep it strictly Xgrid this time, and leave the other cool stuff for another occasion.
Between a Rock and XGridFoundation
There is something of a tradition amongst Cocoa tutorial writers to try and impress the reader with a full-featured application created from a bare minimum of source code. Want to write a web browser with a single line of code? How about a personal organizer with no code at all? If this is how you measure the quality of your Cocoa tutorials, I'm afraid you're about to be disappointed, because a line of code won't even get you in the front gate with XGridFoundation.
I have to admit that when I started working with XGridFoundation, I had expected it to be considerably easier going than it turned out to be. I have quite a few years of Cocoa developing experience, and I can't think of many corners of the frameworks that I have had to struggle with as much as XGridFoundation. It almost makes getting your teeth pulled seem like a weekend in Acapulco.
To give you a rough idea of what I am talking about, the simple application I will develop in this tutorial is equivalent to around three of the
xgrid commands discussed last time, but requires in the vicinity of 500 lines of source code to achieve! There is definitely an opportunity for a third party to step in and write a simplified interface to XGridFoundation, and I know that some efforts in this direction are already underway.
To be fair, Xgrid deals with a particularly difficult part of programming: networking. Networking tends to be difficult, because everything happens asynchronously. Networking activities are slow, and it's unacceptable for a program to lock up while a request is made to a server. Network programming thus tends to be nonlinear, with the instigation of an action being followed some time later by a "callback," a bit like the echo that follows your shout in a large cavern.
Apple has given developers an all-areas pass with XGridFoundation. It is very powerful, but the learning curve is steep. The Xgrid documentation in Xcode is quite sparse, consisting of automatically-generated class descriptions, and although there are sample projects (in
/Developer/Examples/Xgrid), they are fully-fledged applications, making it difficult to discern the forest for the trees. My objective here is to create one of the simplest Xgrid-enabled Cocoa applications imaginable, so that you get more forest and less tree.
Before I begin the tutorial proper, I need to send a big thanks to Charles Parnot, author of GridStuffer, and founder of the Xgrid@Stanford project. Charles probably knows more about XGridFoundation than anyone outside Apple, and he successfully helped the author avert a nasty outbreak of premature baldness during the background work for this tutorial.
As I mentioned earlier, the application we are going to develop is about as simple an Xgrid-Cocoa app as you could imagine. The intention is to strip everything back to the most basic steps required to write an Xgrid client. The application, which is called "Central Command," is basically a simplified, graphical version of the command-line
xgrid tool. You can download the Xcode project for Central Command here. To use it, you enter an Xgrid controller host name, an optional password, and a UNIX command, and then click the Run button. The command is submitted to the controller, which sends it to an Xgrid agent, where it is run. The output is returned via the controller to Central Command, which displays the results. It doesn't get much simpler than that!
Figure 1. The Central Command user interface
I'm going to focus on the aspects of Central Command that relate specifically to Xgrid, and not on the minutiae of developing a Cocoa application. I won't discuss how you use Interface Builder to create a user interface, for example, or how you connect up the elements of the interface to the attributes of classes using Cocoa Bindings. If these things are alien to you, you're probably already out of your depth. You'll need to have some Cocoa programming experience to grok what follows.