Интерактивный доступ к команде с помощью NSTask

Я использую NSTask для запуска приложения Cli в интерактивном режиме. Я использую его для передачи данных через USB-соединение.

Я открываю NSTask с контуром и каналом ошибок, но когда я запускаю команду, она работает, но вращает пляжный мяч в цикле, извлекая данные из вывода.

Я ожидаю, что смогу помять кнопку - (IBAction)clicked:(id), которая выполняет команду справки и возвращает результат:

NSTask *usbCommandTask;
NSPipe *outPipe;
NSPipe *inPipe;
NSPipe *errorPipe;
NSFileHandle *inFile;
NSFileHandle *outFile;
NSTimer *pollTimer;
dispatch_queue_t mtpTask;

@implementation AppDelegate


- (void)commandNotification:(NSNotification *)notification
{
    NSData *data = nil;

    while ((data = [outFile availableData]) && [data length]){
        NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"Data: %@",myString);
        }
    NSLog(@"Execution never gets here");

}

-(void)checkTask
{
    if(usbCommandTask){
        if([usbCommandTask isRunning])NSLog(@"Task running"); else NSLog(@"Task dead");

    } else NSLog(@"Task is nil");


}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {



    usbCommandTask = [[NSTask alloc] init];
    [usbCommandTask setLaunchPath:@"/Applications/usb-debugger-cli"];
    [usbCommandTask setCurrentDirectoryPath:@"/"];
    [usbCommandTask setArguments:[NSArray arrayWithObject:@"-i"]];
    inPipe = [NSPipe pipe];
    [usbCommandTask setStandardInput:inPipe];




    outPipe = [NSPipe pipe];
    [usbCommandTask setStandardOutput:outPipe];
    errorPipe = [NSPipe pipe];
    [usbCommandTask setStandardError:errorPipe];

    outFile = [outPipe fileHandleForReading];
    inFile = [inPipe fileHandleForWriting];

     [outFile waitForDataInBackgroundAndNotify];



    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(commandNotification:)
                                                 name:NSFileHandleDataAvailableNotification
                                               object:nil];



    pollTimer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(checkTask) userInfo:nil repeats:TRUE];
    [usbCommandTask launch];
    NSLog(@"Launched");



}


- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
    if(usbCommandTask)[usbCommandTask terminate];


}


- (IBAction)clicked:(id)sender {

    if(usbCommandTask){
    NSString *command=@"help\n";
    NSData *commandData=[NSData dataWithBytes:command.UTF8String length:command.length];

    [inFile writeData:commandData];
    }
    else
    {NSLog(@"Comamnd task dead");
    }
}

1 ответ

[outFile availableData] 

на самом деле это блокирующий вызов, нет данных или явного EOF, так что это всегда будет блокировать, вместо этого я использовал:

data = [outFile availableData];
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data: %@",myString);
[outFile waitForDataInBackgroundAndNotify];
Другие вопросы по тегам