-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplePingHelper.m
More file actions
executable file
·99 lines (78 loc) · 2.55 KB
/
SimplePingHelper.m
File metadata and controls
executable file
·99 lines (78 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// SimplePingHelper.m
// LAN Scan
//
// Created by Mongi Zaidi on 24 February 2014.
// Copyright (c) 2014 Smart Touch. All rights reserved.
//
#import "SimplePingHelper.h"
@interface SimplePingHelper()
@property(nonatomic,retain) SimplePing* simplePing;
@property(nonatomic,retain) id target;
@property(nonatomic,assign) SEL sel;
- (id)initWithAddress:(NSString*)address target:(id)_target sel:(SEL)_sel;
- (void)go;
@end
@implementation SimplePingHelper
@synthesize simplePing, target, sel;
#pragma mark - Run it
// Pings the address, and calls the selector when done. Selector must take a NSnumber which is a bool for success
+ (void)ping:(NSString*)address target:(id)target sel:(SEL)sel {
// The helper retains itself through the timeout function
[[[SimplePingHelper alloc] initWithAddress:address target:target sel:sel] go];
}
#pragma mark - Init/dealloc
- (void)dealloc {
self.simplePing = nil;
self.target = nil;
}
- (id)initWithAddress:(NSString*)address target:(id)_target sel:(SEL)_sel {
if (self = [self init]) {
self.simplePing = [SimplePing simplePingWithHostName:address];
self.simplePing.delegate = self;
self.target = _target;
self.sel = _sel;
}
return self;
}
#pragma mark - Go
- (void)go {
[self.simplePing start];
[self performSelector:@selector(endTime) withObject:nil afterDelay:1]; // This timeout is what retains the ping helper
}
#pragma mark - Finishing and timing out
// Called on success or failure to clean up
- (void)killPing {
[self.simplePing stop];
self.simplePing = nil;
}
- (void)successPing {
[self killPing];
[target performSelector:sel withObject:[NSNumber numberWithBool:YES]];
}
- (void)failPing:(NSString*)reason {
[self killPing];
[target performSelector:sel withObject:[NSNumber numberWithBool:NO]];
}
// Called 1s after ping start, to check if it timed out
- (void)endTime {
if (self.simplePing) { // If it hasn't already been killed, then it's timed out
[self failPing:@"timeout"];
}
}
#pragma mark - Pinger delegate
// When the pinger starts, send the ping immediately
- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address {
[self.simplePing sendPingWithData:nil];
}
- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error {
[self failPing:@"didFailWithError"];
}
- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet error:(NSError *)error {
// Eg they're not connected to any network
[self failPing:@"didFailToSendPacket"];
}
- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet {
[self successPing];
}
@end