Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.api.cs.ServerSocketThread.java

Here you can find all the information about the file org.dgpf.search.api.cs.ServerSocketThread.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-12 06:35:09
 * Original Filename: org.dgpf.search.api.cs.ServerSocketThread.java
 * Version          : 2.1.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.cs;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.SocketAddress;

import org.dgpf.search.api.SearchContext;
import org.dgpf.search.api.SearchDefinition;
import org.dgpf.search.api.SearchTask;
import org.sfc.events.ErrorEvent;
import org.sfc.events.IEventListener;
import org.sfc.parallel.SfcThread;
import org.sfc.parallel.SfcThreadGroup;
import org.sfc.utils.Typesafe;

/**
 * The server socket thread processes exactly one server socket.
 *
 * @param <Genotype>    The sort of genotype used to represent individuals.
 *                      This must be a serializable type.
 *                      
 * @author Thomas Weise
 */

final class ServerSocketThread<Genotype extends Serializable> 
                                        extends SfcThread
  {
/**
 * The socket currently processed.
 */

          volatile  Socket                  m_socket  ;
/**
 * The last command.
 */

  private volatile  int                     m_cmd     ;
/**
 * The owning search server.
 */

  private final     SearchServer<Genotype>  m_owner  ;
/**
 * The search context.
 */

  private final     SearchContext<Genotype> m_context ;
  
/**
 * Create a new server socket thread.
 * @param p_owner  The owning search server.
 * @param p_group The sfc-thread group owning this thread. This can be
 *                <code>null</code>.
 * @param p_events  The event listener to be used internally.
 * @param p_definition  The search definition governing this thread.
 */

  ServerSocketThread  (final SearchServer<Genotype>     p_owner,
                       final SfcThreadGroup             p_group,
                       final IEventListener             p_events,
                       final SearchDefinition<Genotype> p_definition)
    {
    super(p_group, p_events);
    this.m_owner   = p_owner;
    this.m_context = p_definition.create_context(p_events);
    }
  
/**
 * Process the given socket.
 * @param p_socket  The socket to process.
 * @param p_cmd     The command of the socket.
 * @return  <code>true</code> if the thread was able to begin to process
 *          the socket. 
 */

  final synchronized  boolean process (final Socket p_socket,
                                       final int    p_cmd)
    {        
    if(this.m_socket != null) return false;
    
    this.m_socket = p_socket;
    this.m_cmd    = p_cmd;
    this.notifyAll();
        
    return true;
    }
  
/**
 * Assign the internally used context to the specified data definition.
 * @param p_data  The search definition to assign to.
 */

  final void  assign  (final Object p_data)
    {
    synchronized(this.m_context)
      {
      this.m_context.assign(p_data);
      }
    }
  
/**
 * 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  ()
    {
    ObjectInputStream     l_ois;
    ObjectOutputStream    l_oos;
    SocketAddress         l_sa;
    Socket                l_s;
    int                   l_cmd;
    SearchTask<Genotype>  l_st;
    
    synchronized(this)
      {
      if(this.m_socket == null)
        {
        try
          {
          this.wait();
          }
        catch(Throwable l_t)
          {
          return false;
          }
        return true;
        }
      }
    
    l_s   = this.m_socket;
    l_sa  = l_s.getRemoteSocketAddress();
    l_cmd = this.m_cmd;
    
    l_oos = null;
    l_ois = null;
    
    try
      {
      try
        {
        l_ois = new ObjectInputStream(l_s.getInputStream());
        l_oos = new ObjectOutputStream(l_s.getOutputStream());
        l_oos.flush();
        
        do
          {          
          
          
          switch(l_cmd)
            { 
            
            case CSSearchUtils.CMD_SETTINGS:
                {
                this.m_owner.settings(l_ois.readUnshared());
                break;
                }
                
            case CSSearchUtils.CMD_TASK:
                {
                
                
                l_st = Typesafe.cast(l_ois.readUnshared());
                synchronized(this.m_context)
                  {
                  l_st.execute(this.m_context);
                  }
                SearchTask.transmit_result(l_oos, l_st);
                l_oos.flush();
                this.m_owner.task_done();
                break;
                }
                
            case CSSearchUtils.CMD_SHUTDOWN:
                {
                this.m_owner.shutdown();
                return false;
                }
            }
          
          } while( ((l_cmd = l_ois.read()) >= 0) && this.is_running() );
        
        }
      finally
        {
        this.m_socket = null;
        l_s.close();
        }
      }
    catch(Throwable l_t)
      {
      this.get_listener().receive(new ErrorEvent(l_sa, l_t));
      }
    
    return true;
    }
  
/**
 * This method is called by <code>abort()</code> to perform some work before
 * halting the thread.
 * @see #abort()
 */

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

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

/**
 * Obtain the name of the thread.
 * @return  A human readable name of the sfc thread.
 */

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

File Information:

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