-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.m
More file actions
265 lines (224 loc) · 6.72 KB
/
Server.m
File metadata and controls
265 lines (224 loc) · 6.72 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
// Server.m
// ServerStatus
//
// Created by Florian Mutter on 14.05.10.
// Copyright 2010 skweez.net. All rights reserved.
//
#import "Server.h"
#include <sys/socket.h>
#include <netdb.h>
@implementation Server
@synthesize serverName, serverHost, serverError, lastKnownAddress, serverStatus, previousStatus, active, pinging, pingTimeout, pingTimeoutCount;
@synthesize pinger = _pinger;
+ (Server *)server {
return [[[Server alloc] init] autorelease];
}
#pragma mark -
#pragma mark Private
- (void)startPinging {
if (!self.pinging) {
DLog(@"%@: Start pinging", self.serverName);
self.pinging = YES;
[self.pinger start];
/* shedule timer to detect ping timeout */
self.pingTimeout = [NSTimer scheduledTimerWithTimeInterval:PingTimoutInSeconds
target:self
selector:@selector(pingTimedOut:)
userInfo:nil
repeats:NO];
}
}
- (void)stopPinging {
if (self.pinging) {
DLog(@"%@: Stop pinging", self.serverName);
if (self.pingTimeout) {
[self.pingTimeout invalidate];
}
self.pinging = NO;
[self.pinger stop];
}
}
- (void)setServerStatus:(ServerStatus)status {
self.previousStatus = serverStatus;
serverStatus = status;
}
- (void)setServerHost:(NSString *)aServerHost {
if (serverHost == aServerHost) {
return;
}
if (self.pinger) {
[self stopPinging];
self.pinger = nil;
}
[serverHost autorelease];
serverHost = [aServerHost retain];
if (serverHost) {
self.pinger = [SimplePing simplePingWithHostName:serverHost];
self.pinger.delegate = self;
}
}
- (void)setServerName:(NSString *)aServerName {
if ([self.serverHost isEqualToString:@""]) {
self.serverHost = aServerName;
}
[serverName autorelease];
serverName = [aServerName retain];
}
- (NSString *)DisplayAddressForAddress:(NSData *) address
// Returns a dotted decimal string for the specified address (a (struct sockaddr)
// within the address NSData).
{
int err;
NSString * result;
char hostStr[NI_MAXHOST];
result = nil;
if (address != nil) {
err = getnameinfo([address bytes], (socklen_t) [address length], hostStr, sizeof(hostStr), NULL, 0, NI_NUMERICHOST);
if (err == 0) {
result = [NSString stringWithCString:hostStr encoding:NSASCIIStringEncoding];
assert(result != nil);
}
}
return result;
}
#pragma mark -
#pragma mark Public
- (NSString *)lastKnownAddressAsString {
NSString * result = [self DisplayAddressForAddress:lastKnownAddress];
return result ? result : @"Unknown";
}
#pragma mark -
#pragma mark ping
- (void)pingTimedOut:(NSTimer *)timer {
[self stopPinging];
self.pingTimeoutCount ++;
DLog(@"%@: Ping timed out (%ld/%d)", self.serverName, (long)self.pingTimeoutCount, MaxPingTimeoutCount);
if (self.pingTimeoutCount >= MaxPingTimeoutCount) {
self.serverError = [NSError errorWithDomain:PingTimeoutError
code:PingTimeoutErrorCode
userInfo:[NSDictionary
dictionaryWithObject:@"Ping timed out"
forKey:NSLocalizedDescriptionKey]];
self.serverStatus = SERVER_FAIL;
self.pingTimeoutCount = 0;
} else {
[self startPinging];
}
}
- (void)ping {
[self startPinging];
}
#pragma mark -
#pragma mark Observer
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"active"]) {
if (!self.active) {
[self stopPinging];
self.serverStatus = SERVER_UNKNOWN;
}
}
if ([keyPath isEqualToString:@"serverHost"]) {
if (self.active) {
[self stopPinging];
self.serverStatus = SERVER_UNKNOWN;
[self startPinging];
}
}
}
#pragma mark -
#pragma mark Network Status
- (void)networkConnectionChanged:(NSNotification *)notification {
BOOL hasConnection = [[[notification userInfo] objectForKey:@"networkAvailable"] boolValue];
if (!hasConnection) {
[self stopPinging];
self.serverStatus = SERVER_UNKNOWN;
}
}
#pragma mark -
#pragma mark SimplePingDelegate
- (void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address {
self.lastKnownAddress = address;
[self.pinger sendPingWithData:nil];
}
- (void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet
error:(NSError *)e {
self.serverError = e;
[self stopPinging];
self.serverStatus = SERVER_ERROR;
DLog(@"%@: Failed to send Packet: %@", self.serverName, [e localizedDescription]);
}
- (void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet {
[self stopPinging];
self.serverStatus = SERVER_OK;
self.pingTimeoutCount = 0;
DLog(@"%@: Received ping response packet.", self.serverName);
}
- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)e {
self.serverError = e;
[self stopPinging];
self.serverStatus = SERVER_ERROR;
DLog(@"%@: Failed with Error: %@", self.serverName, [e localizedDescription]);
}
#pragma mark -
#pragma mark Init
- (void)initializeValues {
[self addObserver:self forKeyPath:@"active" options:0 context:NULL];
[self addObserver:self forKeyPath:@"serverHost" options:0 context:NULL];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(networkConnectionChanged:)
name:NetworkChangeNotification
object:nil];
self.serverStatus = SERVER_UNKNOWN;
self.previousStatus = SERVER_UNKNOWN;
self.pinging = NO;
self.pingTimeoutCount = 0;
}
- (id)init
{
self = [super init];
if (self != nil) {
self.serverName = @"New Server";
self.serverHost = @"";
self.lastKnownAddress = nil;
self.active = NO;
self.serverError = nil;
[self initializeValues];
}
return self;
}
- (void)dealloc {
self.serverStatus = SERVER_UNKNOWN;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self];
[self removeObserver:self forKeyPath:@"active"];
[self removeObserver:self forKeyPath:@"serverHost"];
[self stopPinging];
self.pinger = nil;
self.pingTimeout = nil;
self.lastKnownAddress = nil;
self.serverName = nil;
self.serverHost = nil;
self.serverError = nil;
[super dealloc];
}
#pragma mark NSCoding
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self != nil) {
self.serverName = [aDecoder decodeObjectForKey:@"serverName"];
self.serverHost = [aDecoder decodeObjectForKey:@"serverHost"];
self.lastKnownAddress = [aDecoder decodeObjectForKey:@"lastKnownAddress"];
self.active = [aDecoder decodeBoolForKey:@"active"];
[self initializeValues];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.serverName forKey:@"serverName"];
[aCoder encodeObject:self.serverHost forKey:@"serverHost"];
[aCoder encodeObject:self.lastKnownAddress forKey:@"lastKnownAddress"];
[aCoder encodeBool:self.active forKey:@"active"];
}
@end