Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 822aceb99e
Fetching contributors…

Cannot retrieve contributors at this time

file 992 lines (852 sloc) 37.936 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
//
// ZSyncTouchHandler.m
// SampleTouch
//
// Created by Marcus S. Zarra on 11/11/09.
// Copyright 2009 Zarra Studios, LLC. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#import "ZSyncTouchHandler.h"
#import "ZSyncShared.h"
#import "ServerBrowser.h"


#import "Reachability.h"

#define zsUUIDStringLength 55

@interface ZSyncTouchHandler()

- (void)requestDeregistration;
- (void)uploadDataToServer;

@property (nonatomic, assign) id delegate;
@property (nonatomic, retain) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@end

@implementation ZSyncTouchHandler

+ (id)shared;
{
  static ZSyncTouchHandler *sharedTouchHandler;
  if (sharedTouchHandler) return sharedTouchHandler;
  @synchronized(sharedTouchHandler) {
    sharedTouchHandler = [[ZSyncTouchHandler alloc] init];
    [[NSNotificationCenter defaultCenter] addObserver:sharedTouchHandler
                                             selector:@selector(applicationWillTerminate:)
                                                 name:UIApplicationWillTerminateNotification
                                               object:nil];
  }
  return sharedTouchHandler;
}

- (void)applicationWillTerminate:(NSNotification*)notification
{
  DLog(@"closing connection");
  if ([self connection]) {
    [[self connection] close];
  }
  if ([self serviceBrowser]) {
    [[self serviceBrowser] setDelegate:nil];
    [[self serviceBrowser] stop];
    [_serviceBrowser release], _serviceBrowser = nil;
  }
}

- (NSString*)cachePath
{
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  NSString *filePath = [paths objectAtIndex:0];
  
  return filePath;
}

- (void)networkTimeout:(NSTimer*)timer
{
  DLog(@"timeout on local network");
  networkTimer = nil;
  [self setServerAction:ZSyncServerActionNoActivity];
  if ([[self delegate] respondsToSelector:@selector(zSyncServerUnavailable:)]) {
    [[self delegate] zSyncServerUnavailable:self];
  }
  [[timer userInfo] stopNotifer];
}

- (void)reachabilityChanged:(NSNotification*)notification
{
  Reachability *reachability = [notification object];
  if ([reachability currentReachabilityStatus] == NotReachable) return;
  DLog(@"local network now available");
  [reachability stopNotifer];
  [networkTimer invalidate], networkTimer = nil;
  NSString *serverUUID = [[NSUserDefaults standardUserDefaults] valueForKey:zsServerUUID];
  if (serverUUID) {
    networkTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
                            target:self
                            selector:@selector(networkTimeout:)
                            userInfo:nil
                             repeats:NO];
  }
  [serverUUID release], serverUUID = nil;
  [[self serviceBrowser] start];
}

- (void)startServerSearch
{
  if ([self serviceBrowser]) {
    DLog(@"service browser is not nil");
    [[self serviceBrowser] setDelegate:nil];
    [[self serviceBrowser] stop];
    [self setServiceBrowser:nil];
  }
  
  _serviceBrowser = [[ServerBrowser alloc] init];
  _serviceBrowser.delegate = self;
  
  Reachability *reachability = [Reachability reachabilityForLocalWiFi];
  if ([reachability currentReachabilityStatus] == NotReachable) {
    DLog(@"local network not available");
    //start notifying
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];
    
    [reachability startNotifer];
    networkTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
                                                    target:self
                                                  selector:@selector(networkTimeout:)
                                                  userInfo:reachability
                                                   repeats:NO];
    
    
    return;
  } else {
    NSString *serverUUID = [[NSUserDefaults standardUserDefaults] valueForKey:zsServerUUID];
    if (serverUUID) {
      networkTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
                              target:self
                            selector:@selector(networkTimeout:)
                            userInfo:reachability
                             repeats:NO];
    }
    [serverUUID release], serverUUID = nil;
    [[self serviceBrowser] start];
  }
}

- (void)requestSync;
{
  ZAssert([self serverAction] == ZSyncServerActionNoActivity, @"Attempt to sync while another action is active");
  [self setServerAction:ZSyncServerActionSync];
  if ([self connection]) {
    [self uploadDataToServer];
  } else {
    [self startServerSearch];
  }
}

- (void)stopRequestingSync
{
  [[self serviceBrowser] setDelegate:nil];
  [[self serviceBrowser] stop];
  [self setServerAction:ZSyncServerActionNoActivity];
}

- (void)deregister;
{
  DLog(@"deregister request received");
  if ([self serverAction] != ZSyncServerActionNoActivity) {
    if ([[self delegate] respondsToSelector:@selector(zSync:errorOccurred:)]) {
      NSString *errorString = [NSString stringWithFormat:@"Another activity in progress: %i", [self serverAction]];
      NSDictionary *dict = [NSDictionary dictionaryWithObject:errorString forKey:NSLocalizedDescriptionKey];
      NSError *error = [NSError errorWithDomain:zsErrorDomain code:zsErrorAnotherActivityInProgress userInfo:dict];
      [[self delegate] zSync:self errorOccurred:error];
      return;
    }
  }
  if (![[NSUserDefaults standardUserDefaults] valueForKey:zsServerUUID] || ![[NSUserDefaults standardUserDefaults] valueForKey:zsDeviceID]) {
    if ([[self delegate] respondsToSelector:@selector(zSync:errorOccurred:)]) {
      NSString *errorString = [NSString stringWithFormat:@"Client is not registered with a server", [self serverAction]];
      NSDictionary *dict = [NSDictionary dictionaryWithObject:errorString forKey:NSLocalizedDescriptionKey];
      NSError *error = [NSError errorWithDomain:zsErrorDomain code:zsErrorNoSyncClientRegistered userInfo:dict];
      [[self delegate] zSync:self errorOccurred:error];
      return;
    }
  }
  [self setServerAction:ZSyncServerActionDeregister];
  if ([self connection]) {
    DLog(@"requesting deregister");
    [self requestDeregistration];
  } else {
    DLog(@"searching for server");
    [self startServerSearch];
  }
}

- (void)disconnectPairing;
{
  [[NSUserDefaults standardUserDefaults] removeObjectForKey:zsServerName];
  [[NSUserDefaults standardUserDefaults] removeObjectForKey:zsServerUUID];
  if ([self connection]) {
    [[self connection] close];
    [self setConnection:nil];
  }
  [self setServerAction:ZSyncServerActionNoActivity];
}

- (NSString*)serverName;
{
  return [[NSUserDefaults standardUserDefaults] valueForKey:zsServerName];
}

- (void)cancelPairing;
{
  if (![self connection]) return;
  
  //Start a pairing request
  DLog(@"sending a pairing cancel");
  NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  [dictionary setValue:zsActID(zsActionCancelPairing) forKey:zsAction];
  
  NSString *deviceUUID = [[NSUserDefaults standardUserDefaults] valueForKey:zsDeviceID];
  if (!deviceUUID) {
    deviceUUID = [[NSProcessInfo processInfo] globallyUniqueString];
    [[NSUserDefaults standardUserDefaults] setValue:deviceUUID forKey:zsDeviceID];
  }
  
  [dictionary setValue:deviceUUID forKey:zsDeviceID];
  
  BLIPRequest *request = [BLIPRequest requestWithBody:nil properties:dictionary];
  [[self connection] sendRequest:request];
}

- (void)requestPairing:(ZSyncService*)server;
{
  NSNetService *service = [server service];
  BLIPConnection *conn = [[BLIPConnection alloc] initToNetService:service];
  [self setConnection:conn];
  [conn setDelegate:self];
  [conn open];
  [conn release], conn = nil;
}

- (void)authenticatePairing:(NSString*)code;
{
  if (![self connection]) return;
  
  //Start a pairing request
  DLog(@"sending a pairing code");
  NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  [dictionary setValue:zsActID(zsActionAuthenticatePairing) forKey:zsAction];
  
  NSString *deviceUUID = [[NSUserDefaults standardUserDefaults] valueForKey:zsDeviceID];
  if (!deviceUUID) {
    deviceUUID = [[NSProcessInfo processInfo] globallyUniqueString];
    [[NSUserDefaults standardUserDefaults] setValue:deviceUUID forKey:zsDeviceID];
  }
  [dictionary setValue:deviceUUID forKey:zsDeviceID];

  NSData *codeData = [code dataUsingEncoding:NSUTF8StringEncoding];
  BLIPRequest *request = [BLIPRequest requestWithBody:codeData properties:dictionary];
  [[self connection] sendRequest:request];
}

- (void)beginSyncWithService:(NSNetService*)service
{
  BLIPConnection *conn = [[BLIPConnection alloc] initToNetService:service];
  [self setConnection:conn];
  [conn setDelegate:self];
  [conn open];
  [conn release], conn = nil;
}

#pragma mark -
#pragma mark Overridden getters/setter

- (NSMutableArray *)availableServers
{
  if (!availableServers) {
    availableServers = [[NSMutableArray alloc] init];
  }
  
  return availableServers;
}

- (NSMutableArray *)discoveredServers
{
  if (!discoveredServers) {
    discoveredServers = [[NSMutableArray alloc] init];
  }
  
  return discoveredServers;
}

- (NSObject *)lock
{
  if (!lock) {
    lock = [[NSObject alloc] init];
  }
  
  return lock;
}

#pragma mark -
#pragma mark ServerBrowserDelegate methods

- (void)updateServerList
{
  [lock lock];
  NSLog(@"START %s", __PRETTY_FUNCTION__);
  [networkTimer invalidate], networkTimer = nil;
  [[self availableServers] removeAllObjects];
    for (NSNetService *service in [self discoveredServers]) {
    [service setDelegate:nil];
    [service stop];
  }
  [[self discoveredServers] removeAllObjects];

  [[self discoveredServers] addObjectsFromArray:[_serviceBrowser servers]];
    for (NSNetService *service in [self discoveredServers]) {
    NSLog(@"%@", [service name]);
    [service setDelegate:self];
    [service resolveWithTimeout:15.0];
  }
  
  if (![self discoveredServers] || [[self discoveredServers] count] == 0) {
    NSString *serverUUID = [[NSUserDefaults standardUserDefaults] valueForKey:zsServerUUID];
    if (!serverUUID) {
      [[self delegate] zSyncNoServerPaired:[self availableServers]];
    }
  } else {
    NSString *serverUUID = [[NSUserDefaults standardUserDefaults] valueForKey:zsServerUUID];
    if (serverUUID) {
      networkTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
                              target:self
                              selector:@selector(networkTimeout:)
                              userInfo:nil
                               repeats:NO];
    }
    
  }

  NSLog(@"END %s", __PRETTY_FUNCTION__);
  [lock unlock];
}

#pragma mark -
#pragma mark NSNetServiceDelegate methods

/* Sent to the NSNetService instance's delegate prior to resolving a service on the network. If for some reason the resolution cannot occur, the delegate will not receive this message, and an error will be delivered to the delegate via the delegate's -netService:didNotResolve: method.
*/
- (void)netServiceWillResolve:(NSNetService *)sender
{
  NSLog(@"%s", __PRETTY_FUNCTION__);
}

/* Sent to the NSNetService instance's delegate when one or more addresses have been resolved for an NSNetService instance. Some NSNetService methods will return different results before and after a successful resolution. An NSNetService instance may get resolved more than once; truly robust clients may wish to resolve again after an error, or to resolve more than once.
*/
- (void)netServiceDidResolveAddress:(NSNetService *)bonjourService
{
  NSLog(@"%s", __PRETTY_FUNCTION__);
  
  NSString *serverUUID = [[NSUserDefaults standardUserDefaults] valueForKey:zsServerUUID];
  
  @try {
    if (!serverUUID) { //See if the server is in this list
      NSString *serverName = [bonjourService name];
      DLog(@"[bonjourService TXTRecordData] %@", [bonjourService TXTRecordData]);
      NSDictionary *txtRecordDictionary = [NSNetService dictionaryFromTXTRecordData:[bonjourService TXTRecordData]];
      if (!txtRecordDictionary) {
        DLog(@"The NSNetService named %@ did not contain a TXT record", [bonjourService name]);
        return;
      }
      
      NSData *serverUUIDData = [txtRecordDictionary objectForKey:zsServerUUID];
      NSString *serverUUID = [[NSString alloc] initWithData:serverUUIDData encoding:NSUTF8StringEncoding];
      if (!serverUUID || [serverUUID length] == 0) {
        DLog(@"The TXT record did not contain a UUID.");
        [serverUUID release], serverUUID = nil;
        return;
      }
      
      ZSyncService *zSyncService = [[ZSyncService alloc] init];
      [zSyncService setService:bonjourService];
      [zSyncService setName:serverName];
      [zSyncService setUuid:serverUUID];
      [[self availableServers] addObject:zSyncService];
      [zSyncService release], zSyncService = nil;
      
      [[self delegate] zSyncNoServerPaired:[self availableServers]];
      
      [serverUUID release], serverUUID = nil;
      
      return;
    }
    
    NSDictionary *txtRecordDictionary = [NSNetService dictionaryFromTXTRecordData:[bonjourService TXTRecordData]];
    if (!txtRecordDictionary) {
      DLog(@"Found a serverUUID, but the NSNetService named %@ did not contain a TXT record", [bonjourService name]);
      return;
    }
    
    NSData *incomingServerUUIDData = [txtRecordDictionary objectForKey:zsServerUUID];
    if (!incomingServerUUIDData) return;

    NSString *incomingServerUUID = [[NSString alloc] initWithData:incomingServerUUIDData encoding:NSUTF8StringEncoding];
    // NSString *incomingServerUUID = [components objectAtIndex:1];
    if (![incomingServerUUID isEqualToString:serverUUID]) {
      [incomingServerUUID release], incomingServerUUID = nil;
      return;
    }
    
    [networkTimer invalidate], networkTimer = nil;
    DLog(@"our server found");
    //Found our server, start the sync
    [self beginSyncWithService:bonjourService];
    return;
  } @finally {
    // Not sure what to put in here, we don't need to stop the service browser each time anymore.
  }
}

/* Sent to the NSNetService instance's delegate when an error in resolving the instance occurs. The error dictionary will contain two key/value pairs representing the error domain and code (see the NSNetServicesError enumeration above for error code constants).
*/
- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
{
  NSLog(@"%s", __PRETTY_FUNCTION__);
  [[self discoveredServers] removeObject:sender];

  //Did not find our registered server. Fail
  if ([[self discoveredServers] count] == 0) {
    [[self delegate] zSyncServerUnavailable:self];
    [self setServerAction:ZSyncServerActionNoActivity];
  }
}

/* Sent to the NSNetService instance's delegate when the instance's previously running publication or resolution request has stopped.
*/
- (void)netServiceDidStop:(NSNetService *)sender
{
  NSLog(@"%s", __PRETTY_FUNCTION__);
// if ([[self discoveredServers] count] == 0) {
// [[self delegate] zSyncServerUnavailable:self];
// [self setServerAction:ZSyncServerActionNoActivity];
// }
}

/* Sent to the NSNetService instance's delegate when the instance is being monitored and the instance's TXT record has been updated. The new record is contained in the data parameter.
*/
- (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data
{
  NSLog(@"%s", __PRETTY_FUNCTION__);
}

#pragma mark -

- (void)registerDelegate:(id<ZSyncDelegate>)delegate withPersistentStoreCoordinator:(NSPersistentStoreCoordinator*)coordinator;
{
  [self setDelegate:delegate];
  [self setPersistentStoreCoordinator:coordinator];
}

- (void)receiveFile:(BLIPRequest*)request
{
  ZAssert([request complete], @"Message is incomplete");
  DLog(@"file received");
  if (!receivedFileLookupDictionary) {
    receivedFileLookupDictionary = [[NSMutableDictionary alloc] init];
  }
  NSMutableDictionary *fileDict = [[NSMutableDictionary alloc] init];
  [fileDict setValue:[request valueOfProperty:zsStoreIdentifier] forKey:zsStoreIdentifier];
  if (![[request valueOfProperty:zsStoreConfiguration] isEqualToString:@"PF_DEFAULT_CONFIGURATION_NAME"]) {
    [fileDict setValue:[request valueOfProperty:zsStoreConfiguration] forKey:zsStoreConfiguration];
  }
  [fileDict setValue:[request valueOfProperty:zsStoreType] forKey:zsStoreType];
  
  NSString *tempFilename = [[NSProcessInfo processInfo] globallyUniqueString];
  NSString *tempPath = [[self cachePath] stringByAppendingPathComponent:tempFilename];
  DLog(@"file written to \n%@", tempPath);
  
  DLog(@"request length: %i", [[request body] length]);
  [[request body] writeToFile:tempPath atomically:YES];
  [fileDict setValue:tempPath forKey:zsTempFilePath];
  
  [receivedFileLookupDictionary setValue:fileDict forKey:[request valueOfProperty:zsStoreIdentifier]];
  [fileDict release], fileDict = nil;
  
  BLIPResponse *response = [request response];
  [response setValue:zsActID(zsActionFileReceived) ofProperty:zsAction];
  [response setValue:[request valueOfProperty:zsStoreIdentifier] ofProperty:zsStoreIdentifier];
  [response send];
}

- (BOOL)switchStore:(NSPersistentStore*)store withReplacement:(NSDictionary*)replacement error:(NSError**)error
{
  NSDictionary *storeOptions = [[[store options] copy] autorelease];
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSPersistentStoreCoordinator *psc = [self persistentStoreCoordinator];
  
  NSString *newFileTempPath = [replacement valueForKey:zsTempFilePath];
  NSString *fileOriginalPath = [[store URL] path];
  NSString *originalFileTempPath = [fileOriginalPath stringByAppendingPathExtension:@"zsync_"];
  
  if (![psc removePersistentStore:store error:error]) return NO;
  
  if ([fileManager fileExistsAtPath:originalFileTempPath]) {
    DLog(@"deleting stored file");
    if (![fileManager removeItemAtPath:originalFileTempPath error:error]) return NO;
  }
  
  if ([fileManager fileExistsAtPath:fileOriginalPath]) {
    if (![fileManager moveItemAtPath:fileOriginalPath toPath:originalFileTempPath error:error]) return NO;
  }
  
  if (![fileManager moveItemAtPath:newFileTempPath toPath:fileOriginalPath error:error]) return NO;
  
  NSURL *fileURL = [NSURL fileURLWithPath:fileOriginalPath];
  if (![psc addPersistentStoreWithType:[replacement valueForKey:zsStoreType] configuration:[replacement valueForKey:zsStoreConfiguration] URL:fileURL options:storeOptions error:error]) return NO;
  
  DLog(@"store switched");
  return YES;
}

- (void)completeSync
{
  [[self persistentStoreCoordinator] lock];
  
  //First we need to verify that we received every file. Otherwise we fail
  for (NSPersistentStore *store in [[self persistentStoreCoordinator] persistentStores]) {
    if ([receivedFileLookupDictionary objectForKey:[store identifier]]) continue;
    
    DLog(@"Store ID: %@\n%@", [store identifier], [receivedFileLookupDictionary allKeys]);
    //Fail
    if ([[self delegate] respondsToSelector:@selector(zSync:errorOccurred:)]) {
      //Flush the temp files
      for (NSDictionary *fileDict in [receivedFileLookupDictionary allValues]) {
        NSError *error = nil;
        [[NSFileManager defaultManager] removeItemAtPath:[fileDict valueForKey:zsTempFilePath] error:&error];
        // We want to explode on this failure in dev but in prod just note it
        ZAssert(error == nil, @"Error deleting temp file: %@", [error localizedDescription]);
      }
      NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[store identifier] forKey:zsStoreIdentifier];
      NSError *error = [NSError errorWithDomain:zsErrorDomain code:zsErrorFailedToReceiveAllFiles userInfo:userInfo];
      [[self delegate] zSync:self errorOccurred:error];
      [receivedFileLookupDictionary release], receivedFileLookupDictionary = nil;
    }
    [[self persistentStoreCoordinator] unlock];
    return;
  }
  
  //We have all of the files now we need to swap them out.
  for (NSPersistentStore *store in [[self persistentStoreCoordinator] persistentStores]) {
    NSDictionary *replacement = [receivedFileLookupDictionary valueForKey:[store identifier]];
    ZAssert(replacement != nil, @"Missing the replacement file for %@\n%@", [store identifier], [receivedFileLookupDictionary allKeys]);
    NSError *error = nil;
    if ([self switchStore:store withReplacement:replacement error:&error]) continue;
    ZAssert(error == nil, @"Error switching stores: %@\n%@", [error localizedDescription], [error userInfo]);
    
    //TODO: We failed in the migration and need to roll back
  }
  
  [receivedFileLookupDictionary release], receivedFileLookupDictionary = nil;
  
  [[self connection] close];
  [self setConnection:nil];
  
  if ([[self delegate] respondsToSelector:@selector(zSyncFinished:)]) {
    [[self delegate] zSyncFinished:self];
  }
  
  [[self persistentStoreCoordinator] unlock];
  
  [self setServerAction:ZSyncServerActionNoActivity];
}

/*
* We want to start looking for desktops to sync with here. Once started
* We want to maintain a list of computers found and also send out a notification
* for every server that we discover
*/
- (void)startBrowser;
{
  if (_serviceBrowser) return;
  _serviceBrowser = [[ServerBrowser alloc] init];
  _serviceBrowser.delegate = self;
 [_serviceBrowser start];
}

//- (NSArray*)availableServers;
//{
// NSMutableSet *set = [NSMutableSet set];
// for (NSNetService *bonjourService in [_serviceBrowser servers]) {
// [bonjourService setDelegate:self];
// [bonjourService resolveWithTimeout:3.0];
// NSString *serverName = [bonjourService name];
//// NSArray *components = [serverName componentsSeparatedByString:zsServerNameSeperator];
//// if (!components || [components count] != 2) {
//// NSLog(@"Wrong number of components: %i\n%@", [components count], serverName);
//// continue;
//// }
////
//// ZAssert([components count] == 2,@"Wrong number of components: %i\n%@", [components count], serverName);
//// NSString *serverUUID = [components objectAtIndex:1];
// DLog(@"[bonjourService TXTRecordData] %@", [bonjourService TXTRecordData]);
// NSDictionary *txtRecordDictionary = [NSNetService dictionaryFromTXTRecordData:[bonjourService TXTRecordData]];
// if (!txtRecordDictionary) {
// DLog(@"The NSNetService named %@ did not contain a TXT record", [bonjourService name]);
// continue;
// }
//
// for (NSString *key in [txtRecordDictionary allKeys]) {
// DLog(@"key: %@ object: %@", key, [txtRecordDictionary objectForKey:key]);
// }
//
// NSLog(@"zsServerUUID: %@ zsServerName: %@", zsServerUUID, zsServerName);
//
// NSString *serverUUID = [txtRecordDictionary objectForKey:zsServerUUID];
// if (!serverUUID || [serverUUID length] == 0) {
// DLog(@"The TXT record did not contain a UUID.");
// }
// // serverName = [components objectAtIndex:0];
//
// ZSyncService *zSyncService = [[ZSyncService alloc] init];
// [zSyncService setService:bonjourService];
// [zSyncService setName:serverName];
// [zSyncService setUuid:serverUUID];
// [set addObject:zSyncService];
// [zSyncService release], zSyncService = nil;
// }
//
// NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
// NSArray *result = [set allObjects];
// result = [result sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
// [sort release], sort = nil;
//
// return result;
//}

- (void)sendUploadComplete
{
  DLog(@"sending upload complete");
  NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
  [dictionary setValue:zsActID(zsActionPerformSync) forKey:zsAction];
  BLIPRequest *request = [BLIPRequest requestWithBody:nil properties:dictionary];
  [request setNoReply:YES];
  [[self connection] sendRequest:request];
  [dictionary release], dictionary = nil;
}

- (void)uploadDataToServer;
{
  [[self serviceBrowser] setDelegate:nil];
  [[self serviceBrowser] stop];
  [self setServiceBrowser:nil];
  
  [[self delegate] zSyncStarted:self];
  
  storeFileIdentifiers = [[NSMutableArray alloc] init];
  
  NSAssert([self persistentStoreCoordinator] != nil, @"The persistent store coordinator was nil. Make sure you are calling registerDelegate:withPersistentStoreCoordinator: before trying to sync.");
  
  for (NSPersistentStore *store in [[self persistentStoreCoordinator] persistentStores]) {
    NSData *data = [[NSData alloc] initWithContentsOfMappedFile:[[store URL] path]];
    DLog(@"url %@\nIdentifier: %@\nSize: %i", [store URL], [store identifier], [data length]);
    
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setValue:[store identifier] forKey:zsStoreIdentifier];
    if (![[store configurationName] isEqualToString:@"PF_DEFAULT_CONFIGURATION_NAME"]) {
      [dictionary setValue:[store configurationName] forKey:zsStoreConfiguration];
    }
    [dictionary setValue:[store type] forKey:zsStoreType];
    [dictionary setValue:zsActID(zsActionStoreUpload) forKey:zsAction];
    
    BLIPRequest *request = [BLIPRequest requestWithBody:data properties:dictionary];
    // TODO: Compression is not working. Need to find out why
    [request setCompressed:YES];
    [[self connection] sendRequest:request];
    [data release], data = nil;
    [dictionary release], dictionary = nil;
    DLog(@"file uploaded");
    
    [storeFileIdentifiers addObject:[store identifier]];
  }
  DLog(@"finished");
}

- (void)processTestFileTransfer:(BLIPRequest*)request
{
  NSData *data = [request body];
  DLog(@"length %i", [data length]);
  NSString *path = [self cachePath];
  path = [path stringByAppendingPathComponent:@"test.jpg"];
  
  NSError *error = nil;
  if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
    DLog(@"deleting old file");
    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
    ZAssert(error == nil, @"error removing test file: %@", [error localizedDescription]);
  }
  
  [data writeToFile:path atomically:YES];
  DLog(@"file written\n%@", path);
}

- (NSString*)generatePairingCode
{
  NSMutableString *string = [NSMutableString string];
  [string appendFormat:@"%i", (arc4random() % 10)];
  [string appendFormat:@"%i", (arc4random() % 10)];
  [string appendFormat:@"%i", (arc4random() % 10)];
  [string appendFormat:@"%i", (arc4random() % 10)];
  return string;
}

- (void)requestDeregistration;
{
  DLog(@"issuing deregister command");
  NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
  [dictionary setValue:zsActID(zsActionDeregisterClient) forKey:zsAction];
  
  NSString *syncUUID = [[NSUserDefaults standardUserDefaults] valueForKey:zsSyncGUID];
  
  NSData *body = [syncUUID dataUsingEncoding:NSUTF8StringEncoding];
  
  BLIPRequest *request = [BLIPRequest requestWithBody:body properties:dictionary];
  [[self connection] sendRequest:request];
}

- (void)connectionEstablished
{
  switch ([self serverAction]) {
    case ZSyncServerActionSync:
      if ([[NSUserDefaults standardUserDefaults] valueForKey:zsServerUUID]) {
        //Start a sync by pushing the data file to the server
        [self uploadDataToServer];
      } else {
        //We are not paired so we need to request a pairing session
        NSString *deviceUUID = [[NSUserDefaults standardUserDefaults] valueForKey:zsDeviceID];
        if (!deviceUUID) {
          deviceUUID = [[NSProcessInfo processInfo] globallyUniqueString];
          [[NSUserDefaults standardUserDefaults] setValue:deviceUUID forKey:zsDeviceID];
        }
        
        [self setPasscode:[self generatePairingCode]];
        BLIPRequest *request = [BLIPRequest requestWithBodyString:[self passcode]];
        [request setValue:zsActID(zsActionRequestPairing) ofProperty:zsAction];
        [request setValue:deviceUUID ofProperty:zsDeviceID];
        [[self connection] sendRequest:request];
        
        [[self delegate] zSyncHandler:self displayPairingCode:[self passcode]];
      }
      break;
    case ZSyncServerActionDeregister:
      [self requestDeregistration];
      break;
    default:
      ALog(@"Unknown server action: %i", serverAction);
  }
}


#pragma mark -
#pragma mark BLIP Delegate

/* Two possible states at this point. If we have a server UUID
* then we are ready to start a sync. If we do not have a server UUID
* then we need to start a pairing.
*/
- (void)connectionDidOpen:(BLIPConnection*)connection
{
  DLog(@"entered");
  //Start by confirming that the server still supports our schema and version
  
  NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  [dict setValue:zsActID(zsActionVerifySchema) forKey:zsAction];
  [dict setValue:zsActID([self majorVersionNumber]) forKey:zsSchemaMajorVersion];
  [dict setValue:zsActID([self minorVersionNumber]) forKey:zsSchemaMinorVersion];
  [dict setValue:[[UIDevice currentDevice] name] forKey:zsDeviceName];
  [dict setValue:[[UIDevice currentDevice] uniqueIdentifier] forKey:zsDeviceGUID];
  NSString *deviceGUID = [[[NSBundle mainBundle] infoDictionary] objectForKey:zsSchemaIdentifier];
  [dict setValue:deviceGUID forKey:zsSchemaIdentifier];
  
  NSString *syncGUID = [[NSUserDefaults standardUserDefaults] stringForKey:zsSyncGUID];
  if (!syncGUID) {
    syncGUID = [[NSProcessInfo processInfo] globallyUniqueString];
    [[NSUserDefaults standardUserDefaults] setValue:syncGUID forKey:zsSyncGUID];
  }
  NSData *data = [syncGUID dataUsingEncoding:NSUTF8StringEncoding];
  BLIPRequest *request = [connection requestWithBody:data properties:dict];
  [request send];
  [dict release], dict = nil;
  DLog(@"initial send complete");
}

/* We had an error talking to the server. Push this error on to our delegate
* and close the connection
*/
- (void)connection:(TCPConnection*)connection failedToOpen:(NSError*)error
{
  DLog(@"entered");
  [_connection close], [_connection release], _connection = nil;
  [[self delegate] zSync:self errorOccurred:error];
}

- (void)connection:(BLIPConnection*)connection receivedResponse:(BLIPResponse*)response;
{
  if (![[response properties] valueOfProperty:zsAction]) {
    DLog(@"received empty response, ignoring");
    return;
  }
  DLog(@"entered\n%@", [[response properties] allProperties]);
  NSInteger action = [[[response properties] valueOfProperty:zsAction] integerValue];
  switch (action) {
    case zsActionDeregisterClient:
      if ([[self delegate] respondsToSelector:@selector(zSyncDeregisterComplete:)]) {
        [[self delegate] zSyncDeregisterComplete:self];
      }
      [self setServerAction:ZSyncServerActionNoActivity];
      [[self connection] close];
      [self setConnection:nil];
      return;
    case zsActionFileReceived:
      ZAssert(storeFileIdentifiers != nil, @"zsActionFileReceived with a nil storeFileIdentifiers");
      [storeFileIdentifiers removeObject:[[response properties] valueOfProperty:zsStoreIdentifier]];
      if ([storeFileIdentifiers count] == 0) {
        [self sendUploadComplete];
        [storeFileIdentifiers release], storeFileIdentifiers = nil;
      }
      return;
    case zsActionRequestPairing:
      //Server has accepted the pairing request
      //Notify the delegate to present a pairing dialog
      if ([[self delegate] respondsToSelector:@selector(zSyncPairingRequestAccepted:)]) {
        // ???: This does nothing currently!
        //[[self delegate] zSyncPairingRequestAccepted:self];
      }
      return;
    case zsActionAuthenticatePassed:
      ALog(@"%s server UUID accepted: %@", [response valueOfProperty:zsServerUUID]);
// [[NSUserDefaults standardUserDefaults] setValue:[response valueOfProperty:zsServerUUID] forKey:zsServerUUID];
// [[NSUserDefaults standardUserDefaults] setValue:[response valueOfProperty:zsServerName] forKey:zsServerName];
// if ([[self delegate] respondsToSelector:@selector(zSyncPairingCodeApproved:)]) {
// [[self delegate] zSyncPairingCodeApproved:self];
// }
// [[self serviceBrowser] stop];
// [_serviceBrowser release], _serviceBrowser = nil;
// [self uploadDataToServer];
      return;
    case zsActionSchemaUnsupported:
      if ([[self delegate] respondsToSelector:@selector(zSync:serverVersionUnsupported:)]) {
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[response bodyString] forKey:NSLocalizedDescriptionKey];
        NSError *error = [NSError errorWithDomain:zsErrorDomain code:[[response valueOfProperty:zsErrorCode] integerValue] userInfo:userInfo];
        
        [[self delegate] zSync:self serverVersionUnsupported:error];
      }
      [[self connection] close];
      [self setConnection:nil];
      [self setServerAction:ZSyncServerActionNoActivity];
      return;
    case zsActionSchemaSupported:
      [self connectionEstablished];
      return;
    default:
      ALog(@"%s unknown action received %i", action);
  }
}

- (BOOL)connection:(BLIPConnection*)connection receivedRequest:(BLIPRequest*)request
{
  NSInteger action = [[[request properties] valueOfProperty:zsAction] integerValue];
  switch (action) {
    case zsActionAuthenticateFailed:
      /*
* The pairing code was not entered correctly so we reset back to a default state
* so that the user can start all over again.
*/
      [[self connection] close];
      [self setConnection:nil];
      if ([[self delegate] respondsToSelector:@selector(zSyncPairingCodeRejected:)]) {
        [[self delegate] zSyncPairingCodeRejected:self];
      }
      [self setServerAction:ZSyncServerActionNoActivity];
      [[NSUserDefaults standardUserDefaults] removeObjectForKey:zsServerUUID];
      [[NSUserDefaults standardUserDefaults] removeObjectForKey:zsServerName];
      return YES;
    case zsActionAuthenticatePairing:
      if ([[request bodyString] isEqualToString:[self passcode]]) {
        [[request response] setValue:zsActID(zsActionAuthenticatePassed) ofProperty:zsAction];
        [[NSUserDefaults standardUserDefaults] setValue:[request valueOfProperty:zsServerUUID] forKey:zsServerUUID];
        [self performSelector:@selector(uploadDataToServer) withObject:nil afterDelay:0.1];
      } else {
        [[request response] setValue:zsActID(zsActionAuthenticateFailed) ofProperty:zsAction];
      }
      [[self delegate] zSyncPairingCodeCompleted:self];
      return YES;
    case zsActionTestFileTransfer:
      [self processTestFileTransfer:request];
      return YES;
    case zsActionCompleteSync:
      DLog(@"completeSync");
      [self performSelector:@selector(completeSync) withObject:nil afterDelay:0.01];
      return YES;
    case zsActionStoreUpload:
      DLog(@"receiveFile");
      [self receiveFile:request];
      return YES;
    case zsActionCancelPairing:
      DLog(@"Cancel Pairing request received");
      [[self connection] close];
      [self setConnection:nil];
      if ([[self delegate] respondsToSelector:@selector(zSyncPairingCodeCancelled:)]) {
        [[self delegate] zSyncPairingCodeCancelled:self];
      }
      [self setServerAction:ZSyncServerActionNoActivity];
      [[NSUserDefaults standardUserDefaults] removeObjectForKey:zsServerUUID];
      [[NSUserDefaults standardUserDefaults] removeObjectForKey:zsServerName];
      return YES;
    default:
      ALog(@"Unknown action received: %i", action);
      return NO;
  }
}

- (void)connectionDidClose:(TCPConnection*)connection;
{
  if (![self connection]) return;
  
  //premature closing
  [self setConnection:nil];
  [self setServerAction:ZSyncServerActionNoActivity];
  
  if (![[self delegate] respondsToSelector:@selector(zSync:errorOccurred:)]) return;

  NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"Server Hung Up", @"Server Hung Up message text") forKey:NSLocalizedDescriptionKey];
  NSError *error = [NSError errorWithDomain:zsErrorDomain code:zsErrorServerHungUp userInfo:userInfo];
  [[self delegate] zSync:self errorOccurred:error];
}

@synthesize delegate = _delegate;

@synthesize serviceBrowser = _serviceBrowser;
@synthesize connection = _connection;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

@synthesize majorVersionNumber;
@synthesize minorVersionNumber;
@synthesize passcode;
@synthesize serverAction;
@synthesize availableServers;
@synthesize discoveredServers;
@synthesize lock;

@end

@implementation ZSyncService

@synthesize name;
@synthesize uuid;
@synthesize service;

- (NSString*)description
{
  return [NSString stringWithFormat:@"[%@:%@]", [self name], [self uuid]];
}

- (NSUInteger)hash
{
  return [[self description] hash];
}

- (BOOL)isEqual:(id)object
{
  if (!object || ![object isKindOfClass:[ZSyncService class]]) return NO;
  
  if (![[object name] isEqualToString:[self name]]) return NO;
  if (![[object uuid] isEqualToString:[self uuid]]) return NO;
  
  return YES;
}

@end
Something went wrong with that request. Please try again.