Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.api.p2p.IndividualEmigrator.java

Here you can find all the information about the file org.dgpf.search.api.p2p.IndividualEmigrator.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-04-10 11:37:36
 * Original Filename: org.dgpf.search.api.p2p.IndividualEmigrator.java
 * Version          : 2.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.dgpf.search.api.p2p;

import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;

import org.dgpf.search.api.Individual;
import org.sfc.events.ErrorEvent;
import org.sfc.parallel.SfcThread;
import org.sfc.parallel.SfcThreadGroup;

/**
 * This thread performs the emigration of inidivuals.
 * @param <Genotype>  The genotype of the individuals evolved. It must be
 *                    serializable.
 *
 * @author Thomas Weise
 */

final class IndividualEmigrator<Genotype extends Serializable>
                                          extends SfcThread
  {
/**
 * The owning peer to peer node.
 */

  private final SearchP2P<Genotype> m_owner ;

/**
 * Create a new genetic emigrator thread.
 * @param p_owner   The owning peer-to-peer node.
 * @param p_threads The thread group provided.
 */

  IndividualEmigrator  (final SearchP2P<Genotype>  p_owner,
                        final SfcThreadGroup       p_threads)
    {
    super(p_threads);
    this.m_owner = p_owner;
    }


/**
 * 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  ()
    {
    Individual<Genotype>                l_pi;
    CircularIndividualBuffer<Genotype>  l_gcb;
    int                                 l_i, l_j, l_avail;
    Socket                              l_sock;
    ObjectOutputStream                  l_oos;
    InputStream                         l_is;

    l_gcb = this.m_owner.m_outgoing;

    l_pi = l_gcb.dequeue();
    if(l_pi == null) return false;
    l_i  = l_gcb.get_count();

    for(;;)
      {
      l_sock = this.m_owner.get_random_peer();      
      if(l_sock != null) break;
      if(!(this.safe_sleep(200))) return false;
      }

    try
      {
      try
        {
        l_is = l_sock.getInputStream();
        try
          {
          l_avail  = (l_is.read() & 0xff);
          l_avail |= ((l_is.read() & 0xff) << 8);
          l_avail |= ((l_is.read() & 0xff) << 16);
          l_avail |= ((l_is.read() & 0xff) << 24);
          
          if(l_avail > 0)
            {
            l_oos = new ObjectOutputStream(l_sock.getOutputStream());          
            l_j   = 0;
            try
              {
              l_oos.flush();
              l_i++;
              if(l_i > l_avail) l_i = l_avail;
              l_oos.writeInt(l_i);
              
              synchronized(this.m_owner)
                {
                l_oos.writeLong(this.m_owner.m_level);
                }
              l_oos.flush();
    
              for(; l_i > 1; l_i--)
                {
                l_pi.transmit(l_oos);
                l_oos.flush();
                l_j++;
                l_pi = l_gcb.nonblocking_dequeue();
                if(l_pi == null) return false;
                }
    
              l_pi.transmit(l_oos);
              }
            finally
              {
              this.m_owner.add_emigrants(l_j);
              l_oos.close();
              }
            }
          }
        finally
          {
          l_is.close();
          }
        }
      finally
        {
        l_sock.close();
        }
      }
    catch(Throwable l_t)
      {
      if(this.is_running() && this.m_owner.is_running() &&
         this.m_owner.m_owner.is_running())
        {
        this.get_listener().receive(new ErrorEvent(
            ((l_sock != null) ? l_sock.getRemoteSocketAddress() : this),
            l_t));
        }
      }

    return true;
    }

/**
 * Obtain the human readable representation of this thread.
 * @return  The name of this thread.
 */

  @Override
  public  final String  toString  ()
    {
    return "Emigration Thread of " + this.m_owner.toString();
    }
  }

File Information:

file name:IndividualEmigrator.java
package:org.dgpf.search.api.p2p
qualified name:org.dgpf.search.api.p2p.IndividualEmigrator.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/api/p2p/IndividualEmigrator.java
size:5.059 KB (5181 B)
uploaded: 2015-07-22 04:11:00 GMT+0000
last update: 2006-08-27 11:15:19 GMT+0000
last access: 2017-11-24 00:19:57 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 2017-11-24 00:19:57 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo