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.io.*;
022:
023: import org.homedns.dade.jcgrid.*;
024: import org.homedns.dade.jcgrid.worker.*;
025: import org.homedns.dade.jcgrid.util.*;
026:
027: import org.apache.log4j.*;
028:
029: public class POVWorker implements Worker {
030: private final static String className = POVWorker.class.getName();
031: private static Logger log = Logger.getLogger(className);
032: private static Logger logDetail = Logger.getLogger("DETAIL."
033: + className);
034:
035: public final static String TEMP_IMG_FILE = "jcgrid-worker-tmp-img-file.ppm";
036:
037: private boolean usePVEngineEXE;
038:
039: public POVWorker(boolean useEXE) {
040: usePVEngineEXE = useEXE;
041: }
042:
043: public WorkResult doWork(WorkRequest work, String workDir)
044: throws Exception {
045: POVWorkRequest povwr = ((POVWorkRequest) work);
046:
047: // Create command line
048:
049: String tmpFileName = workDir + File.separator + TEMP_IMG_FILE;
050: String[] cmd;
051: if (usePVEngineEXE) {
052: if (povwr.isAnimationEnabled()) {
053: String[] cmdAnim = { "pvengine.exe", "/exit",
054: "/render", "-V", "-D", "+W" + povwr.getWidth(),
055: "+H" + povwr.getHeight(),
056: "+SC" + (povwr.getStartCol() + 1),
057: "+EC" + (povwr.getEndCol() + 1),
058: "+SR" + (povwr.getStartRow() + 1),
059: "+ER" + (povwr.getEndRow() + 1), "+FP",
060: "+O" + tmpFileName, "+SF" + povwr.getFrame(),
061: "+EF" + povwr.getFrame(),
062: povwr.getINIFileName() };
063: cmd = cmdAnim;
064: } else {
065: String[] cmdNoAnim = {
066: "C:\\programmi\\povray-3.6\\bin\\pvengine.exe",
067: "/exit", "/render", "-V", "-D",
068: "+W" + povwr.getWidth(),
069: "+H" + povwr.getHeight(),
070: "+SC" + (povwr.getStartCol() + 1),
071: "+EC" + (povwr.getEndCol() + 1),
072: "+SR" + (povwr.getStartRow() + 1),
073: "+ER" + (povwr.getEndRow() + 1), "+FP",
074: "+O" + tmpFileName, povwr.getINIFileName() };
075: cmd = cmdNoAnim;
076: }
077: } else {
078: if (povwr.isAnimationEnabled()) {
079: String[] cmdAnim = { "povray", "-V", "-D",
080: "+W" + povwr.getWidth(),
081: "+H" + povwr.getHeight(),
082: "+SC" + (povwr.getStartCol() + 1),
083: "+EC" + (povwr.getEndCol() + 1),
084: "+SR" + (povwr.getStartRow() + 1),
085: "+ER" + (povwr.getEndRow() + 1), "+FP",
086: "+O" + tmpFileName, "+SF" + povwr.getFrame(),
087: "+EF" + povwr.getFrame(),
088: povwr.getINIFileName() };
089: cmd = cmdAnim;
090: } else {
091: String[] cmdNoAnim = { "povray", "-V", "-D",
092: "+W" + povwr.getWidth(),
093: "+H" + povwr.getHeight(),
094: "+SC" + (povwr.getStartCol() + 1),
095: "+EC" + (povwr.getEndCol() + 1),
096: "+SR" + (povwr.getStartRow() + 1),
097: "+ER" + (povwr.getEndRow() + 1), "+FP",
098: "+O" + tmpFileName, povwr.getINIFileName() };
099: cmd = cmdNoAnim;
100: }
101: }
102:
103: // Execute POVRay
104:
105: ConsoleCmd cc = new ConsoleCmd(cmd, null, workDir);
106:
107: if (cc.run() != 0) {
108: StringBuffer sb = new StringBuffer();
109: sb.append("Failed POVRay command execution:\n");
110:
111: sb.append("[WorkDir: \n");
112: sb.append("<");
113: sb.append(workDir);
114: sb.append(">");
115:
116: sb.append("\n]\n[Cmd:\n");
117: for (int i = 0; i < cmd.length; i++) {
118: sb.append("<");
119: sb.append(cmd[i]);
120: sb.append(">\n");
121: }
122:
123: sb.append("]\n[stderr:\n");
124: sb.append(new String(cc.getStdErr()));
125: sb.append("\n]\n[stdout:\n");
126: sb.append(new String(cc.getStdOut()));
127: sb.append("\n]");
128:
129: log.warn(sb.toString());
130:
131: throw new Exception("Failed POVRay command execution");
132: }
133:
134: if (cc.getStdErr().length > 0)
135: log.warn("Stderr execution in POVWorker.doWork(): "
136: + new String(cc.getStdErr()));
137:
138: // Read temporary file. I can't use POVRay stdout because a bug of
139: // java CreateProcess under Windows (0x0a are replaced with 0x0d0a)
140:
141: File tmpFile = new File(tmpFileName);
142: FileInputStream fis = new FileInputStream(tmpFile);
143: byte[] data = ByteVector.readAll(fis);
144: fis.close();
145: tmpFile.delete();
146:
147: // Create work result
148:
149: POVWorkResult res = new POVWorkResult(povwr.getSessionName(),
150: povwr.getRID(), data, (povwr.getEndCol()
151: - povwr.getStartCol() + 1)
152: * (povwr.getEndRow() - povwr.getStartRow() + 1));
153:
154: return res;
155: }
156: }
|