From 12ca0e92e9277db67f0e6294192aa261538e9d0a Mon Sep 17 00:00:00 2001 From: Luke Sadler Date: Thu, 11 May 2017 15:01:11 +0100 Subject: [PATCH] added ping time keeping and number of fails --- RealReachability/Ping/PingHelper.h | 3 +++ RealReachability/Ping/PingHelper.m | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/RealReachability/Ping/PingHelper.h b/RealReachability/Ping/PingHelper.h index 0cfbe5e..d204fc2 100755 --- a/RealReachability/Ping/PingHelper.h +++ b/RealReachability/Ping/PingHelper.h @@ -21,6 +21,9 @@ extern NSString *const kPingResultNotification; // Ping timeout. Default is 2 seconds @property (nonatomic, assign) NSTimeInterval timeout; +@property (readonly) NSTimeInterval previousSuccessDuration; +@property (readonly) int numberOfFailedPings; + + (instancetype)sharedInstance; /** diff --git a/RealReachability/Ping/PingHelper.m b/RealReachability/Ping/PingHelper.m index 785adb2..9b850df 100755 --- a/RealReachability/Ping/PingHelper.m +++ b/RealReachability/Ping/PingHelper.m @@ -22,6 +22,7 @@ @interface PingHelper() @property (nonatomic, strong) NSMutableArray *completionBlocks; @property(nonatomic, strong) PingFoundation *pingFoundation; @property (nonatomic, assign) BOOL isPinging; +@property (nonatomic) NSDate *pingStartTime; @end @@ -107,6 +108,7 @@ - (void)clearPingFoundation - (void)startPing { //NSLog(@"startPing"); + _pingStartTime = [NSDate date]; [self clearPingFoundation]; self.isPinging = YES; @@ -135,6 +137,15 @@ - (void)setHost:(NSString *)host - (void)endWithFlag:(BOOL)isSuccess { + if (isSuccess) { + + NSTimeInterval duration = [_pingStartTime timeIntervalSinceNow]; + _previousSuccessDuration = fabs(duration*100); + + }else{ + _numberOfFailedPings ++; + } + // TODO(optimization): //somewhere around here we should introduce a double check after 3 seconds on another host, // if maybe not truely failed. @@ -188,6 +199,10 @@ - (void)pingFoundation:(PingFoundation *)pinger didFailToSendPacket:(NSData *)pa - (void)pingFoundation:(PingFoundation *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber { //NSLog(@"didReceivePingResponsePacket, sequenceNumber = %@", @(sequenceNumber)); + + NSTimeInterval duration = [_pingStartTime timeIntervalSinceNow]; + _previousSuccessDuration = duration/1000; + [self endWithFlag:YES]; }