Strawberry advocacy with Zeroconf and DNSSD

Strawberry ice cream has a bad rep.
It’s not as sexy as chocolate and not as safe as plain vanilla. Instead it’s immediately relegated to third place in most ice cream debates.
Not satisfied with this situation I did what any coder would do and created a web site to evangalize strawberry ice cream using Sinatra.
Sinatra is great platform for quickly creating and deploying web applications, and works well for this example since the whole web site can be created with the following lines of code:
require 'rubygems' require 'sinatra' get '/' do 'I love Strawberry ice cream!' end
Now that I’ve got it running though, I realised it’s not going to do me much good if no one can find it. I could email a link to everyone or post it on Facebook or Twitter, but I want something that will work automatically and even if I don’t have access to the Internet. You may laugh and wonder how often that will be a problem but it’s something I ran into just this past weekend while at RailsCamp5 in the middle of the Australian hills. Cut off from the rest of the world we had no Internet and no tools to publish the web site. Or did we?
Applications like iTunes and Safari are able to find other computers and the services they offer without using an Apple server or the Internet. To test this for yourself just try sharing your iTunes music library with someone else on your network. If you unplug the Internet from your router the computers can still see each other and still see each others shared music. But how do they do this?
The answer is a magically technology called Zeroconf.
Zeroconf
Zeroconf is a way for computers to find the names and addresses of the other machines on their network and to register the services they provide without using a central server. Zeroconf started life in the labs at Apple where it was originally called Rendezvous, until it was renamed Bonjour for copyright reasons. Apple originally released implementations of Bonjour for windows under a proprietary license which proved controvertial with the open source community. This lead to the creation of the independent Avahi implementation which was released under a more liberal GPL license. Since the creation of Avahi Apple has relicensed Bonjour under an Apache-style license, however Avahi remains the most popular implementation on most Linux distributions. Interested readers should check out Zeroconf’s Wikipedia entry for all the dirt.
Zeroconf is a well supported technology and is available on Mac OS X, Windows, Linux. There’s even support for the iPhone and Android phones. It’s also got bindings or implementation written in C, C++, C#, Java, Python and Ruby. Neat, eh?
DNSSD
To unleash the power of Zeroconf in Ruby we’ll be using a ruby gem called DNSSD. This gem is a wrapper around Apple’s Bonjour library on Windows and Mac OS X. On Linux DNSSD wraps Avahi’s Bonjour compatibility layer. To develop using DNSSD you’ll need to make sure that you’ve installed these libraries or else DNSSD won’t compile. Windows and Mac users can download the required file from the Apple website. You’ll also need to install the developer tools on Mac OS X. I’d appreciate feedback from Windows users on what tools they’re using to develop with DNSSD.
Ubuntu users can install Avahi and it’s Bonjour compatibility layer by typing:
sudo apt-get install avahi-daemon avahi-utils libavahi-client-dev \ libavahi-common-dev libavahi-compat-libdnssd-dev libnss-mdns \ avahi-autoip
Download and install any dependencies these libraries require. If you haven’t already you’ll also need to grab the Ubuntu build tools:
sudo apt-get install build-essential
Once you’ve got the required Bonjour or Avahi libraries installed you can get DNSSD by running:
sudo gem install dnssd
Assuming everything works you’ll see the gem install build the native extensions, and then install the DNSSD documentation into rdoc and ri. I’ve tested this with DNSSD 0.7.1 and found it to work on Linux however I have had problems compiling DNSSD 0.6.0 in the past. If you see errors while compiling you should make sure your gem repository is pointing to the latest gem repository available on rubyforge.
If you’ve made it this far you’ve now got DNSSD installed. It’s worth taking a few seconds to start up your gem rdoc server:
gem server
and then browsing the DNSSD documentation. It’s a little outdated but most of the examples should work with only some minor modifcations.
In the next post we’ll put Zeroconf to use by using DNSSD to register our strawberry server on the network.










