forked from ptxmac/PingBar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPBPinger.m
More file actions
100 lines (71 loc) · 2.33 KB
/
Copy pathPBPinger.m
File metadata and controls
100 lines (71 loc) · 2.33 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
100
//
// PBPinger.m
// PingBar
//
// Created by Peter Kristensen on 04/02/10.
// Copyright 2010 Lucky Software. All rights reserved.
//
#import "PBPinger.h"
@interface PBPinger (Private)
- (void)setup;
@end
@implementation PBPinger
@synthesize host;
- (NSTimeInterval)pingOnce {
//NSLog(@"ping start");
NSTask *task = [[NSTask alloc] init];
NSDate *date = [NSDate date];
[task setLaunchPath:@"/sbin/ping"];
[task setArguments:[NSArray arrayWithObjects:
@"-o",
@"-c",@"1",
@"-q",
host,
nil]];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task launch];
[task waitUntilExit];
//[NSThread sleepForTimeInterval:4.0];
//NSLog(@"ping done: %@",host);
int termStatus = [task terminationStatus];
[task release];
NSFileHandle *hd = [pipe fileHandleForReading];
NSData *data = [hd readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (termStatus != 0) {
NSLog(@"infinity: %i",termStatus);
return -1.0;
} else {
__block NSTimeInterval dt = [[NSDate date] timeIntervalSinceDate:date];
[string enumerateLinesUsingBlock:^(NSString *str, BOOL *stop) {
if ([str hasPrefix:@"round-trip"]) {
*stop = YES;
NSArray *comps = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/ "]];
[comps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
float d = [obj floatValue];
if (d != 0.0) {
*stop = YES;
dt = d/1000.0;
//NSLog(@"Found: (%@) %f",obj,d);
}
}];
}
}];
return dt;
}
}
+ (PBPinger *)pingerWithHost:(NSString *)host {
return [[[PBPinger alloc] initWithHost:host] autorelease];
}
- (id)initWithHost:(NSString *)h {
self = [super init];
if (self != nil) {
self.host = h;
[self setup];
}
return self;
}
- (void)setup {
}
@end