main
 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
 9#import "KWProbePoller.h"
10
11@interface KWTimeout : NSObject
12{
13  NSDate *timeoutDate;
14}
15- (id)initWithTimeout:(NSTimeInterval)timeout;
16- (BOOL)hasTimedOut;
17@end
18
19@implementation KWTimeout
20
21- (id)initWithTimeout:(NSTimeInterval)timeout
22{
23  if ((self = [super init])) {
24    timeoutDate = [[NSDate alloc] initWithTimeIntervalSinceNow:timeout];
25  }
26  return self;
27}
28
29- (void)dealloc
30{
31  [timeoutDate release];
32  [super dealloc];
33}
34
35- (BOOL)hasTimedOut
36{
37  return [timeoutDate timeIntervalSinceDate:[NSDate date]] < 0;
38}
39
40@end
41
42#pragma mark -
43
44@implementation KWProbePoller
45
46- (id)initWithTimeout:(NSTimeInterval)theTimeout delay:(NSTimeInterval)theDelay;
47{
48  if ((self = [super init])) {
49    timeoutInterval = theTimeout;
50    delayInterval = theDelay;
51  }
52  return self;
53}
54
55- (BOOL)check:(id<KWProbe>)probe;
56{
57  KWTimeout *timeout = [[KWTimeout alloc] initWithTimeout:timeoutInterval];
58
59  while (![probe isSatisfied]) {
60    if ([timeout hasTimedOut]) {
61      [timeout release];
62      return NO;
63    }
64    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:delayInterval]];
65    [probe sample];
66  }
67  [timeout release];
68
69  return YES;
70}
71
72@end