/* LindaProgram.java: This is an example of a LindaProgram for a very simple master/worker parallel program. It simply outs 3 tasks (integer values) for the evals (the evals compute the squares of the integers). It then ins the results from the evals, adds them all together and prints the result. Although it is simple, it clearly demonstrates the basic structure of master/worker parallel programs in Java-Linda. */ package EDU.Yale.CS.TupleSpace; import java.io.*; import java.net.*; import LennyT.*; public class LindaProgram { protected static String[] nodes; protected static EvalServer myEvalServer; private static void ReadNodesFile(String fn) { try { int nl = NumLines(fn); nodes = new String[nl]; RandomAccessFile file = new RandomAccessFile(fn,"r"); long filePointer = 0; long length = file.length(); long line_ct = 0; while (filePointer < length) { String s = file.readLine(); filePointer = file.getFilePointer(); if ((s == "") || (s.startsWith(" ")) || (s == "\n")) continue; nodes[((int) line_ct)] = s; line_ct++; } return; } catch (IOException e) { System.out.println("Got a:\t" + e); System.exit(1); } } private static int NumLines(String fn) { long line_ct = 0; try { RandomAccessFile file = new RandomAccessFile(fn,"r"); for (long filePointer = 0, length = file.length(); filePointer < length; line_ct++) { String s = file.readLine(); filePointer = file.getFilePointer(); if ((s == "") || (s.startsWith(" ")) || (s == "\n")) continue; } return((int) line_ct); } catch (IOException e) { System.out.println("Got a:\t" + e); System.exit(1); } return((int) line_ct); } /** Subclasses should not override this. It does some setup work, and passes control to realMain(). */ public final static void main(String argv[]) throws IOException, ClassNotFoundException { System.out.print("Inside LindaProgram.main!!\n"); if (argv.length != 0) { // the user has specified the machines on the cmd-line nodes = argv; } else { // read the machine list from the default file ReadNodesFile("tsnet.nodes"); } realMain(argv); } /** This must be overridden in subclasses; Java provides no mechanism for declaring an abstract static method, but if it did this would be one. */ static protected void realMain(String argv[]) throws IOException, ClassNotFoundException { if (argv.length > 0) nodes = argv; myEvalServer = new EvalServer(); TupleSpace ts = new TupleSpace(nodes, 1, new Server(InetAddress.getLocalHost(), myEvalServer.getPort())); if (ts != null) System.out.print("Made a tuplespace!!\n"); Result res1,res2,res3,tmp_res; ts.eval(new TestEval()); ts.eval(new TestEval()); ts.eval(new TestEval()); ts.out(new Task(1)); ts.out(new Task(2)); ts.out(new Task(3)); tmp_res = new Result(0); res1 = (Result) ts.in(tmp_res); res2 = (Result) ts.in(tmp_res); res3 = (Result) ts.in(tmp_res); System.out.println("The result was: " + (res1.result_int + res2.result_int + res3.result_int)); myEvalServer.stop(); return; } }