You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ha...@apache.org on 2005/07/01 17:47:48 UTC

cvs commit: ws-axis/c/tests/utils/monitor/org/apache/test ConnectionNotEstablishedException.java TestClientListener.java TestClientThread.java

hawkeye     2005/07/01 08:47:48

  Modified:    c/tests/utils/monitor/org/apache/test
                        TestClientListener.java TestClientThread.java
  Added:       c/tests/utils/monitor/org/apache/test
                        ConnectionNotEstablishedException.java
  Log:
  Improved monitor so it closes more gracefully
  
  Revision  Changes    Path
  1.9       +6 -0      ws-axis/c/tests/utils/monitor/org/apache/test/TestClientListener.java
  
  Index: TestClientListener.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/c/tests/utils/monitor/org/apache/test/TestClientListener.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TestClientListener.java	6 Jun 2005 17:07:42 -0000	1.8
  +++ TestClientListener.java	1 Jul 2005 15:47:48 -0000	1.9
  @@ -116,6 +116,12 @@
                               .println("TestClientListener got a Stop monitor message");
                       stayAlive=false;
                   }
  +                catch(ConnectionNotEstablishedException connectionNotEstablishedException)
  +                {
  +                    // this is thrown when we cannot connect to the server
  +                    System.err.println( "Cannot connect to server");
  +                    stayAlive=false;
  +                }
                   catch (ConnectException connectException)
                   {
                       // OK, well for whatever reasons the socket is closed so go
  
  
  
  1.7       +73 -37    ws-axis/c/tests/utils/monitor/org/apache/test/TestClientThread.java
  
  Index: TestClientThread.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/c/tests/utils/monitor/org/apache/test/TestClientThread.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestClientThread.java	8 Jun 2005 15:52:06 -0000	1.6
  +++ TestClientThread.java	1 Jul 2005 15:47:48 -0000	1.7
  @@ -23,6 +23,10 @@
       private boolean          continueToRun                 =true;
       // the responder back to the client
       private ClientReturner   clientReturner                =null;
  +    // We need to keep hold of this so that we can tell what state it's in when
  +    // the
  +    // read fails
  +    private Socket           clientSocket;
   
       // I didn't want to make this global but it has to be for the constructor
       // pattern to work :-(
  @@ -30,7 +34,7 @@
       private static final int READ_BUFFER_SIZE              =10000;                     // 4096=4k
       private char[]           readBuffer                    =new char[READ_BUFFER_SIZE];
   
  -    // the request from the client 
  +    // the request from the client
       private BufferedReader   clientRequestStream           =null;
       // the connection to the server where we forwaard the clients request to.
       private BufferedWriter   streamToServer                =null;
  @@ -52,9 +56,10 @@
        */
       public TestClientThread(Socket clientSocket, String serviceHostName,
               int servicePort) throws StopRequestException , IOException ,
  -            ConnectException
  +            ConnectException , ConnectionNotEstablishedException
       {
           //        System.out.println( "TestClientThread(3): entry");
  +        this.clientSocket=clientSocket;
           IsStopMessage(clientSocket);
           Socket serviceSocket=createSocketToServer(serviceHostName, servicePort);
           writeToServer(readBuffer, bytesRead);
  @@ -144,51 +149,70 @@
        */
       public void run( )
       {
  -        try
  +        while (continueToRun)
           {
  -            while (continueToRun
  -                    &&(bytesRead=clientRequestStream.read(readBuffer, 0,
  -                            READ_BUFFER_SIZE))!=-1)
  -            {
  -                //                System.out.println( "About to write some bytes to the
  -                // server");
  -                writeToServer(readBuffer, bytesRead);
  -                //System.out.println("Wrote some bytes to the server: "
  -                  //      +new String(readBuffer, 0, bytesRead));
  +            try
  +            {
  +                bytesRead=clientRequestStream.read(readBuffer, 0,
  +                        READ_BUFFER_SIZE);
  +                if (bytesRead==-1)
  +                {
  +                    continueToRun=false;
  +                }
  +                else
  +                {
   
  -                TCPMonitor.getInstance( ).writeRequest(readBuffer, bytesRead);
  +                    //                System.out.println( "About to write some bytes to the
  +                    // server");
  +                    try
  +                    {
  +                        writeToServer(readBuffer, bytesRead);
  +                        TCPMonitor.getInstance( ).writeRequest(readBuffer,
  +                                bytesRead);
  +                    }
  +                    catch (Exception exception)
  +                    {
  +                        System.err
  +                                .println("TestClientThread#run(): IOException when forwarding the request to the server");
  +                        exception.printStackTrace(System.err);
  +                        continueToRun=false;
  +                    }
  +                }
  +            }
  +            catch (SocketException socketException)
  +            {
  +                continueToRun=false;
  +                if(socketException.getMessage()=="Connection reset")
  +                {
  +                    // tihs appears to happen when the client has stopped sending us data and we should close down gracefully
  +                    // but when I check the socket for it's status it tells me that all is well but for 
  +                    // the fact that the stream is not ready() but ready() returning false is not a reason to shut !
  +                    // ah well - never mind - let's close gracefully.
  +                    // no need to print this out as an exception
  +                    System.out.println( "TestClientThread#run(): Connection reset when reading from client - closing gracefully");
  +                }
  +                else
  +                {
  +                    socketException.printStackTrace(System.err);
  +                }
  +            }
  +            catch (IOException exception)
  +            {
  +                System.err
  +                        .println("TestClientThread#run(): IOException when reading clients request: "
  +                                +exception);
  +                throw new RuntimeException(
  +                        "TestClientThread#run(): IOException when reading clients request: "
  +                                +exception);
               }
  -            // so we've either got continueToRun=false or read =-1;
  -            //            System.out.println( "TestClientThread#run(): TestClientThread
  -            // bytesRead = "+bytesRead);
  -            //            System.out.println( "TestClientThread#run(): continueToRun =
  -            // "+continueToRun);
  -        }
  -        catch (SocketException socketException)
  -        {
  -            System.err
  -                    .println("TestClientThread#run(): SocketException when reading client request:");
  -            socketException.printStackTrace(System.err);
  -        }
  -        catch (IOException exception)
  -        {
  -            System.err
  -                    .println("TestClientThread#run(): IOException when reading clients request: "
  -                            +exception);
  -            throw new RuntimeException(
  -                    "TestClientThread#run(): IOException when reading clients request: "
  -                            +exception);
           }
  -
  -        // need to ensure we close down the clientFileReturner we created for
  -        // this connection
           clientReturner.continueToRun=false;
   
           //        System.out.println( "TestClientThread#run(): exit");
       }
   
       public Socket createSocketToServer(String serviceHostName, int servicePort)
  -            throws IOException
  +            throws IOException, ConnectionNotEstablishedException
       {
           Socket serviceSocket=null;
           int retry=CREATE_SOCKET_TO_SERVER_RETRY;
  @@ -203,6 +227,14 @@
                   // oh dear !
                   throw unknownHostException;
               }
  +            catch (ConnectException connectException)
  +            {
  +                System.err
  +                        .println("ConnectionException when Monitor connecting to server "
  +                                +connectException.getMessage( ));
  +                connectException.printStackTrace(System.err);
  +                throw new ConnectionNotEstablishedException(connectException);
  +            }
               catch (Exception se)
               {
                   System.err.println("Failed to open socket to service: "+se);
  @@ -223,6 +255,10 @@
                       }
                       System.err.println("Woke up ");
                   }
  +                else
  +                {
  +                    throw new ConnectionNotEstablishedException(se);
  +                }
               }
           }
           while (serviceSocket==null&&retry-->0);
  
  
  
  1.1                  ws-axis/c/tests/utils/monitor/org/apache/test/ConnectionNotEstablishedException.java
  
  Index: ConnectionNotEstablishedException.java
  ===================================================================
  /*
   * Created on 16-Jun-2005
   *
   * TODO To change the template for this generated file go to
   * Window - Preferences - Java - Code Style - Code Templates
   */
  package org.apache.test;
  
  /**
   * @author hawkeye
   *
   * TODO To change the template for this generated type comment go to
   * Window - Preferences - Java - Code Style - Code Templates
   */
  public class ConnectionNotEstablishedException extends Exception
  {
      public ConnectionNotEstablishedException(Exception cause)
      {
          super(cause);
      }
  }