Skip to content

Commit

Permalink
executeWhenPeerCalibrates is now executeWhenPeersCalibrate
Browse files Browse the repository at this point in the history
  • Loading branch information
Ge0rges committed Mar 3, 2017
1 parent 371a080 commit d143372
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Synaction Product/include/Synaction/Synaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

@end

typedef void(^ _Nullable calibrationBlock)(MCPeerID * _Nullable peer);
typedef void(^ _Nullable calibrationBlock)(NSArray <MCPeerID *> * _Nullable peers);

@interface Synaction : NSObject <ConnectivityManagerDelegate>

Expand All @@ -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 <MCPeerID *> * _Nonnull)peer block:(calibrationBlock)completionBlock;// Once peer calibrates this will execute completionBlock

@property (strong, nonatomic) NSMutableSet <MCPeerID*> * _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
Expand Down
Binary file modified Synaction Product/libSynaction.a
Binary file not shown.
2 changes: 1 addition & 1 deletion Synaction.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
Binary file not shown.
4 changes: 2 additions & 2 deletions Synaction/Synaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

@end

typedef void(^ _Nullable calibrationBlock)(MCPeerID * _Nullable peer);
typedef void(^ _Nullable calibrationBlock)(NSArray <MCPeerID *> * _Nullable peers);

@interface Synaction : NSObject <ConnectivityManagerDelegate>

Expand All @@ -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 <MCPeerID *> * _Nonnull)peer block:(calibrationBlock)completionBlock;// Once peer calibrates this will execute completionBlock

@property (strong, nonatomic) NSMutableSet <MCPeerID*> * _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
Expand Down
28 changes: 23 additions & 5 deletions Synaction/Synaction.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 <MCPeerID *> * _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 <MCPeerID*>* _Nonnull)peers {
// Send the "sync" command to peers to trigger their offset calculations.
NSMutableDictionary *payloadDic = [[NSMutableDictionary alloc] initWithDictionary:@{@"command": @"sync"}];
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/Synaction.build/Debug-iphoneos/Synaction.build/dgph
Binary file not shown.

0 comments on commit d143372

Please sign in to comment.