KWProbePoller.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // KWProbePoller.m
  3. // iOSFalconCore
  4. //
  5. // Created by Luke Redpath on 13/01/2011.
  6. // Copyright 2011 LJR Software Limited. All rights reserved.
  7. //
  8. #import "KWProbePoller.h"
  9. @interface KWTimeout : NSObject
  10. @property (nonatomic) CFTimeInterval timeoutDateStamp;
  11. @end
  12. @implementation KWTimeout
  13. - (id)initWithTimeout:(NSTimeInterval)timeout
  14. {
  15. self = [super init];
  16. if (self) {
  17. _timeoutDateStamp = CFAbsoluteTimeGetCurrent() + timeout;
  18. }
  19. return self;
  20. }
  21. - (BOOL)hasTimedOut {
  22. return (_timeoutDateStamp - CFAbsoluteTimeGetCurrent()) < 0;
  23. }
  24. @end
  25. @interface KWProbePoller()
  26. @property (nonatomic, assign) NSTimeInterval timeoutInterval;
  27. @property (nonatomic, assign) NSTimeInterval delayInterval;
  28. @property (nonatomic, assign) BOOL shouldWait;
  29. @end
  30. @implementation KWProbePoller
  31. - (id)initWithTimeout:(NSTimeInterval)theTimeout
  32. delay:(NSTimeInterval)theDelay
  33. shouldWait:(BOOL)wait {
  34. self = [super init];
  35. if (self) {
  36. _timeoutInterval = theTimeout;
  37. _delayInterval = theDelay;
  38. _shouldWait = wait;
  39. }
  40. return self;
  41. }
  42. - (BOOL)check:(id<KWProbe>)probe; {
  43. KWTimeout *timeout = [[KWTimeout alloc] initWithTimeout:self.timeoutInterval];
  44. while (self.shouldWait || ![probe isSatisfied]) {
  45. if ([timeout hasTimedOut]) {
  46. return [probe isSatisfied];
  47. }
  48. CFRunLoopRunInMode(kCFRunLoopDefaultMode, _delayInterval, false);
  49. [probe sample];
  50. }
  51. return YES;
  52. }
  53. @end