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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
//
// ClientManager.m
// Liaison
//
// Created by Brian Cully on Sun Feb 16 2003.
// Copyright (c) 2003 Brian Cully. All rights reserved.
//
#import "ClientManager.h"
#import "LiDataTranslator.h"
#import "RenIPC.h"
#import <netinet/in.h>
#import <string.h>
#import <sys/socket.h>
#import <unistd.h>
@interface ClientManager (Downloader)
- (void)startCopyOfFileHandle: (LiFileHandle *)aFileHandle;
@end
@implementation ClientManager
- (BOOL)sendCommand: (NSDictionary *)aCmd
{
NSData *cmdData;
cmdData = [aCmd encodedData];
if (cmdData != nil) {
[(NSFileHandleExtensions *)[self file] writeDataInBackground: cmdData];
return YES;
} else
return NO;
}
- (NSDictionary *)processData: (NSData *)someData
{
NSDictionary *msg;
[[self buffer] appendData: someData];
msg = [NSDictionary dictionaryWithEncodedData: [self buffer]];
if (msg != nil)
[self setBuffer: [NSMutableData data]];
return msg;
}
- (id)initWithFile: (NSFileHandle *)aFile
andFileStore: (LiFileStore *)aFileStore
{
NSNotificationCenter *defaultCenter;
self = [super init];
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver: self
selector: @selector(remoteClosed:)
name: FileHandleClosed
object: aFile];
[self setFile: aFile];
[self setFileStore: aFileStore];
[self setCopyFile: nil];
[self setBuffer: [NSMutableData data]];
return self;
}
- (void)dealloc
{
NSNotificationCenter *defaultCenter;
[LiLog logAsDebug: @"[ClientManager dealloc]"];
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver: self];
[self setFile: nil];
[self setFileStore: nil];
[self setCopyFile: nil];
[self setBuffer: nil];
[super dealloc];
}
- (void)startup
{
NSNotificationCenter *defaultCenter;
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver: self
selector: @selector(doHandshake:)
name: NSFileHandleReadCompletionNotification
object: [self file]];
[[self file] readInBackgroundAndNotify];
}
- (BOOL)filePassesFilter: (LiFileHandle *)aFileHandle
withCustomAttributes: (NSDictionary *)someAttrs
{
NSArray *groups;
if ([aFileHandle url] == nil || [[aFileHandle url] isFileURL] == NO)
return NO;
groups = [someAttrs objectForKey: LiGroupsAttribute];
if (groups != nil) {
return [groups count] > 0;
} else {
return [[aFileHandle groups] count] > 0;
}
}
- (BOOL)filePassesFilter: (LiFileHandle *)aFileHandle
{
return [self filePassesFilter: aFileHandle
withCustomAttributes: nil];
}
- (NSMutableDictionary *)filteredAttributesForAttributes:
(NSDictionary *)someAttrs
{
NSMutableDictionary *filteredAttributes;
[LiLog indentDebugLog];
filteredAttributes = [NSMutableDictionary dictionaryWithDictionary: someAttrs];
[filteredAttributes setObject: [NSNumber numberWithBool: NO]
forKey: LiIsEditableAttribute];
[filteredAttributes removeObjectForKey: LiDirectoryAttribute];
[filteredAttributes removeObjectForKey: LiApplicationAttribute];
[filteredAttributes removeObjectForKey: @"LiAliasAttribute"];
[LiLog unindentDebugLog];
return filteredAttributes;
}
- (NSMutableDictionary *)filteredAttributesForFileHandle:
(LiFileHandle *)aFileHandle
{
return [self filteredAttributesForAttributes: [[aFileHandle fileStore] attributesForFileHandle: aFileHandle]];
}
- (void)sendFileList: (NSArray *)aFileList
{
LiFileHandle *file;
NSMutableArray *filteredChanges;
[LiLog logAsDebug: @"Filtering file list."];
filteredChanges = [[NSMutableArray alloc] init];
for (file in aFileList) {
if ([self filePassesFilter: file]) {
NSDictionary *fileAttrs;
fileAttrs = [self filteredAttributesForFileHandle: file];
[filteredChanges addObject: fileAttrs];
}
}
[LiLog logAsDebug: @"Done filtering file list."];
if ([filteredChanges count] > 0) {
NSDictionary *tmpDict;
[LiLog logAsDebug: @"Creating command dictionary."];
tmpDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: RenFilesAddedKey, filteredChanges, nil]
forKeys:
[NSArray arrayWithObjects: @"type", @"arg", nil]];
[LiLog logAsDebug: @"Done creating command dictionary."];
[self sendCommand: tmpDict];
}
}
- (void)sendDeleteList: (NSArray *)aFileList
{
NSDictionary *fileInfo;
NSMutableArray *deleteList;
deleteList = [NSMutableArray array];
for (fileInfo in aFileList) {
LiFileHandle *file;
NSDictionary *oldAttributes;
file = [fileInfo objectForKey: LiFileHandleAttribute];
oldAttributes = [fileInfo objectForKey: LiFileOldAttributes];
if ([self filePassesFilter: file
withCustomAttributes: oldAttributes]) {
[deleteList addObject: [file fileID]];
}
}
if ([deleteList count] > 0) {
NSDictionary *tmpDict;
tmpDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: RenFilesRemovedKey, deleteList, nil]
forKeys:
[NSArray arrayWithObjects: @"type", @"arg", nil]];
[self sendCommand: tmpDict];
}
}
- (void)sendChangeList: (NSArray *)aFileList
{
NSDictionary *changeDict, *fileInfo;
NSMutableArray *changeList, *addList, *deleteList;
changeList = [NSMutableArray array];
addList = [NSMutableArray array];
deleteList = [NSMutableArray array];
for (fileInfo in aFileList) {
LiFileHandle *file;
NSDictionary *oldAttributes;
file = [fileInfo objectForKey: LiFileHandleAttribute];
oldAttributes = [fileInfo objectForKey: LiFileOldAttributes];
if ([self filePassesFilter: file
withCustomAttributes: oldAttributes]) {
NSDictionary *fileAttrs;
if ([self filePassesFilter: file]) {
NSEnumerator *attrEnum;
NSMutableDictionary *filtAttrs;
NSString *attr;
filtAttrs = [[NSMutableDictionary alloc] init];
fileAttrs = [self filteredAttributesForAttributes: oldAttributes];
attrEnum = [oldAttributes keyEnumerator];
while ((attr = [attrEnum nextObject]) != nil) {
[filtAttrs setObject: [file valueForAttribute: attr]
forKey: attr];
}
[filtAttrs setObject: [file fileID]
forKey: LiFileHandleAttribute];
[changeList addObject: filtAttrs];
[filtAttrs release];
} else
[deleteList addObject:
[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: file, oldAttributes, nil]
forKeys:
[NSArray arrayWithObjects: LiFileHandleAttribute, LiFileOldAttributes, nil]]];
} else if ([self filePassesFilter: file])
[addList addObject: file];
}
[self sendDeleteList: deleteList];
[self sendFileList: addList];
if ([changeList count] > 0) {
changeDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: RenFilesChangedKey, changeList, nil]
forKeys:
[NSArray arrayWithObjects: @"type", @"arg", nil]];
[self sendCommand: changeDict];
}
}
/*
* Figure out what type of connection this is, and dispatch appropriately.
*/
- (void)doHandshake: (NSNotification *)aNotification
{
NSData *data;
NSDictionary *clientMsg;
NSFileHandle *remoteSocket;
NSNotificationCenter *defaultCenter;
remoteSocket = [aNotification object];
defaultCenter = [NSNotificationCenter defaultCenter];
data = [[aNotification userInfo] objectForKey:
NSFileHandleNotificationDataItem];
clientMsg = [self processData: data];
if (clientMsg != nil) {
NSString *clientType;
clientType = [clientMsg objectForKey: @"type"];
if ([clientType isEqualToString: @"browser"]) {
[defaultCenter removeObserver: self
name: NSFileHandleReadCompletionNotification
object: [self file]];
[self sendFileList: [[self fileStore] allFileHandles]];
[defaultCenter addObserver: self
selector: @selector(respondToFileChanged:)
name: LiFileChangedNotification
object: [self fileStore]];
} else if ([clientType isEqualToString: @"download"]) {
LiFileHandle *tmpFile;
id handle;
handle = [clientMsg objectForKey: LiFileHandleAttribute];
tmpFile = [[[LiFileHandle alloc] init] autorelease];
[tmpFile setFileStore: [self fileStore]];
[tmpFile setFileID: handle];
if ([self filePassesFilter: tmpFile]) {
[defaultCenter removeObserver: self
name: NSFileHandleReadCompletionNotification
object: [self file]];
[self startCopyOfFileHandle: tmpFile];
} else {
[LiLog logAsWarning: @"attempt to access non-shared file: %@.", [tmpFile url]];
[self shutdown];
return;
}
}
}
[remoteSocket readInBackgroundAndNotify];
}
- (void)shutdown
{
NSNotificationCenter *defaultCenter;
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter postNotificationName: CLIENTMANAGERDEATHNOTIFICATION
object: self
userInfo: nil];
}
- (void)sendHostname
{
NSDictionary *tmpDict;
tmpDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: RenHostnameKey, [self hostname], nil]
forKeys:
[NSArray arrayWithObjects: @"type", @"arg", nil]];
[self sendCommand: tmpDict];
}
- (void)respondToFileChanged: (NSNotification *)aNotification
{
NSDictionary *fileDict;
fileDict = [aNotification userInfo];
if ([fileDict objectForKey: LiFilesAdded] != nil) {
[self sendFileList: [fileDict objectForKey: LiFilesAdded]];
}
if ([fileDict objectForKey: LiFilesChanged]) {
[self sendChangeList: [fileDict objectForKey: LiFilesChanged]];
}
if ([fileDict objectForKey: LiFilesRemoved]) {
[self sendDeleteList: [fileDict objectForKey: LiFilesRemoved]];
}
}
- (void)remoteClosed: (NSNotification *)aNotification
{
[self shutdown];
}
@synthesize theFile;
@synthesize theFileStore;
@synthesize theHostname;
@synthesize theBuffer;
@synthesize theCopyFile;
@end
@implementation ClientManager (Accessors)
- (NSMutableData *)buffer
{
return theBuffer;
}
- (void)setBuffer: (NSMutableData *)aBuffer
{
[aBuffer retain];
[theBuffer release];
theBuffer = aBuffer;
}
- (NSFileHandle *)copyFile
{
return theCopyFile;
}
- (void)setCopyFile: (NSFileHandle *)aFile
{
[aFile retain];
[theCopyFile release];
theCopyFile = aFile;
}
- (NSFileHandle *)file
{
return theFile;
}
- (void)setFile: (NSFileHandle *)aFile
{
[aFile retain];
[theFile release];
theFile = aFile;
}
- (NSString *)hostname
{
return theHostname;
}
- (void)setHostname: (NSString *)aHostname
{
[aHostname retain];
[theHostname release];
theHostname = aHostname;
}
- (LiFileStore *)fileStore
{
return theFileStore;
}
- (void)setFileStore: (LiFileStore *)aFileStore
{
[aFileStore retain];
[theFileStore release];
theFileStore = aFileStore;
}
@end
@implementation ClientManager (Downloader)
#define XMITBUFFLEN 1400
- (void)readyToWrite: (NSNotification *)aNotification
{
NSData *fileData;
NSFileHandle *remoteSocket;
remoteSocket = [aNotification object];
if (remoteSocket == nil)
remoteSocket = [self file];
fileData = [[self copyFile] readDataOfLength: XMITBUFFLEN];
if ([fileData length] < XMITBUFFLEN) {
NSNotificationCenter *defaultCenter;
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver: self
name: FileHandleWriteComplete
object: remoteSocket];
[defaultCenter addObserver: self
selector: @selector(finishedCopying:)
name: FileHandleWriteComplete
object: remoteSocket];
}
[(NSFileHandleExtensions *)remoteSocket writeDataInBackground: fileData];
}
- (void)startCopyOfFileHandle: (LiFileHandle *)aFileHandle
{
NSNotificationCenter *defaultCenter;
NSURL *fileURL;
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver: self
selector: @selector(readyToWrite:)
name: FileHandleWriteComplete
object: [self file]];
fileURL = [aFileHandle url];
if (fileURL != nil && [fileURL isFileURL]) {
[self setCopyFile:
[NSFileHandle fileHandleForReadingAtPath: [fileURL path]]];
}
[self readyToWrite: nil];
}
- (void)finishedCopying: (NSNotification *)aNotification
{
NSFileHandle *remoteSocket;
NSNotificationCenter *defaultCenter;
remoteSocket = [aNotification object];
defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver: self
name: FileHandleWriteComplete
object: remoteSocket];
[defaultCenter removeObserver: self
name: NSFileHandleReadCompletionNotification
object: remoteSocket];
[self setCopyFile: nil];
[self shutdown];
}
@end
|