Sample code for "simple" sharing using Bonjour?
Sample code for "simple" sharing using Bonjour?
- Subject: Sample code for "simple" sharing using Bonjour?
- From: Graham Cox <email@hidden>
- Date: Wed, 28 Jul 2010 22:32:34 +1000
Hi all,
I'm looking into adding local-area sharing of something within my app, which is analogous to playlists in iTunes. What I want is for users to be able to share these collections with other users of the app on a local network using a simple "share this" command. Any other machine then displays the shared collections that have been published in this way.
I feel I'm only understanding what I need to do in bits and pieces, and not really seeing the big picture. This is what I have so far:
I can publish a service using NSNetService.
I can look for other apps running the same service using NSNetServiceBrowser.
Having kicked these off I use delegate callbacks to receive notifications that a) the service has indeed been published and b) whenever any similar service is found on another machine.
So far so good, but then where I'm having more trouble is understanding how to communicate using the NSNetService objects associated with these things.
For each NSNetService I have (whether my own or one representing another machine), I create a wrapper object which gets the NSInputStream and NSOutputStream for each service, sets itself as the delegate, and schedules these streams on the current run loop. This wrapper is intended to act as the interface between the I/O streams and the high level objects that the app actually shares. Thus I end up with one wrapper object for each external machine on the network that is serving objects, plus one for my own (and here I'm assuming that having published a service, I can use the single NSOutputStream to talk to any of the interested clients whenever any of them ask for data???).
I noticed that 10.6 adds the following NSPropertyListSerialization methods:
+ (id)propertyListWithStream:(NSInputStream *)stream options:(NSPropertyListReadOptions)optformat:(NSPropertyListFormat *)format error:(NSError **)error;
+ (NSInteger)writePropertyList:(id)plist toStream:(NSOutputStream *)streamformat:(NSPropertyListFormat)format options:(NSPropertyListWriteOptions)opt error:(NSError**)error;
These seem to be potentially very useful for what I'm trying to do, since I can certainly send information about my served objects in the form of property lists, as they already use them to a large extent. Being new though, I can find very little information about them, when they should and should not be used, and any sample code. For example, should I use the -propertyListWithStream;... method in response to the NSStreamEventHasBytesAvailable event?
I've looked at the PictureSharing sample code but it seems a bit too simple, and searching for other examples has so far not turned up anything really appropriate. I've also looked at DO which might be appropriate but also possibly overkill for the task. The fact is I just can't figure out what is the best technology fit for this.
Is there any more realistic sample code out there that shows how to do something akin to iTunes playlist sharing?
Any other insights or comments welcome.
--Graham_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden