Permalink
Browse files

Daemon: The service name is now set automatically

Daemon: Server name and UUID are now put into the NSNetService TXT record
Device: Now pulls UUID out of the NSNetService TXT record
Device: Now much more robust about handling loss and discovery of available services
  • Loading branch information...
Michael Fey Michael Fey
Michael Fey authored and Michael Fey committed Aug 12, 2010
1 parent 6fd8a18 commit 822aceb99e369653a4fc5d540d553765099fd12e
Showing with 338 additions and 141 deletions.
  1. +97 −93 DesktopCode/ZSyncHandler.m
  2. +8 −2 DeviceCode/ZSyncTouchHandler.h
  3. +233 −46 DeviceCode/ZSyncTouchHandler.m
View
@@ -58,36 +58,36 @@ + (id)shared;
- (NSMutableArray*)connections
{
- if (_connections) return _connections;
- _connections = [[NSMutableArray alloc] init];
- return _connections;
+ if (_connections) return _connections;
+ _connections = [[NSMutableArray alloc] init];
+ return _connections;
}
- (NSManagedObjectContext*)managedObjectContext
{
- if (managedObjectContext) return managedObjectContext;
-
- NSString *path = [[NSBundle mainBundle] pathForResource:@"ZSyncModel" ofType:@"mom"];
- NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]];
-
- ZAssert(model != nil, @"Failed to find model at path: %@", path);
-
- NSString *basePath = [ZSyncDaemon basePath];
- NSError *error = nil;
- ZAssert([ZSyncDaemon checkBasePath:basePath error:&error], @"Failed to check base path: %@", error);
-
- NSString *filePath = [basePath stringByAppendingPathComponent:@"SyncHistory.sqlite"];
-
- NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
- ZAssert(psc != nil, @"Failed to initialize NSPersistentStoreCoordinator");
-
- ZAssert([psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:filePath] options:nil error:&error], @"Error adding persistent store: %@\n%@", [error localizedDescription], [error userInfo]);
-
- managedObjectContext = [[NSManagedObjectContext alloc] init];
- [managedObjectContext setPersistentStoreCoordinator:psc];
- [psc release], psc = nil;
- [model release], model = nil;
- return managedObjectContext;
+ if (managedObjectContext) return managedObjectContext;
+
+ NSString *path = [[NSBundle mainBundle] pathForResource:@"ZSyncModel" ofType:@"mom"];
+ NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]];
+
+ ZAssert(model != nil, @"Failed to find model at path: %@", path);
+
+ NSString *basePath = [ZSyncDaemon basePath];
+ NSError *error = nil;
+ ZAssert([ZSyncDaemon checkBasePath:basePath error:&error], @"Failed to check base path: %@", error);
+
+ NSString *filePath = [basePath stringByAppendingPathComponent:@"SyncHistory.sqlite"];
+
+ NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
+ ZAssert(psc != nil, @"Failed to initialize NSPersistentStoreCoordinator");
+
+ ZAssert([psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:filePath] options:nil error:&error], @"Error adding persistent store: %@\n%@", [error localizedDescription], [error userInfo]);
+
+ managedObjectContext = [[NSManagedObjectContext alloc] init];
+ [managedObjectContext setPersistentStoreCoordinator:psc];
+ [psc release], psc = nil;
+ [model release], model = nil;
+ return managedObjectContext;
}
#pragma mark -
@@ -108,10 +108,14 @@ - (void)startBroadcasting;
uuid = [[NSProcessInfo processInfo] globallyUniqueString];
[[NSUserDefaults standardUserDefaults] setValue:uuid forKey:zsServerUUID];
}
- NSString *broadcastName = [[self serverName] stringByAppendingFormat:@"%@%@", zsServerNameSeperator, uuid];
+// NSString *broadcastName = [[self serverName] stringByAppendingFormat:@"%@%@", zsServerNameSeperator, uuid];
- [_listener setBonjourServiceName:broadcastName];
+ [_listener setBonjourServiceName:@""];
+
+// [_listener setBonjourServiceName:broadcastName];
[_listener open];
+ NSDictionary *txtRecordDictionary = [NSDictionary dictionaryWithObjectsAndKeys:uuid, zsServerUUID, [self serverName], zsServerName, nil];
+ [_listener setBonjourTXTRecord:txtRecordDictionary];
}
- (void)stopBroadcasting;
@@ -122,87 +126,87 @@ - (void)stopBroadcasting;
- (void)connectionClosed:(ZSyncConnectionDelegate*)delegate;
{
- [[self connections] removeObject:delegate];
+ [[self connections] removeObject:delegate];
}
- (void)unregisterApplication:(NSManagedObject*)applicationObject;
{
- [[self managedObjectContext] deleteObject:applicationObject];
+ [[self managedObjectContext] deleteObject:applicationObject];
}
- (NSManagedObject*)registerDevice:(NSString*)deviceUUID withName:(NSString*)deviceName;
{
- NSManagedObjectContext *moc = [self managedObjectContext];
- NSFetchRequest *request = [[NSFetchRequest alloc] init];
-
- [request setEntity:[NSEntityDescription entityForName:@"Device" inManagedObjectContext:moc]];
- [request setPredicate:[NSPredicate predicateWithFormat:@"uuid == %@", deviceUUID]];
-
- NSError *error = nil;
- id device = [[moc executeFetchRequest:request error:&error] lastObject];
- [request release], request = nil;
- ZAssert(error == nil, @"Failed to retrieve device: %@\n%@", [error localizedDescription], [error userInfo]);
-
- if (!device) {
- device = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:moc];
- [device setValue:deviceUUID forKey:@"uuid"];
- }
-
- [device setValue:deviceName forKey:@"name"];
- return device;
+ NSManagedObjectContext *moc = [self managedObjectContext];
+ NSFetchRequest *request = [[NSFetchRequest alloc] init];
+
+ [request setEntity:[NSEntityDescription entityForName:@"Device" inManagedObjectContext:moc]];
+ [request setPredicate:[NSPredicate predicateWithFormat:@"uuid == %@", deviceUUID]];
+
+ NSError *error = nil;
+ id device = [[moc executeFetchRequest:request error:&error] lastObject];
+ [request release], request = nil;
+ ZAssert(error == nil, @"Failed to retrieve device: %@\n%@", [error localizedDescription], [error userInfo]);
+
+ if (!device) {
+ device = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:moc];
+ [device setValue:deviceUUID forKey:@"uuid"];
+ }
+
+ [device setValue:deviceName forKey:@"name"];
+ return device;
}
- (NSManagedObject*)registerApplication:(NSString*)schema withClient:(NSString*)clientUUID withDevice:(NSManagedObject*)device;
{
- //Find any other clients registered for this device with this schema and remove them
- NSSet *clients = [[device valueForKey:@"applications"] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"uuid != %@", clientUUID]];
- for (NSManagedObject *client in clients) {
- ISyncClient *syncClient = [[ISyncManager sharedManager] clientWithIdentifier:[client valueForKey:@"uuid"]];
- if (syncClient) {
- [[ISyncManager sharedManager] unregisterClient:syncClient];
- }
- [[self managedObjectContext] deleteObject:client];
- }
-
- id client = [[[device valueForKey:@"applications"] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"uuid == %@", clientUUID]] anyObject];
-
- if (client) return client;
-
- client = [NSEntityDescription insertNewObjectForEntityForName:@"Application" inManagedObjectContext:[self managedObjectContext]];
- [client setValue:device forKey:@"device"];
- [client setValue:clientUUID forKey:@"uuid"];
- [client setValue:schema forKey:@"schema"];
-
- NSError *error = nil;
- ZAssert([[self managedObjectContext] save:&error], @"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
-
- return client;
+ //Find any other clients registered for this device with this schema and remove them
+ NSSet *clients = [[device valueForKey:@"applications"] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"uuid != %@", clientUUID]];
+ for (NSManagedObject *client in clients) {
+ ISyncClient *syncClient = [[ISyncManager sharedManager] clientWithIdentifier:[client valueForKey:@"uuid"]];
+ if (syncClient) {
+ [[ISyncManager sharedManager] unregisterClient:syncClient];
+ }
+ [[self managedObjectContext] deleteObject:client];
+ }
+
+ id client = [[[device valueForKey:@"applications"] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"uuid == %@", clientUUID]] anyObject];
+
+ if (client) return client;
+
+ client = [NSEntityDescription insertNewObjectForEntityForName:@"Application" inManagedObjectContext:[self managedObjectContext]];
+ [client setValue:device forKey:@"device"];
+ [client setValue:clientUUID forKey:@"uuid"];
+ [client setValue:schema forKey:@"schema"];
+
+ NSError *error = nil;
+ ZAssert([[self managedObjectContext] save:&error], @"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
+
+ return client;
}
- (NSBundle*)pluginForSchema:(NSString*)schema;
{
- NSString *pluginPath = [ZSyncDaemon pluginPath];
- DLog(@"pluginPath %@", pluginPath);
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSError *error = nil;
- NSArray *pluginArray = [fileManager contentsOfDirectoryAtPath:pluginPath error:&error];
- ZAssert(pluginArray != nil && error == nil, @"Error fetching plugins: %@\n%@", [error localizedDescription], [error userInfo]);
-
- for (NSString *filename in pluginArray) {
- DLog(@"item found in plugin directory: '%@'", filename);
- if (![filename hasSuffix:@"zsyncPlugin"]) continue;
- NSString *pluginResourcePath = [pluginPath stringByAppendingPathComponent:filename];
- NSBundle *bundle = [NSBundle bundleWithPath:pluginResourcePath];
- NSString *schemaID = [[bundle infoDictionary] objectForKey:zsSchemaIdentifier];
- if ([[schemaID lowercaseString] isEqualToString:[schema lowercaseString]]) {
- DLog(@"plugin for '%@' found at %@", schema, pluginResourcePath);
- return bundle;
- }
- }
-
- DLog(@"failed to find plugin for schema '%@'", schema);
- return nil;
+ NSString *pluginPath = [ZSyncDaemon pluginPath];
+ DLog(@"pluginPath %@", pluginPath);
+
+ NSFileManager *fileManager = [NSFileManager defaultManager];
+ NSError *error = nil;
+ NSArray *pluginArray = [fileManager contentsOfDirectoryAtPath:pluginPath error:&error];
+ ZAssert(pluginArray != nil && error == nil, @"Error fetching plugins: %@\n%@", [error localizedDescription], [error userInfo]);
+
+ for (NSString *filename in pluginArray) {
+ DLog(@"item found in plugin directory: '%@'", filename);
+ if (![filename hasSuffix:@"zsyncPlugin"]) continue;
+ NSString *pluginResourcePath = [pluginPath stringByAppendingPathComponent:filename];
+ NSBundle *bundle = [NSBundle bundleWithPath:pluginResourcePath];
+ NSString *schemaID = [[bundle infoDictionary] objectForKey:zsSchemaIdentifier];
+ if ([[schemaID lowercaseString] isEqualToString:[schema lowercaseString]]) {
+ DLog(@"plugin for '%@' found at %@", schema, pluginResourcePath);
+ return bundle;
+ }
+ }
+
+ DLog(@"failed to find plugin for schema '%@'", schema);
+ return nil;
}
#pragma mark -
@@ -124,13 +124,14 @@ typedef enum {
ZSyncServerActionDeregister
} ZSyncServerAction;
-@interface ZSyncTouchHandler : NSObject <BLIPConnectionDelegate, ServerBrowserDelegate>
+@interface ZSyncTouchHandler : NSObject <BLIPConnectionDelegate, ServerBrowserDelegate, NSNetServiceDelegate>
{
NSTimer *networkTimer;
NSDate *findServerTimeoutDate;
NSMutableArray *storeFileIdentifiers;
NSMutableArray *availableServers;
+ NSMutableArray *discoveredServers;
ServerBrowser *_serviceBrowser;
BLIPConnection *_connection;
@@ -150,6 +151,8 @@ typedef enum {
NSPersistentStoreCoordinator *_persistentStoreCoordinator;
ZSyncServerAction serverAction;
+
+ NSLock *lock;
}
@property (nonatomic, assign) ZSyncServerAction serverAction;
@@ -158,6 +161,9 @@ typedef enum {
@property (nonatomic, assign) NSInteger majorVersionNumber;
@property (nonatomic, assign) NSInteger minorVersionNumber;
@property (nonatomic, copy) NSString *passcode;
+@property (retain) NSMutableArray *availableServers;
+@property (retain) NSMutableArray *discoveredServers;
+@property (retain) NSLock *lock;
/* This shared singleton design should probably go away. We cannot assume
* that the parent app will want to keep us around all of the time and may
@@ -181,6 +187,6 @@ typedef enum {
- (void)deregister;
- (NSString*)serverName;
-- (NSArray*)availableServers;
+//- (NSArray*)availableServers;
@end
Oops, something went wrong.

0 comments on commit 822aceb

Please sign in to comment.