KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > distr > Master


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.distr;
11
12 import java.io.*;
13 import org.jgap.util.*;
14
15 /**
16  * Represents an IMaster instance. Distributes work to IWorker instances.
17  * Allows to receive new tasks and send them to the workers when applicable.
18  *
19  * @author Klaus Meffert
20  * @since 2.3
21  */

22 public abstract class Master {
23   /** String containing the CVS revision. Read out via reflection!*/
24   private final static String JavaDoc CVS_REVISION = "$Revision: 1.11 $";
25
26   /**
27    * Information needed by workers
28    */

29   private MasterInfo m_masterinfo;
30
31   /**
32    * Dispatcher of a request
33    */

34   private RequestDispatcher m_dispatcher;
35
36   /**
37    * Listener to requests and answers from workers (= KKMultiServer)
38    */

39   private WorkerListener m_workerListener;
40
41   /**
42    * Constructor.
43    * @param a_dispatcher the dispatcher to use for requests to workers
44    * @param a_workerListener the listener to use for listening to worker
45    * messages
46    * @throws Exception
47    *
48    * @author Klaus Meffert
49    * @since 2.4
50    */

51   public Master(final RequestDispatcher a_dispatcher,
52                 final WorkerListener a_workerListener)
53       throws Exception JavaDoc {
54     m_dispatcher = a_dispatcher;
55     m_workerListener = a_workerListener;
56     m_masterinfo = new MasterInfo();
57     m_masterinfo.m_IPAddress = NetworkKit.getLocalIPAddress();
58     m_masterinfo.m_name = NetworkKit.getLocalHostName();
59   }
60
61   /**
62    * Starts the master listener. Implement in specific implementations of
63    * Master.
64    * @throws Exception
65    *
66    * @author Klaus Meffert
67    * @since 2.4
68    */

69   public abstract void start()
70       throws Exception JavaDoc;
71
72   /**
73    * Stops the master from being working.
74    */

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

81   }
82
83   /**
84    * @return information about this master
85    *
86    * @author Klaus Meffert
87    * @since 2.4
88    */

89   public MasterInfo getMasterInfo() {
90     return m_masterinfo;
91   }
92
93   /**
94    * Sends a command to a worker.
95    * @param a_worker the worker to send the command to
96    * @param a_command the command to send
97    * @throws IOException
98    */

99   public void sendToWorker(final IWorker a_worker,
100                            final WorkerCommand a_command)
101       throws IOException {
102     /**@todo implement*/
103     m_dispatcher.dispatch(a_worker, a_command);
104   }
105
106   /**
107    *
108    * @return the RequestDispatcher used
109    *
110    * @author Klaus Meffert
111    * @since 2.4
112    */

113   public RequestDispatcher getDispatcher() {
114     return m_dispatcher;
115   }
116
117   /**
118    * @return the WorkerListener used
119    *
120    * @author Klaus Meffert
121    * @since 2.4
122    */

123   public WorkerListener getWorkerListener() {
124     return m_workerListener;
125   }
126
127 }
128
Popular Tags