diff --git a/Synaction Product/include/Synaction/Synaction.h b/Synaction Product/include/Synaction/Synaction.h index bc3b944..c0125b8 100644 --- a/Synaction Product/include/Synaction/Synaction.h +++ b/Synaction Product/include/Synaction/Synaction.h @@ -45,7 +45,7 @@ @end -typedef void(^ _Nullable calibrationBlock)(MCPeerID * _Nullable peer); +typedef void(^ _Nullable calibrationBlock)(NSArray * _Nullable peers); @interface Synaction : NSObject @@ -55,7 +55,7 @@ typedef void(^ _Nullable calibrationBlock)(MCPeerID * _Nullable peer); - (void)calculateTimeOffsetWithHost;// Calculate the time difference in nanoseconds between us and the host device. - (uint64_t)currentNetworkTime;// The current host time adjusted for offset (offset = 0 if host) - (void)atExactTime:(uint64_t)val runBlock:(dispatch_block_t _Nonnull)block;// Run block at the exact host adjusted time val adjusted -- (void)executeBlockWhenPeerCalibrates:(MCPeerID * _Nonnull)peer block:(calibrationBlock)completionBlock;// Once peer calibrates this will execute completionBlock +- (void)executeBlockWhenPeersCalibrate:(NSArray * _Nonnull)peer block:(calibrationBlock)completionBlock;// Once peer calibrates this will execute completionBlock @property (strong, nonatomic) NSMutableSet * _Nullable calibratedPeers;// Array of all peers that have already calibrated @property (nonatomic) uint64_t numberOfCalibrations;// The number of calibrations to be used to calculate the avergae offset diff --git a/Synaction Product/libSynaction.a b/Synaction Product/libSynaction.a index 9881518..8937ec4 100644 Binary files a/Synaction Product/libSynaction.a and b/Synaction Product/libSynaction.a differ diff --git a/Synaction.xcodeproj/project.pbxproj b/Synaction.xcodeproj/project.pbxproj index 08b79fb..6ed1095 100644 --- a/Synaction.xcodeproj/project.pbxproj +++ b/Synaction.xcodeproj/project.pbxproj @@ -154,7 +154,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "# define output folder environment variable\nUNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal\n\n# Step 1. Build Device and Simulator versions\nxcodebuild -target Synaction ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR=\"${BUILD_DIR}\" BUILD_ROOT=\"${BUILD_ROOT}\"\nxcodebuild -target Synaction -configuration ${CONFIGURATION} -sdk iphonesimulator -arch i386 BUILD_DIR=\"${BUILD_DIR}\" BUILD_ROOT=\"${BUILD_ROOT}\"\n\n# make sure the output directory exists\nmkdir -p \"${UNIVERSAL_OUTPUTFOLDER}\"\n\n# Step 2. Create universal binary file using lipo\nlipo -create -output \"${UNIVERSAL_OUTPUTFOLDER}/lib${PROJECT_NAME}.a\" \"${BUILD_DIR}/${CONFIGURATION}-iphoneos/lib${PROJECT_NAME}.a\" \"${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/lib${PROJECT_NAME}.a\"\n\n# Last touch. copy the header files. Just for convenience\ncp -R \"${BUILD_DIR}/${CONFIGURATION}-iphoneos/include\" \"${UNIVERSAL_OUTPUTFOLDER}/\""; + shellScript = "#Configurations.\n#This script designed for Mac OS X command-line, so does not use Xcode build variables.\n#But you can use it freely if you want.\n\nTARGET=Synaction\nACTION=\"clean build\"\nFILE_NAME=libSynaction.a\n\nDEVICE=iphoneos3.2\nSIMULATOR=iphonesimulator3.2\n\n#Build for all platforms/configurations.\n\nxcodebuild -configuration Debug -target ${TARGET} -sdk ${DEVICE} ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO\nxcodebuild -configuration Debug -target ${TARGET} -sdk ${SIMULATOR} ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO\n\n#Merge all platform binaries as a fat binary for each configurations.\n\nDEBUG_DEVICE_DIR=${SYMROOT}/Debug-iphoneos\nDEBUG_SIMULATOR_DIR=${SYMROOT}/Debug-iphonesimulator\nDEBUG_UNIVERSAL_DIR=${SYMROOT}/Debug-universal\n\nrm -rf \"${DEBUG_UNIVERSAL_DIR}\"\nmkdir \"${DEBUG_UNIVERSAL_DIR}\"\n\nlipo -create -output \"${DEBUG_UNIVERSAL_DIR}/${FILE_NAME}\" \"${DEBUG_DEVICE_DIR}/${FILE_NAME}\" \"${DEBUG_SIMULATOR_DIR}/${FILE_NAME}\""; }; /* End PBXShellScriptBuildPhase section */ diff --git a/Synaction.xcodeproj/project.xcworkspace/xcuserdata/GeorgesKanaan.xcuserdatad/UserInterfaceState.xcuserstate b/Synaction.xcodeproj/project.xcworkspace/xcuserdata/GeorgesKanaan.xcuserdatad/UserInterfaceState.xcuserstate index b14313d..f44edeb 100644 Binary files a/Synaction.xcodeproj/project.xcworkspace/xcuserdata/GeorgesKanaan.xcuserdatad/UserInterfaceState.xcuserstate and b/Synaction.xcodeproj/project.xcworkspace/xcuserdata/GeorgesKanaan.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Synaction/Synaction.h b/Synaction/Synaction.h index bc3b944..c0125b8 100644 --- a/Synaction/Synaction.h +++ b/Synaction/Synaction.h @@ -45,7 +45,7 @@ @end -typedef void(^ _Nullable calibrationBlock)(MCPeerID * _Nullable peer); +typedef void(^ _Nullable calibrationBlock)(NSArray * _Nullable peers); @interface Synaction : NSObject @@ -55,7 +55,7 @@ typedef void(^ _Nullable calibrationBlock)(MCPeerID * _Nullable peer); - (void)calculateTimeOffsetWithHost;// Calculate the time difference in nanoseconds between us and the host device. - (uint64_t)currentNetworkTime;// The current host time adjusted for offset (offset = 0 if host) - (void)atExactTime:(uint64_t)val runBlock:(dispatch_block_t _Nonnull)block;// Run block at the exact host adjusted time val adjusted -- (void)executeBlockWhenPeerCalibrates:(MCPeerID * _Nonnull)peer block:(calibrationBlock)completionBlock;// Once peer calibrates this will execute completionBlock +- (void)executeBlockWhenPeersCalibrate:(NSArray * _Nonnull)peer block:(calibrationBlock)completionBlock;// Once peer calibrates this will execute completionBlock @property (strong, nonatomic) NSMutableSet * _Nullable calibratedPeers;// Array of all peers that have already calibrated @property (nonatomic) uint64_t numberOfCalibrations;// The number of calibrations to be used to calculate the avergae offset diff --git a/Synaction/Synaction.m b/Synaction/Synaction.m index 3a4df5e..adcdd46 100644 --- a/Synaction/Synaction.m +++ b/Synaction/Synaction.m @@ -45,20 +45,38 @@ + (instancetype _Nonnull)sharedManager { #pragma mark - Network Time Sync // Host -- (void)executeBlockWhenPeerCalibrates:(MCPeerID * _Nonnull)peer block:(calibrationBlock)completionBlock { - if ([self.calibratedPeers containsObject:peer]) {// Already calibrated - completionBlock(peer); +- (void)executeBlockWhenPeersCalibrate:(NSArray * _Nonnull)peers block:(calibrationBlock)completionBlock { + // Check if these peers already had the time to calibrate + NSSet *peersSet = [NSSet setWithArray:peers]; + + if ([peersSet isEqualToSet:self.calibratedPeers]) {// Already calibrated + completionBlock(peers); return; } + // They didn't. Register to receive notifications. Execute when all are calibrated. __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"peerCalibrated" object:self.calibratedPeers queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull notification) { - if ([notification.userInfo[@"calibratedPeer"] isEqual:peer]) { - completionBlock(peer); + + __block BOOL executeBlock = YES; + + // Check that every object in peers is contained in calibratedPeers + [peers enumerateObjectsUsingBlock:^(MCPeerID * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + if (executeBlock) {// Make sure a NO doesn't get switched to a YES. + executeBlock = [self.calibratedPeers containsObject:obj]; + } + + *stop = !executeBlock;// Stop if executeBlock is NO. + }]; + + // Check if we should execute the block + if (executeBlock) { + completionBlock(peers); [[NSNotificationCenter defaultCenter] removeObserver:observer]; } }]; } + - (void)askPeersToCalculateOffset:(NSArray * _Nonnull)peers { // Send the "sync" command to peers to trigger their offset calculations. NSMutableDictionary *payloadDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"command": @"sync"}]; diff --git a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/Synaction.dia b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/Synaction.dia index 29ccd98..8cc12ce 100644 Binary files a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/Synaction.dia and b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/Synaction.dia differ diff --git a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/Synaction.o b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/Synaction.o index 10744e8..e3329a6 100644 Binary files a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/Synaction.o and b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/Synaction.o differ diff --git a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/libSynaction.a b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/libSynaction.a index 647333a..7f6950d 100644 Binary files a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/libSynaction.a and b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/arm64/libSynaction.a differ diff --git a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/Synaction.dia b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/Synaction.dia index 29ccd98..8cc12ce 100644 Binary files a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/Synaction.dia and b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/Synaction.dia differ diff --git a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/Synaction.o b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/Synaction.o index de0a4ee..1945f2d 100644 Binary files a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/Synaction.o and b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/Synaction.o differ diff --git a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/libSynaction.a b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/libSynaction.a index 715e408..8f5ba86 100644 Binary files a/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/libSynaction.a and b/build/Synaction.build/Debug-iphoneos/Synaction.build/Objects-normal/armv7/libSynaction.a differ diff --git a/build/Synaction.build/Debug-iphoneos/Synaction.build/dgph b/build/Synaction.build/Debug-iphoneos/Synaction.build/dgph index ac6ea99..6755574 100644 Binary files a/build/Synaction.build/Debug-iphoneos/Synaction.build/dgph and b/build/Synaction.build/Debug-iphoneos/Synaction.build/dgph differ