The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,551,710
since: 19 Jan 2005

Distributed Objects and Bonjour : It's like Peanut Butter and Chocolate!

posted Fri 13 Apr 07

I recently sat down with Cocoa and decide that I was going to write something a bit more complex than just sample hello world stuff. If you've been reading my blog, you probably know what my next move was. I typically ask two questions of new technology:

  • Can you game with it?
  • Can you network it?

So, I thought - I wonder what it would be like to do some down and dirty networking with Cocoa? There were a couple of options available to me, as there are plenty of different networking APIs, but I wanted to try something high level and Distributed Objects stuck out as something cool worth looking into.

Basically the way DO's work is this: In the host application you create a connection on a given port (this can be a private intra-thread port, an IPC-style machine-local port, or a TCP-based socket port) and then you put your hosted object on the "root" of that connection. This is called "vending", though some texts and books I've found don't actually call it that.

Once you've got a host vending an object, you can then create your client. To create the client, you create a connection on a port, and then you ask the connection to give you the object on the root context (which will be the object vended by the server). You are now free to invoke methods on that object as if it were local. Objective-C's dynamic message passing nature and the ability to serialize invocations onto a wire format make the inclusion of transparent proxies that are doing the forwarding/marshalling seamless.

Now.. here's where it gets really cool. Say you've got a server hosting an object on your LAN. Let's say that you're in a newspaper office and this server is hosting an object that accepts article submissions. Let's say there's often one of these servers for each department (say, Obits, Headlines, Sports, Entertainment, etc). How do you make it so that you can submit to these servers without having to hand-code the IP address of each server into the client's configuration file? Bonjour, of course.

Bonjour is the latest incarnation of what used to be Rendezvous, a mini-DNS protocol for service publication in a network. To continue the scenario, when the article submission servers come up, they each register the fact that they are hosting a service (call it _articlepub._tcp. for simplicity).

Again, it just gets cooler from here. When the writer has finished editing their article and everything looks great, they hit the "Publish" button. Up comes a sheet with a list of all valid publication locations (basically a list of ports available on the LAN that are currently advertising the fact that they are running an instance of the _articlepub._tcp. service). Click the location you want (the user sees things like "Obit" "Sports" "Entertainment", etc), and then the client application uses the information from Bonjour to establish a connection to the Distributed Object, invokes the submitArticle:fromClient: method and Bob's your Uncle.

There are dozens of sample apps floating around the internet that do this, but if you folks think you'd be interested, let me know and I'll work up a sample of this scenario and post some code snippets.

tags:                

links: digg this    del.icio.us    technorati    reddit




1. Shayan Ostad Hassan left...
Fri 13 Apr 07 11:27 am

Hi! I always wanted to try networking in cocoa, but I've never gotten into it... I'd be happy if you post some snippets to jump start me! Thanks in advance...


2. James Williams left...
Fri 13 Apr 07 2:23 pm

I'm a .Net developer in my day job and a budding Cocoa programmer in my spare time, so I've been following your blog with interest. I'd love to see your examples of working with this tech.


3. Shawn left...
Sat 14 Apr 07 10:35 am

Take a look at PictureSharing and PictureSharingBrowser. In those look for NSNetService and NSNetServiceBrowser which are the Cocoa level representation of Bonjour.

Not these examples use a simple socket based system to do the communication between server and client... it doesn't use distributed objects.

Humm cannot find the distributed object sample code I am looking for...


4. Shawn left...
Sat 14 Apr 07 10:42 am

Cocoa Dev Central has many nice articles on stuff like this... ah yeah here is one DO and stepping back to your bindings related posts Cocoa Bindings Intro


5. Lionel Costa left...
Mon 16 Apr 07 12:09 pm

Just check my (old) article about distributed object and Cocoa (http://www.stepwise.com/Articles/Technical/TrojanHorse.html)


6. Sam Stigler left...
Sat 28 Apr 07 2:01 am

Thanks for posting this; I have been curious about networking on the Mac for a while but had always found it to be much lower-level than in Java (which is what I used before Cocoa). Now, thanks to your post, I'll take a much closer looks at Distributed Objects. Just to be clear, are they basically the equivalent of RMI (Remote Method Invocation) in Java?


7. abdul.majeed left...
Tue 04 Dec 07 11:17 pm

Hi,Nce one!!! I would be very happy if you can pass some code snippets/sample of this to me.Thanks in advance.


Tag Related Posts

MobileMe vs. Live Mesh Throwdown - Round 1

Wed 16 Jul 08 10:33 A GMT-05

Building Model Classes in C# and Cocoa

Sun 15 Jun 08 3:13 P GMT-05
tags:            

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

My Macbook Air is masculine, dammit!

Mon 17 Mar 08 6:59 P GMT-05
tags:          

iPhone Underrated as a Gaming Device?

Fri 14 Mar 08 1:50 P GMT-05
tags:        

My take on the iPhone SDK

Sat 08 Mar 08 1:39 P GMT-05

Jobs says "not likely" to Flash on the iPhone

Thu 06 Mar 08 1:39 A GMT-05
tags:          

My Macbook Air Review

Sun 02 Mar 08 4:20 P GMT-05

iPhone Roadmap March 6th

Fri 29 Feb 08 10:41 P GMT-05
tags:        

Video of the Macbook Air in Action

Wed 20 Feb 08 3:04 P GMT-05

Macbook Airはきれいですよ!

Sun 17 Feb 08 2:38 A GMT-05

Why is O'Reilly Condoning iPhone Hacking?

Mon 11 Feb 08 3:55 P GMT-05

Evaluating my next laptop purchase

Wed 06 Feb 08 8:40 P GMT-05

The iPhone SDK key has been leaked! Oh Noez!!!1

Tue 29 Jan 08 11:36 A GMT-05
tags:        

Why Geeks just don't "get" the Macbook Air

Thu 17 Jan 08 2:30 P GMT-05

Popcorn + TiVo + Macbook Pro + iPhone == Hell Yeah!

Tue 15 Jan 08 3:11 P GMT-05
tags:          

How my ADC membership changed my life

Mon 31 Dec 07 3:57 P GMT-05
tags:      

Leopard Code Sample : Sprinkling in some Bonjour

Tue 27 Nov 07 2:32 P GMT-05
tags:        

Leopard Sample: A Bound NSCollectionView

Mon 29 Oct 07 1:41 A GMT-05

Leopard is out - let the code samples begin!

Fri 26 Oct 07 10:09 A GMT-05
tags:          

My life is complete : iPhone SDK is CONFIRMED.

Wed 17 Oct 07 6:38 P GMT-05
tags:          

Leopard Shipping October 26th!!

Tue 16 Oct 07 4:59 P GMT-05
tags:        

My iPhone Review

Mon 23 Jul 07 1:09 P GMT-05
tags:        

Microsoft Codename Acropolis - Unwrapped

Wed 20 Jun 07 3:22 P GMT-05
tags:              

The dreaded language bleed-over has begun

Tue 19 Jun 07 6:23 P GMT-05
tags:        

My TiVo is using Bonjour...

Mon 18 Jun 07 12:57 A GMT-05
tags:    

Exploring the Delegate Design Pattern

Mon 14 May 07 6:30 P GMT-05

Core Data - Almost too Easy?

Wed 18 Apr 07 2:23 P GMT-05

Will Silverlight be DOA?

Mon 16 Apr 07 8:02 P GMT-05