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 {
2015-11-17 11:55:55 +00:00
xercesc : : XMLPlatformUtils : : Initialize ( ) ;
2014-06-26 11:10:51 +00:00
}
2015-11-17 11:55:55 +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 ) ;
2015-11-17 11:55:55 +00:00
xercesc : : XMLString : : release ( & ini_error ) ;
2014-06-26 11:10:51 +00:00
throw new_exception ;
}
jobfilename = NULL ;
setFilenamePattern ( the_jobfilenamepattern ) ;
2015-11-17 11:55:55 +00:00
parser = new xercesc : : XercesDOMParser ( ) ;
2014-06-26 11:10:51 +00:00
if ( parser = = NULL ) {
delete jobfilename ;
throw job_exception ( " could not allocate parser " ) ;
}
2015-11-17 11:55:55 +00:00
parser - > setValidationScheme ( xercesc : : XercesDOMParser : : Val_Always ) ;
2014-06-26 11:10:51 +00:00
parser - > setDoNamespaces ( true ) ;
2015-11-17 11:55:55 +00:00
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 ( ) ) ;
}
2015-11-17 11:55:55 +00:00
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 ) ;
2015-11-17 11:55:55 +00:00
xercesc : : XMLString : : release ( & message ) ;
2014-06-26 11:10:51 +00:00
throw je ;
}
2015-11-17 11:55:55 +00:00
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 ) ;
2015-11-17 11:55:55 +00:00
xercesc : : XMLString : : release ( & message ) ;
2014-06-26 11:10:51 +00:00
throw je ;
}
2015-11-17 11:55:55 +00:00
catch ( const xercesc : : SAXParseException & toCatch ) {
2014-06-26 11:10:51 +00:00
// more verbose for parser errors
2015-11-17 11:55:55 +00:00
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 ] ;
2015-11-17 11:55:55 +00:00
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 ) ;
2015-11-17 11:55:55 +00:00
xercesc : : XMLString : : release ( & message ) ;
2014-06-26 11:10:51 +00:00
throw je ;
}
2015-11-17 11:55:55 +00:00
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 ) ;
2015-11-17 11:55:55 +00:00
xercesc : : XMLString : : release ( & message ) ;
2014-06-26 11:10:51 +00:00
throw je ;
}
// extract root element, root attributes and root name
2015-11-17 11:55:55 +00:00
xercesc : : DOMDocument * doc = parser - > getDocument ( ) ;
2014-06-26 11:10:51 +00:00
if ( doc = = NULL ) throw job_exception ( " xml job document not found " ) ;
2015-11-17 11:55:55 +00:00
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 " ) ;
2015-11-17 11:55:55 +00:00
char * docname = xercesc : : XMLString : : transcode ( rootelement - > getNodeName ( ) ) ;
xercesc : : DOMNamedNodeMap * rootattrs = rootelement - > getAttributes ( ) ;
2014-06-26 11:10:51 +00:00
// check the job number
2015-11-17 11:55:55 +00:00
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 ) {
2015-11-17 11:55:55 +00:00
docnoname = xercesc : : XMLString : : transcode ( " no " ) ;
2014-06-26 11:10:51 +00:00
jobno_attr = rootattrs - > getNamedItem ( docnoname ) ;
2015-11-17 11:55:55 +00:00
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 {
2015-11-17 11:55:55 +00:00
char * docno = xercesc : : XMLString : : transcode ( jobno_attr - > getNodeValue ( ) ) ;
2014-06-26 11:10:51 +00:00
no = strtoul ( docno , NULL , 0 ) ;
2015-11-17 11:55:55 +00:00
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 ) ;
}
2015-11-17 11:55:55 +00:00
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 ) ;
2015-11-17 11:55:55 +00:00
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 ) ;
}
2015-11-17 11:55:55 +00:00
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 ) ;
2015-11-17 11:55:55 +00:00
xercesc : : XMLString : : release ( & domerrmsg ) ;
2014-06-26 11:10:51 +00:00
// cleanup missing
throw je ;
}
}
2015-11-17 11:55:55 +00:00
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 ;
2015-11-17 11:55:55 +00:00
xercesc : : XMLPlatformUtils : : Terminate ( ) ;
2014-06-26 11:10:51 +00:00
}