-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
TPHostsManager.m
327 lines (269 loc) · 7.83 KB
/
TPHostsManager.m
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//
// TPHostsManager.m
// teleport
//
// Created by JuL on 30/01/05.
// Copyright 2003-2005 abyssoft. All rights reserved.
//
#import "TPHostsManager.h"
#import "TPLocalHost.h"
#import "TPClientController.h"
#import "TPNetworkConnection.h"
#import "TPAuthenticationManager.h"
#import "TPPreferencesManager.h"
#import "TPMessage.h"
#define DUPLICATE 0
#define HOSTS_VERSION 6
NSString * TPHostsVersionKey = @"TPHostsVersion";
NSString * TPHostsKey = @"TPHosts";
NSString *TPHostsConfigurationDidChangeNotification = @"TPHostsConfigurationDidChangeNotification";
static TPHostsManager * _defaultHostsManager = nil;
@implementation TPHostsManager
+ (TPHostsManager*)defaultManager
{
if(_defaultHostsManager == nil)
_defaultHostsManager = [[TPHostsManager alloc] init];
return _defaultHostsManager;
}
- (instancetype) init
{
self = [super init];
_hosts = [[NSMutableDictionary alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyChanges) name:TPHostDidUpdateNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveHosts) name:TPHostDidUpdateNotification object:nil];
return self;
}
#pragma mark -
#pragma mark Persistence
- (void)loadHosts
{
int hostsVersion = [[TPPreferencesManager sharedPreferencesManager] intForPref:TPHostsVersionKey];
if(hostsVersion != HOSTS_VERSION)
return;
NSData * archivedData = [[TPPreferencesManager sharedPreferencesManager] valueForPref:TPHostsKey];
if(archivedData != nil) {
[_hosts removeAllObjects];
NSArray * newHosts = nil;
@try {
newHosts = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
}
@catch(NSException * e) {
newHosts = nil;
}
if(newHosts != nil) {
NSEnumerator * newHostsEnum = [newHosts objectEnumerator];
TPRemoteHost * newHost;
while((newHost = [newHostsEnum nextObject])) {
TPHostState state = [newHost hostState];
switch(state) {
case TPHostSharedState:
case TPHostIncompatibleState:
break;
case TPHostUndefState:
case TPHostPeeredOfflineState:
_hosts[[newHost identifier]] = newHost;
break;
case TPHostPeeredOnlineState:
case TPHostControlledState:
[newHost setHostState:TPHostPeeredOfflineState];
_hosts[[newHost identifier]] = newHost;
break;
default:
break;
}
}
}
}
}
- (void)saveHosts
{
NSMutableDictionary * hostsToSave = [_hosts mutableCopy];
NSEnumerator * hostsEnum = [_hosts objectEnumerator];
TPRemoteHost * host;
while((host = [hostsEnum nextObject])) {
TPHostState state = [host hostState];
switch(state) {
case TPHostSharedState:
case TPHostIncompatibleState:
case TPHostUndefState:
[hostsToSave removeObjectForKey:[host identifier]];
break;
default:
break;
}
}
[[TPPreferencesManager sharedPreferencesManager] setValue:[NSKeyedArchiver archivedDataWithRootObject:hostsToSave] forKey:TPHostsKey];
[[TPPreferencesManager sharedPreferencesManager] setValue:@HOSTS_VERSION forKey:TPHostsVersionKey];
}
#pragma mark -
#pragma mark Setters
//- (void)addRemoteHost:(TPRemoteHost*)host
//{
// TPRemoteHost * knownHost = [self hostWithIdentifier:[host identifier]];
// BOOL shouldNotify = YES;
//
// DebugLog(@"new host: %@%@", host, knownHost?@" (known)":@"");
//
// if(knownHost != nil) {
// if([host address] != nil && [knownHost address] == nil) {
// [knownHost setAddress:[host address]];
// [knownHost setScreenSize:[host screenSize]];
// }
// }
// else
// [_hosts setObject:host forKey:[host identifier]];
//
// if(shouldNotify)
// [self notifyChanges];
//}
- (void)removeRemoteHost:(TPRemoteHost*)host
{
TPRemoteHost * knownHost = [self hostWithIdentifier:[host identifier]];
BOOL shouldNotify = YES;
DebugLog(@"remove host: %@", knownHost);
if(knownHost != nil) {
TPHostState state = [knownHost hostState];
switch(state) {
case TPHostUndefState:
shouldNotify = NO;
break;
case TPHostSharedState:
case TPHostIncompatibleState:
[knownHost setHostState:TPHostUndefState];
break;
case TPHostPeeredOfflineState:
shouldNotify = NO;
DebugLog(@"error: offline peered host disappeared on rdv");
break;
case TPHostPeeredOnlineState:
[knownHost setHostState:TPHostPeeredOfflineState];
break;
case TPHostControlledState:
[knownHost setHostState:TPHostPeeredOfflineState];
[[TPClientController defaultController] stopControl];
break;
default:
break;
}
}
else
shouldNotify = NO;
if(shouldNotify)
[self notifyChanges];
}
- (void)addBonjourHost:(TPRemoteHost*)bonjourHost
{
TPRemoteHost * knownHost = [self hostWithIdentifier:[bonjourHost identifier]];
BOOL shouldNotify = YES;
DebugLog(@"new bonjour host: %@%@", bonjourHost, knownHost?@" (known)":@"");
if(knownHost != nil) {
if([bonjourHost address] != nil) {
TPHostState state = [knownHost hostState];
[knownHost setAddress:[bonjourHost address]];
[knownHost setPort:[bonjourHost port]];
[knownHost setScreens:[bonjourHost screens]];
switch(state) {
case TPHostUndefState:
[knownHost setHostState:TPHostSharedState];
break;
case TPHostSharedState:
shouldNotify = NO;
break;
case TPHostPeeredOfflineState:
[knownHost setHostState:TPHostPeeredOnlineState];
break;
case TPHostPeeredOnlineState:
break;
case TPHostControlledState:
break;
default:
break;
}
}
else {
[knownHost setHostState:TPHostIncompatibleState];
}
}
else {
_hosts[[bonjourHost identifier]] = bonjourHost;
#if DUPLICATE
int i;
for(i=0; i<DUPLICATE; i++) {
TPRemoteHost * fakeHost = [bonjourHost copy];
NSString * identifier = [[bonjourHost identifier] stringByAppendingString:[NSString stringWithFormat:@"%d", i]];
[fakeHost setIdentifier:identifier];
[fakeHost setHostState:TPHostOnlineState];
[_hosts setObject:fakeHost forKey:identifier];
[fakeHost release];
}
#endif
}
if(shouldNotify)
[self notifyChanges];
}
- (void)removeBonjourHost:(TPRemoteHost*)bonjourHost
{
[self removeRemoteHost:bonjourHost];
}
- (TPRemoteHost*)updatedHostFromData:(NSData*)hostData
{
TPRemoteHost * host = (TPRemoteHost*)[TPRemoteHost hostFromHostData:hostData];
TPRemoteHost * knownHost = [self hostWithIdentifier:[host identifier]];
if(knownHost != nil) {
[knownHost setComputerName:[host computerName]];
[knownHost setCapabilities:[host capabilities]];
[knownHost setMACAddress:[host MACAddress]];
[knownHost setOSVersion:[host osVersion]];
return knownHost;
}
else
return host;
}
- (void)addClientHost:(TPRemoteHost*)clientHost
{
if(_clientHosts == nil) {
_clientHosts = [[NSMutableDictionary alloc] init];
}
_clientHosts[[clientHost identifier]] = clientHost;
}
#pragma mark -
#pragma mark Getters
- (NSArray*)hostsWithState:(TPHostState)state
{
return [[_hosts allValues] hostsWithState:state];
}
- (TPRemoteHost*)hostWithIdentifier:(NSString*)identifier
{
return _hosts[identifier];
}
- (TPRemoteHost*)hostWithAddress:(NSString*)address
{
NSEnumerator * hostEnum = [_hosts objectEnumerator];
TPRemoteHost * host;
while((host = [hostEnum nextObject])) {
if(![host isKindOfClass:[TPRemoteHost class]])
continue;
if([[host address] isEqualToString:address])
return host;
}
hostEnum = [_clientHosts objectEnumerator];
while((host = [hostEnum nextObject])) {
if(![host isKindOfClass:[TPRemoteHost class]])
continue;
if([[host address] isEqualToString:address])
return host;
}
return nil;
}
#pragma mark -
#pragma mark Misc
- (void)notifyChangesNow
{
[[NSNotificationCenter defaultCenter] postNotificationName:TPHostsConfigurationDidChangeNotification object:nil userInfo:nil];
}
- (void)notifyChanges
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(notifyChangesNow) object:nil];
[self performSelector:@selector(notifyChangesNow) withObject:nil afterDelay:0.1];
}
@end