KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > eratosthenes > Main


1 package org.objectweb.proactive.examples.eratosthenes;
2
3 /*
4 * ################################################################
5 *
6 * ProActive: The Java(TM) library for Parallel, Distributed,
7 * Concurrent computing with Security and Mobility
8 *
9 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
10 * Contact: proactive-support@inria.fr
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 * USA
26 *
27 * Initial developer(s): The ProActive Team
28 * http://www.inria.fr/oasis/ProActive/contacts.html
29 * Contributor(s):
30 *
31 * ################################################################
32 *
33 */

34
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.ActionListener JavaDoc;
37
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JFrame JavaDoc;
40 import javax.swing.JToggleButton JavaDoc;
41
42 import org.apache.log4j.Logger;
43 import org.objectweb.proactive.Body;
44 import org.objectweb.proactive.InitActive;
45 import org.objectweb.proactive.ProActive;
46 import org.objectweb.proactive.core.ProActiveException;
47 import org.objectweb.proactive.core.config.ProActiveConfiguration;
48 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
49 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
50 import org.objectweb.proactive.core.node.Node;
51 import org.objectweb.proactive.core.node.NodeFactory;
52
53 /**
54  * @author Jonathan Streit
55  * Main program for the Eratosthenes example. This class starts
56  * an output listener, the first ActivePrimeContainer and a number source.
57  * It also serves as creator of new ActivePrimeContainers.<br>
58  * An XML descriptor file can be passed as first parameter, in this case,
59  * the active objects are created in the nodes described by the virtual nodes
60  * Containers, NumberSource and OutputListener. <br>
61  * Main is not migratable due to the VirtualNode object.<br>
62  * A control window allows to terminate the application and to pause
63  * temporarily the NumberSource.
64  * */

65 public class Main implements ActivePrimeContainerCreator, InitActive {
66     
67     static Logger logger = Logger.getLogger(Main.class.getName());
68   private PrimeOutputListener outputListener;
69   private NumberSource source;
70   private VirtualNode containersVirtualNode;
71   private Node listenerNode;
72   private Node sourceNode;
73   private ProActiveDescriptor pad;
74   private boolean gui;
75   private Node lastNode;
76   private int nodeCount;
77   /** Place four ActivePrimeContainers in a Node before using the next for the distributed version. */
78   private static final int ACTIVEPRIMECONTAINERS_PER_NODE = 4;
79
80
81   /**
82    * Constructor for Main.
83    */

84   public Main() {
85   }
86   
87   public Main(String JavaDoc xmlDescriptor, boolean gui) throws ProActiveException {
88     // read XML Descriptor
89
if (xmlDescriptor.length() > 0)
90       pad = ProActive.getProactiveDescriptor(xmlDescriptor);
91     this.gui = gui;
92   }
93
94   /** Creates a new ActivePrimeContainer starting with number n */
95   public ActivePrimeContainer newActivePrimeContainer(long n, Slowable previous) {
96     try {
97         int containerSize;
98         /* Create the new container with size = SQRT(n) * 20,
99          * but at least 100 and at most 1000 */

100         containerSize = (int)Math.sqrt(n)*20;
101         if (containerSize < 100) containerSize = 100;
102         else if (containerSize > 1000) containerSize = 1000;
103
104         // find correct node or use default node
105
Node node;
106         if (containersVirtualNode != null) { // alternate between nodes for creating containers
107
if (lastNode == null) {
108               lastNode = containersVirtualNode.getNode();
109               node = sourceNode;
110               nodeCount = 0;
111             } else if (nodeCount < ACTIVEPRIMECONTAINERS_PER_NODE) {
112               node = lastNode;
113               nodeCount ++;
114             } else {
115               lastNode = node = containersVirtualNode.getNode();
116               nodeCount = 1;
117             }
118         }
119         else node = NodeFactory.getDefaultNode();
120         
121         logger.info(" Creating container with size "+containerSize+" starting with number "+n);
122         ActivePrimeContainer result =
123            (ActivePrimeContainer) ProActive.newActive(ActivePrimeContainer.class.getName(),
124             new Object JavaDoc [] { ProActive.getStubOnThis(), outputListener,
125                 new Integer JavaDoc(containerSize), new Long JavaDoc(n), previous }, node);
126                 
127         // Workaround for a little bug in ProActive (Exception in receiveRequest)
128
// may be removed as the bug is fixed
129
// This call makes us wait while the newly created object is not yet in his runActivity() method
130
long v = result.getValue();
131         
132         return result;
133     } catch (ProActiveException e) {
134         e.printStackTrace();
135         return null;
136     }
137   }
138
139   public void initActivity(Body b) {
140     try {
141       if (pad != null) {
142         // create nodes
143
pad.activateMappings();
144         containersVirtualNode = pad.getVirtualNode("Containers");
145         listenerNode = pad.getVirtualNode("OutputListener").getNode();
146         sourceNode = pad.getVirtualNode("NumberSource").getNode();
147       } else {
148         listenerNode = sourceNode = NodeFactory.getDefaultNode();
149       }
150       
151       // create output listener
152
outputListener = (PrimeOutputListener) ProActive.newActive(ConsolePrimeOutputListener.class.getName(),
153             new Object JavaDoc[] {}, listenerNode);
154     
155       outputListener.newPrimeNumberFound(2);
156     
157         // create number source
158
source = (NumberSource) ProActive.newActive(NumberSource.class.getName(),
159             new Object JavaDoc[] { }, sourceNode);
160
161         // create first container
162
ActivePrimeContainer first = newActivePrimeContainer(3, source);
163       
164       source.setFirst(first);
165       
166       if (gui) new ControlFrame(this);
167       else source.pause(false); // start immediately if no gui
168

169     } catch (Exception JavaDoc ex) {
170         ex.printStackTrace();
171     }
172   }
173
174   public void exit() {
175         if (containersVirtualNode != null) {
176             logger.info("Killing nodes...");
177             logger.info("This may print out some exception messages, but that's OK.");
178             java.util.Vector JavaDoc killedRTs = new java.util.Vector JavaDoc();
179             for (int i = -2; i < containersVirtualNode.getNodeCount(); i ++) {
180                 try {
181                   Node node;
182                   if (i == -2) node = sourceNode;
183                   else if (i == -1) node = listenerNode;
184                   else node = containersVirtualNode.getNode(i);
185                   if (!NodeFactory.isNodeLocal(node) &&
186                        !killedRTs.contains(node.getProActiveRuntime().getURL())) {
187                     killedRTs.add(node.getProActiveRuntime().getURL());
188                     
189                     // this method will catch and print out an exception,
190
// there is nothing we can do about it
191
node.getProActiveRuntime().killRT(false);
192                   }
193                 } catch (Throwable JavaDoc ex) {
194                 }
195             }
196             logger.info("Killed nodes.");
197         }
198         System.exit(0);
199   }
200
201   public NumberSource getSource() {
202     return source;
203   }
204
205   public static void main(String JavaDoc[] args) throws ProActiveException {
206     String JavaDoc xmlDescriptor = "";
207     boolean gui = true;
208     if (args.length > 0) {
209       if (args[0].equalsIgnoreCase("-nogui")) {
210         gui = false;
211         if (args.length > 1) xmlDescriptor = args[1];
212       } else xmlDescriptor = args[0];
213     }
214     ProActiveConfiguration.load();
215     Main main = (Main)ProActive.newActive(Main.class.getName(),
216       new Object JavaDoc[] {xmlDescriptor, new Boolean JavaDoc(gui)});
217   }
218
219   /** class for control window. */
220   class ControlFrame extends JFrame JavaDoc implements ActionListener JavaDoc {
221     private JButton JavaDoc exitButton;
222     private JToggleButton JavaDoc pauseButton;
223     private Main main;
224     ControlFrame(Main m) {
225       super("Eratosthenes control window");
226       main = m;
227       setSize(300, 80);
228       getContentPane().setLayout(new java.awt.FlowLayout JavaDoc());
229       pauseButton = new JToggleButton JavaDoc("Pause", true);
230       exitButton = new JButton JavaDoc("Exit");
231       pauseButton.addActionListener(this);
232       exitButton.addActionListener(this);
233       getContentPane().add(pauseButton);
234       getContentPane().add(exitButton);
235       show();
236     }
237     public void actionPerformed(ActionEvent JavaDoc e) {
238         if (e.getSource() == exitButton) {
239             main.exit();
240         } else if (e.getSource() == pauseButton) {
241             main.getSource().pause(pauseButton.isSelected());
242         }
243     }
244   }
245   
246 }
247
Popular Tags