2 Copyright (C) 2002-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "FileObjectHolder.h"
23 #include "NSRunLoop+FileObjects.h"
24 #import <Foundation/NSException.h>
25 #import <Foundation/NSNotification.h>
26 #import <Foundation/NSFileHandle.h>
29 @implementation FileObjectHolder
31 - (id)initWithFileObject:(id)_object
32 activities:(int)_activities
33 mode:(NSString *)_mode
39 self->fileObject = [_object retain];
40 self->fd = [_object fileDescriptor];
41 self->activities = _activities;
42 self->mode = [_mode copy];
44 [[NSFileHandle alloc] initWithFileDescriptor:self->fd closeOnDealloc:NO];
45 NSAssert(self->fileHandle, @"couldn't create filehandle ...");
47 [[self notificationCenter]
48 addObserver:self selector:@selector(_dataAvailable:)
49 name:NSFileHandleDataAvailableNotification
50 object:self->fileHandle];
51 [[self notificationCenter]
52 addObserver:self selector:@selector(_acceptAvailable:)
53 name:NSFileHandleConnectionAcceptedNotification
54 object:self->fileHandle];
61 [[self notificationCenter] removeObserver:self];
63 [self->fileHandle release];
64 [self->fileObject release];
70 - (int)fileDescriptor {
73 - (NSFileHandle *)fileHandle {
74 return self->fileHandle;
77 return self->fileObject;
80 return self->activities;
87 return [NSArray arrayWithObject:[self mode]];
92 - (NSNotificationCenter *)notificationCenter {
93 return [NSNotificationCenter defaultCenter];
96 - (void)handleException:(NSException *)_exception {
97 NSLog(@"%s: catched: %@", __PRETTY_FUNCTION__, _exception);
100 - (void)_dataAvailable:(NSNotification *)_notification {
101 if ([_notification object] != self->fileHandle) {
102 NSLog(@"%s: notification object %@ does not match file handle %@",
103 __PRETTY_FUNCTION__, [_notification object], self->fileHandle);
107 if (![self->fileObject isOpen]) {
108 //NSLog(@"file object is closed ...");
113 self->waitActive = NO;
115 [[self notificationCenter]
116 postNotificationName:NSFileObjectBecameActiveNotificationName
117 object:self->fileObject];
120 [self handleException:localException];
124 - (void)_acceptAvailable:(NSNotification *)_notification {
125 NSLog(@"accept available ...");
126 if ([_notification object] != self->fileHandle) {
127 NSLog(@"%s: notification object %@ does not match file handle %@",
128 __PRETTY_FUNCTION__, [_notification object], self->fileHandle);
131 [[self notificationCenter]
132 postNotificationName:NSFileObjectBecameActiveNotificationName
133 object:self->fileObject];
139 if (self->waitActive) return;
141 if (![self->fileObject isOpen]) return;
143 self->waitActive = YES;
146 /* use accept for passive fileobjects ?, wait seems to work too ... :-) */
147 if ([self->fileObject isPassive]) {
148 NSLog(@"add passive %@ ..", self->fileObject);
149 // => this also accepts the connection :-(
150 [self->fileHandle acceptConnectionInBackgroundAndNotifyForModes:
155 [self->fileHandle waitForDataInBackgroundAndNotifyForModes:[self modes]];
157 - (void)stopWaiting {
162 - (BOOL)isEqualToFileObjectHolder:(FileObjectHolder *)_other {
163 if (self->fd != _other->fd) return NO;
164 if (![self->mode isEqualToString:_other->mode]) return NO;
165 if (![self->fileObject isEqual:_other->fileObject]) return NO;
168 - (BOOL)isEqual:(id)_other {
169 if (_other == self) return YES;
170 if (_other == nil) return NO;
171 if ([_other class] != [self class]) return NO;
172 return [self isEqualToFileObjectHolder:_other];
177 @end /* FileObject */