Files
@ ec90f545efc5
Branch filter:
Location: DistRen/src/server/distrenjob.c
ec90f545efc5
10.1 KiB
text/plain
Fix random warnings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | /*
Copyright 2010 Nathan Phillip Brink <ohnobinki@ohnopublishing.net>
This file is a part of DistRen.
DistRen is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DistRen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with DistRen. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/config.h"
#include "distrenjob.h"
#include "slavefuncs.h"
#include "common/asprintf.h"
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlwriter.h>
#include <time.h>
void distrenjob_free(struct distrenjob **distrenjob)
{
struct distrenjob *dj;
dj = *distrenjob;
xmlFree(dj->name);
xmlFree(dj->submitter);
free(dj->frameset);
free(dj);
*distrenjob = NULL;
}
int distrenjob_new(struct distrenjob **distrenjob)
{
struct distrenjob *dj;
dj = malloc(sizeof(struct distrenjob));
if(!dj)
{
/* try to catch code that doesn't respect return values
faster: */
*distrenjob = NULL;
return 1;
}
*distrenjob = dj;
dj->next = NULL;
dj->jobnum = 0; /*< @todo there should be a central jobnum allocator and a way to save the maximum jobnumber allocated */
dj->type = 1;
dj->name = (char *)NULL;
dj->submitter = (char *)NULL;
dj->output_format = (char *)NULL;
dj->width = 0;
dj->height = 0;
dj->priority = 0;
dj->completed_frames = 0;
dj->assigned_frames = 0;
dj->total_frames = 0;
dj->prev_frame_index = -1;
dj->total_render_time = 0;
dj->assigned_render_power = 0;
dj->watchdog_forgiveness = 3600; // initialize watchdog forgiveness at 1 hour
dj->hibernate = 0;
dj->frameset = (struct frameset *)NULL; /*< @todo does frameset need to be initialized here? */
return 0;
}
/**
read an unsigned integer property from an xmlNode
convenience function for distrenjob_unserialize()
@param xmlnode may be NULL for convenience
@return 0 on success, other on error
*/
int _distrenjob_xml_readuint(xmlNodePtr xmlnode, xmlChar *propname, unsigned int *num)
{
xmlChar *string;
if(!xmlnode)
return 1;
string = xmlGetProp(xmlnode, propname);
if(!string)
{
fprintf(stderr, "_distrenjob_xml_readuint(): warning: unable to get property ``%s''\n",
propname);
return 1;
}
*num = (unsigned int)strtoul((char *)string, (char **)NULL, 10);
xmlFree(string);
return 0;
}
/**
Loads a distrenjob that was serialized with distrenjob_serialize()
@see distrenjob_serialize()
@param pathtoxml path to XML file
@param distrenjob will be set to a pointer
*/
int distrenjob_unserialize(struct distrenjob **distrenjob, char *pathtoxml)
{
struct distrenjob *dj;
struct frameset *fs;
unsigned int start_frame;
unsigned int end_frame;
unsigned int counter;
struct tm tm;
xmlDocPtr xmldoc;
xmlNodePtr xmlnode;
xmlChar *xmlchar;
xmlXPathContextPtr xmlxpathcontext;
int tmp;
if(distrenjob_new(distrenjob))
return 1;
dj = *distrenjob;
xmldoc = xmlReadFile(pathtoxml, NULL, XML_PARSE_PEDANTIC);
if(!xmldoc)
{
/**
@todo are we able to depend on libxml2's printed errors or
channel them into syslog output (eventually)? Currently,
this error is repetitious of a libxml2 error printed on stderr
for us.
*/
fprintf(stderr, "error reading XML file ``%s''\n", pathtoxml);
distrenjob_free(distrenjob);
return 2;
}
xmlxpathcontext = xmlXPathNewContext(xmldoc);
xmlnode = xml_quickxpath(xmlxpathcontext, (xmlChar *)"/job");
if(!xmlnode)
{
distrenjob_free(distrenjob);
return 3;
}
/*< @todo should we use xmlChar everywhere too? */
dj->name = (char *)xmlGetProp(xmlnode, (xmlChar *)"name");
if(!dj->name)
{
distrenjob_free(distrenjob);
return 4;
}
/*< @todo validation needs to be done on usernames. e.g., currently, they shouldn't contain the '"' char */
dj->submitter = (char *)xmlGetProp(xmlnode, (xmlChar *)"submitter");
if(!dj->submitter)
{
distrenjob_free(distrenjob);
return 5;
}
tmp = _distrenjob_xml_readuint(xmlnode, (xmlChar *)"priority", &dj->priority);
if(tmp)
{
distrenjob_free(distrenjob);
return 6;
}
dj->output_format = (char *)xmlGetProp(xmlnode, (xmlChar *)"output_format");
if(!dj->output_format)
{
distrenjob_free(distrenjob);
return 6;
}
tmp = _distrenjob_xml_readuint(xmlnode, (xmlChar *)"jobnum", &dj->jobnum);
if(tmp)
{
distrenjob_free(distrenjob);
return 7;
}
xmlnode = xml_quickxpath(xmlxpathcontext, (xmlChar *)"/job/resolution");
tmp = _distrenjob_xml_readuint(xmlnode, (xmlChar *)"width" , &dj->width );
tmp += _distrenjob_xml_readuint(xmlnode, (xmlChar *)"height" , &dj->height );
xmlnode = xml_quickxpath(xmlxpathcontext, (xmlChar *)"/job/video");
tmp += _distrenjob_xml_readuint(xmlnode, (xmlChar *)"start_frame", &start_frame);
tmp += _distrenjob_xml_readuint(xmlnode, (xmlChar *)"end_frame", &end_frame);
if(tmp)
{
fprintf(stderr, "distrenjob_unserialize(): error reading integer values from ``%s''\n",
pathtoxml);
distrenjob_free(distrenjob);
return 8;
}
/**
total_render_time and watchdog stuff doesn't need error
checking, just good defaults
*/
xmlnode = xml_quickxpath(xmlxpathcontext, (xmlChar *)"/job/stats");
dj->total_render_time = 0;
xmlchar = xmlGetProp(xmlnode, (xmlChar *)"total_render_time");
if(xmlchar)
{
/*
* even though GCC says this is implicitly declared, POSIX
* requires that time.h declare this function. Thus, we don't
* have to worry about the warning.
*/
if(strptime((char *)xmlchar, "%D %T", &tm))
dj->total_render_time = mktime(&tm);
xmlFree(xmlchar);
}
xmlnode = xml_quickxpath(xmlxpathcontext, (xmlChar *)"/job/watchdog");
dj->watchdog_forgiveness = 3600;
tmp =_distrenjob_xml_readuint(xmlnode, (xmlChar *)"forgiveness", &dj->watchdog_forgiveness);
if(tmp)
fprintf(stderr, "distrenjob_unserialize(): warning: watchdog forgiveness is unspecified in ``%s'', defaulting to 3600\n", pathtoxml);
xmlXPathFreeContext(xmlxpathcontext);
xmlFreeDoc(xmldoc);
/**
reconstruct the frameset
*/
dj->total_frames = end_frame - start_frame + 1;
#ifndef NDEBUG
fprintf(stderr, "distrenjob_unserialize(): restoring %d frames\n", dj->total_frames);
#endif
dj->frameset = malloc(sizeof(struct frameset) * dj->total_frames);
if(!dj->frameset)
{
fprintf(stderr, "OOM!\n");
distrenjob_free(distrenjob);
return 9;
}
fs = dj->frameset;
for(counter = start_frame; counter <= end_frame; counter ++)
{
fs->num = counter;
fs->status = FRAMESETSTATUS_UNASSIGNED; /*< @todo job partial completion and resumption support */
fs ++;
}
#ifndef NDEBUG
fprintf(stderr, "distrenjob_unserialize(): finished loading ``%s''\n", pathtoxml);
#endif
return 0;
}
int distrenjob_serialize(struct distrenjob *job, char *outfile)
{
xmlTextWriterPtr writer;
char *tmp;
char *tmpfile;
int tmprtn;
/**
transactional FS access for POSIX systems...
(probably not implemented correctly)
*/
_distren_asprintf(&tmpfile, "%s_", outfile);
/* create xml document at the location tmp with no compression */
writer = xmlNewTextWriterFilename(tmpfile, 0);
xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
/**
write distrenjob element and add its attributes */
xmlTextWriterStartElement(writer, (xmlChar*)"job");
xmlTextWriterWriteAttribute(writer, (xmlChar*)"name", (xmlChar*)job->name);
xmlTextWriterWriteAttribute(writer, (xmlChar*)"submitter", (xmlChar*)job->submitter);
_distren_asprintf(&tmp, "%d", job->priority);
xmlTextWriterWriteAttribute(writer, (xmlChar*)"priority", (xmlChar*)tmp);
free(tmp);
_distren_asprintf(&tmp, "%d", job->jobnum);
xmlTextWriterWriteAttribute(writer, (xmlChar *)"jobnum", (xmlChar *)tmp);
free(tmp);
xmlTextWriterWriteAttribute(writer, (xmlChar *)"output_format", (xmlChar *)job->output_format);
/**
write resolution element and add its attributes */
xmlTextWriterStartElement(writer, (xmlChar*)"resolution");
_distren_asprintf(&tmp, "%d", job->width);
xmlTextWriterWriteAttribute(writer, (xmlChar*)"width", (xmlChar*)tmp);
free(tmp);
_distren_asprintf(&tmp, "%d", job->height);
xmlTextWriterWriteAttribute(writer, (xmlChar*)"height", (xmlChar*)tmp);
free(tmp);
xmlTextWriterEndElement(writer);
/**
write video element and its attributes */
xmlTextWriterStartElement(writer, (xmlChar*)"video");
_distren_asprintf(&tmp, "%d", job->frameset[0].num);
xmlTextWriterWriteAttribute(writer, (xmlChar*)"start_frame", (xmlChar*)tmp);
free(tmp);
_distren_asprintf(&tmp, "%d", job->frameset[ job->total_frames - 1 ].num);
xmlTextWriterWriteAttribute(writer, (xmlChar*)"end_frame", (xmlChar*)tmp);
free(tmp);
xmlTextWriterWriteAttribute(writer, (xmlChar*)"output_format", (xmlChar*)job->output_format);
xmlTextWriterEndElement(writer);
/**
write watchdog forgiveness element */
xmlTextWriterStartElement(writer, (xmlChar*)"watchdog");
_distren_asprintf(&tmp, "%d", job->watchdog_forgiveness);
xmlTextWriterWriteAttribute(writer, (xmlChar *)"forgiveness", (xmlChar *)tmp);
free(tmp);
xmlTextWriterEndElement(writer); /* </watchdog> */
xmlTextWriterEndElement(writer); /* </job> */
/**
end document */
xmlTextWriterEndDocument(writer);
/**
free writer and save xml file to disk */
xmlFreeTextWriter(writer);
/**
This is the key to transactioanl POSIX
FS programming.
*/
tmprtn = rename(tmpfile, outfile);
if(tmprtn == -1)
{
fprintf(stderr, "%s:%d: Error renaming ``%s'' to ``%s''\n",
__FILE__, __LINE__,
tmpfile, outfile);
perror("rename");
tmprtn = 1;
}
free(tmpfile);
return tmprtn;
}
|