diff --git a/src/server/distrenjob.c b/src/server/distrenjob.c --- a/src/server/distrenjob.c +++ b/src/server/distrenjob.c @@ -23,6 +23,7 @@ #include #include +#include void distrenjob_free(struct distrenjob **distrenjob) { @@ -76,7 +77,7 @@ int distrenjob_new(struct distrenjob **d /** writes struct from xml */ -int xml2distrenjob(struct distrenjob **distrenjob, char *pathtoxml) +int distrenjob_unserialize(struct distrenjob **distrenjob, char *pathtoxml) { struct distrenjob *dj; @@ -155,3 +156,87 @@ int xml2distrenjob(struct distrenjob **d 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); + + /** + 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_fame", (xmlChar*)tmp); + free(tmp); + + xmlTextWriterWriteAttribute(writer, (xmlChar*)"output_format", (xmlChar*)job->output_format); + xmlTextWriterEndElement(writer); + + /** + write watchdog forgiveness element */ + _distren_asprintf(&tmp, "%d", job->watchdog_forgiveness); + xmlTextWriterWriteElement(writer, (xmlChar*)"wd_forgiveness", (xmlChar*)tmp); + free(tmp); + + /** + 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; +}