asmuts      2005/06/06 07:10:35

  Modified:    src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery
                        UDPDiscoverySenderThread.java
                        UDPDiscoverySender.java
  Log:
  UDP connections were not getting closed in the UDP discovery process.
  I fixed the problem.
  The number of file handles stays constant on countless reconnects and 
broadcasts.
  Will do more long term testing.
  
  Revision  Changes    Path
  1.2       +31 -3     
jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoverySenderThread.java
  
  Index: UDPDiscoverySenderThread.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoverySenderThread.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UDPDiscoverySenderThread.java     3 Jun 2005 20:25:54 -0000       1.1
  +++ UDPDiscoverySenderThread.java     6 Jun 2005 14:10:35 -0000       1.2
  @@ -65,6 +65,7 @@
        *            host name we can be found at
        * @param myPort
        *            port we are listening on
  +     * @param cacheNames List of strings of the names of the regiond 
participating.
        */
       public UDPDiscoverySenderThread( String discoveryAddress, int 
discoveryPort, String myHostName, int myPort,
                                       ArrayList cacheNames )
  @@ -83,12 +84,13 @@
                   + discoveryPort + "] myHostName = [" + myHostName + "] and 
port = [" + myPort + "]" );
           }
   
  +        UDPDiscoverySender sender = null;
           try
           {
               // move this to the run method and determine how often to call 
it.
  -            UDPDiscoverySender sender = new UDPDiscoverySender( 
discoveryAddress, discoveryPort );
  +            sender = new UDPDiscoverySender( discoveryAddress, discoveryPort 
);
               sender.requestBroadcast();
  -
  +            
               if ( log.isDebugEnabled() )
               {
                   log.debug( "Sent a request broadcast to the group" );
  @@ -98,6 +100,20 @@
           {
               log.error( "Problem sending a Request Broadcast", e );
           }
  +        finally
  +        {
  +            try
  +            {
  +                if ( sender != null )
  +                {
  +                    sender.destroy();                                    
  +                }
  +            }
  +            catch ( Exception e )
  +            {
  +                log.error( "Problem closing Request Broadcast sender", e );
  +            }
  +        }
       }
   
       /*
  @@ -107,11 +123,12 @@
        */
       public void run()
       {
  +        UDPDiscoverySender sender = null;
           try
           {
               // create this connection each time.
               // more robust
  -            UDPDiscoverySender sender = new UDPDiscoverySender( 
discoveryAddress, discoveryPort );
  +            sender = new UDPDiscoverySender( discoveryAddress, discoveryPort 
);
   
               sender.passiveBroadcast( myHostName, myPort, cacheNames );
   
  @@ -128,5 +145,16 @@
           {
               log.error( "Problem calling the UDP Discovery Sender", e );
           }
  +        finally
  +        {
  +            try
  +            {
  +                sender.destroy();                
  +            }
  +            catch ( Exception e )
  +            {
  +                log.error( "Problem closing Passive Broadcast sender", e );
  +            }
  +        }
       }
   }
  
  
  
  1.2       +43 -0     
jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoverySender.java
  
  Index: UDPDiscoverySender.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/lateral/socket/tcp/discovery/UDPDiscoverySender.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UDPDiscoverySender.java   3 Jun 2005 20:25:54 -0000       1.1
  +++ UDPDiscoverySender.java   6 Jun 2005 14:10:35 -0000       1.2
  @@ -46,6 +46,10 @@
   
       /**
        * Constructor for the UDPDiscoverySender object
  +     * <p>
  +     * This sender can be used to send multiple messages.
  +     * <p>
  +     * When you are done sending, you should destroy the socket sender.
        * 
        * @param host
        * @param port
  @@ -74,6 +78,35 @@
       }
   
       /**
  +     * Closes the socket connection.
  +     *
  +     */
  +    public void destroy()
  +    {
  +        try 
  +        {
  +            if ( this.m_localSocket != null && 
!this.m_localSocket.isClosed() )
  +            {
  +                this.m_localSocket.close();
  +            }
  +        }
  +        catch( Exception e )
  +        {
  +            log.error( "Problem destrying sender", e );
  +        }
  +    }
  +    
  +    /**
  +     * Just being careful about closing the socket.
  +     */
  +    public void finalize()
  +    throws Throwable
  +    {
  +        super.finalize();
  +        destroy();
  +    }
  +    
  +    /**
        * Send messages.
        * 
        * @param message
  @@ -82,6 +115,16 @@
       public void send( UDPDiscoveryMessage message )
           throws IOException
       {
  +        if ( this.m_localSocket == null) 
  +        {
  +            throw new IOException( "Socket is null, cannot send message." ); 
           
  +        }
  +        
  +        if (this.m_localSocket.isClosed() )
  +        {
  +            throw new IOException( "Socket is closed, cannot send message." 
);
  +        }
  +        
           if ( log.isDebugEnabled() )
           {
               log.debug( "sending UDPDiscoveryMessage, message = " + message );
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to