Database Reference
In-Depth Information
This bit of C code determines whether the address received from the NSSocketPort
is IPv4 or IPv6 and, based on that decision, extracts the port information from
the address and returns it to the caller.
With the port number in hand, we next construct an NSConnection and assign
the AppDelegate as the root object. The root object is what will be “proxied” to
any clients, and any methods they call on that proxy object will be transferred
to the root object on the receiver. In a more complex example, it would make
sense to have a separate object used as the proxy instead of the AppDelegate .
With the NSConnection created, we can initialize the NSNetService . The NSNetService
handles the broadcasting using Bonjour. Bonjour requires four pieces of
information: the domain, type, name, and port. We discovered the port previ-
ously, and we defined the domain and type within the PPDistributedProtocol . The
last value is the name of server and should be unique per machine. With this
information, we can instantiate the NSNetService , set its delegate, and publish
it. Once we call -publish , other machines can see the service.
Starting the Server
The -startBroadcasting method is invoked from -applicationDidFinishLaunching:
DistributedCDServer/AppDelegate.m
- ( void )applicationDidFinishLaunching:(NSNotification*)notification
{
[self startBroadcasting];
saveTimer = [NSTimer scheduledTimerWithTimeInterval:(5.0 * 60.0)
target:self
selector: @selector (saveAction:)
userInfo:nil
repeats:YES];
}
In addition to broadcasting the service on start-up, we schedule an autosave
of the NSManagedObjectContext . In this example, we automatically save every five
minutes.
DistributedCDServer/AppDelegate.m
- (IBAction)saveAction:(id)sender
{
NSError *error = nil;
NSManagedObjectContext *context = [self managedObjectContext];
if (![context hasChanges]) return ;
if (![context save:&error]) {
[self logError:error];
}
}
 
 
 
Search WWH ::




Custom Search