blob: 38323d39cfa9b52d2dec2e74c3ad2afc944331da (
plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
//
// RenManager.m
// Liaison
//
// Created by Brian Cully on Sun Feb 16 2003.
// Copyright (c) 2003 Brian Cully. All rights reserved.
//
#import "RenIPC.h"
#import "RenManager.h"
#import "ClientManager.h"
#import "ServerManager.h"
#import <netinet/in.h>
#import <sys/socket.h>
@implementation RenManager (NetServiceDelegate)
- (void)serverManagerDied: (NSNotification *)aNotification
{
ServerManager *deadServer;
[LiLog logAsDebug: @"server manager died."];
deadServer = [aNotification object];
[theServersByName removeObjectForKey: [[deadServer service] name]];
}
- (void)clientManagerDied: (NSNotification *)aNotification
{
ClientManager *client;
client = [aNotification object];
if (client != nil) {
[LiLog logAsDebug: @"removing client for %@!", [client file]];
[theClients removeObjectForKey: [client file]];
[LiLog logAsDebug: @"removed client!"];
}
}
- (void)acceptConnection: (NSNotification *)aNotification
{
ClientManager *client;
NSDictionary *userInfo;
NSFileHandle *socket;
NSNotificationCenter *defaultCenter;
userInfo = [aNotification userInfo];
socket = [userInfo objectForKey:
NSFileHandleNotificationFileHandleItem];
client = [[ClientManager alloc] initWithFile: socket
andFileStore: [self fileStore]];
//[socket release];
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver: self
selector: @selector(clientManagerDied:)
name: CLIENTMANAGERDEATHNOTIFICATION
object: client];
[theClients setObject: client forKey: [client file]];
[client startup];
[client release];
[[self listenSocket] acceptConnectionInBackgroundAndNotify];
}
- (void)netServiceWillPublish:(NSNetService *)sender
{
theShareIsEnabled = YES;
[self setService: sender];
}
- (void)netServiceDidStop: (NSNetService *)sender
{
theShareIsEnabled = NO;
[self setService: nil];
}
- (void)netServiceBrowser: (NSNetServiceBrowser *)aNetServiceBrowser
didFindService: (NSNetService *)aNetService
moreComing: (BOOL)moreComing
{
[aNetService setDelegate: self];
[aNetService resolve];
}
- (void)netServiceBrowserDidStopSearch: (NSNetServiceBrowser *)browser
{
[LiLog logAsDebug: @"stopping search."];
}
- (void)netServiceWillResolve: (NSNetService *)aNetService
{
}
- (void)netServiceDidResolveAddress: (NSNetService *)aNetService
{
ServerManager *server;
if ([theServersByName objectForKey: [aNetService name]] == nil) {
server = [[ServerManager alloc] initWithNetService: aNetService];
if (server != nil) {
[theServersByName setObject: server forKey: [aNetService name]];
[server startup];
}
}
}
// XXX - should set group name to IP address and proceed.
- (void)netService: (NSNetService *)aNetService
didNotResolve: (NSDictionary *)someErrors
{
[LiLog logAsDebug: @"XXX: Couldn't resolve address for %@.", [aNetService name]];
}
- (void)netServiceBrowser: (NSNetServiceBrowser *)aNetServiceBrowser
didRemoveService: (NSNetService *)aNetService
moreComing: (BOOL)moreComing
{
ServerManager *removedServer;
[LiLog logAsDebug: @"removing service: %@", [aNetService name]];
removedServer = [theServersByName objectForKey: [aNetService name]];
[LiLog logAsDebug: @"\tremoved file store: %@", [[removedServer fileStore] storeID]];
[removedServer shutdown];
}
@end
@implementation RenManager
static RenManager *sharedManager = nil;
+ (RenManager *)sharedManager
{
if (sharedManager == nil)
sharedManager = [[RenManager alloc] init];
return sharedManager;
}
- (id)init
{
NSNotificationCenter *defaultCenter;
self = [super init];
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver: self
selector: @selector(serverManagerDied:)
name: SERVERMANAGERDEATHNOTIFICATION
object: nil];
[self setFileStore: nil];
theServersByName = [[NSMutableDictionary alloc] init];
theClients = [[NSMutableDictionary alloc] init];
[self setListenSocket: nil];
[self setListenPort: nil];
[self setService: nil];
[self setBrowser: nil];
theShareIsEnabled = NO;
return self;
}
- (void)dealloc
{
NSNotificationCenter *defaultCenter;
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver: self];
[self setFileStore: nil];
[theServersByName release];
[theClients release];
[self setListenSocket: nil];
[self setListenPort: nil];
[self setService: nil];
[self setBrowser: nil];
[super dealloc];
}
- (void)startup
{
NSNetServiceBrowser *browser;
if ([[Preferences sharedPreferences] networkEnabled])
[self startSharing];
browser = [[NSNetServiceBrowser alloc] init];
[browser autorelease];
[browser setDelegate: self];
[browser searchForServicesOfType: LiRendezvousPortName
inDomain: @""];
[self setBrowser: browser];
}
- (void)startSharing
{
NSFileHandle *listenSocket;
NSNetService *service;
NSNotificationCenter *defaultCenter;
NSSocketPort *listenPort;
NSString *hostname;
struct sockaddr *addr;
int port;
if (theShareIsEnabled)
return;
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver: self
selector: @selector(acceptConnection:)
name: NSFileHandleConnectionAcceptedNotification
object: nil];
listenPort = [[NSSocketPort alloc] init];
[listenPort autorelease];
listen([listenPort socket], 10);
[self setListenPort: listenPort];
listenSocket = [[NSFileHandle alloc] initWithFileDescriptor: [listenPort socket]
closeOnDealloc: YES];
[listenSocket autorelease];
[listenSocket acceptConnectionInBackgroundAndNotify];
[self setListenSocket: listenSocket];
hostname = [[Preferences sharedPreferences] hostname];
addr = (struct sockaddr *)[[listenPort address] bytes];
if (addr->sa_family == AF_INET6)
port = ((struct sockaddr_in6 *)addr)->sin6_port;
else
port = ((struct sockaddr_in *)addr)->sin_port;
service = [[NSNetService alloc] initWithDomain: @""
type: LiRendezvousPortName
name: hostname
port: port];
[service autorelease];
[service setDelegate: self];
[service publish];
}
- (void)stopSharing
{
if (theShareIsEnabled) {
NSNotificationCenter *defaultCenter;
NSEnumerator *clientEnum;
id clientKey;
[[self service] stop];
[self setListenSocket: nil];
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver: self
name: NSFileHandleConnectionAcceptedNotification
object: nil];
clientEnum = [theClients keyEnumerator];
while ((clientKey = [clientEnum nextObject]) != nil) {
ClientManager *client;
[LiLog logAsDebug: @"shutting down client"];
client = [theClients objectForKey: clientKey];
[client shutdown];
}
[LiLog logAsDebug: @"sharing stopped."];
}
}
- (void)updateHostname
{
if (theShareIsEnabled) {
ClientManager *client;
NSEnumerator *clientEnum;
clientEnum = [theClients objectEnumerator];
while ((client = [clientEnum nextObject]) != nil) {
[client setHostname: [[Preferences sharedPreferences] hostname]];
[client sendHostname];
}
}
}
@synthesize theBrowser;
@synthesize theServersByName;
@synthesize theService;
@synthesize theListenSocket;
@synthesize theFileStore;
@synthesize theShareIsEnabled;
@synthesize theClients;
@synthesize theListenPort;
@end
@implementation RenManager (Accessors)
- (NSNetServiceBrowser *)browser
{
return theBrowser;
}
- (void)setBrowser: (NSNetServiceBrowser *)aBrowser
{
[aBrowser retain];
[theBrowser release];
theBrowser = aBrowser;
}
- (NSSocketPort *)listenPort
{
return theListenPort;
}
- (void)setListenPort: (NSSocketPort *)aPort
{
[aPort retain];
[theListenPort release];
theListenPort = aPort;
}
- (NSFileHandle *)listenSocket
{
return theListenSocket;
}
- (void)setListenSocket: (NSFileHandle *)aSocket
{
[aSocket retain];
[theListenSocket release];
theListenSocket = aSocket;
}
- (NSNetService *)service
{
return theService;
}
- (void)setService: (NSNetService *)aService
{
[aService retain];
[theService release];
theService = aService;
}
- (LiFileStore *)fileStore
{
return theFileStore;
}
- (void)setFileStore: (LiFileStore *)aFileStore
{
[aFileStore retain];
[theFileStore release];
theFileStore = aFileStore;
}
@end
|