KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > distr > grid > JGAPWorkers


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.grid;
11
12 import org.apache.log4j.*;
13 import org.homedns.dade.jcgrid.*;
14 import org.homedns.dade.jcgrid.worker.*;
15 import org.apache.commons.cli.*;
16 import org.homedns.dade.jcgrid.cmd.MainCmd;
17
18 /**
19  * A worker receives work units from a JGAPServer and sends back computed
20  * solutions to a JGAPServer.
21  *
22  * @author Klaus Meffert
23  * @since 3.01
24  */

25 public class JGAPWorkers {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.4 $";
28
29   private Class JavaDoc m_workerClass;
30
31   private Class JavaDoc m_workerFeedbackClaas;
32
33   private final static String JavaDoc className = JGAPWorkers.class.getName();
34
35   private static Logger log = Logger.getLogger(className);
36
37   public JGAPWorkers(String JavaDoc[] args)
38       throws Exception JavaDoc {
39     MainCmd.setUpLog4J("worker", true);
40     GridNodeWorkerConfig config = new GridNodeWorkerConfig();
41     Options options = new Options();
42     CommandLine cmd = MainCmd.parseCommonOptions(options, config, args);
43     if (config.getSessionName().equals("none")) {
44       config.setSessionName("MyGAWorker_session");
45     }
46     getNeededFiles(config);
47     startWork(config);
48   }
49
50   /**
51    * Get jar and other files needed for computation from server .
52    *
53    * @param a_config GridNodeWorkerConfig
54    */

55   public void getNeededFiles(GridNodeWorkerConfig a_config) {
56     /**@todo*/
57     // determine files
58

59     // request files
60

61     // get files
62

63     // class-load jar files
64
}
65
66   public JGAPWorkers(GridNodeWorkerConfig a_config)
67       throws Exception JavaDoc {
68     startWork(a_config);
69   }
70
71   protected void startWork(GridNodeWorkerConfig a_config)
72       throws Exception JavaDoc {
73     // Start all required workers.
74
// ---------------------------
75
GridWorker[] gw = new GridWorker[a_config.getWorkerCount()];
76     for (int i = 0; i < a_config.getWorkerCount(); i++) {
77       // Instantiate worker via reflection.
78
// ----------------------------------
79
gw[i] = new GridWorker();
80       gw[i].setNodeConfig( (GridNodeGenericConfig) a_config.clone());
81       ( (GridNodeGenericConfig) gw[i].getNodeConfig()).
82           setSessionName(a_config.getSessionName() + "_" + i);
83       ( (GridNodeGenericConfig) gw[i].getNodeConfig()).
84           setWorkingDir(a_config.getWorkingDir() + "_" + i);
85       Worker myWorker = new JGAPWorker();
86       // Instantiate worker feedback.
87
// ----------------------------
88
gw[i].setWorker(myWorker);
89       // GridWorkerFeedback myWorkerFeedback = (GridWorkerFeedback)
90
// a_workerFeedbackClaas.newInstance();
91
// gw[i].setWorkerFeedback(myWorkerFeedback);
92
// Start single worker.
93
// --------------------
94
gw[i].start();
95     }
96     // Wait for shutdown of workers.
97
// -----------------------------
98
for (int i = 0; i < a_config.getWorkerCount(); i++) {
99       gw[i].waitShutdown();
100     }
101   }
102
103   /**
104    * Convenience method to start a workers or multiple instances of it.
105    * For possible parameters see method
106    * parseCommonOptions in class org.homedns.dade.jcgrid.cmd.MainCmd. The most
107    * important parameters are:
108    * -n <session name without spaces>
109    * -s <server IP address>
110    * -d <working directory>
111    * -c <number of workers to run>
112    *
113    * @param args see above
114    * @throws Exception
115    *
116    * @author Klaus Meffert
117    * @since 3.2
118    */

119   public static void main(String JavaDoc[] args)
120       throws Exception JavaDoc {
121     MainCmd.setUpLog4J("worker", true);
122     GridNodeWorkerConfig config = new GridNodeWorkerConfig();
123     Options options = new Options();
124     CommandLine cmd = MainCmd.parseCommonOptions(options, config, args);
125     // Start worker(s).
126
// ----------------
127
new JGAPWorkers(config);
128   }
129 }
130
Popular Tags