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.povray;
020:
021: import java.util.*;
022:
023: import org.homedns.dade.jcgrid.message.*;
024: import org.homedns.dade.jcgrid.client.*;
025:
026: public class POVRenderingClient extends Thread {
027: private GridNodeClientConfig clientCfg;
028: private POVRenderingClientFeedback clientFeedback;
029:
030: private POVWorkRequest povWReq;
031: private int fragSize;
032: private boolean hasAnimation;
033: private int startFrame;
034: private int endFrame;
035:
036: private POVRenderingFrame[] frames;
037:
038: public POVRenderingClient(GridNodeClientConfig cfg,
039: POVRenderingClientFeedback feedback, POVWorkRequest req,
040: int frag) {
041: clientCfg = cfg;
042: clientFeedback = feedback;
043:
044: povWReq = req;
045: fragSize = frag;
046: hasAnimation = false;
047:
048: frames = new POVRenderingFrame[1];
049: frames[0] = null;
050: }
051:
052: public POVRenderingClient(GridNodeClientConfig cfg,
053: POVRenderingClientFeedback feedback, POVWorkRequest req,
054: int frag, int start, int end) {
055: this (cfg, feedback, req, frag);
056:
057: hasAnimation = true;
058: startFrame = start;
059: endFrame = end;
060:
061: frames = new POVRenderingFrame[endFrame - startFrame + 1];
062: Arrays.fill(frames, null);
063: }
064:
065: public void run() {
066: try {
067: // Start Client
068:
069: GridClient gc = new GridClient();
070: gc.setNodeConfig(clientCfg);
071: gc.start();
072:
073: long t1 = System.currentTimeMillis();
074: try {
075: // Splitting the work
076:
077: int xStep = fragSize;
078: int yStep = (int) (fragSize * povWReq.getHeight() / (double) povWReq
079: .getWidth());
080: POVWorkRequest[] workList;
081: if (hasAnimation)
082: workList = povWReq.split(startFrame, endFrame,
083: xStep, yStep);
084: else
085: workList = povWReq.split(xStep, yStep);
086:
087: int fragCount = povWReq.fragmentPerFrame(xStep, yStep);
088:
089: clientFeedback.setProgressMaximum(0);
090: clientFeedback
091: .setProgressMaximum(2 * workList.length - 1);
092:
093: clientFeedback.renderingBegin();
094:
095: // Sending work requests
096:
097: for (int i = 0; i < workList.length; i++) {
098: clientFeedback.setProgressValue(i);
099: clientFeedback.sendingFrgamentRequest(workList[i]);
100:
101: gc.send(new GridMessageWorkRequest(workList[i]));
102:
103: if (this .isInterrupted())
104: break;
105: }
106:
107: // Receiving work results
108:
109: for (int i = 0; i < workList.length; i++) {
110: clientFeedback
111: .setProgressValue(i + workList.length);
112:
113: GridMessageWorkResult gmwr = (GridMessageWorkResult) gc
114: .recv();
115: POVWorkResult povWRes = (POVWorkResult) gmwr
116: .getWorkResult();
117:
118: int idx = povWRes.getRID() - 1;
119: int frameIdx = workList[idx].getFrame()
120: - startFrame;
121:
122: if (frames[frameIdx] == null) {
123: // If the framebuffer doesn't exist create it
124:
125: frames[frameIdx] = new POVRenderingFrame(
126: workList[idx].getWidth(), workList[idx]
127: .getHeight(), fragCount);
128: }
129: frames[frameIdx].addFragmentResult(workList[idx],
130: povWRes);
131:
132: clientFeedback.receivedFragmentResult(
133: workList[idx], povWRes, workList[idx]
134: .getFrame(), frames[frameIdx]);
135:
136: if (frames[frameIdx].isComplete()) {
137: clientFeedback.completeFrame(workList[idx]
138: .getFrame(), frames[frameIdx]);
139:
140: // Free memory
141:
142: frames[frameIdx] = null;
143: }
144:
145: if (this .isInterrupted())
146: break;
147: }
148: } finally {
149: long t2 = System.currentTimeMillis();
150:
151: clientFeedback.setRenderingTime(povWReq, t2 - t1);
152:
153: try {
154: gc.stop();
155: } catch (Exception ex) {
156: }
157: }
158: } catch (Exception ex) {
159: clientFeedback.error("Error while doing the rendering", ex);
160: }
161:
162: clientFeedback.renderingEnd();
163: }
164: }
|