import java.io.*;
/*
Every HPJava program will extends from Node to utilize all the helping
methods provided by Node class.
When implments Runnable interface, we enabled the multi-task on HPJava.
More than one browser can run the HPJava program at the same time.
*/
public class HPJ extends Node implements Runnable {
int N, NITER;
int mode1, mode2;
public HPJ() {
/*
Before this point, the Node's constructer already complete and all
connections are ready.
*/
/*
Now getParam() will return the parameter from HPJava Client. Cause
HPJava Client will only connect to id 0 instance (To avoid Java
security exception), the id 0 will responsible for brocast this
parameter to every other HPJava instance.
*/
N = getParam(); // get size of life
NITER = getParam(); // get number of life iteration
mode1 = getParam(); // get x-dim processor mode (BLOCK/CYCLIC)
mode2 = getParam(); // get y-dim processor mode (BLOCK/CYCLIC)
initialize(N*N*4+4); // Adjust output buffer
new Thread(this).start(); // Start thread for this instance
}
public void run() {
int snp = (int)Math.sqrt(np);
Procs p = new Procs(this, snp, np/snp); // create process distribution
if ( p.off() ) { // if this instance is not assigned then ...
finalize();
return;
}
Range x = new Range(N, mode1, p, 0); // Create iteration range on X
Range y = new Range(N, mode2, p, 1); // Create iteration range on Y
Array r = new Array(p, x, y) ; // Create 2D iteration array
int xys = r.seg(); // seg will return the current segment size
/*
We decide to use 1D array to stand for multi-dimension array for
effecient reason. Java's OOP lack of template and pointer
also make API design become complcate.
*/
int[] c = new int[xys];
int[] w = new int[xys];
int[] cn_ = new int[xys];
int[] cp_ = new int[xys];
int[] c_n = new int[xys];
int[] c_p = new int[xys];
int[] cnn = new int[xys];
int[] cnp = new int[xys];
int[] cpn = new int[xys];
int[] cpp = new int[xys];
int M = N/2;
int size = N*N;
int[] col = (id == 0) ? new int[size] : null;
int[] tmp = (id == 0) ? new int[size] : null;
/* Create the Color map and send to Client for reference */
for (int i=0; i<c.length; i++) c[i] = id;
collect(col, c, r);
sendHost(col);
/* Create life's initial mapping and send to Client */
for(r.forall(); r.test(); r.next())
w[r.sub()] = ((x.idx() == M)||(y.idx() == M)) ? 1 : 0;
collect(tmp, w, r);
sendHost(tmp);
/* Start Life cycle */
for (int k=0; k<NITER; k++) {
/* use shifts to get the neighbor cells */
shift(cn_, w, r, 0, 1, CYCLIC);
shift(cp_, w, r, 0, -1, CYCLIC);
shift(c_n, w, r, 1, 1, CYCLIC);
shift(c_p, w, r, 1, -1, CYCLIC);
shift(cnn, cn_, r, 1, 1, CYCLIC);
shift(cnp, cn_, r, 1, -1, CYCLIC);
shift(cpn, cp_, r, 1, 1, CYCLIC);
shift(cpp, cp_, r, 1, -1, CYCLIC);
for(r.forall(); r.test(); r.next()) {
int xy = r.sub();
switch (cn_[xy]+cp_[xy]+c_n[xy]+c_p[xy]+
cnn[xy]+cnp[xy]+cpn[xy]+cpp[xy]) {
case 2 : break;
case 3 : w[xy] = 1; break;
default: w[xy] = 0; break;
}
}
/* collect result and send to Client */
collect(tmp, w, r);
sendHost(tmp);
}
/* call finalize to disconnect sock connections */
finalize();
}
}
...
canvas.setImage(null);
((CardLayout)card2.getLayout()).show(card2, tag2[2]);
/* build active host list */
int np = selectList.countItems();
String str = "HPJ"+Separator+np+Separator;
for (int i=0; i<np; i++)
str += selectList.getItem(i)+Separator;
/* send run-remote-class command to server */
issueCommand(Rn, str);
try {
/* build sock connection to id 0 instance */
Socket sock = new Socket(nodeHost, nodePort);
DataInputStream fromNode = new DataInputStream(sock.getInputStream());
DataOutputStream toNode = new DataOutputStream(sock.getOutputStream());
/* obtain parameters and send to id 0 instance */
int N = new Integer(tf1.getText()).intValue();
int NITER = new Integer(tf2.getText()).intValue();
if ( N < 2 ) N = 2;
if ( NITER < 0 ) NITER = 0;
toNode.writeInt(N);
toNode.writeInt(NITER);
toNode.writeInt((cbg1.getCurrent() == cb11) ? 0 : 1);
toNode.writeInt((cbg2.getCurrent() == cb21) ? 0 : 1);
toNode.flush();
int size = N*N;
int w = canvas.size().width;
int h = canvas.size().height;
int dx = w/N;
int dy = h/N;
int x, y;
int[][] map = new int[N][N];
Image img = createImage(w, h);
Graphics g = img.getGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, w, h);
/* receive the color map (proc id) */
for (y=0; y<N; y++) {
for (x=0; x<N; x++)
map[y][x] = fromNode.readInt() % dark.length;
}
/* receive pattern for each life cycle */
for (int k=0; k<=NITER; k++) {
for (y=0; y<N; y++) {
for (x=0; x<N; x++) {
g.setColor(
(fromNode.readInt() == 1) ?
bright[map[y][x]] : dark[map[y][x]]);
g.fillRect(x*dx, y*dy, dx-2, dy-2);
}
}
/* display the life pattern to screen */
canvas.setImage(img);
}
g.dispose();
/* close socket connection */
sock.close();
} catch (Exception e) {
System.out.println(e);
}
...