NSTask / NSPipe wird vom Unix-Befehl gelesen

Ich schreibe eine Cocoa-Anwendung, die ein UNIX-Programm ausführen und dessen Ausgabe Zeile für Zeile lesen muss, während sie erstellt werden. Ich habe eine NSTask und eine NSPipe als solche eingerichtet:

task = [[NSTask alloc] init];
pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
//... later ...
[task setArguments:...];
[task setLaunchPath:@"..."];
[task launch];
handle = [[task fileHandleForReading] retain];

Der Befehl wird erst beendet, wenn das Programm ihn dazu auffordert[task terminate]. Ich habe verschiedene Methoden ausprobiert, um am Griff zu lesen, wie z-readInBackgroundAndNotify, while([(data = [handle availableData]) length] > 0), und-waitForDataInBackgroundAndNotify, aber die Pipe scheint nie irgendwelche Daten zu liefern. Gibt es eine Möglichkeit, die ich "stecken" kann?NSTask oderNSPipe die Daten durch zu spülen?

BEARBEITEN: mit-readInBackgroundAndNotify:

[handle readInBackgroundAndNotify];
notification_block_t handlerBlock =
    ^(NSNotification *notification) {
         NSData *data = [[notification userInfo]
                             objectForKey: NSFileHandleNotificationDataItem];
         /*... do stuff ...*/
         [self addNotification: handle block: handlerBlock];
     };
[self addNotification: handler block: handlerBlock];
//...
- (void)addNotification:(id)handle block:(notification_block_t)block {
    [[NSNotificationCenter defaultCenter]
         addObserverForName: NSFileHandleReadCompletionNotification
         object: handle
         queue: [NSOperationQueue mainQueue]
         usingBlock: block];
}

mit-waitForDataInBackgroundAndNotify:

[handle waitForDataInBackgroundAndNotify];
notification_block_t handlerBlock =
    ^(NSNotification *notification) {
        NSData *data = [handle availableData];
        /*... do stuff ...*/
    };
[self addNotification: handler block: handlerBlock];

mitwhile Schleife:

[self startProcessingThread: handle];
//...
- (void)startProcessingThread:(NSFileHandle *)handle {
    [[NSOperationQueue mainQueue]
         addOperation: [[[NSInvocationOperation alloc]
                             initWithTarget: self
                             selector: @selector(dataLoop:)
                             object: handle] autorelease]];
}
- (void)dataLoop:(NSFileHandle *)handle {
    NSData *data;
    while([(data = [handle availableData]) length] > 0) {
        /*... do stuff ...*/
    }
}

BEARBEITEN 2: Die Argumente werden wie folgt gesetzt (der Befehl lautettshark):

NSArray *cmd = [NSArray arrayWithObjects:@"-R", @"http.request", 
                                         @"-Tfields", @"-Eseparator='|'", 
                                         @"-ehttp.host", @"-ehttp.request.method", 
                                         @"-ehttp.request.uri", nil];
cmd = [[cmd arrayByAddingObjectsFromArray:[self.ports map:^(id arg1, NSUInteger idx) {
           return [NSString stringWithFormat:@"-d tcp.port==%d,http", [arg1 intValue]];
       }]] 
        arrayByAddingObject:[@"dst " stringByAppendingString:
            [self.hosts componentsJoinedByString:@" or dst "]]];
[self.tsharktask setArguments:cmd];

Antworten auf die Frage(2)

Ihre Antwort auf die Frage