1 package org.objectweb.proactive.examples.eratosthenes; 2 3 34 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.ActionListener ; 37 38 import javax.swing.JButton ; 39 import javax.swing.JFrame ; 40 import javax.swing.JToggleButton ; 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 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 78 private static final int ACTIVEPRIMECONTAINERS_PER_NODE = 4; 79 80 81 84 public Main() { 85 } 86 87 public Main(String xmlDescriptor, boolean gui) throws ProActiveException { 88 if (xmlDescriptor.length() > 0) 90 pad = ProActive.getProactiveDescriptor(xmlDescriptor); 91 this.gui = gui; 92 } 93 94 95 public ActivePrimeContainer newActivePrimeContainer(long n, Slowable previous) { 96 try { 97 int containerSize; 98 100 containerSize = (int)Math.sqrt(n)*20; 101 if (containerSize < 100) containerSize = 100; 102 else if (containerSize > 1000) containerSize = 1000; 103 104 Node node; 106 if (containersVirtualNode != null) { 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 [] { ProActive.getStubOnThis(), outputListener, 125 new Integer (containerSize), new Long (n), previous }, node); 126 127 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 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 outputListener = (PrimeOutputListener) ProActive.newActive(ConsolePrimeOutputListener.class.getName(), 153 new Object [] {}, listenerNode); 154 155 outputListener.newPrimeNumberFound(2); 156 157 source = (NumberSource) ProActive.newActive(NumberSource.class.getName(), 159 new Object [] { }, sourceNode); 160 161 ActivePrimeContainer first = newActivePrimeContainer(3, source); 163 164 source.setFirst(first); 165 166 if (gui) new ControlFrame(this); 167 else source.pause(false); 169 } catch (Exception 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 killedRTs = new java.util.Vector (); 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 node.getProActiveRuntime().killRT(false); 192 } 193 } catch (Throwable 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 [] args) throws ProActiveException { 206 String 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 [] {xmlDescriptor, new Boolean (gui)}); 217 } 218 219 220 class ControlFrame extends JFrame implements ActionListener { 221 private JButton exitButton; 222 private JToggleButton 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 ()); 229 pauseButton = new JToggleButton ("Pause", true); 230 exitButton = new JButton ("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 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 |