axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hawk...@apache.org
Subject cvs commit: ws-axis/c/tests/utils/monitor/org/apache/test StopTCPMonitor.java TestClientListener.java TCPMonitor.java TestClientThread.java
Date Tue, 19 Apr 2005 16:29:33 GMT
hawkeye     2005/04/19 09:29:33

  Modified:    c/tests/utils/monitor/org/apache/test StopTCPMonitor.java
                        TestClientListener.java TCPMonitor.java
                        TestClientThread.java
  Log:
  Changed Monitor so it has a better closing pattern. It now waits until it has finished receiving
the
  output from the server before closing down - even when told to do so. this may
  cause problems when it *really* should stop but we'll see !
  
  Revision  Changes    Path
  1.4       +3 -3      ws-axis/c/tests/utils/monitor/org/apache/test/StopTCPMonitor.java
  
  Index: StopTCPMonitor.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/c/tests/utils/monitor/org/apache/test/StopTCPMonitor.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- StopTCPMonitor.java	23 Mar 2005 15:45:12 -0000	1.3
  +++ StopTCPMonitor.java	19 Apr 2005 16:29:33 -0000	1.4
  @@ -20,6 +20,7 @@
   import java.net.*;
   
   public class StopTCPMonitor {
  +    public static final String STOPTCPMON = "STOPTCPM";
   
   	private String hostname = null;
   	private int port = 0;
  @@ -33,10 +34,9 @@
   		Socket socket = null;
   		BufferedWriter dos = null;
   		try {
  -			String stopString = "STOPTCPM";
  -			socket = new Socket(hostname, port);
  +		socket = new Socket(hostname, port);
   			dos = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  -			dos.write(stopString);
  +			dos.write(STOPTCPMON);
   		} catch (UnknownHostException uhe) {
   			uhe.printStackTrace();
   		} catch (ConnectException ce) {
  
  
  
  1.7       +88 -50    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestClientListener.java	23 Mar 2005 15:45:12 -0000	1.6
  +++ TestClientListener.java	19 Apr 2005 16:29:33 -0000	1.7
  @@ -15,7 +15,6 @@
   
   package org.apache.test;
   
  -import java.lang.*;
   import java.io.*;
   import java.net.*;
   
  @@ -23,8 +22,8 @@
    * TestClientListener runs as a thread of the
    * 
    * @see TestSingleton class and creates a ServerSocket object on port <b>6323
  - *      <b>and from this creates a socket that accepts incoming requests. When
  - *      a request is received new threads are created of type
  + *      <b>and from this creates a socket that accepts incoming requests. When a
  + *      request is received new threads are created of type
    * @see TestClientThread which do all the communication.
    * @author Andrew Perry
    * @since 1.0
  @@ -38,7 +37,7 @@
       String                  serviceHost                   =null;
       boolean                 stayAlive                     =false;
       ServerSocket            server                        =null;
  -    Thread                  T                             =null;
  +    Thread                  thisThread                    =null;
   
       public static final int CAPTURE_REQUEST               =1;
       public static final int CAPTURE_RESPONSE              =2;
  @@ -56,14 +55,14 @@
   
       public void startListener( )
       {
  -        if (T!=null&&T.isAlive( ))
  +        if (thisThread!=null&&thisThread.isAlive( ))
               throw new IllegalStateException("ServerManager already running");
   
           try
           {
               server=new ServerSocket(listenPort);
  -            T=new Thread(this);
  -            T.start( );
  +            thisThread=new Thread(this);
  +            thisThread.start( );
           }
           catch (Exception ioe)
           {
  @@ -74,15 +73,17 @@
       public void stopListener( )
       {
           stayAlive=false;
  -        try
  +        if (thisThread.isAlive( ))
           {
  -            if (T.isAlive( ))
  +            try
               {
  -                T.join(500);
  +                thisThread.join( );
  +            }
  +            catch (InterruptedException interruptedException)
  +            {
  +                // this is fine
  +                interruptedException.printStackTrace( );
               }
  -        }
  -        catch (Exception e)
  -        {
           }
       }
   
  @@ -103,39 +104,12 @@
               TestClientThread responseReader=null;
               while (stayAlive==true)
               {
  -                //server.setSoTimeout(500);
  +                // server.setSoTimeout(500);
                   try
                   {
                       clientSocket=server.accept( );
  +                    serviceSocket = createSocketToServer();
   
  -                    // Now create the socket to the server
  -                    int retry=CREATE_SOCKET_TO_SERVER_RETRY;
  -                    do
  -                    {
  -                        try
  -                        {
  -                            serviceSocket=new Socket(serviceHost, servicePort);
  -                        }
  -                        catch (Exception se)
  -                        {
  -                            System.err
  -                                    .println("Failed to open socket to service: "
  -                                            +se);
  -                            if (retry<=0)
  -                            {
  -                                stayAlive=false;
  -                                continue;
  -                            }
  -                            else
  -                            {
  -                                // go to sleep
  -                                System.err.println("Going to sleep");
  -                                Thread.currentThread( ).sleep(2500);
  -                                System.err.println("Woke up ");
  -                            }
  -                        }
  -                    }
  -                    while (serviceSocket==null&&retry-->0);
                       if (serviceSocket==null)
                       {
                           continue;
  @@ -143,16 +117,38 @@
   
                       requestReader=new TestClientThread(clientSocket,
                               serviceSocket, CAPTURE_REQUEST);
  +                    
                       responseReader=new TestClientThread(clientSocket,
                               serviceSocket, CAPTURE_RESPONSE);
                       requestReader.start( );
  -                    responseReader.start( );
  +                    // wait for it to receive a request before starting the responsereader
  +                    int bytes =0;
  +                    while((bytes=requestReader.getBytes())==0)
  +                    {
  +                        // sleep here while waiting for them to receive their first bytes.
  +                        thisThread.sleep(100);
  +                    }
  +                    
  +                    // OK so the requestreader has some bytes; Now see whether they have
the number of
  +                    // bytes that we expect them to get for a stoptcpmon request
  +                    if(bytes==StopTCPMonitor.STOPTCPMON.length())
  +                    {
  +                        // probably means that they have got a stop request
  +                        // yield to the other threads and see if they stop
  +                        thisThread.yield();
  +                        
  +                        // now see if they are still alive
  +                        // if they've been told to stop then we should stop listening for
  +                        // new requests
  +                        stayAlive = requestReader.continueToRun;
  +                    }
  +                    else
  +                    {
  +                        responseReader.start( );
  +                    }
  +                    
                       try
                       {
  -                        if (requestReader.isAlive( ))
  -                        {
  -                            requestReader.join(10000);
  -                        }
                           // If the response reader is still running then
                           // ask it to stop and wait for it.
                           if (responseReader.isAlive( ))
  @@ -160,7 +156,8 @@
                               responseReader.cease( );
                               // Wait for upto another .5 secs for the request
                               // reader to finish
  -                            responseReader.join(2000);
  +//                            responseReader.join(2000);
  +                            responseReader.join();
                           }
                       }
                       catch (Exception me)
  @@ -184,13 +181,13 @@
                   }
                   catch (SocketTimeoutException ste)
                   {
  -                    // interrupt the accept call so the loop can end gracefully
  +                    ste.printStackTrace();
                   }
               }
           }
           catch (Exception e)
           {
  -            System.out.println("TestClientListener: "+e.getMessage( ));
  +            System.err.println("TestClientListener exception: "+e.getMessage( ));
           }
           if (server!=null)
               try
  @@ -208,5 +205,46 @@
           server=null;
           stayAlive=false;
       }
  +    
  +    public Socket createSocketToServer()
  +    {
  +        Socket serviceSocket=null;
  +        int retry=CREATE_SOCKET_TO_SERVER_RETRY;
  +        do
  +        {
  +            try
  +            {
  +                serviceSocket=new Socket(serviceHost, servicePort);
  +            }
  +            catch (Exception se)
  +            {
  +                System.err
  +                        .println("Failed to open socket to service: "
  +                                +se);
  +                if (retry<=0)
  +                {
  +                    stayAlive=false;
  +                    continue;
  +                }
  +                else
  +                {
  +                    // go to sleep
  +                    System.err.println("Going to sleep");
  +                    try
  +                    {
  +                        Thread.currentThread( ).sleep(2500);
  +                    }
  +                    catch(InterruptedException interruptedException)
  +                    {
  +                        // don't this is an issue?
  +                        System.out.println( "Got an interruptedxception sleeping on this
thread "+interruptedException);
  +                    }
  +                    System.err.println("Woke up ");
  +                }
  +            }
  +        }
  +        while (serviceSocket==null&&retry-->0);
  +        return serviceSocket;
  +    }
   }
   
  
  
  
  1.5       +6 -6      ws-axis/c/tests/utils/monitor/org/apache/test/TCPMonitor.java
  
  Index: TCPMonitor.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/c/tests/utils/monitor/org/apache/test/TCPMonitor.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TCPMonitor.java	23 Mar 2005 15:45:12 -0000	1.4
  +++ TCPMonitor.java	19 Apr 2005 16:29:33 -0000	1.5
  @@ -32,7 +32,7 @@
   	private static FileWriter requestFileWriter;
   	private static FileWriter responseFileWriter;
   	private static boolean responseFileWriterOpen = false;
  -	private static TestClientListener T = null;
  +	private static TestClientListener testClientListener = null;
   
   	/**
   	 * Creates a new TCPMonitor listening on the given port for incoming requests (this is
always on localhost of course!)
  @@ -57,8 +57,8 @@
   		/*
   		 * Create a thread which listens for incoming requests
   		 */
  -		T = new TestClientListener(listenerPort, serviceHost, servicePort);
  -		T.startListener();
  +		testClientListener = new TestClientListener(listenerPort, serviceHost, servicePort);
  +		testClientListener.startListener();
   	}
   
   	public static TCPMonitor getInstance() throws Exception {
  @@ -81,15 +81,15 @@
   	}
   
   	public static void stop() {
  -		T.stopListener();
  +		testClientListener.stopListener();
   		try {
   			requestFileWriter.close();
   			responseFileWriter.close();
   		} catch (Exception e) {
  -			;
  +		    e.printStackTrace();
  +			
   		}
   		singleton=null;
  -		System.exit(0);
   	}
   
   	public void writeRequest(String inputLine) {
  
  
  
  1.4       +156 -88   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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestClientThread.java	23 Mar 2005 15:45:12 -0000	1.3
  +++ TestClientThread.java	19 Apr 2005 16:29:33 -0000	1.4
  @@ -13,105 +13,173 @@
   // See the License for the specific language governing permissions and
   // limitations under the License.
   
  -
   package org.apache.test;
   
   import java.io.*;
   import java.net.*;
   
   /**
  - * TestClientThread is a child thread of TestClientListener and 
  - * handles all communication between the original requestor and 
  - * the TCPMonitor class.
  - *
  + * TestClientThread is a child thread of TestClientListener and handles all
  + * communication between the original requestor and the TCPMonitor class.
  + * 
    * @author Andrew Perry
    * @since 1.0
    * @see TestClientListener
    */
   
  -public class TestClientThread extends Thread {
  +public class TestClientThread extends Thread
  +{
   
  -	private Socket clientSocket = null;
  -	private Socket serviceSocket = null;
  -	private int mode = 0;
  -	private int clientPort = 0;
  -	boolean continueToRun = true;
  -
  -	public static final int CAPTURE_REQUEST = 1;
  -	public static final int CAPTURE_RESPONSE = 2;
  -
  -	/**
  -	 * Class constructor with the client socket used to communitate
  -	 * with the client.
  -	 * @param socket	reference to the socket connected to the client
  -	 */ 
  -	public TestClientThread(Socket clientSocket, Socket serviceSocket, int mode) {
  -		this.clientSocket = clientSocket;
  -		this.serviceSocket = serviceSocket;
  -		this.mode = mode;
  -	}
  -
  -	public void cease() {
  -		continueToRun = false;
  -	}
  -
  -	/**
  -	 * Reads the request from the client and if of a valid format will 
  -	 * extract the test ID and required data and call the
  -	 * TestSingleton class to set or get the information.
  -	 * It is assumed that all requests are UTF Strings.
  -	 * <p>
  -	 * If the incoming request does not contain a test ID, or is not of a
  -	 * recognised format then the socket will be closed and this object will
  -	 * finish.
  -	 * </p>
  -	 */
  -	public void run() {
  -		BufferedWriter dos = null;
  -		BufferedReader dis = null;
  -		String strMode = "unknown";
  -		
  -		try {
  -			char[] buffer = new char[4096]; // 4K buffer
  -
  -			if(mode == CAPTURE_REQUEST) {
  -				strMode = "REQUEST - ";
  -				dos = new BufferedWriter(new OutputStreamWriter(serviceSocket.getOutputStream()));
  -				dis = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  -			} else if(mode == CAPTURE_RESPONSE) {
  -				strMode = "RESPONSE - ";
  -				dos = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
  -				dis = new BufferedReader(new InputStreamReader(serviceSocket.getInputStream()));
  -			}
  -			int readReturn = 0;
  -			while( (readReturn = dis.read(buffer, 0, 4096)) != -1 ){
  -				String inputLine = new String(buffer, 0 , readReturn);
  -
  -				try {
  -					if(inputLine.indexOf("STOPTCPM") != -1) {
  -						TCPMonitor.getInstance().stop();
  -						continueToRun = false;
  -						break;
  -					}
  -					if(mode == CAPTURE_REQUEST) {
  -						TCPMonitor.getInstance().writeRequest(inputLine);
  -					} else if(mode == CAPTURE_RESPONSE) {
  -						TCPMonitor.getInstance().writeResponse(inputLine);
  -					}
  -				} catch (Exception exe) {
  -				}
  -				dos.write(inputLine);
  -				dos.flush();
  -			}
  -
  -		} catch (EOFException eof) {
  -		} catch (IOException e) {
  -		} finally {
  -			try {
  -				dis.close();
  -				dos.close();
  -			} catch (Exception e) { }
  -		}
  -	}
  +    private Socket          clientSocket     =null;
  +    private Socket          serviceSocket    =null;
  +    private int             mode             =0;
  +    private int             clientPort       =0;
  +    boolean         continueToRun    =true;
  +    private int             totalBytesRead   =0;
  +
  +    public static final int CAPTURE_REQUEST  =1;
  +    public static final int CAPTURE_RESPONSE =2;
  +
  +    /**
  +     * Class constructor with the client socket used to communitate with the
  +     * client.
  +     * 
  +     * @param socket reference to the socket connected to the client
  +     */
  +    public TestClientThread(Socket clientSocket, Socket serviceSocket, int mode)
  +    {
  +        this.clientSocket=clientSocket;
  +        this.serviceSocket=serviceSocket;
  +        this.mode=mode;
  +    }
  +
  +    public void cease( )
  +    {
  +        // we only cease if we have are capturing the request from the client
  +        // if we are capturing the request from the server, and we have got some already
  +        // then we must continue to capture the request and end nicely
  +        if(mode == CAPTURE_REQUEST)
  +        {
  +            continueToRun=false;
  +//            System.out.println( "been told to cease");
  +        }
  +        else
  +        {
  +//            System.out.println( "been told to cease but ignoring because I haven't finished");
  +        }
  +    }
  +
  +    /**
  +     * Reads the request from the client and if of a valid format will extract
  +     * the test ID and required data and call the TestSingleton class to set or
  +     * get the information. It is assumed that all requests are UTF Strings.
  +     * <p>
  +     * If the incoming request does not contain a test ID, or is not of a
  +     * recognised format then the socket will be closed and this object will
  +     * finish.
  +     * </p>
  +     */
  +    public void run( )
  +    {
  +        BufferedWriter dos=null;
  +        BufferedReader dis=null;
  +        String strMode="unknown";
  +        int readReturn=0;
  +
  +        try
  +        {
  +            char[] buffer=new char[4096]; // 4K buffer
  +
  +            if (mode==CAPTURE_REQUEST)
  +            {
  +                strMode="REQUEST - ";
  +                dos=new BufferedWriter(new OutputStreamWriter(serviceSocket
  +                        .getOutputStream( )));
  +                dis=new BufferedReader(new InputStreamReader(clientSocket
  +                        .getInputStream( )));
  +            }
  +            else
  +                if (mode==CAPTURE_RESPONSE)
  +                {
  +                    strMode="RESPONSE - ";
  +                    dos=new BufferedWriter(new OutputStreamWriter(clientSocket
  +                            .getOutputStream( )));
  +                    dis=new BufferedReader(new InputStreamReader(serviceSocket
  +                            .getInputStream( )));
  +                }
  +            while (continueToRun && (readReturn=dis.read(buffer, 0, 4096))!=-1)
  +            {
  +                totalBytesRead+=readReturn;
  +                String inputLine=new String(buffer, 0, readReturn);
  +                try
  +                {
  +                    if (inputLine.startsWith(StopTCPMonitor.STOPTCPMON))
  +                    {
  +                        continueToRun=false;
  +                        TCPMonitor.getInstance( ).stop( );
  +                    }
  +                    else
  +                    {
  +                        if (mode==CAPTURE_REQUEST)
  +                        {
  +                            TCPMonitor.getInstance( ).writeRequest(inputLine);
  +                        }
  +                        else
  +                        {
  +                            if (mode==CAPTURE_RESPONSE)
  +                        {
  +                                TCPMonitor.getInstance( ).writeResponse(inputLine);
  +                        	}
  +                        }
  +                    }
  +                }
  +                catch (Exception exception)
  +                {
  +                    exception.printStackTrace();
  +                }
  +                if(continueToRun)
  +                {
  +                    dos.write(inputLine);
  +                    dos.flush( );
  +                }
  +            }
  +        }
  +        catch (EOFException eof)
  +        {
  +            eof.printStackTrace( );
  +        }
  +        catch (IOException e)
  +        {
  +            // ignore these becuase we haven't quite managed to make this system as nice

  +            // as we would like so we get these (when it's working correctly) when
  +            // the readers and writers have been closed by the TCPMonitor instance
  +            // this is fine because it's when the system is shuuting down and we don't
need them
  +        }
  +        finally
  +        {
  +            try
  +            {
  +                dis.close( );
  +                dos.close( );
  +            }
  +            catch (Exception e)
  +            {
  +                e.printStackTrace( );
  +            }
  +        }
  +    }
  +
  +    /**
  +     * This method tells the starting thread whether this objec has started to
  +     * read in bytes yet. If it has then the requestor of this method can tell
  +     * how many bytes have been received and thus see whether the bytes are a
  +     * message to stop or not
  +     * 
  +     * @return the number of bytes received by this reader
  +     */
  +    public int getBytes( )
  +    {
  +        return totalBytesRead;
  +    }
   }
   
  
  
  

Mime
View raw message