Permalink
Browse files

Changed the zsServerNameSeparator

  • Loading branch information...
1 parent 77d2b22 commit f9924d7e86cd6824ad46ef1ceca72e547557381c Michael Fey committed Aug 11, 2010
Showing with 132 additions and 109 deletions.
  1. +6 −0 Daemon/ZSyncDaemon.xcodeproj/project.pbxproj
  2. +125 −108 DesktopCode/ZSyncHandler.m
  3. +1 −1 SharedCode/ZSyncShared.h
View
6 Daemon/ZSyncDaemon.xcodeproj/project.pbxproj
@@ -45,6 +45,9 @@
/* Begin PBXFileReference section */
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
+ 13337CFE121255150040AF6A /* ServerBrowser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ServerBrowser.h; sourceTree = "<group>"; };
+ 13337CFF121255150040AF6A /* ServerBrowser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ServerBrowser.m; sourceTree = "<group>"; };
+ 13337D00121255150040AF6A /* ServerBrowserDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ServerBrowserDelegate.h; sourceTree = "<group>"; };
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
8D1107320486CEB800E47090 /* ZSyncDaemon.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ZSyncDaemon.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -250,6 +253,9 @@
B691FB9810ED879D00207210 /* SharedCode */ = {
isa = PBXGroup;
children = (
+ 13337CFE121255150040AF6A /* ServerBrowser.h */,
+ 13337CFF121255150040AF6A /* ServerBrowser.m */,
+ 13337D00121255150040AF6A /* ServerBrowserDelegate.h */,
B691FB9910ED879D00207210 /* BLIP.h */,
B691FB9A10ED879D00207210 /* BLIPConnection.h */,
B691FB9B10ED879D00207210 /* BLIPDispatcher.h */,
View
233 DesktopCode/ZSyncHandler.m
@@ -39,6 +39,9 @@ @implementation ZSyncHandler
@synthesize connections = _connections;
@synthesize serverName = _serverName;
+#pragma mark -
+#pragma mark Class methods
+
+ (id)shared;
{
static ZSyncHandler *zsSharedSyncHandler;
@@ -50,13 +53,46 @@ + (id)shared;
return zsSharedSyncHandler;
}
+#pragma mark -
+#pragma mark Overridden getters/setters
+
- (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;
}
+#pragma mark -
+#pragma mark Local methods
+
- (void)startBroadcasting;
{
_listener = [[BLIPListener alloc] initWithPort: 1123];
@@ -84,131 +120,112 @@ - (void)stopBroadcasting;
[_listener release], _listener = nil;
}
-- (void)listener:(TCPListener*)listener didAcceptConnection:(BLIPConnection*)connection
+- (void)connectionClosed:(ZSyncConnectionDelegate*)delegate;
{
- DLog(@"fired");
- ZSyncConnectionDelegate *delegate = [[ZSyncConnectionDelegate alloc] init];
- [delegate setConnection:connection];
- [connection setDelegate:delegate];
- [[self connections] addObject:delegate];
- [delegate release], delegate = nil;
+ [[self connections] removeObject:delegate];
}
-- (void)connection:(TCPConnection*)connection failedToOpen:(NSError*)error
+- (void)unregisterApplication:(NSManagedObject*)applicationObject;
{
- DLog(@"entered");
+ [[self managedObjectContext] deleteObject:applicationObject];
}
-- (void)connectionClosed:(ZSyncConnectionDelegate*)delegate;
+- (NSManagedObject*)registerDevice:(NSString*)deviceUUID withName:(NSString*)deviceName;
{
- [[self connections] removeObject:delegate];
+ 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*)managedObjectContext
+- (NSManagedObject*)registerApplication:(NSString*)schema withClient:(NSString*)clientUUID withDevice:(NSManagedObject*)device;
{
- 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;
+ //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;
}
-- (void)unregisterApplication:(NSManagedObject*)applicationObject;
+- (NSBundle*)pluginForSchema:(NSString*)schema;
{
- [[self managedObjectContext] deleteObject:applicationObject];
+ 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;
}
-- (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;
-}
+#pragma mark -
+#pragma mark TCPListenerDelegate methods
-- (NSManagedObject*)registerApplication:(NSString*)schema withClient:(NSString*)clientUUID withDevice:(NSManagedObject*)device;
+- (void)listener:(TCPListener*)listener didAcceptConnection:(BLIPConnection*)connection
{
- //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;
+ DLog(@"fired");
+ ZSyncConnectionDelegate *delegate = [[ZSyncConnectionDelegate alloc] init];
+ [delegate setConnection:connection];
+ [connection setDelegate:delegate];
+ [[self connections] addObject:delegate];
+ [delegate release], delegate = nil;
}
-- (NSBundle*)pluginForSchema:(NSString*)schema;
+#pragma mark -
+#pragma mark BLIPConnectionDelegate methods
+
+- (void)connection:(TCPConnection*)connection failedToOpen:(NSError*)error
{
- 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;
+ DLog(@"entered");
}
+#pragma mark -
+
@end
View
2 SharedCode/ZSyncShared.h
@@ -33,7 +33,7 @@
#define zsDeviceGUID @"zsDeviceGUID"
#define zsSchemaIdentifier @"ZSyncSchemaIdentifier"
-#define zsServerNameSeperator @"**/**"
+#define zsServerNameSeperator @"ZSyncServerNameSeparator"
#define zsActID(__ENUM__) [NSString stringWithFormat:@"%i", __ENUM__]

0 comments on commit f9924d7

Please sign in to comment.