From cf1054852c45326180c6343349443bd617c30adc Mon Sep 17 00:00:00 2001 From: fgerlits Date: Fri, 7 Jan 2005 14:55:19 +0000 Subject: [PATCH] moved XmlRpcTools (and PlayLogEntry) over to the Core module --- livesupport/modules/core/etc/Makefile.in | 10 +- livesupport/modules/core/etc/configure.ac | 8 +- .../include/LiveSupport/Core/PlayLogEntry.h | 170 ++++++ .../include/LiveSupport/Core/XmlRpcTools.h | 358 ++++++++++++ livesupport/modules/core/src/XmlRpcTools.cxx | 529 ++++++++++++++++++ .../modules/core/src/XmlRpcToolsTest.cxx | 200 +++++++ .../modules/core/src/XmlRpcToolsTest.h | 133 +++++ 7 files changed, 1400 insertions(+), 8 deletions(-) create mode 100644 livesupport/modules/core/include/LiveSupport/Core/PlayLogEntry.h create mode 100644 livesupport/modules/core/include/LiveSupport/Core/XmlRpcTools.h create mode 100644 livesupport/modules/core/src/XmlRpcTools.cxx create mode 100644 livesupport/modules/core/src/XmlRpcToolsTest.cxx create mode 100644 livesupport/modules/core/src/XmlRpcToolsTest.h diff --git a/livesupport/modules/core/etc/Makefile.in b/livesupport/modules/core/etc/Makefile.in index e05862a95..33477227f 100644 --- a/livesupport/modules/core/etc/Makefile.in +++ b/livesupport/modules/core/etc/Makefile.in @@ -21,7 +21,7 @@ # # # Author : $Author: fgerlits $ -# Version : $Revision: 1.20 $ +# Version : $Revision: 1.21 $ # Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/etc/Makefile.in,v $ # # @configure_input@ @@ -117,7 +117,8 @@ CORE_LIB_OBJS = ${TMP_DIR}/UniqueId.o \ ${TMP_DIR}/Thread.o \ ${TMP_DIR}/LocalizedObject.o \ ${TMP_DIR}/LocalizedConfigurable.o \ - ${TMP_DIR}/Md5.o + ${TMP_DIR}/Md5.o \ + ${TMP_DIR}/XmlRpcTools.o TEST_RUNNER_OBJS = ${TMP_DIR}/TestRunner.o \ ${TMP_DIR}/UniqueIdTest.o \ @@ -130,7 +131,8 @@ TEST_RUNNER_OBJS = ${TMP_DIR}/TestRunner.o \ ${TMP_DIR}/ThreadTest.o \ ${TMP_DIR}/LocalizedObjectTest.o \ ${TMP_DIR}/LocalizedConfigurableTest.o \ - ${TMP_DIR}/Md5Test.o + ${TMP_DIR}/Md5Test.o \ + ${TMP_DIR}/XmlRpcToolsTest.o TEST_RUNNER_RES = ${TMP_DIR}/${PACKAGE_NAME}_root.res \ ${TMP_DIR}/${PACKAGE_NAME}_en.res \ @@ -138,7 +140,7 @@ TEST_RUNNER_RES = ${TMP_DIR}/${PACKAGE_NAME}_root.res \ ${TMP_DIR}/${PACKAGE_NAME}_jp.res TEST_RUNNER_LIBS = -l${CORE_LIB} ${ICU_LIBS} -lboost_date_time-gcc \ - -lcppunit -ldl + -lcppunit -ldl -lxmlrpc++ #------------------------------------------------------------------------------- diff --git a/livesupport/modules/core/etc/configure.ac b/livesupport/modules/core/etc/configure.ac index b0ed69686..1b57b84e9 100644 --- a/livesupport/modules/core/etc/configure.ac +++ b/livesupport/modules/core/etc/configure.ac @@ -20,8 +20,8 @@ dnl along with LiveSupport; if not, write to the Free Software dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA dnl dnl -dnl Author : $Author: maroy $ -dnl Version : $Revision: 1.7 $ +dnl Author : $Author: fgerlits $ +dnl Version : $Revision: 1.8 $ dnl Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/etc/configure.ac,v $ dnl----------------------------------------------------------------------------- @@ -35,14 +35,14 @@ dnl----------------------------------------------------------------------------- AC_INIT(Core, 1.0, bugs@campware.org) AC_PREREQ(2.59) AC_COPYRIGHT([Copyright (c) 2004 Media Development Loan Fund under the GNU GPL]) -AC_REVISION($Revision: 1.7 $) +AC_REVISION($Revision: 1.8 $) AC_CONFIG_SRCDIR(../src/UniqueId.cxx) AC_CONFIG_HEADERS(configure.h) AC_PROG_CXX() -AC_CHECK_HEADERS(getopt.h sys/time.h) +AC_CHECK_HEADERS(getopt.h sys/time.h time.h) PKG_CHECK_MODULES(LIBXMLPP,[libxml++-2.6 >= 2.6.0]) AC_SUBST(LIBXMLPP_CFLAGS) diff --git a/livesupport/modules/core/include/LiveSupport/Core/PlayLogEntry.h b/livesupport/modules/core/include/LiveSupport/Core/PlayLogEntry.h new file mode 100644 index 000000000..c518796f7 --- /dev/null +++ b/livesupport/modules/core/include/LiveSupport/Core/PlayLogEntry.h @@ -0,0 +1,170 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: fgerlits $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/PlayLogEntry.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef LiveSupport_Core_PlayLogEntry_h +#define LiveSupport_Core_PlayLogEntry_h + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include +#include +#include +#include + +#include "LiveSupport/Core/Ptr.h" +#include "LiveSupport/Core/UniqueId.h" + + +namespace LiveSupport { +namespace Core { + +using namespace boost::posix_time; +using namespace LiveSupport::Core; + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/** + * A class representing a play log entry. + * PlayLogEntries contain information about the audio clips played. + * + * @author $Author: fgerlits $ + * @version $Revision: 1.1 $ + */ +class PlayLogEntry +{ + private: + /** + * The unique id of the play log entry. + */ + Ptr::Ref id; + + /** + * The id of the audio clip referenced by this play log entry. + */ + Ptr::Ref audioClipId; + + /** + * The time this audio clip was played. + */ + Ptr::Ref timestamp; + + + public: + /** + * Default constructor. + */ + PlayLogEntry(void) throw () + { + } + + /** + * Create a play log entry by specifying all details. + * + * @param id the ID of the play log entry. + * @param audioClipId the ID of the audio clip logged + * @param timestamp the time this audio clip was played. + */ + PlayLogEntry(Ptr::Ref id, + Ptr::Ref audioClipId, + Ptr::Ref timestamp) throw() + { + this->id = id; + this->audioClipId = audioClipId; + this->timestamp = timestamp; + } + + /** + * A virtual destructor, as this class has virtual functions. + */ + virtual + ~PlayLogEntry(void) throw () + { + } + + /** + * Return the ID of the play log entry. + * + * @return the unique ID of the play log entry. + */ + Ptr::Ref + getId(void) const throw () + { + return id; + } + + /** + * Return the ID of the audio clip referenced by this entry. + * + * @return the unique ID of the audio clip. + */ + Ptr::Ref + getAudioClipId(void) const throw () + { + return audioClipId; + } + + /** + * Return the time this audio clip was played. + * + * @return the the time the audio clip was played. + */ + Ptr::Ref + getTimestamp(void) const throw () + { + return timestamp; + } + +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace Core +} // namespace LiveSupport + +#endif // LiveSupport_Core_PlayLogEntry_h + diff --git a/livesupport/modules/core/include/LiveSupport/Core/XmlRpcTools.h b/livesupport/modules/core/include/LiveSupport/Core/XmlRpcTools.h new file mode 100644 index 000000000..3e103b134 --- /dev/null +++ b/livesupport/modules/core/include/LiveSupport/Core/XmlRpcTools.h @@ -0,0 +1,358 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: fgerlits $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/XmlRpcTools.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef LiveSupport_Core_XmlRpcTools_h +#define LiveSupport_Core_XmlRpcTools_h + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include +#include +#include +#include +#include +#include + +#include "LiveSupport/Core/Ptr.h" +#include "LiveSupport/Core/SessionId.h" +#include "LiveSupport/Core/Playlist.h" +#include "LiveSupport/Core/ScheduleEntry.h" +#include "LiveSupport/Core/PlayLogEntry.h" + + +namespace LiveSupport { +namespace Core { + +using namespace LiveSupport; +using namespace LiveSupport::Core; + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/** + * A toolbox for converting between inner representations of classes + * and XmlRpcValues. Used by almost all XmlRpcServerMethod subclasses + * in the Scheduler. + * + * @author $Author: fgerlits $ + * @version $Revision: 1.1 $ + */ +class XmlRpcTools +{ + private: + + /** + * Convert a boost::posix_time::ptime to an XmlRpcValue + * + * @param ptime the ptime to convert + * @param xmlRpcValue the output parameter holding the value of + * the conversion. + */ + static void + ptimeToXmlRpcValue(Ptr::Ref ptime, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (); + + /** + * Convert a PlayLogEntry to an XmlRpcValue + * + * @param playLogEntry the PlayLogEntry to convert. + * @param xmlRpcValue the output parameter holding the result of + * the conversion. + */ + static void + playLogEntryToXmlRpcValue(Ptr::Ref playLogEntry, + XmlRpc::XmlRpcValue & returnValue) + throw (); + + public: + /** + * Extract the schedule entry id from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return a UniqueId that was found in the XML-RPC parameter. + * @exception std::invalid_argument if there was no UniqueId + * in xmlRpcValue + */ + static Ptr::Ref + extractScheduleEntryId(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Extract the playlist id from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return a UniqueId that was found in the XML-RPC parameter. + * @exception std::invalid_argument if there was no playlistId + * member in xmlRpcValue + */ + static Ptr::Ref + extractPlaylistId(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Extract the audio clip id from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return a UniqueId that was found in the XML-RPC parameter. + * @exception std::invalid_argument if there was no audioClipId + * member in xmlRpcValue + */ + static Ptr::Ref + extractAudioClipId(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Extract the relative offset from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return a time_duration that was found in the XML-RPC parameter. + * @exception std::invalid_argument if there was no relativeOffset + * member in xmlRpcValue + */ + static Ptr::Ref + extractRelativeOffset(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Convert a Playlist to an XmlRpcValue + * + * @param playlist the Playlist to convert. + * @param xmlRpcValue the output parameter holding the result of + * the conversion. + */ + static void + playlistToXmlRpcValue(Ptr::Ref playlist, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (); + + /** + * Convert a vector of Playlists to an XML-RPC return value. + * + * @param playlistVector a list of Playlists. + * @param returnValue the output parameter holding an XML-RPC + * representation of the list of Playlists. + */ + static void + playlistVectorToXmlRpcValue( + const Ptr::Ref> >::Ref playlistVector, + XmlRpc::XmlRpcValue & returnValue) + throw (); + + /** + * Convert an AudioClip to an XmlRpcValue + * + * @param audioClip the AudioClip to convert. + * @param xmlRpcValue the output parameter holding the result of + * the conversion. + */ + static void + audioClipToXmlRpcValue(Ptr::Ref audioClip, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (); + + /** + * Convert a vector of AudioClips to an XML-RPC return value. + * + * @param audioClipVector a list of AudioClips. + * @param returnValue the output parameter holding an XML-RPC + * representation of the list of AudioClips. + */ + static void + audioClipVectorToXmlRpcValue( + const Ptr::Ref> >::Ref audioClipVector, + XmlRpc::XmlRpcValue & returnValue) + throw (); + + /** + * Convert an error code, message pair to an XML-RPC fault response. + * This is done by throwing an XmlRpc::XmlRpcException. The client + * receives a fault response, and the return value is set to a + * { faultCode, faultString } structure holding the error code and + * message. + * + * @param errorCode the numerical code of the error. + * @param errorMessage a short English description of the error. + * @param xmlRpcValue remains here from an earlier version + * TODO: remove this later. + */ + static void + markError(int errorCode, const std::string errorMessage, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (XmlRpc::XmlRpcException); + + /** + * Convert the valid status of a playlist to an XmlRpcValue + * + * @param validStatus true if the playlist is valid, false otherwise. + * @param xmlRpcValue the output parameter holding the result of + * the conversion. + */ + static void + validStatusToXmlRpcValue(bool validStatus, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (); + + /** + * Extract the from time parameter from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return the time value for the from parameter + * @exception std::invalid_argument if there was no from parameter + * in xmlRpcValue + */ + static Ptr::Ref + extractFromTime(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Extract the to parameter from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return the time value for the to parameter + * @exception std::invalid_argument if there was no to parameter + * in xmlRpcValue + */ + static Ptr::Ref + extractToTime(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Extract the playtime from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return the playing time, as stored in the XML-RPC parameter + * @exception std::invalid_argument if there was no playtime + * in xmlRpcValue + */ + static Ptr::Ref + extractPlayschedule(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Extract the fade in time from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return a time_duration that was found in the XML-RPC parameter. + * @exception std::invalid_argument if there was no fadeIn + * member in xmlRpcValue + */ + static Ptr::Ref + extractFadeIn(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Extract the fade out time from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return a time_duration that was found in the XML-RPC parameter. + * @exception std::invalid_argument if there was no fadeOut + * member in xmlRpcValue + */ + static Ptr::Ref + extractFadeOut(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Convert a vector of ScheduleEntries to an XML-RPC return value. + * + * @param scheduleEntries a list of ScheduleEntries. + * @param returnValue the output parameter holding an XML-RPC + * representation of the suppied schedule entires. + */ + static void + scheduleEntriesToXmlRpcValue( + Ptr::Ref> >::Ref scheduleEntries, + XmlRpc::XmlRpcValue & returnValue) + throw (); + + /** + * Convert a schedule entry ID (a UniqueId) to an XmlRpcValue + * + * @param scheduleEntryId the UniqueId to convert. + * @param returnValue the output parameter holding the result of + * the conversion. + */ + static void + scheduleEntryIdToXmlRpcValue( + Ptr::Ref scheduleEntryId, + XmlRpc::XmlRpcValue & returnValue) throw (); + + /** + * Convert a vector of PlayLogEntries to an XML-RPC return value. + * + * @param playLogVector a list of PlayLogEntries. + * @param returnValue the output parameter holding an XML-RPC + * representation of the list of PlayLogEntries. + */ + static void + playLogVectorToXmlRpcValue( + Ptr::Ref> >::Ref + playLogVector, + XmlRpc::XmlRpcValue & returnValue) + throw (); + + /** + * Extract the session ID from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return a SessionId that was found in the XML-RPC parameter. + * @exception std::invalid_argument if there was no sessionId + * member in xmlRpcValue + */ + static Ptr::Ref + extractSessionId(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + +}; + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace Core +} // namespace LiveSupport + +#endif // LiveSupport_Core_XmlRpcTools_h + diff --git a/livesupport/modules/core/src/XmlRpcTools.cxx b/livesupport/modules/core/src/XmlRpcTools.cxx new file mode 100644 index 000000000..9d790ee61 --- /dev/null +++ b/livesupport/modules/core/src/XmlRpcTools.cxx @@ -0,0 +1,529 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: fgerlits $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/XmlRpcTools.cxx,v $ + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#ifdef HAVE_TIME_H +#include +#else +#error need time.h +#endif + + +#include +#include "LiveSupport/Core/TimeConversion.h" + +#include "LiveSupport/Core/XmlRpcTools.h" + + +using namespace boost; +using namespace boost::posix_time; + +using namespace LiveSupport; +using namespace LiveSupport::Core; + +/* =================================================== local data structures */ + +/*------------------------------------------------------------------------------ + * The name of the playlist ID member in the XML-RPC parameter structure + *----------------------------------------------------------------------------*/ +static const std::string playlistIdName = "playlistId"; + +/*------------------------------------------------------------------------------ + * The name of the audio clip ID member in the XML-RPC parameter structure + *----------------------------------------------------------------------------*/ +static const std::string audioClipIdName = "audioClipId"; + +/*------------------------------------------------------------------------------ + * The name of the relative offset member in the XML-RPC parameter structure + *----------------------------------------------------------------------------*/ +static const std::string relativeOffsetName = "relativeOffset"; + +/*------------------------------------------------------------------------------ + * The name of the from member in the XML-RPC parameter structure. + *----------------------------------------------------------------------------*/ +static const std::string fromTimeName = "from"; + +/*------------------------------------------------------------------------------ + * The name of the to member in the XML-RPC parameter structure. + *----------------------------------------------------------------------------*/ +static const std::string toTimeName = "to"; + +/*------------------------------------------------------------------------------ + * The name of the playlist id member in the XML-RPC parameter structure. + *----------------------------------------------------------------------------*/ +static const std::string scheduleEntryIdName = "scheduleEntryId"; + +/*------------------------------------------------------------------------------ + * The name of the playtime member in the XML-RPC parameter structure. + *----------------------------------------------------------------------------*/ +static const std::string playtimeName = "playtime"; + +/*------------------------------------------------------------------------------ + * The name of the fade in member in the XML-RPC parameter structure. + *----------------------------------------------------------------------------*/ +static const std::string fadeInName = "fadeIn"; + +/*------------------------------------------------------------------------------ + * The name of the fade out member in the XML-RPC parameter structure. + *----------------------------------------------------------------------------*/ +static const std::string fadeOutName = "fadeOut"; + +/*------------------------------------------------------------------------------ + * The name of the session ID member in the XML-RPC parameter structure + *----------------------------------------------------------------------------*/ +static const std::string sessionIdName = "sessionId"; + + +/* ================================================ local constants & macros */ + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Extract the schedule entry ID from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractScheduleEntryId( + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(scheduleEntryIdName) + || xmlRpcValue[scheduleEntryIdName].getType() + != XmlRpc::XmlRpcValue::TypeString) { + throw std::invalid_argument("missing or bad schedule entry ID " + "argument"); + } + + Ptr::Ref id(new UniqueId(std::string( + xmlRpcValue[scheduleEntryIdName] ))); + return id; +} + + +/*------------------------------------------------------------------------------ + * Extract the playlist ID from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractPlaylistId(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(playlistIdName) + || xmlRpcValue[playlistIdName].getType() + != XmlRpc::XmlRpcValue::TypeString) { + throw std::invalid_argument("missing or bad playlist ID argument"); + } + + Ptr::Ref id(new UniqueId(std::string( + xmlRpcValue[playlistIdName] ))); + return id; +} + + +/*------------------------------------------------------------------------------ + * Extract the audio clip ID from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractAudioClipId(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(audioClipIdName) + || xmlRpcValue[audioClipIdName].getType() + != XmlRpc::XmlRpcValue::TypeString) { + throw std::invalid_argument("missing or bad audio clip ID argument"); + } + + Ptr::Ref id(new UniqueId(std::string( + xmlRpcValue[audioClipIdName] ))); + return id; +} + + +/*------------------------------------------------------------------------------ + * Extract the relative offset from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractRelativeOffset(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(relativeOffsetName) + || xmlRpcValue[relativeOffsetName].getType() + != XmlRpc::XmlRpcValue::TypeInt) { + throw std::invalid_argument("missing relative offset argument"); + } + + Ptr::Ref relativeOffset(new time_duration(0,0, + int(xmlRpcValue[relativeOffsetName]), 0)); + return relativeOffset; +} + + +/*------------------------------------------------------------------------------ + * Convert a Playlist to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: playlistToXmlRpcValue( + Ptr::Ref playlist, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw () +{ + xmlRpcValue["id"] = std::string(*playlist->getId()); + xmlRpcValue["playlength"] = int(playlist->getPlaylength()->total_seconds()); +} + + +/*------------------------------------------------------------------------------ + * Convert a vector of Playlists into an XML-RPC value. + * This function returns an XML-RPC array of XML-RPC structures. + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: playlistVectorToXmlRpcValue( + const Ptr::Ref> >::Ref playlistVector, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + returnValue.setSize(playlistVector->size()); + // a call to setSize() makes sure it's an XML-RPC + // array + + std::vector::Ref>::const_iterator it = + playlistVector->begin(); + int arraySize = 0; + while (it != playlistVector->end()) { + Ptr::Ref playlist = *it; + XmlRpc::XmlRpcValue returnStruct; + playlistToXmlRpcValue(playlist, returnStruct); + returnValue[arraySize++] = returnStruct; + ++it; + } +} + + +/*------------------------------------------------------------------------------ + * Convert an AudioClip to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: audioClipToXmlRpcValue( + Ptr::Ref audioClip, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw () +{ + xmlRpcValue["id"] = std::string(*audioClip->getId()); + xmlRpcValue["playlength"] = int(audioClip->getPlaylength() + ->total_seconds()); +} + + +/*------------------------------------------------------------------------------ + * Convert a vector of AudioClips into an XML-RPC value. + * This function returns an XML-RPC array of XML-RPC structures. + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: audioClipVectorToXmlRpcValue( + const Ptr::Ref> >::Ref audioClipVector, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + returnValue.setSize(audioClipVector->size()); + // a call to setSize() makes sure it's an XML-RPC + // array + + std::vector::Ref>::const_iterator it = + audioClipVector->begin(); + int arraySize = 0; + while (it != audioClipVector->end()) { + Ptr::Ref audioClip = *it; + XmlRpc::XmlRpcValue returnStruct; + audioClipToXmlRpcValue(audioClip, returnStruct); + returnValue[arraySize++] = returnStruct; + ++it; + } +} + + +/*------------------------------------------------------------------------------ + * Convert an error code, error message pair to an XML-RPC fault response + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: markError(int errorCode, const std::string errorMessage, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (XmlRpc::XmlRpcException) +{ + throw XmlRpc::XmlRpcException(errorMessage, errorCode); +} + + +/*------------------------------------------------------------------------------ + * Convert the valid status of a playlist to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: validStatusToXmlRpcValue( + bool validStatus, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw () +{ + xmlRpcValue["valid"] = XmlRpc::XmlRpcValue(validStatus); +} + + +/*------------------------------------------------------------------------------ + * Extract the from time from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractFromTime( + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(fromTimeName) + || xmlRpcValue[fromTimeName].getType() + != XmlRpc::XmlRpcValue::TypeDateTime) { + throw std::invalid_argument("missing or bad 'from' time in " + "parameter structure"); + } + + struct tm time = (struct tm) xmlRpcValue[fromTimeName]; + return TimeConversion::tmToPtime(&time); +} + + +/*------------------------------------------------------------------------------ + * Extract the to time from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractToTime( + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(toTimeName) + || xmlRpcValue[toTimeName].getType() + != XmlRpc::XmlRpcValue::TypeDateTime) { + throw std::invalid_argument("missing or bad 'to' time in " + "parameter structure"); + } + + struct tm time = (struct tm) xmlRpcValue[toTimeName]; + return TimeConversion::tmToPtime(&time); +} + + +/*------------------------------------------------------------------------------ + * Convert a boost::posix_time::ptime to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: ptimeToXmlRpcValue( + Ptr::Ref ptimeParam, + XmlRpc::XmlRpcValue& xmlRpcValue) + throw () +{ + struct tm time; + Ptr::Ref myPtime(new ptime(*ptimeParam)); // get rid of const + + TimeConversion::ptimeToTm(myPtime, time); + xmlRpcValue = XmlRpc::XmlRpcValue(&time); +} + + +/*------------------------------------------------------------------------------ + * Convert a vector of ScheduleEntries into an XML-RPC value. + * This function returns an XML-RPC array of XML-RPC structures. + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: scheduleEntriesToXmlRpcValue( + Ptr::Ref> >::Ref scheduleEntries, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + returnValue.setSize(scheduleEntries->size()); + // a call to setSize() makes sure it's an XML-RPC + // array + + std::vector::Ref>::iterator it + = scheduleEntries->begin(); + int arraySize = 0; + while (it != scheduleEntries->end()) { + Ptr::Ref entry = *it; + XmlRpc::XmlRpcValue returnStruct; + returnStruct["id"] = std::string(*entry->getId()); + returnStruct["playlistId"] = std::string(*entry->getPlaylistId()); + + XmlRpc::XmlRpcValue time; + ptimeToXmlRpcValue(entry->getStartTime(), time); + returnStruct["start"] = time; + + ptimeToXmlRpcValue(entry->getEndTime(), time); + returnStruct["end"] = time; + + returnValue[arraySize++] = returnStruct; + ++it; + } +} + + +/*------------------------------------------------------------------------------ + * Extract the playtime from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractPlayschedule( + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(playtimeName) + || xmlRpcValue[playtimeName].getType() + != XmlRpc::XmlRpcValue::TypeDateTime) { + throw std::invalid_argument("missing or bad playtime in " + "parameter structure"); + } + + struct tm time = (struct tm) xmlRpcValue[playtimeName]; + return TimeConversion::tmToPtime(&time); +} + + +/*------------------------------------------------------------------------------ + * Extract the fade in time from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractFadeIn(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(fadeInName) + || xmlRpcValue[fadeInName].getType() + != XmlRpc::XmlRpcValue::TypeInt) { + throw std::invalid_argument("missing or bad 'fade in' argument"); + } + + Ptr::Ref fadeIn(new time_duration(0,0, + int(xmlRpcValue[fadeInName]), 0)); + return fadeIn; +} + + +/*------------------------------------------------------------------------------ + * Extract the fade out time from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractFadeOut(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(fadeOutName) + || xmlRpcValue[fadeOutName].getType() + != XmlRpc::XmlRpcValue::TypeInt) { + throw std::invalid_argument("missing or bad 'fade out' argument"); + } + + Ptr::Ref fadeOut(new time_duration(0,0, + int(xmlRpcValue[fadeOutName]), 0)); + return fadeOut; +} + + +/*------------------------------------------------------------------------------ + * Convert a schedule entry ID (a UniqueId) to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: scheduleEntryIdToXmlRpcValue( + Ptr::Ref scheduleEntryId, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + returnValue[scheduleEntryIdName] = std::string(*scheduleEntryId); +} + + +/*------------------------------------------------------------------------------ + * Convert a PlayLogEntry to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: playLogEntryToXmlRpcValue( + Ptr::Ref playLogEntry, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + returnValue["audioClipId"] = std::string(*playLogEntry->getAudioClipId()); + + XmlRpc::XmlRpcValue timestamp; + ptimeToXmlRpcValue(playLogEntry->getTimestamp(), timestamp); + returnValue["timestamp"] = timestamp; +} + + +/*------------------------------------------------------------------------------ + * Convert a vector of PlayLogEntries into an XML-RPC value. + * This function returns an XML-RPC array of XML-RPC structures. + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: playLogVectorToXmlRpcValue( + Ptr::Ref> >::Ref + playLogVector, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + returnValue.setSize(playLogVector->size()); + // a call to setSize() makes sure it's an XML-RPC + // array + + std::vector::Ref>::const_iterator it = + playLogVector->begin(); + int arraySize = 0; + while (it != playLogVector->end()) { + Ptr::Ref playLog = *it; + XmlRpc::XmlRpcValue returnStruct; + playLogEntryToXmlRpcValue(playLog, returnStruct); + returnValue[arraySize++] = returnStruct; + ++it; + } +} + + +/*------------------------------------------------------------------------------ + * Extract the session ID from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractSessionId( + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(sessionIdName) + || xmlRpcValue[sessionIdName].getType() + != XmlRpc::XmlRpcValue::TypeString) { + throw std::invalid_argument("missing or bad session ID argument"); + } + + Ptr::Ref id(new SessionId(std::string( + xmlRpcValue[sessionIdName] ))); + return id; +} + diff --git a/livesupport/modules/core/src/XmlRpcToolsTest.cxx b/livesupport/modules/core/src/XmlRpcToolsTest.cxx new file mode 100644 index 000000000..ad58c7eaf --- /dev/null +++ b/livesupport/modules/core/src/XmlRpcToolsTest.cxx @@ -0,0 +1,200 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: fgerlits $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/XmlRpcToolsTest.cxx,v $ + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#if HAVE_UNISTD_H +#include +#else +#error "Need unistd.h" +#endif + + +#include +#include +#include + +#include "LiveSupport/Core/XmlRpcTools.h" +#include "XmlRpcToolsTest.h" + + +using namespace LiveSupport::Core; + +using namespace std; +using namespace XmlRpc; + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + +CPPUNIT_TEST_SUITE_REGISTRATION(XmlRpcToolsTest); + +/** + * The name of the configuration file for the playlist. + */ +const std::string configFileName = "etc/playlist.xml"; + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Configure a Configurable with an XML file. + *----------------------------------------------------------------------------*/ +void +XmlRpcToolsTest :: configure( + Ptr::Ref configurable, + const std::string fileName) + throw (CPPUNIT_NS::Exception) +{ + try { + Ptr::Ref parser( + new xmlpp::DomParser(configFileName, true)); + const xmlpp::Document * document = parser->get_document(); + const xmlpp::Element * root = document->get_root_node(); + + configurable->configure(*root); + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL("semantic error in configuration file"); + } catch (xmlpp::exception &e) { + CPPUNIT_FAIL("error parsing configuration file"); + } +} + + +/*------------------------------------------------------------------------------ + * Set up the test environment + *----------------------------------------------------------------------------*/ +void +XmlRpcToolsTest :: setUp(void) throw () +{ +} + + +/*------------------------------------------------------------------------------ + * Clean up the test environment + *----------------------------------------------------------------------------*/ +void +XmlRpcToolsTest :: tearDown(void) throw () +{ +} + + +/*------------------------------------------------------------------------------ + * Just a very simple smoke test + *----------------------------------------------------------------------------*/ +void +XmlRpcToolsTest :: firstTest(void) + throw (CPPUNIT_NS::Exception) +{ + XmlRpcValue xmlRpcPlaylist; + XmlRpcValue xmlRpcAudioClip; + Ptr::Ref playlist = Ptr::Ref(new Playlist); + Ptr::Ref audioClip; + + // set up a playlist instance + configure(playlist, configFileName); + audioClip = playlist->begin()->second->getAudioClip(); + + // run the packing methods + XmlRpcTools :: playlistToXmlRpcValue(playlist, xmlRpcPlaylist); + XmlRpcTools :: audioClipToXmlRpcValue(audioClip, xmlRpcAudioClip); + + CPPUNIT_ASSERT(xmlRpcPlaylist.hasMember("id")); + CPPUNIT_ASSERT(xmlRpcPlaylist["id"].getType() == XmlRpcValue::TypeString); + CPPUNIT_ASSERT(std::string(xmlRpcPlaylist["id"]) == "0000000000000001"); + + CPPUNIT_ASSERT(xmlRpcPlaylist.hasMember("playlength")); + CPPUNIT_ASSERT(xmlRpcPlaylist["playlength"].getType() + == XmlRpcValue::TypeInt); + CPPUNIT_ASSERT(int(xmlRpcPlaylist["playlength"]) == 34); + + CPPUNIT_ASSERT(xmlRpcAudioClip.hasMember("id")); + CPPUNIT_ASSERT(xmlRpcAudioClip["id"].getType() == XmlRpcValue::TypeString); + CPPUNIT_ASSERT(std::string(xmlRpcAudioClip["id"]) == "0000000000010001"); + + CPPUNIT_ASSERT(xmlRpcAudioClip.hasMember("playlength")); + CPPUNIT_ASSERT(xmlRpcAudioClip["playlength"].getType() + == XmlRpcValue::TypeInt); + CPPUNIT_ASSERT(int(xmlRpcAudioClip["playlength"]) == 11); + + XmlRpcValue xmlRpcPlaylistId; + Ptr::Ref playlistId(new UniqueId(rand())); + Ptr::Ref audioClipId(new UniqueId(rand())); + Ptr::Ref relativeOffset(new time_duration(0,0,rand(),0)); + + xmlRpcPlaylistId["playlistId"] = std::string(*playlistId); + xmlRpcPlaylistId["audioClipId"] = std::string(*audioClipId); + xmlRpcPlaylistId["relativeOffset"] = relativeOffset->total_seconds(); + + // run the unpacking methods + Ptr::Ref newPlaylistId; + Ptr::Ref newAudioClipId; + Ptr::Ref newRelativeOffset; + try { + newPlaylistId = XmlRpcTools::extractPlaylistId(xmlRpcPlaylistId); + newAudioClipId = XmlRpcTools::extractAudioClipId(xmlRpcPlaylistId); + newRelativeOffset = XmlRpcTools::extractRelativeOffset(xmlRpcPlaylistId); + } + catch (std::invalid_argument &e) { + CPPUNIT_FAIL(e.what()); + } + + CPPUNIT_ASSERT(*playlistId == *newPlaylistId); + CPPUNIT_ASSERT(*audioClipId == *newAudioClipId); + CPPUNIT_ASSERT(*relativeOffset == *newRelativeOffset); +} + + +/*------------------------------------------------------------------------------ + * Testing markError() + *----------------------------------------------------------------------------*/ +void +XmlRpcToolsTest :: errorTest(void) + throw (CPPUNIT_NS::Exception) +{ + XmlRpcValue xmlRpcValue; + + try { + XmlRpcTools :: markError(42, "this is an error", xmlRpcValue); + CPPUNIT_FAIL("did not throw exception in markError()"); + } + catch (XmlRpc::XmlRpcException &e) { + CPPUNIT_ASSERT(e.getCode() == 42); + CPPUNIT_ASSERT(e.getMessage() == "this is an error"); + } +} + diff --git a/livesupport/modules/core/src/XmlRpcToolsTest.h b/livesupport/modules/core/src/XmlRpcToolsTest.h new file mode 100644 index 000000000..35f91c79a --- /dev/null +++ b/livesupport/modules/core/src/XmlRpcToolsTest.h @@ -0,0 +1,133 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: fgerlits $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/XmlRpcToolsTest.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef XmlRpcToolsTest_h +#define XmlRpcToolsTest_h + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include + + +namespace LiveSupport { +namespace Core { + +using namespace LiveSupport; +using namespace LiveSupport::Core; + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/** + * Unit test for the XmlRpcTools class. + * + * @author $Author: fgerlits $ + * @version $Revision: 1.1 $ + * @see XmlRpcTools + */ +class XmlRpcToolsTest : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(XmlRpcToolsTest); + CPPUNIT_TEST(firstTest); + CPPUNIT_TEST(errorTest); + CPPUNIT_TEST_SUITE_END(); + + private: + + /** + * Configure a configurable with an XML file. + * + * @param configurable configure this + * @param fileName the name of the XML file to configure with. + * @exception CPPUNIT_NS::Exception on XML parsing errors. + */ + void + configure(Ptr::Ref configurable, + std::string fileName) + throw (CPPUNIT_NS::Exception); + + protected: + + /** + * A simple test. + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + firstTest(void) throw (CPPUNIT_NS::Exception); + + /** + * Testing the method for error message packaging. + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + errorTest(void) throw (CPPUNIT_NS::Exception); + + + public: + + /** + * Set up the environment for the test case. + */ + void + setUp(void) throw (); + + /** + * Clean up the environment after the test case. + */ + void + tearDown(void) throw (); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace Scheduler +} // namespace LiveSupport + +#endif // XmlRpcToolsTest_h +