Adding a method to clean up the code for reading the channel
authorRobert Sesek <rsesek@bluestatic.org>
Tue, 26 Feb 2008 16:35:15 +0000 (11:35 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Tue, 26 Feb 2008 16:35:15 +0000 (11:35 -0500)
* Source/AppController.m
([AppController readChannel:]): New method

Source/AppController.m

index 56a7ae4c92c1a7854ad1e42ed8e97a8da079d128..feaafda8336d321d3d6e5456710012c07a3b6080 100644 (file)
@@ -25,6 +25,8 @@
 //- (BOOL)uploadFile;
 - (void)setStatus:(NSString *)msg isError:(BOOL)error;
 
+- (void)readChannel:(LIBSSH2_CHANNEL *)channel;
+
 @end
 
 
        }
 }
 
+/**
+ * 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
  */
        
        [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];