Skip to content

Commit

Permalink
change NetworkClock to cache the offset and do lazy evaluation, but m…
Browse files Browse the repository at this point in the history
…ake sure the offset is rediscovered periodically.
  • Loading branch information
gavineadie@gmail.com committed Nov 5, 2010
1 parent 85fa9d6 commit 3a1c894
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
1 change: 1 addition & 0 deletions Classes/NetworkClock.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@interface NetworkClock : NSObject {

NSTimeInterval timeIntervalSinceDeviceTime;
NSDate * timeLastSynchronized;

NSMutableArray * timeAssociations;

Expand Down
40 changes: 24 additions & 16 deletions Classes/NetworkClock.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,38 @@ - (id) init {
if (nil == [super init]) return nil;

timeAssociations = [[NSMutableArray arrayWithCapacity:48] retain];
timeIntervalSinceDeviceTime = 0.0;
timeLastSynchronized = [[NSDate dateWithTimeIntervalSince1970:0.0] retain];

return self;
}

/*┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Scan the associations and get the best time offset, then return adjusted time ┃
┃ Return the device clock time adjusted for the offset to network-derived UTC. If we have not ┃
┃ synchronized for five minutes, get a new offset by scanning the associations. ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛*/
- (NSDate *) networkTime {
short usefulCount = 0;
timeIntervalSinceDeviceTime = 0.0;
if ([timeLastSynchronized timeIntervalSinceNow] < -90.0) {
[timeLastSynchronized release];

for (NetAssociation * timeAssociation in timeAssociations) {
if (timeAssociation.useful) {
usefulCount++;
timeIntervalSinceDeviceTime += timeAssociation.offset;
short usefulCount = 0;
timeIntervalSinceDeviceTime = 0.0;

for (NetAssociation * timeAssociation in timeAssociations) {
if (timeAssociation.useful) {
usefulCount++;
timeIntervalSinceDeviceTime += timeAssociation.offset;
}
}
}

if (usefulCount == 0)
NSLog(@"no useful associations");
else {
timeIntervalSinceDeviceTime /= usefulCount;
}

if (usefulCount == 0)
NSLog(@"no useful associations");
else {
NSLog(@"%i useful associations", usefulCount);
timeIntervalSinceDeviceTime /= usefulCount;
}

timeLastSynchronized = [[NSDate date] retain];
}

return [[NSDate date] dateByAddingTimeInterval:-timeIntervalSinceDeviceTime];
}
Expand Down Expand Up @@ -160,4 +168,4 @@ - (NSString *) hostAddress:(struct sockaddr_in *) sockAddr {

SYNTHESIZE_SINGLETON_FOR_CLASS(NetworkClock);

@end
@end
8 changes: 4 additions & 4 deletions Classes/ntpAAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[window makeKeyAndVisible];

NSTimer * repeatingTimer = [[NSTimer alloc]
initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:1.0]
interval:30.0
initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:10.0]
interval:10.0
target:self
selector:@selector(repeatingMethod:)
userInfo:nil
Expand All @@ -37,8 +37,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}

- (void) repeatingMethod:(NSTimer*) theTimer {
NSLog(@"clock: %@", [NSDate date]);
NSLog(@"timer: %@", [[NetworkClock sharedNetworkClock] networkTime]);
NSLog(@"sys clock: %@", [NSDate date]);
NSLog(@"net clock: %@", [[NetworkClock sharedNetworkClock] networkTime]);
}

- (void)applicationWillTerminate:(UIApplication *)application {
Expand Down

0 comments on commit 3a1c894

Please sign in to comment.