Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsxmlclientinterface.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
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     //xmlChar *name, *attr;
00070 
00071     if (!doc)
00072         return false;
00073 #if 0
00074     /* check root element */
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     /* get attribute rc */
00082     *rc = 0;
00083     attr = xmlTextReaderGetAttribute(reader, (const xmlChar*)"rc");
00084     if(attr) *rc = atoi((const char*)attr);
00085     xmlFree(attr);
00086 
00087     /* get attribute error */
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     /* build request */
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     /* call server */
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     /* parse answer */
00126     if(parseAnswer(&abuf, rc, error)) {
00127         /* parse for more values here */
00128 
00129         return true;
00130     }
00131 
00132     return false;
00133 }
00134 
00135 #endif /* LIBXML_READER_ENABLED */
00136 

Generated by doxygen