00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <libxml/xmlversion.h>
00034 #include <string.h>
00035
00036 #ifdef LIBXML_READER_ENABLED
00037 #include "mmsbase/mmsxmlclientinterface.h"
00038 #include <libxml/parser.h>
00039 #include <libxml/tree.h>
00040
00041 #include "mmstools/tools.h"
00042
00043 MMSXMLClientInterface::MMSXMLClientInterface(string host, unsigned int port) {
00044 LIBXML_TEST_VERSION;
00045
00046 this->tcl = new MMSTCPClient(host, port);
00047 }
00048
00049 bool MMSXMLClientInterface::parseAnswer(string *answer, int *rc, string *error) {
00050 bool ret = false;
00051 xmlDocPtr doc;
00052
00053 doc = xmlReadMemory(answer->c_str(),answer->length(),"memory.xml",NULL,0);
00054
00055 if(!doc) {
00056 DEBUGMSG("MMSXMLClientInterface", "Error initializing doc()");
00057 return false;
00058 }
00059 if(checkRoot(doc, rc, error)) {
00060 ret = true;
00061 }
00062
00063 xmlFreeDoc(doc);
00064
00065 return ret;
00066 }
00067
00068 bool MMSXMLClientInterface::checkRoot(xmlDocPtr doc, int *rc, string *error) {
00069
00070
00071 if (!doc)
00072 return false;
00073 #if 0
00074
00075 name = (xmlChar*)xmlTextReaderConstName(reader);
00076 if(!name || !xmlStrEqual(name, (const xmlChar*)"ret")) {
00077 DEBUGMSG("MMSXMLClientInterface", "The root element must be <ret> and not <%s>.", name);
00078 return false;
00079 }
00080
00081
00082 *rc = 0;
00083 attr = xmlTextReaderGetAttribute(reader, (const xmlChar*)"rc");
00084 if(attr) *rc = atoi((const char*)attr);
00085 xmlFree(attr);
00086
00087
00088 *error = "";
00089 attr = xmlTextReaderGetAttribute(reader, (const xmlChar*)"error");
00090 if(attr) *error = strdup((const char*)attr);
00091 xmlFree(attr);
00092 #endif
00093 return true;
00094 }
00095
00096 bool MMSXMLClientInterface::funcSendEvent(string heading, int pluginid, int *rc, string *error) {
00097 return funcSendEvent(heading, NULL, pluginid, rc, error);
00098 }
00099
00100 bool MMSXMLClientInterface::funcSendEvent(string heading, map<string, string> *params, int pluginid, int *rc, string *error) {
00101 string rbuf, abuf;
00102
00103
00104 rbuf = "<func name=\"SendEvent\" heading=\"" + heading + "\"";
00105 if (pluginid>=0) rbuf+= " pluginid=\"" + iToStr(pluginid) + "\"";
00106 if (NULL == params || 0 == params->size()) {
00107 rbuf+= "/>";
00108 }
00109 else {
00110 rbuf +=">";
00111 for (map<string, string>::iterator iter = params->begin(); iter != params->end(); iter++) {
00112 rbuf += "<param "+iter->first+"=\""+XMLencode(iter->second)+"\" />";
00113 }
00114 rbuf += "</func>";
00115 }
00116
00117
00118 if(!tcl->connectToServer()) {
00119 DEBUGMSG("MMSBASE", "connection to server failed");
00120 }
00121
00122 tcl->sendAndReceive(rbuf, &abuf);
00123
00124 DEBUGMSG("MMSBASE", "got response %s", abuf.c_str());
00125
00126 if(parseAnswer(&abuf, rc, error)) {
00127
00128
00129 return true;
00130 }
00131
00132 return false;
00133 }
00134
00135 #endif
00136