damaris-backends/core/job_receiver.cpp

179 lines
6.1 KiB
C++
Raw Normal View History

2014-06-26 11:10:51 +00:00
/* **************************************************************************
Author: Achim Gaedke
Created: June 2004
****************************************************************************/
#include "job_receiver.h"
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMException.hpp>
job_receiver::job_receiver(std::string the_jobfilenamepattern) {
try {
xercesc::XMLPlatformUtils::Initialize();
2014-06-26 11:10:51 +00:00
}
catch (const xercesc::XMLException& toCatch) {
char* ini_error=xercesc::XMLString::transcode(toCatch.getMessage());
2014-06-26 11:10:51 +00:00
job_exception new_exception(std::string("xerces initialisation error: ")+ini_error);
xercesc::XMLString::release(&ini_error);
2014-06-26 11:10:51 +00:00
throw new_exception;
}
jobfilename=NULL;
setFilenamePattern(the_jobfilenamepattern);
parser=new xercesc::XercesDOMParser();
2014-06-26 11:10:51 +00:00
if (parser==NULL) {
delete jobfilename;
throw job_exception("could not allocate parser");
}
parser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
2014-06-26 11:10:51 +00:00
parser->setDoNamespaces(true);
errHandler = (xercesc::ErrorHandler*) new xercesc::HandlerBase();
2014-06-26 11:10:51 +00:00
parser->setErrorHandler(errHandler);
}
void job_receiver::setFilenamePattern(const std::string& filenamepattern) {
if (jobfilename!=NULL) delete jobfilename;
jobfilenamepattern=filenamepattern;
jobfilenamesize=filenamepattern.size()+20;
jobfilename=new char[jobfilenamesize];
if (jobfilename==NULL) throw job_exception("could not allocate memory for filename");
}
job* job_receiver::receive(size_t no) {
snprintf(jobfilename,jobfilenamesize+20,jobfilenamepattern.c_str(),no);
job* new_job=receive(std::string(jobfilename));
if (new_job->no()!=no) fprintf(stderr, "expected job number %" SIZETPRINTFLETTER " and specified number %" SIZETPRINTFLETTER " are different\n", no, new_job->no() );
new_job->job_no=no;
return new_job;
}
job* job_receiver::receive(const std::string& filename) {
try {
parser->parse(filename.c_str());
}
catch(const xercesc::XMLException& toCatch) {
char* message = xercesc::XMLString::transcode(toCatch.getMessage());
2014-06-26 11:10:51 +00:00
job_exception je(std::string("XML error: ")+message);
xercesc::XMLString::release(&message);
2014-06-26 11:10:51 +00:00
throw je;
}
catch(const xercesc::DOMException& toCatch) {
char* message = xercesc::XMLString::transcode(toCatch.msg);
2014-06-26 11:10:51 +00:00
job_exception je(std::string("XML DOM error: ")+message);
xercesc::XMLString::release(&message);
2014-06-26 11:10:51 +00:00
throw je;
}
catch(const xercesc::SAXParseException& toCatch) {
2014-06-26 11:10:51 +00:00
// more verbose for parser errors
char* message = xercesc::XMLString::transcode(toCatch.getMessage());
2014-06-26 11:10:51 +00:00
job_exception je(std::string("XML SAX Parser error: ")+message);
char location[100];
snprintf(location,sizeof(location),", line %ld column %ld",(long int) toCatch.getLineNumber(),(long int)toCatch.getColumnNumber());
2014-06-26 11:10:51 +00:00
je.append(location);
xercesc::XMLString::release(&message);
2014-06-26 11:10:51 +00:00
throw je;
}
catch(const xercesc::SAXException& toCatch) {
char* message = xercesc::XMLString::transcode(toCatch.getMessage());
2014-06-26 11:10:51 +00:00
job_exception je(std::string("XML SAX error: ")+message);
xercesc::XMLString::release(&message);
2014-06-26 11:10:51 +00:00
throw je;
}
// extract root element, root attributes and root name
xercesc::DOMDocument* doc=parser->getDocument();
2014-06-26 11:10:51 +00:00
if (doc==NULL) throw job_exception("xml job document not found");
xercesc::DOMElement* rootelement=doc->getDocumentElement();
2014-06-26 11:10:51 +00:00
if (rootelement==NULL) throw job_exception("xml job root document not found");
char* docname=xercesc::XMLString::transcode(rootelement->getNodeName());
xercesc::DOMNamedNodeMap* rootattrs=rootelement->getAttributes();
2014-06-26 11:10:51 +00:00
// check the job number
XMLCh* docnoname=xercesc::XMLString::transcode("no");
xercesc::DOMNode* jobno_attr=rootattrs->getNamedItem(docnoname);
xercesc::XMLString::release(&docnoname);
2014-06-26 11:10:51 +00:00
if (jobno_attr==NULL) {
docnoname=xercesc::XMLString::transcode("no");
2014-06-26 11:10:51 +00:00
jobno_attr=rootattrs->getNamedItem(docnoname);
xercesc::XMLString::release(&docnoname);
2014-06-26 11:10:51 +00:00
}
size_t no=0;
if (jobno_attr==NULL) fprintf(stderr,"Warning: job %" SIZETPRINTFLETTER ": root element has no job number\n",no);
else {
char* docno=xercesc::XMLString::transcode(jobno_attr->getNodeValue());
2014-06-26 11:10:51 +00:00
no=strtoul(docno,NULL,0);
xercesc::XMLString::release(&docno);
2014-06-26 11:10:51 +00:00
}
job* this_job=NULL;
// determine the job type by name
if (strcasecmp(docname,"quit")==0) {
this_job=new quit_job(no);
}
else if (strcasecmp(docname,"nop")==0) {
this_job=new do_nothing_job(no);
}
else if (strcasecmp(docname,"pause")==0) {
this_job=new pause_job(no);
} /* pause */
else if (strcasecmp(docname,"restart")==0) {
this_job=new restart_job(no);
} /* restart */
else if (strcasecmp(docname,"wait")==0) {
this_job=new wait_job(no,rootattrs);
} /* wait */
else if (strcasecmp(docname,"experiment")==0) {
try {
this_job=new experiment(no, rootelement);
}
catch (const xercesc::DOMException& de) {
char* domerrmsg=xercesc::XMLString::transcode(de.msg);
2014-06-26 11:10:51 +00:00
char domerrno[5];
snprintf(domerrno,5,"%d",de.code);
job_exception je("sorry, something happend while parsing experiment job: ");
je.append(domerrmsg);
je.append(", code ");
je.append(domerrno);
xercesc::XMLString::release(&domerrmsg);
2014-06-26 11:10:51 +00:00
// cleanup missing
throw je;
}
}
else if (strcasecmp(docname,"configuration")==0) {
try {
this_job=new configuration(no, rootelement);
}
catch (const xercesc::DOMException& de) {
char* domerrmsg=xercesc::XMLString::transcode(de.msg);
2014-06-26 11:10:51 +00:00
char domerrno[5];
snprintf(domerrno,5,"%d",de.code);
job_exception je("sorry, something happend while parsing configuration job: ");
je.append(domerrmsg);
je.append(", code ");
je.append(domerrno);
xercesc::XMLString::release(&domerrmsg);
2014-06-26 11:10:51 +00:00
// cleanup missing
throw je;
}
}
xercesc::XMLString::release(&docname);
2014-06-26 11:10:51 +00:00
parser->reset();
parser->resetDocument();
parser->resetDocumentPool();
return this_job;
}
job_receiver::~job_receiver() {
delete jobfilename;
delete errHandler;
delete parser;
xercesc::XMLPlatformUtils::Terminate();
2014-06-26 11:10:51 +00:00
}