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

mmsxmlserverinterface.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 
00035 #ifdef LIBXML_READER_ENABLED
00036 
00037 #include "mmsbase/mmsxmlserverinterface.h"
00038 #include "mmsbase/mmsevent.h"
00039 #include "mmstools/tools.h"
00040 
00041 MMSXMLServerInterface::MMSXMLServerInterface() : MMSServerInterface("MMSXMLServerInterface") {
00042     LIBXML_TEST_VERSION;
00043 }
00044 
00045 MMSXMLServerInterface::~MMSXMLServerInterface() {
00046 }
00047 
00048 bool MMSXMLServerInterface::processRequest(string *request, string *answer) {
00049     if(!request || !answer) {
00050         DEBUGMSG("MMSXMLServerInterface","processRequest() error in cmdline");
00051         return false;
00052     }
00053     DEBUGMSG("MMSXMLServerInterface","got request %s", request->c_str());
00054 
00055     *answer = "";
00056 
00057     DEBUGMSG("MMSXMLServerInterface","new reader");
00058     xmlDocPtr doc = xmlReadMemory(request->c_str(),request->length(),"memory.xml",NULL,0);
00059 
00060     if(!doc) {
00061         *answer = "<ret error=\"Problems with xml request.\"/>";
00062         DEBUGMSG("MMSXMLServerInterface", "Error initializing xmlReader()");
00063         return false;
00064     }
00065 
00066     DEBUGMSG("MMSXMLServerInterface","throughdoc");
00067     throughDoc(doc, answer);
00068     if(*answer == "")
00069             *answer = "<ret error=\"Unknown error.\"/>";
00070 
00071     xmlFreeDoc(doc);
00072 
00073     return true;
00074 }
00075 
00076 bool MMSXMLServerInterface::throughDoc(xmlDocPtr doc, string *answer) {
00077 
00078     if(!doc|| !answer)  {
00079         DEBUGMSG("MMSXMLServerInterface","throughdoc, error in cmdline");
00080         return false;
00081     }
00082 
00083     xmlNodePtr root =  xmlDocGetRootElement(doc);
00084     if(!root)
00085         return false;
00086 
00087     if(xmlStrEqual(root->name, (const xmlChar*)"func")==0) {
00088         DEBUGMSG("MMSXMLServerInterface", "The root element must be <func> and not <%s>.", root->name);
00089         return false;
00090     }
00091 
00092     return throughFunc(root, answer);
00093 }
00094 
00095 bool MMSXMLServerInterface::throughFunc(xmlNodePtr node, string *answer) {
00096     xmlChar *name;
00097 
00098     if(!node|| !answer) return false;
00099 
00100     /* get name of function to be called */
00101     name = xmlGetProp(node, (const xmlChar*)"name");
00102     if(!name) {
00103         /* function not specified */
00104         *answer = "<ret error=\"Function not specified.\"/>";
00105         DEBUGMSG("MMSXMLServerInterface", "Function not specified.");
00106         return false;
00107     }
00108 
00109     /* check function */
00110     if(!xmlStrEqual(name, (const xmlChar*)"SendEvent")) {
00111         /* unknown function */
00112         *answer = "<ret error=\"Unknown function '" + string((const char*)name) + "'.\"/>";
00113         DEBUGMSG("MMSXMLServerInterface", "Unknown function '%s'.", name);
00114         return false;
00115     }
00116 
00117     return funcSendEvent(node, answer);
00118 }
00119 
00120 bool MMSXMLServerInterface::funcSendEvent(xmlNodePtr node, string *answer) {
00121     xmlChar  *heading, *name, *value;/**pluginid*/
00122     xmlTextReader *reader;
00123     MMSEvent *event;
00124 
00125     if(!node || !answer) return false;
00126 
00127     /* get attributes */
00128     heading  = xmlGetProp(node, (const xmlChar*)"heading");
00129     reader = xmlReaderWalker(node->doc);
00130     //pluginid = xmlTextReaderGetAttribute(reader, (const xmlChar*)"pluginid");
00131 
00132     if(!heading /*|| !pluginid*/) return false;
00133 
00134     event = new MMSEvent((const char*)heading);
00135 
00136     /* through <func/> childs */
00137     while(xmlTextReaderRead(reader)) {
00138         name = (xmlChar*)xmlTextReaderConstName(reader);
00139         if(name && xmlStrEqual(name, (const xmlChar*)"param")) {
00140             while(xmlTextReaderMoveToNextAttribute(reader)) {
00141                 name  = xmlTextReaderName(reader);
00142                 value = xmlTextReaderValue(reader);
00143                 event->setData((const char*)name, (const char*)value);
00144                 xmlFree(name);
00145                 xmlFree(value);
00146             }
00147         }
00148     }
00149     /* build answer */
00150     *answer = "<ret/>";
00151 
00152     event->send();
00153 
00154 
00155     return true;
00156 }
00157 
00158 #endif /* LIBXML_READER_ENABLED */

Generated by doxygen