Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.search.api.cs.SearchServer.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:02:40
 * Original Filename: org.dgpf.search.api.cs.SearchServer.java
 * Version          : 2.1.0
 * Last modification: 2006-05-12
 *                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.InputStream;
import java.io.Serializable;
import java.net.Socket;

import org.dgpf.search.api.SearchDefinition;
import org.dgpf.search.api.SearchThreadGroup;
import org.dgpf.search.api.cs.events.SearchServerAdaptionEvent;
import org.sfc.events.EventPropagator;
import org.sfc.net.cs.Server;
import org.sfc.utils.Typesafe;

/**
 * The search server provides simple means to execute search tasks remotely.
 *
 * @param <Genotype>    The sort of genotype used to represent individuals.
 *                      This must be a serializable type.
 *                      
 * @author Thomas Weise
 */

public final  class SearchServer<Genotype extends Serializable>
                                          extends Server         
  {
/**
 * The search task threads used.
 */

  private final ServerSocketThread<Genotype>[]  m_threads ;
  
/**
 * The current count of processed tasks.
 */

  private long  m_current_tasks ;
/**
 * The count of tasks totally processed.
 */

  private long  m_task_count   ;
/**
 * The start time of the server.
 */

  private long  m_start_time    ;
/**
 * The time of the last adaption.
 */

  private long  m_last_adaption ;
/**
 * The total count of adaptations performed.
 */

  private long  m_adaptation_count  ;
  
/**
 * Create a new server.   
 * @param p_definition  The search definition used for this search server.
 *                      It defines what should be searched, how the search
 *                      should proceed and how the individuals are
 *                      evaluated.
 * @param p_port        The port the server listenes at.
 */

  public SearchServer  (final  SearchDefinition<Genotype>  p_definition,
                               int                         p_port)
    {
    super(new SearchThreadGroup("Search Server"), null, p_port);
    
    ServerSocketThread[]  l_st;
    int                   l_i;
    SearchThreadGroup     l_stg;
    EventPropagator       l_ep;
    
    l_stg = ((SearchThreadGroup)(this.get_thread_group()));
    l_ep  = this.get_event_propagator();
    
    l_i = Runtime.getRuntime().availableProcessors();
    l_st = new ServerSocketThread[l_i];
    
    for(--l_i; l_i >= 0; l_i--)
      {
      l_st[l_i] = new ServerSocketThread<Genotype>(this, l_stg, l_ep,
                                                   p_definition);
      }
    
    this.m_threads = Typesafe.cast(l_st);
    }
  
/**
 * Update the search server's settings.
 * @param p_data  The settings data structure.
 */

  synchronized  final void  settings  (final Object p_data)
    {
    ServerSocketThread<Genotype>[]    l_st;
    int                               l_i;
    long                              l_t;
    
    l_st = this.m_threads;
    for(l_i = (l_st.length-1); l_i >= 0; l_i--)
      {
      l_st[l_i].assign(p_data);
      }
    
    this.m_task_count += this.m_current_tasks;
    
    
    l_t = System.currentTimeMillis();
    this.get_event_propagator().receive(
        new SearchServerAdaptionEvent(
            thisthis.m_task_count, this.m_current_tasks,
            this.m_start_time, l_t - this.m_start_time,
            l_t - this.m_last_adaption,
            ++this.m_adaptation_count, p_data));
    this.m_current_tasks  = 0;
    this.m_last_adaption  = l_t;    
    }
  
/**
 * Called whenever a task has been done.
 */

  synchronized  final void  task_done()
    {
    this.m_current_tasks++;
    }
  
/**
 * This method is called by the server thread whenever it receives an
 * incomming connection.
 * @param p_socket  The socket to process.
 * @return  <code>false</code> if the socket should immediately be closed,
 *          <code>true</code> if it should stay open.
 * @throws  Throwable When something goes wrong.
 */

  @Override
  protected boolean process_socket (final Socket p_socket) throws Throwable
    {
    InputStream           l_is;
    ServerSocketThread[]  l_sso;
    int                   l_i, l_j, l_c;
    
    l_is = p_socket.getInputStream();
    
    switch(l_c = l_is.read())
      {
      case CSSearchUtils.CMD_SHUTDOWN:
        {
        this.shutdown();
        return false;
        }
      
      case CSSearchUtils.CMD_QUERY_THREADS: 
        {
        l_sso = this.m_threads;
        l_j   = 0;
        for(l_i = (l_sso.length-1); l_i >= 0; l_i--)
          {
          if(l_sso[l_i].m_socket == null) l_j++;
          }        
        
        p_socket.getOutputStream().write(l_j);
        return false;
        }
        
      case CSSearchUtils.CMD_SETTINGS: 
      case CSSearchUtils.CMD_TASK:
        {
        l_sso = this.m_threads;
        for(l_i = (l_sso.length-1); l_i >= 0; l_i--)
          {
          if(l_sso[l_i].m_socket == null)
            {
            if(l_sso[l_i].process(p_socket, l_c)) return true;
            }
          }
        }
      }
    
    return false;
    }
  
/**
 * Override this method to provide some additional behavior.
 * @see #start()
 */

  @Override
  protected final void  do_start()
    {
    ServerSocketThread<Genotype>[]    l_st;
    int                               l_i;
    
    this.m_last_adaption = this.m_start_time = System.currentTimeMillis();
    
    super.do_start();
    
    l_st = this.m_threads;
    for(l_i = (l_st.length-1); l_i >= 0; l_i--)
      {
      l_st[l_i].start();
      }
    }
  
  

/**
 * This method aborts the activity. It is guaranteed to be called only once
 * for aborting and once for shutdown.
 * @see #abort()
 * @see #shutdown()
 */

  @Override
  protected void  do_abort  ()
    {
    ServerSocketThread<Genotype>[]    l_st;
    int                               l_i;
    
    super.do_abort();    
    
    l_st = this.m_threads;
    for(l_i = (l_st.length-1); l_i >= 0; l_i--)
      {
      l_st[l_i].abort();
      }
    }

/**
 * Wait until this server node has aborted fully.
 */

  @Override
  public  final void  wait_for  ()
    {
    ServerSocketThread<Genotype>[]    l_st;
    int                               l_i;
    
    super.wait_for();
    
    l_st = this.m_threads;
    for(l_i = (l_st.length-1); l_i >= 0; l_i--)
      {
      l_st[l_i].wait_for();
      }
    }  
  
/**
 * Obtain a human readable name of this search server.
 * @return A human readable name of this search server.
 */

  @Override
  public  String  toString()
    {
    return ("Search " + super.toString());
    }
  }

File Information:

file name:SearchServer.java
package:org.dgpf.search.api.cs
qualified name:org.dgpf.search.api.cs.SearchServer.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/api/cs/SearchServer.java
size:7.564 KB (7746 B)
uploaded: 2015-07-22 04:11:00 GMT+0000
last update: 2006-05-12 12:45:54 GMT+0000
last access: 2017-11-20 18:59:58 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-20 18:59:58 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo