1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XMLNode.hxx"
21 #include "XMLPorts.hxx"
23 #include "TypeCode.hxx"
24 #include "AutoLocker.hxx"
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
32 #include <sys/types.h>
42 #include "YacsTrace.hxx"
44 using namespace YACS::ENGINE;
47 const char XmlNode::IMPL_NAME[]="XML";
48 const char XmlNode::KIND[]="xmlsh";
49 static YACS::BASES::Mutex MUTEX;
51 XmlNode::XmlNode(const XmlNode& other, ComposedNode *father)
52 : _script(other._script), ServiceNode(other, father)
54 _implementation=IMPL_NAME;
58 XmlNode::XmlNode(const std::string& name)
61 _implementation=IMPL_NAME;
64 Node *XmlNode::simpleClone(ComposedNode *father, bool editionOnly) const
66 return new XmlNode(*this,father);
69 void XmlNode::setRef(const std::string& ref)
71 //No component instance here
75 void XmlNode::setScript(const std::string& script)
80 std::string XmlNode::getScript() const
85 std::string XmlNode::getKind() const
90 void XmlNode::execute()
93 char dir[]="yacsXXXXXX";
94 // add a lock around mkdtemp (seems not thread safe)
96 YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&MUTEX);
99 GetTempPath(MAX_PATH+1, mdir);
100 CreateDirectory(mdir, NULL);
102 char* mdir=mkdtemp(dir);
106 perror("mkdtemp failed");
107 std::cerr << "Problem in mkdtemp " << dir << " " << mdir << std::endl;
108 throw Exception("Execution problem in mkdtemp");
111 std::string sdir(dir);
112 std::string input=sdir+"/input";
113 std::ofstream f(input.c_str());
114 f<<"<methodCall> <methodName>" << _method << "</methodName> <params>"<<std::endl;
115 DEBTRACE("---------------XmlNode::inputs---------------");
116 list<InputPort *>::iterator iter;
117 for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
119 InputXmlPort *p=(InputXmlPort *)*iter;
120 DEBTRACE("port name: " << p->getName());
121 DEBTRACE("port kind: " << p->edGetType()->kind());
122 const char* ob=p->getXml();
123 DEBTRACE("Xml: " << ob );
124 f<<"<param>" << ob << "</param>"<<std::endl;
126 f<<"</params>"<<std::endl;
127 f<<"</methodCall>"<<std::endl;
129 DEBTRACE("--------------XmlNode::calculation---------------" << _ref );
130 std::string call=sdir+"/run.sh";
131 std::ofstream run(call.c_str());
132 run << "#!/bin/sh" << std::endl;
133 run << "cd " << sdir << std::endl;
134 if(_ref[0]=='/' || _ref[0]=='~')
135 run << _ref << "> stdout 2>&1 " << std::endl;
137 run << "../"<<_ref << "> stdout 2>&1 " << std::endl;
138 //run << "cat stdout" << std::endl;
140 chmod(call.c_str(),00777);
142 std::string call2="/bin/sh "+call;
143 int ret=system(call2.c_str());
146 std::cerr << "Problem: " << ret << std::endl;
147 DEBTRACE("Problem: " << ret);
148 throw Exception("Execution problem");
150 std::string output=sdir+"/output";
152 doc = xmlReadFile(output.c_str(), NULL, 0);
155 DEBTRACE("Failed to parse " << output);
156 throw Exception("Execution problem");
159 cur = xmlDocGetRootElement(doc);
162 DEBTRACE("empty document " );
164 throw Exception("Execution problem");
166 if (xmlStrcmp(cur->name, (const xmlChar *) "methodResponse"))
168 DEBTRACE("document of the wrong type, root node != methodResponse");
170 throw Exception("Execution problem");
172 cur = cur->xmlChildrenNode;
173 xmlBufferPtr buf=xmlBufferCreate();
174 list<OutputPort *>::iterator iter2;
175 iter2 = _setOfOutputPort.begin();
177 p=(OutputXmlPort *)*iter2;
182 if ((!xmlStrcmp(cur->name, (const xmlChar *)"fault")))
184 DEBTRACE("exception in shell" );
186 throw Exception("Execution problem");
188 if ((!xmlStrcmp(cur->name, (const xmlChar *)"params")))
190 xmlNodePtr cur0 = cur->xmlChildrenNode;
193 if ((!xmlStrcmp(cur0->name, (const xmlChar *)"param")))
195 xmlNodePtr cur1 = cur0->xmlChildrenNode;
198 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"value")))
200 xmlNodePtr cur2=cur1->xmlChildrenNode;
203 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"int")))
206 if(getNumberOfOutputPorts()!=1)
211 throw Exception("Execution problem:mismatch in output numbers");
214 xmlNodeDump(buf,doc,cur1,0,0);
215 DEBTRACE(xmlBufferContent(buf));
216 p->put(xmlBufferContent(buf));
218 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"double")))
221 if(getNumberOfOutputPorts()!=1)
226 throw Exception("Execution problem:mismatch in output numbers");
229 xmlNodeDump(buf,doc,cur1,0,0);
230 DEBTRACE(xmlBufferContent(buf));
231 p->put(xmlBufferContent(buf));
233 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"string")))
236 if(getNumberOfOutputPorts()!=1)
241 throw Exception("Execution problem:mismatch in output port numbers");
244 xmlNodeDump(buf,doc,cur1,0,0);
245 DEBTRACE(xmlBufferContent(buf));
246 p->put(xmlBufferContent(buf));
248 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"boolean")))
251 if(getNumberOfOutputPorts()!=1)
256 throw Exception("Execution problem:mismatch in output port numbers");
259 xmlNodeDump(buf,doc,cur1,0,0);
260 DEBTRACE(xmlBufferContent(buf));
261 p->put(xmlBufferContent(buf));
263 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"objref")))
266 if(getNumberOfOutputPorts()!=1)
271 throw Exception("Execution problem:mismatch in output port numbers");
274 xmlNodeDump(buf,doc,cur1,0,0);
275 DEBTRACE(xmlBufferContent(buf));
276 p->put(xmlBufferContent(buf));
278 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"struct")))
281 if(getNumberOfOutputPorts()!=1)
286 throw Exception("Execution problem:mismatch in output port numbers");
289 xmlNodeDump(buf,doc,cur1,0,0);
290 DEBTRACE(xmlBufferContent(buf));
291 p->put(xmlBufferContent(buf));
293 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"array")))
295 //got a tuple of results or only one result (but a list)
296 if(getNumberOfOutputPorts()==1)
298 //It's a one result list
300 xmlNodeDump(buf,doc,cur1,0,0);
301 DEBTRACE(xmlBufferContent(buf));
302 p->put(xmlBufferContent(buf));
306 //It's a list of results
307 xmlNodePtr cur3=cur2->xmlChildrenNode;
310 if ((!xmlStrcmp(cur3->name, (const xmlChar *)"data")))
312 xmlNodePtr cur4=cur3->xmlChildrenNode;
315 if ((!xmlStrcmp(cur4->name, (const xmlChar *)"value")))
318 if(nres > getNumberOfOutputPorts())
323 throw Exception("Execution problem:mismatch in output port numbers");
326 xmlNodeDump(buf,doc,cur4,0,0);
327 DEBTRACE(xmlBufferContent(buf));
328 p=(OutputXmlPort *)*iter2;
329 p->put(xmlBufferContent(buf));