Logo
Distributed Genetic Programming Framework
print print

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

Here you can find all the information about the file org.dgpf.search.api.cs.SearchClient.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-11 16:22:56
 * Original Filename: org.dgpf.search.api.cs.SearchClient.java
 * Version          : 2.1.2
 * Last modification: 2006-07-15
 *                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.Serializable;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;

import org.dgpf.search.api.SearchEngine;
import org.dgpf.search.api.SearchEngineActivity;
import org.dgpf.search.api.SearchTaskQueue;
import org.sfc.collections.Collections;
import org.sfc.events.ErrorEvent;
import org.sfc.events.EventPropagator;
import org.sfc.net.cs.IClient;

/**
 * A search client is a powerful construct that hosts many client threads
 * that are able to transmit tasks to different search servers in the
 * network.
 *
 * @param <Genotype>    The sort of genotype used to represent individuals.
 *                      This must be a serializable type.
 *
 * @author Thomas Weise
 */

public class SearchClient<Genotype extends    Serializable>
                                   extends    SearchEngineActivity<Genotype>
                                   implements IClient
  {
/**
 * The search task queue to use.
 */

          final     SearchTaskQueue<Genotype>     m_queue   ;
/**
 * The list with the client threads.
 */

          final     List<ClientThread<Genotype>>  m_clients ;

/**
 * Create a new search client.
 * @param p_queue   The search task queue to use to obtain search tasks
 *                  from.
 * @param p_se  The search engine to work for.
 */

  public  SearchClient  (final SearchTaskQueue<Genotype> p_queue,
                         final SearchEngine<Genotype>    p_se)
    {
    super(p_se);

    this.m_queue = p_queue;
    this.m_clients = Collections.create_list(-1);
    }

/**
 * Add a new server to the list of servers to use.
 * @param p_addr  The internet socket address of the server.
 */

  public synchronized final void add_server(final InetSocketAddress p_addr)
    {
    int                           l_i;
    List<ClientThread<Genotype>>  l_l;
    ClientThread<Genotype>        l_ct;
    Socket                        l_s;
    EventPropagator               l_ep;

    if((p_addr == null) || this.has_terminated()) return;

    l_l = this.m_clients;
    for(l_i = (l_l.size()-1); l_i >= 0; l_i--)
      {
      l_ct = l_l.get(l_i);
      if(l_ct.m_addr.equals(p_addr)) return;
      }

    try
      {
      l_s = new Socket();
      l_s.connect(p_addr);
      try
        {
        l_s.getOutputStream().write(CSSearchUtils.CMD_QUERY_THREADS);
        l_i = l_s.getInputStream().read();
        }
      finally
        {
        l_s.close();
        }
      }
    catch(Throwable l_t)
      {
      if((l_ep = this.get_event_propagator()) != null)
        {
        l_ep.receive(new ErrorEvent(p_addr, l_t));
        }
      }

    for(--l_i; l_i >= 0; l_i--)
      {
      l_ct = new ClientThread<Genotype>(this, p_addr);
      l_l.add(l_ct);
      if(this.is_running()) l_ct.start();
      }
    }

/**
 * Obtain the count of servers running.
 * @return The count of servers running.
 */

  public  synchronized  final int get_server_count()
    {
    return this.m_clients.size();
    }
  
/**
 * Transmit the search settings needed to configure the servers.
 * @param p_settings  The settings to be transmitted.
 */

  public  synchronized  final
                  void transmit_settings(final Serializable p_settings)
    {
    int                           l_i;
    List<ClientThread<Genotype>>  l_l;
    InetSocketAddress             l_a;
    ClientThread<Genotype>        l_c;

    l_l = this.m_clients;
    l_a = null;
    for(l_i = (l_l.size()-1); l_i >= 0; l_i--)
      {
      l_c = l_l.get(l_i);
      if(l_a != l_c.m_addr)
        {
        l_a = l_c.m_addr;
        l_c.transmit_settings(p_settings);
        }
      }
    }

/**
 * Startup the activity. This method will be called by <code>start()</code>.
 * @see #start()
 */

  @Override
  protected final   void  do_start  ()
    {
    int                           l_i;
    List<ClientThread<Genotype>>  l_l;

    super.do_start();

    l_l = this.m_clients;
    for(l_i = (l_l.size()-1); l_i >= 0; l_i--)
      {
      l_l.get(l_i).start();
      }
    }


  
/**
 * Perform a shutdown. It is guaranteed to be called only once for shutdown.
 * This method should only perform the actions needed to shutdown the
 * network of activities. The closing of the activity itself will be done
 * by <code>do_abort</code>, which will subsequently be called
 * automatically.
 * @see #abort()
 * @see #shutdown()
 */

  @Override
  protected void  do_shutdown ()
    {
    int                           l_i;
    List<ClientThread<Genotype>>  l_l;
    ClientThread<Genotype>        l_c;
    InetSocketAddress             l_is;
    

    super.do_shutdown();

    l_l  = this.m_clients;
    l_is = null;
    for(l_i = (l_l.size()-1); l_i >= 0; l_i--)
      {

      l_c = l_l.get(l_i);
      if(l_is != l_c.m_addr)
        {
        l_c.shutdown();
        l_is = l_c.m_addr;
        }
      else l_c.abort();
      }
    }
  
/**
 * 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  ()
    {
    int                           l_i;
    List<ClientThread<Genotype>>  l_l;

    super.do_abort();

    l_l  = this.m_clients;
    for(l_i = (l_l.size()-1); l_i >= 0; l_i--)
      {
      l_l.get(l_i).abort();
      }
    }


/**
 * Wait for this object.
 */

  @Override
  public  final void  wait_for  ()
    {
    int                           l_i;
    List<ClientThread<Genotype>>  l_l;
    ClientThread<Genotype>        l_ct;

    super.wait_for();

    l_l = this.m_clients;

    for(;;)
      {
      synchronized(this)
        {
        if((l_i = l_l.size()) <= 0) return;
        l_ct = l_l.remove(l_i-1);
        }
      l_ct.wait_for();
      }
//    l_l = this.m_clients;
//    for(l_i = (l_l.size()-1); l_i >= 0; l_i--)
//      {
//      l_l.get(l_i).wait_for();
//      }
    }

/**
 * The human readable name of the activity.
 * @return The human readable name of the activity.
 */

  @Override
  public  final String  toString  ()
    {
    return "Search Client of " + this.get_thread_group() + ')';
    }
  

/**
 * Obtain the search engine this thread works for.
 * @return The search engine this thread works for.
 */

  final SearchEngine<Genotype>  do_get_search_engine()
    {
    return this.get_search_engine();
    }
  }

File Information:

file name:SearchClient.java
package:org.dgpf.search.api.cs
qualified name:org.dgpf.search.api.cs.SearchClient.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/api/cs/SearchClient.java
size:7.535 KB (7716 B)
uploaded: 2015-07-22 04:11:00 GMT+0000
last update: 2006-07-15 09:37:00 GMT+0000
last access: 2017-11-23 07:31:22 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-23 07:31:22 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo