Logo
Distributed Genetic Programming Framework
print print

File org.sfc.net.p2p.PendingExternalsThread.java

Here you can find all the information about the file org.sfc.net.p2p.PendingExternalsThread.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-02-22 08:58:01
 * Original Filename: org.sfc.net.p2p.PendingExternalsThread.java
 * Version          : 3.0.1
 * Last modification: 2006-06-08
 *                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.p2p;

import java.net.InetSocketAddress;

import org.sfc.parallel.SfcThread;
import org.sfc.parallel.SyncPoint;


/**
 * This thread waits for pending external connections. If the time has come
 * that an external peer might be contacted again, it pupts it into the
 * peer-to-peer-list again.
 * @see PeerToPeer
 * @author Thomas Weise
 */

final class PendingExternalsThread extends SfcThread
  {
/**
 * The owning peer-to-peer manager.
 */

  private final PeerToPeer  m_owner   ;

/**
 * A linked list of pending externals.
 */

  private       External    m_pending ;

/**
 * The internal sync-point.
 */

  private final Object      m_sync    ;

/**
 * Create a new pending external connections thread.
 * @param p_owner The owning peer node.
 */

  PendingExternalsThread  (final  PeerToPeer       p_owner)
    {
    super(p_owner.do_get_thread_group(), p_owner.do_get_events());
    this.m_owner = p_owner;
    this.m_sync  = new SyncPoint();
    }


/**
 * This method is called by <code>abort()</code> to perform some work before
 * halting the thread.
 * @see #abort()
 */

  @Override
  protected void  do_abort()
    {
    super.do_abort();
    
    synchronized(this.m_sync)
      {
      this.m_sync.notifyAll();
      }
    }
  
/**
 * Override this method to perform something when the thread dies.
 */

  @Override
  protected void  after_termination ()
    {
    super.after_termination();
    
    synchronized(this.m_sync)
      {
      this.m_sync.notifyAll();
      }
    }

/**
 * Remove a peer from the internal peer list.
 * @param p_isa   The internet address and port the peer is located at.
 * @return  <code>true</code> if and only if the peer was known and
 *          has been removed successfully.
 */

  final boolean remove_peer  (final InetSocketAddress p_isa)
    {
    External  l_e, l_oe;

    synchronized(this.m_sync)
      {
      l_oe = null;
      for(l_e = this.m_pending; l_e != null; l_e = l_e.m_next)
        {
        if(l_e.m_address.equals(p_isa))
          {
          if(l_oe != null) l_oe           = l_e.m_next;
          else             this.m_pending = l_e.m_next;
          return true;
          }
        l_oe = l_e;
        }
      }

    return false;
    }

/**
 * Enqueue a new external peer into the list of pending peers.
 * @param p_external  The external peer to be enlisted.
 */

  final void  enqueue (final External p_external)
    {
    long      l_t;
    External  l_e, l_oe;

    l_t = p_external.m_next_time;

    if(this.is_running())
      {
      synchronized(this.m_sync)
        {
        l_e = this.m_pending;

        if( (l_e == null) || (l_t < l_e.m_next_time) )
          {
          p_external.m_next = l_e;
          this.m_pending    = p_external;
          this.safe_interrupt();
          this.m_sync.notifyAll();
          return ;
          }

        for(;;)
          {
          l_oe = l_e;
          l_e  = l_e.m_next;
          if( (l_e == null) || (l_t < l_e.m_next_time) )
            {
            p_external.m_next = l_e;
            l_oe.m_next       = p_external;
            return ;
            }
          }
        }
      }
    }



/**
 * This method must be overriden to contain the thread's code. It will be
 * executed in a loop as long as it returns <code>true</code> and throws
 * <b>no</b> <code>Throwable</code>.
 * @return  <code>true</code> if this method should be executed again.
 */

  @Override
  protected final boolean do_run  ()
    {
    External  l_e;
    long      l_nt;

    synchronized(this.m_sync)
      {
      l_e = this.m_pending;
      if(l_e != null) l_nt = l_e.m_next_time;
      else
        {
        try
          {
          this.m_sync.wait();
          }
        catch(InterruptedException l_oed)
          {
          //
          }
        return true;
        }
      }


    l_nt -= System.currentTimeMillis();
    if(l_nt > 0)
      {
      return this.safe_sleep(l_nt);
      }

    synchronized(this.m_sync)
      {
      l_e            = this.m_pending;

      if(l_e != null)
        {
        this.m_pending = l_e.m_next;
        l_e.m_next     = null;
        this.m_owner.add_external(l_e);
        }
      }

    return true;
    }

/**
 * Abort all pending external connections.
 */

  final void  shutdown  ()
    {
    External  l_e;

    this.abort();

    synchronized(this.m_sync)
      {
      for(l_e = this.m_pending; l_e != null; l_e = l_e.m_next)
        {
        l_e.shutdown();
        }
      }
    }

/**
 * Obtain a string representation of this pending externals manager.
 * @return A string representation of this pending externals manager.
 */

  @Override
  public  String  toString()
    {
    return "Faulty Peer Manager of " + this.m_owner.toString();
    }
  }

File Information:

file name:PendingExternalsThread.java
package:org.sfc.net.p2p
qualified name:org.sfc.net.p2p.PendingExternalsThread.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/net/p2p/PendingExternalsThread.java
size:5.934 KB (6077 B)
uploaded: 2018-01-07 12:03:36 GMT+0000
last update: 2006-06-08 02:55:54 GMT+0000
last access: 2018-04-27 02:29:44 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 2018-01-07 12:03:34 GMT+0000 served at 2018-04-27 02:29:44 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo