summaryrefslogtreecommitdiffstats
path: root/Liaison/WriterThread.m
blob: f34f8c99a9d311a37755d905513c17971718f0fc (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
//
//  WriterThread.m
//  Liaison
//
//  Created by Brian Cully on Wed Feb 26 2003.
//  Copyright (c) 2003 Brian Cully. All rights reserved.
//

#import "NSFileHandleExtensions.h"
#import "WriterThread.h"

#import <errno.h>
#import <sys/types.h>
#import <sys/uio.h>
#import <unistd.h>

#define NO_DATA 0
#define HAS_DATA 1

@implementation WriterThread
- (id)initWithFileHandle: (NSFileHandle *)aFileHandle
{
    self = [super init];

    theKillFlag = NO;
    [self setDataQueue: [NSMutableArray array]];
    [self setQueueLock:
        [[[NSConditionLock alloc] initWithCondition: NO_DATA] autorelease]];
    [self setFile: aFileHandle];

    [NSThread detachNewThreadSelector: @selector(startThreadWithObject:)
                             toTarget: self
                           withObject: nil];

    return self;
}

- (void)dealloc
{
    [self setFile: nil];
    [self setDataQueue: nil];
    [self setQueueLock: nil];
    [super dealloc];
}

- (BOOL)writeBufferedData: (NSData *)someData
{
    if (theConnectionIsOpen) {
        int fileDescriptor;
        ssize_t dataLen, wroteLen;
        void *data;

        fileDescriptor = [[self file] fileDescriptor];
        data = (void *)[someData bytes];
        dataLen = [someData length];
        wroteLen = 0;
        while (wroteLen < dataLen) {
            ssize_t rc;

            rc = write(fileDescriptor,
                       data+wroteLen,
                       dataLen-wroteLen);
            if (rc >= 0) {
                wroteLen += rc;
            } else {
                if (errno == EINTR || errno == EAGAIN)
                    continue;
                theConnectionIsOpen = NO;
                break;
            }
        }
    }

    return theConnectionIsOpen;
}

- (void)sendNextData
{
    NSData *dataToSend;
    NSNotificationCenter *defaultCenter;

    [[self queueLock] lockWhenCondition: HAS_DATA];
    if (theKillFlag || theConnectionIsOpen == NO) {
        return;
    }
    if ([[self dataQueue] count] <= 0) {
        [[self queueLock] unlockWithCondition: NO_DATA];
        [LiLog unindentDebugLog];
        return;
    }

    dataToSend = [[[[self dataQueue] objectAtIndex: 0] retain] autorelease];
    [[self dataQueue] removeObjectAtIndex: 0];
    if ([[self dataQueue] count] > 0)
        [[self queueLock] unlockWithCondition: HAS_DATA];
    else
        [[self queueLock] unlockWithCondition: NO_DATA];

    defaultCenter = [NSNotificationCenter defaultCenter];
    if ([self writeBufferedData: dataToSend] == NO) {
        NSNotification *notification;

        notification = [NSNotification notificationWithName: FileHandleClosed
                                                     object: [self file]];
        [defaultCenter performSelectorOnMainThread: @selector(postNotification:)
                                        withObject: notification
                                     waitUntilDone: YES];
    } else {
        NSNotification *notification;

        notification = [NSNotification notificationWithName: FileHandleWriteComplete
                                                     object: [self file]];
        [defaultCenter performSelectorOnMainThread: @selector(postNotification:)
                                        withObject: notification
                                     waitUntilDone: NO];
    }
}

- (void)startThreadWithObject: (id)anObject
{
    NSAutoreleasePool *rp;
    NSDictionary *userInfo;
    NSNotification *notification;
    NSNotificationCenter *defaultCenter;
    NSNumber *fd;
    
    rp = [[NSAutoreleasePool alloc] init];

    fd = [NSNumber numberWithInt: [[self file] fileDescriptor]];
    theConnectionIsOpen = YES;
    while (theKillFlag == NO) {
        NSAutoreleasePool *srp;
        
        srp = [[NSAutoreleasePool alloc] init];
        [self sendNextData];
        [srp release];
    }

    userInfo = [NSDictionary dictionaryWithObject: fd
                                           forKey: @"FileDescriptorKey"];
    defaultCenter = [NSNotificationCenter defaultCenter];
    notification = [NSNotification notificationWithName: WriterThreadDied
                                                 object: self
                                               userInfo: userInfo];
    [defaultCenter performSelectorOnMainThread: @selector(postNotification:)
                                    withObject: notification
                                 waitUntilDone: YES];
    [rp release];
}

- (void)die
{
    [[self queueLock] lock];
    theKillFlag = YES;
    [[self queueLock] unlockWithCondition: HAS_DATA];
}

- (void)writeData: (NSData *)someData
{
    if (someData == nil)
        return;

    [[self queueLock] lock];
    NS_DURING
        [[self dataQueue] addObject: someData];
    NS_HANDLER
        [LiLog logAsDebug: @"Got exception '%@' trying to add to data queue: %@.", [localException name], [localException reason]];
    NS_ENDHANDLER
    [[self queueLock] unlockWithCondition: HAS_DATA];
}
@synthesize theQueueLock;
@synthesize theConnectionIsOpen;
@synthesize theKillFlag;
@synthesize theDataQueue;
@end

@implementation WriterThread (Accessors)
- (NSMutableArray *)dataQueue
{
    return theDataQueue;
}

- (void)setDataQueue: (NSMutableArray *)aQueue
{
    [aQueue retain];
    [theDataQueue release];
    theDataQueue = aQueue;
}

- (NSConditionLock *)queueLock
{
    return theQueueLock;
}

- (void)setQueueLock: (NSConditionLock *)aLock
{
    [aLock retain];
    [theQueueLock release];
    theQueueLock = aLock;
}

- (NSFileHandle *)file
{
    return theFile;
}

- (void)setFile: (NSFileHandle *)aFile
{
    theFile = aFile;
}
@end