001: /*
002: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
003:
004: This program is free software; you can redistribute it and/or
005: modify it under the terms of the GNU General Public License
006: as published by the Free Software Foundation; either version 2
007: of the License, or (at your option) any later version.
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with this program; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018:
019: package org.homedns.dade.jcgrid.worker.impl.mandel;
020:
021: import java.util.*;
022: import java.awt.*;
023: import java.awt.image.*;
024: import javax.swing.*;
025: import javax.imageio.*;
026:
027: import org.homedns.dade.jcgrid.message.*;
028: import org.homedns.dade.jcgrid.client.*;
029:
030: public class MandelRenderingClient extends Thread {
031: private GridNodeClientConfig clientCfg;
032: private MandelRenderingClientFeedback clientFeedback;
033:
034: private MandelWorkRequest mandelWReq;
035:
036: private MandelRenderingFrame frame;
037:
038: public MandelRenderingClient(GridNodeClientConfig cfg,
039: MandelRenderingClientFeedback feedback,
040: MandelWorkRequest req) {
041: clientCfg = cfg;
042: clientFeedback = feedback;
043:
044: mandelWReq = req;
045:
046: frame = new MandelRenderingFrame(req.getXStep(),
047: req.getYStep(), req.getMaxIter());
048: }
049:
050: public MandelRenderingFrame getFrame() {
051: return frame;
052: }
053:
054: public void run() {
055: try {
056: // Start Client
057:
058: GridClient gc = new GridClient();
059: gc.setNodeConfig(clientCfg);
060: gc.start();
061:
062: long t1 = System.currentTimeMillis();
063: try {
064: // Splitting the work
065:
066: MandelWorkRequest[] workList = mandelWReq.split();
067: int fragCount = mandelWReq.fragmentPerFrame();
068:
069: clientFeedback.setProgressMaximum(0);
070: clientFeedback
071: .setProgressMaximum(2 * workList.length - 1);
072:
073: clientFeedback.renderingBegin();
074:
075: // Sending work requests
076:
077: for (int i = 0; i < workList.length; i++) {
078: clientFeedback.setProgressValue(i);
079: clientFeedback.sendingFrgamentRequest(workList[i]);
080:
081: gc.send(new GridMessageWorkRequest(workList[i]));
082:
083: if (this .isInterrupted())
084: break;
085: }
086:
087: // Receiving work results
088:
089: for (int i = 0; i < workList.length; i++) {
090: clientFeedback
091: .setProgressValue(i + workList.length);
092:
093: GridMessageWorkResult gmwr = (GridMessageWorkResult) gc
094: .recv();
095: MandelWorkResult mandelWRes = (MandelWorkResult) gmwr
096: .getWorkResult();
097: int idx = mandelWRes.getRID();
098:
099: frame.addFragmentResult(workList[idx], mandelWRes,
100: frame);
101: clientFeedback.receivedFragmentResult(
102: workList[idx], mandelWRes, frame);
103:
104: if (this .isInterrupted())
105: break;
106: }
107: } finally {
108: long t2 = System.currentTimeMillis();
109:
110: clientFeedback.setRenderingTime(mandelWReq, t2 - t1);
111:
112: try {
113: gc.stop();
114: } catch (Exception ex) {
115: }
116: }
117: } catch (Exception ex) {
118: clientFeedback.error("Error while doing the rendering", ex);
119: }
120:
121: clientFeedback.renderingEnd();
122: }
123: }
|