From 8469ad06db4368a839a85087c0cffc3a084368cc Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 26 Feb 2008 11:35:15 -0500 Subject: [PATCH] Adding a method to clean up the code for reading the channel * Source/AppController.m ([AppController readChannel:]): New method --- Source/AppController.m | 53 ++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/Source/AppController.m b/Source/AppController.m index 56a7ae4..feaafda 100644 --- a/Source/AppController.m +++ b/Source/AppController.m @@ -25,6 +25,8 @@ //- (BOOL)uploadFile; - (void)setStatus:(NSString *)msg isError:(BOOL)error; +- (void)readChannel:(LIBSSH2_CHANNEL *)channel; + @end @@ -72,6 +74,25 @@ } } +/** + * Reads through a channel (in non-blocking) mode until there is no more left to read + * and then it returns. This calls sleep(1) so that the channel can have time to process. + * Be sure this is threaded otherwis the interface will stall. + */ +- (void)readChannel:(LIBSSH2_CHANNEL *)channel +{ + libssh2_channel_set_blocking(channel, 0); + + char buf[1024]; + int numbytes; + do + { + memset(&buf, '\0', sizeof(buf)); + sleep(1); + numbytes = libssh2_channel_read(channel, buf, sizeof(buf)); + } while (numbytes > 0); +} + /** * Sends an item to the printer */ @@ -179,36 +200,12 @@ [self setStatus:@"Opened remote SSH shell" isError:NO]; - libssh2_channel_flush_ex(channel, LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL); - - /* - char *e = malloc(1024 * sizeof(char)); - libssh2_session_last_error(ssh, &e, NULL, 0); - NSLog(@"e = %s", e); - */ - - libssh2_channel_set_blocking(channel, 0); - - char b[1024]; - int r; - do - { - memset(&b, '\0', sizeof(b)); - sleep(1); - r = libssh2_channel_read(channel, b, sizeof(b)); - NSLog(@"b(%i) = %s", r, b); - } while (r > 0); + // read the banner + [self readChannel:channel]; char *cmd = "touch /u17/ugrad/rsesek/temp.foo\r\n\0"; - NSLog(@"numbytes = %i", libssh2_channel_write(channel, cmd, sizeof(char) * strlen(cmd))); - - do - { - memset(&b, '\0', sizeof(b)); - sleep(1); - r = libssh2_channel_read(channel, b, sizeof(b)); - NSLog(@"b(%i) = %s", r, b); - } while (r > 0); + libssh2_channel_write(channel, cmd, sizeof(char) * strlen(cmd)); + [self readChannel:channel]; [self setStatus:@"Printed!" isError:NO]; -- 2.43.5