master.java

来自「java实现的遗传算法」· Java 代码 · 共 137 行

JAVA
137
字号
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licencing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.distr;

import java.io.*;
import org.jgap.util.*;

/**
 * Represents an IMaster instance. Distributes work to IWorker instances.
 * Allows to receive new tasks and send them to the workers when applicable.
 *
 * @author Klaus Meffert
 * @since 2.3
 */
public abstract class Master {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.9 $";

  /**
   * Information needed by workers
   */
  private MasterInfo m_masterinfo;

  /**
   * Dispatcher of a request
   */
  private RequestDispatcher m_dispatcher;

  /**
   * Listener to requests and answers from workers (= KKMultiServer)
   */
  private WorkerListener m_workerListener;

  /**
   * Constructor.
   * @param a_dispatcher the dispatcher to use for requests to workers
   * @param a_workerListener the listener to use for listening to worker
   * messages
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public Master(RequestDispatcher a_dispatcher, WorkerListener a_workerListener)
      throws Exception {
    m_dispatcher = a_dispatcher;
    m_workerListener = a_workerListener;
    m_masterinfo = new MasterInfo();
    m_masterinfo.IPAddress = NetworkKit.getLocalIPAddress();
    m_masterinfo.name = NetworkKit.getLocalHostName();
  }

  /**
   * Starts the master listener. Implement in specific implementations of
   * Master.
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public abstract void start()
      throws Exception;

  /**
   * Stops the master from being working.
   */
  public void stop() {
    m_workerListener.stop();
    /**@todo notify all workers to stop working???
     * No, better would be: next time master is available it can receive
     * old results from workers. So, the workers need to store them for
     * some time, until the master is able to receive the worker results.*/
  }

  /**
   * @return information about this master
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public MasterInfo getMasterInfo() {
    return m_masterinfo;
  }

  /**
   * Sends a command to a worker.
   * @param a_worker the worker to send the command to
   * @param a_command the command to send
   * @throws IOException
   */
  public void sendToWorker(IWorker a_worker, WorkerCommand a_command)
      throws IOException {
    /**@todo implement*/
    m_dispatcher.dispatch(a_worker, a_command);
  }

  /**
   *
   * @return the RequestDispatcher used
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public RequestDispatcher getDispatcher() {
    return m_dispatcher;
  }

  /**
   *
   * @return MasterInfo of this master
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public MasterInfo getMasterinfo() {
    return m_masterinfo;
  }

  /**
   * @return the WorkerListener used
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public WorkerListener getWorkerListener() {
    return m_workerListener;
  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?