140 likes | 160 Views
Learn Bonjour programming for automatic address distribution, service publishing, and discovery in iPhone applications. Explore the benefits of Bonjour in LANs and follow step-by-step guides for service naming, publishing, and resolving.
E N D
EEC-492/693/793iPhone Application Development Lecture 14 Wenbing Zhao & Nigamanth Sridhar EEC492/693/793 - iPhone Application Development
Outline Bonjour Programming Publish a service on the network Discover services on the network Resolve IP address of services on the network Assignments: Build the Bonjour app (based on the handout) 1/7/2020 2 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development
Bonjour • Three main functions: • Automate address distribution and name mapping • Publish availability of a service • Discover available services • Open protocol Apple submitted to IETF • www.zeroconf.org EEC492/693/793 - iPhone Application Development
Bonjour • Makes LANs self configuring • Requires no administration • Assign addresses without a DHCP server • Map names to addresses without a DNS server • Find services without a directory server EEC492/693/793 - iPhone Application Development
Automatic Addressing • Bonjour will pick a random address, see if it is in use • If it is not in use, it’s yours • If it is in use, try again • Uses “.local.” as a virtual top-level domain • For example: iPhone3G.local. EEC492/693/793 - iPhone Application Development
Advertising Services • Applications provide a service name and port • Follows same DNS specific-to-general model • ServiceName._ServiceType._TransportProtocolName.Domain • Service Name is a human readable descriptive name • Maximum of 63 octets of UTF-8 • All characters are allowed • Service Type is an IANA registered protocol name • Maximum of fourteen characters • Format of [a-z0-9]([a-z0-9\-]*[a-z0-9])? • Transport Protocol Name is either TCP or UDP EEC492/693/793 - iPhone Application Development
Service Naming • Canon MP780._ipp._tcp.local. • Canon MP780: Service name • ipp: Service type (Internet printing protocol) • tcp: transport protocol • local.: top level domain EEC492/693/793 - iPhone Application Development
Publishing a Service • NSNetService is used to publish services via Bonjour NSNetService *service; service = [[NSNetService alloc] initWithDomain:@”” type:@”_ipp._tcp” name:@”Canon MP780” port:4721]; • Leaving domain blank implies “.local.” • Leaving name blank will use the device’s iTunes name EEC492/693/793 - iPhone Application Development
Publishing a Service • NSNetService is entirely asynchronous // Set up delegate to receive callbacks [service setDelegate:self]; // or: server.delegate = self; [service publish]; • Always remember to unset the delegate in dealloc! - (void)dealloc { [service setDelegate:nil]; [service stop]; [service release]; [super dealloc]; } EEC492/693/793 - iPhone Application Development
NSNetService Delegate Methods • Conflict resolution handled automatically • Status is communicated to the delegate - (void)netServiceWillPublish:(NSNetService *)sender - (void)netServiceDidPublish:(NSNetService *)sender - (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary *)errorDict • errorDict is like an NSError - has two keys, one for error domain and one for error code. EEC492/693/793 - iPhone Application Development
Finding a Service • Applications register service names with local daemon which handles responding to lookup queries • Service discovery is completely independent of service implementation • Resolving a service gives you an address and a port • Can also get NSStreams pointing to that location EEC492/693/793 - iPhone Application Development
Finding a Service • NSNetServiceBrowser is used to search for services on the network NSNetServiceBrowser *browser; browser = [[NSNetServiceBrowser alloc] init]; [browser setDelegate:self]; [browser searchForServicesOfType:@”_ipp._tcp.” inDomain:@””]; EEC492/693/793 - iPhone Application Development
NSNetServiceBrowser Delegate Methods • NSNetServiceBrowser browsing is also asynchronous • Delegate methods called as services come and go - (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)browser - (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)browser - (void)netServiceBrowser:(NSNetServiceBrowser *)browser didNotSearch:(NSDictionary *)errorInfo - (void)netServiceBrowser:(NSNetServiceBrowser *)browser didFindService:(NSNetService *)service moreComing:(BOOL)more - (void)netServiceBrowser:(NSNetServiceBrowser *)browser didRemoveService:(NSNetService *)service moreComing:(BOOL)more EEC492/693/793 - iPhone Application Development
Service Resolution • NSNetServices found by NSNetServiceBrowser must have their addresses resolved before use: [netService setDelegate:self]; [netService resolveWithTimeout:5]; • Status communicated asynchronously to delegate: - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict; • Same errorDict as before - (void)netServiceDidResolveAddress:(NSNetService *)sender; • Once a service has been resolved you can use the address information to connect to it EEC492/693/793 - iPhone Application Development