|
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:
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.
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...
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.
Take a look at PictureSharing and PictureSharingBrowser. In
those look for NSNetService and NSNetServiceBrowser which are the Cocoa
level representation of Bonjour.
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
Just check my (old) article about distributed object and Cocoa
(http://www.stepwise.com/Articles/Technical/TrojanHorse.html)
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?
Hi,Nce one!!! I would be very happy if you can pass some code
snippets/sample of this to me.Thanks in advance.