You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by br...@apache.org on 2004/02/08 12:25:40 UTC

cvs commit: jakarta-commons/net/src/java/org/apache/commons/net/telnet TelnetClient.java TelnetInputStream.java

brudav      2004/02/08 03:25:40

  Modified:    net/src/java/org/apache/commons/net/telnet TelnetClient.java
                        TelnetInputStream.java
  Log:
  Added setNoReaderThread() to start connections without the TelnetClient reader thread.
  New functionality requested by Scott Coleman to help add ftps support.
  
  Revision  Changes    Path
  1.9       +26 -1     jakarta-commons/net/src/java/org/apache/commons/net/telnet/TelnetClient.java
  
  Index: TelnetClient.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/net/src/java/org/apache/commons/net/telnet/TelnetClient.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TelnetClient.java	5 Jan 2004 23:56:49 -0000	1.8
  +++ TelnetClient.java	8 Feb 2004 11:25:40 -0000	1.9
  @@ -88,6 +88,7 @@
   {
       private InputStream __input;
       private OutputStream __output;
  +    protected boolean readerThread = true;
   
       /***
        * Default TelnetClient constructor.
  @@ -137,7 +138,10 @@
   
   
           tmp = new TelnetInputStream(input, this);
  -        tmp._start();
  +        if(readerThread)
  +        {
  +            tmp._start();
  +        }
           // __input CANNOT refer to the TelnetInputStream.  We run into
           // blocking problems when some classes use TelnetInputStream, so
           // we wrap it with a BufferedInputStream which we know is safe.
  @@ -305,5 +309,26 @@
       public void unregisterNotifHandler()
       {
           super.unregisterNotifHandler();
  +    }
  +
  +    /***
  +     * Sets the status of the reader thread.
  +     * The reader thread status will apply to all subsequent connections
  +     * <p>
  +     * @param flag - true switches the reader thread on, false switches it off
  +     ***/
  +    public void setReaderThread(boolean flag)
  +    {
  +        readerThread = flag;
  +    }
  +
  +    /***
  +     * Gets the status of the reader thread.
  +     * <p>
  +     * @return true if the reader thread is on, false otherwise
  +     ***/
  +    public boolean getReaderThread()
  +    {
  +        return (readerThread);
       }
   }
  
  
  
  1.10      +111 -35   jakarta-commons/net/src/java/org/apache/commons/net/telnet/TelnetInputStream.java
  
  Index: TelnetInputStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/net/src/java/org/apache/commons/net/telnet/TelnetInputStream.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- TelnetInputStream.java	9 Jan 2004 06:19:23 -0000	1.9
  +++ TelnetInputStream.java	8 Feb 2004 11:25:40 -0000	1.10
  @@ -90,6 +90,8 @@
       /* TERMINAL-TYPE option (end)*/
   
       private boolean _ayt_flag = false;
  +
  +    private boolean __threaded = false;
       TelnetInputStream(InputStream input, TelnetClient client)
       {
           super(input);
  @@ -122,6 +124,7 @@
           __thread.setPriority(priority);
           __thread.setDaemon(true);
           __thread.start();
  +        __threaded = true;
       }
   
   
  @@ -291,7 +294,45 @@
           return ch;
       }
   
  +    // synchronized(__client) critical sections are to protect against
  +    // TelnetOutputStream writing through the telnet client at same time
  +    // as a processDo/Will/etc. command invoked from TelnetInputStream
  +    // tries to write.
  +    private void __processChar(int ch) throws InterruptedException
  +    {
  +        // Critical section because we're altering __bytesAvailable,
  +        // __queueTail, and the contents of _queue.
  +        synchronized (__queue)
  +        {
  +            while (__bytesAvailable >= __queue.length - 1)
  +            {
  +                if(__threaded)
  +                {
  +                    __queue.notify();
  +                    try
  +                    {
  +                        __queue.wait();
  +                    }
  +                    catch (InterruptedException e)
  +                    {
  +                        throw e;
  +                    }
  +                }
  +            }
   
  +            // Need to do this in case we're not full, but block on a read
  +            if (__readIsWaiting && __threaded)
  +            {
  +                __queue.notify();
  +            }
  +
  +            __queue[__queueTail] = ch;
  +            ++__bytesAvailable;
  +
  +            if (++__queueTail >= __queue.length)
  +                __queueTail = 0;
  +        }
  +    }
   
       public int read() throws IOException
       {
  @@ -318,16 +359,68 @@
                           return -1;
   
                       // Otherwise, we have to wait for queue to get something
  -                    __queue.notify();
  -                    try
  +                    if(__threaded)
                       {
  -                        __readIsWaiting = true;
  -                        __queue.wait();
  -                        __readIsWaiting = false;
  +                        __queue.notify();
  +                        try
  +                        {
  +                            __readIsWaiting = true;
  +                            __queue.wait();
  +                            __readIsWaiting = false;
  +                        }
  +                        catch (InterruptedException e)
  +                        {
  +                            throw new IOException("Fatal thread interruption during read.");
  +                        }
                       }
  -                    catch (InterruptedException e)
  +                    else
                       {
  -                        throw new IOException("Fatal thread interruption during read.");
  +                        //__alreadyread = false;
  +                        __readIsWaiting = true;
  +                        int ch;
  +
  +                        do
  +                        {
  +                            try
  +                            {
  +                                if ((ch = __read()) < 0)
  +                                    if(ch != -2)
  +                                        return (ch);
  +                            }
  +                            catch (InterruptedIOException e)
  +                            {
  +                                synchronized (__queue)
  +                                {
  +                                    __ioException = e;
  +                                    __queue.notifyAll();
  +                                    try
  +                                    {
  +                                        __queue.wait(100);
  +                                    }
  +                                    catch (InterruptedException interrupted)
  +                                    {
  +                                    }
  +                                }
  +                                return (-1);
  +                            }
  +
  +
  +                            try
  +                            {
  +                                if(ch != -2)
  +                                {
  +                                    __processChar(ch);
  +                                }
  +                            }
  +                            catch (InterruptedException e)
  +                            {
  +                                if (__isClosed)
  +                                    return (-1);
  +                            }
  +                        }
  +                        while (super.available() > 0);
  +
  +                        __readIsWaiting = false;
                       }
                       continue;
                   }
  @@ -448,6 +541,8 @@
   
               __queue.notifyAll();
           }
  +
  +        __threaded = false;
       }
   
       public void run()
  @@ -491,35 +586,14 @@
                       break _outerLoop;
                   }
   
  -                // Critical section because we're altering __bytesAvailable,
  -                // __queueTail, and the contents of _queue.
  -                synchronized (__queue)
  +                try
                   {
  -                    while (__bytesAvailable >= __queue.length - 1)
  -                    {
  -                        __queue.notify();
  -                        try
  -                        {
  -                            __queue.wait(100);
  -                        }
  -                        catch (InterruptedException e)
  -                        {
  -                            if (__isClosed)
  -                                break _outerLoop;
  -                        }
  -                    }
  -
  -                    // Need to do this in case we're not full, but block on a read
  -                    if (__readIsWaiting)
  -                    {
  -                        __queue.notify();
  -                    }
  -
  -                    __queue[__queueTail] = ch;
  -                    ++__bytesAvailable;
  -
  -                    if (++__queueTail >= __queue.length)
  -                        __queueTail = 0;
  +                    __processChar(ch);
  +                }
  +                catch (InterruptedException e)
  +                {
  +                    if (__isClosed)
  +                        break _outerLoop;
                   }
               }
           }
  @@ -537,6 +611,8 @@
               __hasReachedEOF = true;
               __queue.notify();
           }
  +
  +        __threaded = false;
       }
   }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org