2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 // Created by Helge Hess on Wed Apr 17 2002.
24 #include "FileObjectHolder.h"
25 #include "NSRunLoop+FileObjects.h"
26 #import <Foundation/NSException.h>
27 #import <Foundation/NSNotification.h>
28 #import <Foundation/NSFileHandle.h>
31 @implementation FileObjectHolder
33 - (id)initWithFileObject:(id)_object
34 activities:(int)_activities
35 mode:(NSString *)_mode
41 self->fileObject = [_object retain];
42 self->fd = [_object fileDescriptor];
43 self->activities = _activities;
44 self->mode = [_mode copy];
46 [[NSFileHandle alloc] initWithFileDescriptor:self->fd closeOnDealloc:NO];
47 NSAssert(self->fileHandle, @"couldn't create filehandle ...");
49 [[self notificationCenter]
50 addObserver:self selector:@selector(_dataAvailable:)
51 name:NSFileHandleDataAvailableNotification
52 object:self->fileHandle];
53 [[self notificationCenter]
54 addObserver:self selector:@selector(_acceptAvailable:)
55 name:NSFileHandleConnectionAcceptedNotification
56 object:self->fileHandle];
63 [[self notificationCenter] removeObserver:self];
65 [self->fileHandle release];
66 [self->fileObject release];
72 - (int)fileDescriptor {
75 - (NSFileHandle *)fileHandle {
76 return self->fileHandle;
79 return self->fileObject;
82 return self->activities;
89 return [NSArray arrayWithObject:[self mode]];
94 - (NSNotificationCenter *)notificationCenter {
95 return [NSNotificationCenter defaultCenter];
98 - (void)handleException:(NSException *)_exception {
99 NSLog(@"%s: catched: %@", __PRETTY_FUNCTION__, _exception);
102 - (void)_dataAvailable:(NSNotification *)_notification {
103 if ([_notification object] != self->fileHandle) {
104 NSLog(@"%s: notification object %@ does not match file handle %@",
105 __PRETTY_FUNCTION__, [_notification object], self->fileHandle);
109 if (![self->fileObject isOpen]) {
110 //NSLog(@"file object is closed ...");
115 self->waitActive = NO;
117 [[self notificationCenter]
118 postNotificationName:NSFileObjectBecameActiveNotificationName
119 object:self->fileObject];
122 [self handleException:localException];
126 - (void)_acceptAvailable:(NSNotification *)_notification {
127 NSLog(@"accept available ...");
128 if ([_notification object] != self->fileHandle) {
129 NSLog(@"%s: notification object %@ does not match file handle %@",
130 __PRETTY_FUNCTION__, [_notification object], self->fileHandle);
133 [[self notificationCenter]
134 postNotificationName:NSFileObjectBecameActiveNotificationName
135 object:self->fileObject];
141 if (self->waitActive) return;
143 if (![self->fileObject isOpen]) return;
145 self->waitActive = YES;
148 /* use accept for passive fileobjects ?, wait seems to work too ... :-) */
149 if ([self->fileObject isPassive]) {
150 NSLog(@"add passive %@ ..", self->fileObject);
151 // => this also accepts the connection :-(
152 [self->fileHandle acceptConnectionInBackgroundAndNotifyForModes:
157 [self->fileHandle waitForDataInBackgroundAndNotifyForModes:[self modes]];
159 - (void)stopWaiting {
164 - (BOOL)isEqualToFileObjectHolder:(FileObjectHolder *)_other {
165 if (self->fd != _other->fd) return NO;
166 if (![self->mode isEqualToString:_other->mode]) return NO;
167 if (![self->fileObject isEqual:_other->fileObject]) return NO;
170 - (BOOL)isEqual:(id)_other {
171 if (_other == self) return YES;
172 if (_other == nil) return NO;
173 if ([_other class] != [self class]) return NO;
174 return [self isEqualToFileObjectHolder:_other];
179 @end /* FileObject */