Logo
Distributed Genetic Programming Framework
print print

File org.sfc.net.Reconnection.java

Here you can find all the information about the file org.sfc.net.Reconnection.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2006-01-27 15:03:39
 * Original Filename: org.sfc.net.Reconnection.java
 * Version          : 2.0.1
 * Last modification: 2006-03-07
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */

 
package org.sfc.net;

/**
 * This class holds some global constants that should be used to implement
 * mechanisms to reconnect broken network connections.
 * 
 * @param <Type>  The type of the objects representing a connection
 *                established by this object.
 *                
 * @author Thomas Weise
 */

public abstract class Reconnection<Type>
  {
/**
 * The minimum time to wait between to reconnect attempts.
 */

  private static  final long  MIN_CONNECTION_RETRY_DELAY  = (1 * 1000);
/**
 * The maximum time to wait between to reconnect attempts.
 */

  private static  final long  MAX_CONNECTION_RETRY_DELAY  = (30 * 60 * 1000);
/**
 * The maximum to allow connection retrying.
 */

  private static  final long  MAX_CONNECTION_RETRY_TIME   =
        (7 * 24 * 60 * 60 * 1000);
    
/**
 * This method returns the time to wait until the next reconnection attempt.
 * 
 * @param p_trial The number of the reconnection trial, must be between
 *                1 and MAX_CONNECTION_RETRIES.
 * @return  The new time to wait until the next reconnect attempt.
 */

  private static  final long  get_reconnect_wait_time(final int p_trial)
    {
    return Math.max(MIN_CONNECTION_RETRY_DELAY,
           Math.min(MAX_CONNECTION_RETRY_DELAY,
        Math.round(MIN_CONNECTION_RETRY_DELAY * Math.pow(1.2d, p_trial))));
    }
  

/**
 * The maximum count of re-connect tries.
 */

  private static  final int   MAX_CONNECTION_RETRIES ;
  
  static
    {
    int   l_t;
    long  l_s;
    
    l_t = 0;
    l_s = 0;
    
    while(l_s < MAX_CONNECTION_RETRY_TIME)
      {
      l_s += get_reconnect_wait_time(l_t++);
      }
    
    MAX_CONNECTION_RETRIES = l_t;
    }
  
/**
 * The internal retry count.
 */

  private int m_trials ;
  
  
/**
 * This method must be implemented by sub-classes. It should establish the
 * connection and may fail with an arbitrary error.
 * @return  The connection-representing object if successful.
 * @throws  Throwable An arbitrary error if the connection could not be
 *                    established successfully.
 */

  protected abstract  Type  do_connect () throws Throwable ;
  
/**
 * This method is called when the connection failed.
 * @param p_error         The error raised because of the connection
 *                        failure.
 * @param p_should_retry  <code>true</code> if and only if we should try to
 *                        reconnect, <code>false</code> if we have already
 *                        tried for a long time and now should abort this
 *                        useless attempt.
 * @param p_time_to_wait  The time to wait until the next connection retry
 *                        should be performed.
 */

  protected abstract  void  on_error  (final Throwable  p_error,
                                       final boolean    p_should_retry,
                                       final long       p_time_to_wait);
  
/**
 * Establish the connection.
 * @return  The connection-representing object if successful,
 *          <code>null</code> oterwise.
 */

  public  final Type  connect ()
    {
    Type      l_t;
    Throwable l_e;
    
    try
      {
      l_t = this.do_connect();
      if(l_t != null)
        {
        this.m_trials = 0;
        return l_t;
        }
      
      l_e = null;
      }
    catch(Throwable l_x)
      {
      l_e = l_x;
      }

    this.call_error(l_e);   
    return null;
    }
  
/**
 * Reset the reconnection state manually.
 */

  protected final void  reset ()
    {
    this.m_trials = 0;
    }
  
/**
 * This method is used to call the <code>on_error</code> method with the
 * correct parameters.
 * @param p_t The error caught.
 */

  protected final void  call_error(final Throwable p_t)
    {
    int       l_c;
    
    l_c           = this.m_trials;
    this.m_trials = (l_c+1);
    
    this.on_error(p_t, l_c < MAX_CONNECTION_RETRIES,
                  get_reconnect_wait_time(l_c));
    }
  }

File Information:

file name:Reconnection.java
package:org.sfc.net
qualified name:org.sfc.net.Reconnection.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/net/Reconnection.java
size:5.060 KB (5182 B)
uploaded: 2015-07-22 04:11:12 GMT+0000
last update: 2006-04-12 08:22:50 GMT+0000
last access: 2018-01-22 03:56:08 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2018-01-22 03:56:08 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo