From a08d2b4dcd505ff4594b597b1a95845849214aaf Mon Sep 17 00:00:00 2001 From: maroy Date: Sat, 8 Jan 2005 12:39:33 +0000 Subject: [PATCH] added some more functions --- .../include/LiveSupport/Core/XmlRpcTools.h | 128 +++++++++++- livesupport/modules/core/src/XmlRpcTools.cxx | 195 +++++++++++++++++- 2 files changed, 306 insertions(+), 17 deletions(-) diff --git a/livesupport/modules/core/include/LiveSupport/Core/XmlRpcTools.h b/livesupport/modules/core/include/LiveSupport/Core/XmlRpcTools.h index 3e103b134..d883883aa 100644 --- a/livesupport/modules/core/include/LiveSupport/Core/XmlRpcTools.h +++ b/livesupport/modules/core/include/LiveSupport/Core/XmlRpcTools.h @@ -21,8 +21,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - Author : $Author: fgerlits $ - Version : $Revision: 1.1 $ + Author : $Author: maroy $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/XmlRpcTools.h,v $ ------------------------------------------------------------------------------*/ @@ -73,8 +73,8 @@ using namespace LiveSupport::Core; * and XmlRpcValues. Used by almost all XmlRpcServerMethod subclasses * in the Scheduler. * - * @author $Author: fgerlits $ - * @version $Revision: 1.1 $ + * @author $Author: maroy $ + * @version $Revision: 1.2 $ */ class XmlRpcTools { @@ -117,6 +117,18 @@ class XmlRpcTools extractScheduleEntryId(XmlRpc::XmlRpcValue & xmlRpcValue) throw (std::invalid_argument); + /** + * Extract the generic '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 + extractId(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + /** * Extract the playlist id from the XML-RPC parameters. * @@ -233,10 +245,10 @@ class XmlRpcTools throw (); /** - * Extract the from time parameter from the XML-RPC parameters. + * 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 + * @return the time value for the 'from' parameter * @exception std::invalid_argument if there was no from parameter * in xmlRpcValue */ @@ -245,10 +257,10 @@ class XmlRpcTools throw (std::invalid_argument); /** - * Extract the to parameter from the XML-RPC parameters. + * 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 + * @return the time value for the 'to' parameter * @exception std::invalid_argument if there was no to parameter * in xmlRpcValue */ @@ -256,6 +268,56 @@ class XmlRpcTools extractToTime(XmlRpc::XmlRpcValue & xmlRpcValue) throw (std::invalid_argument); + /** + * Extract the 'start' parameter from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return the time value for the 'start' parameter + * @exception std::invalid_argument if there was no to parameter + * in xmlRpcValue + */ + static Ptr::Ref + extractStartTime(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Extract the 'end' parameter from the XML-RPC parameters. + * + * @param xmlRpcValue the XML-RPC parameter to extract from. + * @return the time value for the 'end' parameter + * @exception std::invalid_argument if there was no to parameter + * in xmlRpcValue + */ + static Ptr::Ref + extractEndTime(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument); + + /** + * Add a 'from' time value to an XmlRpcValue. + * + * @param from the 'from' time value to add. + * @param returnValue an output parameter, which has the + * 'from' time added after the function returns. + */ + static void + fromTimeToXmlRpcValue( + Ptr::Ref from, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (); + + /** + * Add a 'to' time value to an XmlRpcValue. + * + * @param to the 'to' time value to add. + * @param returnValue an output parameter, which has the + * 'to' time added after the function returns. + */ + static void + toTimeToXmlRpcValue( + Ptr::Ref to, + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (); + /** * Extract the playtime from the XML-RPC parameters. * @@ -305,6 +367,19 @@ class XmlRpcTools XmlRpc::XmlRpcValue & returnValue) throw (); + /** + * Convert an XmlRpcValue array, holding schedule entries, + * to a vector of ScheduleEntry object references. + * + * @param xmlRpcValue the XML-RPC array holding the schedule entry + * data + * @return a vector of ScheduleEntry object references, holding + * the same data. + */ + static Ptr::Ref> >::Ref + extractScheduleEntries(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (); + /** * Convert a schedule entry ID (a UniqueId) to an XmlRpcValue * @@ -317,6 +392,43 @@ class XmlRpcTools Ptr::Ref scheduleEntryId, XmlRpc::XmlRpcValue & returnValue) throw (); + /** + * Add a session id to an XmlRpcValue + * + * @param sessionId the session id to add to the XmlRpcValue + * @param returnValue an output parameter, which has the + * session id added after the function returns. + */ + static void + sessionIdToXmlRpcValue( + Ptr::Ref sessionId, + XmlRpc::XmlRpcValue & returnValue) throw (); + + /** + * Add a playlist id to an XmlRpcValue + * + * @param playlist the playlist idt o add to the XmlRpcValue + * @param returnValue an output parameter, which has the + * playlist id added after the function returns. + */ + static void + playlistIdToXmlRpcValue( + Ptr::Ref playlistId, + XmlRpc::XmlRpcValue & returnValue) throw (); + + /** + * Add a playtime value to an XmlRpcValue. + * + * @param playtime the playtime to add to the XmlRpcValue + * @param returnValue an output parameter, which has the + * playtime added after the function returns. + */ + static void + playtimeToXmlRpcValue( + Ptr::Ref playtime, + XmlRpc::XmlRpcValue & returnValue) + throw (); + /** * Convert a vector of PlayLogEntries to an XML-RPC return value. * diff --git a/livesupport/modules/core/src/XmlRpcTools.cxx b/livesupport/modules/core/src/XmlRpcTools.cxx index 9d790ee61..c4d322e08 100644 --- a/livesupport/modules/core/src/XmlRpcTools.cxx +++ b/livesupport/modules/core/src/XmlRpcTools.cxx @@ -21,8 +21,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - Author : $Author: fgerlits $ - Version : $Revision: 1.1 $ + Author : $Author: maroy $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/XmlRpcTools.cxx,v $ ------------------------------------------------------------------------------*/ @@ -54,6 +54,11 @@ using namespace LiveSupport::Core; /* =================================================== local data structures */ +/*------------------------------------------------------------------------------ + * The name of the generic ID member in the XML-RPC parameter structure + *----------------------------------------------------------------------------*/ +static const std::string idName = "id"; + /*------------------------------------------------------------------------------ * The name of the playlist ID member in the XML-RPC parameter structure *----------------------------------------------------------------------------*/ @@ -80,7 +85,17 @@ static const std::string fromTimeName = "from"; static const std::string toTimeName = "to"; /*------------------------------------------------------------------------------ - * The name of the playlist id member in the XML-RPC parameter structure. + * The name of the start member in the XML-RPC parameter structure. + *----------------------------------------------------------------------------*/ +static const std::string startTimeName = "start"; + +/*------------------------------------------------------------------------------ + * The name of the end member in the XML-RPC parameter structure. + *----------------------------------------------------------------------------*/ +static const std::string endTimeName = "end"; + +/*------------------------------------------------------------------------------ + * The name of the schedule entry id member in the XML-RPC parameter structure. *----------------------------------------------------------------------------*/ static const std::string scheduleEntryIdName = "scheduleEntryId"; @@ -134,6 +149,24 @@ XmlRpcTools :: extractScheduleEntryId( } +/*------------------------------------------------------------------------------ + * Extract the generic ID from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractId(XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(idName) + || xmlRpcValue[idName].getType() + != XmlRpc::XmlRpcValue::TypeString) { + throw std::invalid_argument("missing or bad ID argument"); + } + + Ptr::Ref id(new UniqueId(std::string(xmlRpcValue[idName]))); + return id; +} + + /*------------------------------------------------------------------------------ * Extract the playlist ID from an XML-RPC function call parameter *----------------------------------------------------------------------------*/ @@ -300,7 +333,7 @@ XmlRpcTools :: validStatusToXmlRpcValue( /*------------------------------------------------------------------------------ - * Extract the from time from an XML-RPC function call parameter + * Extract the 'from' time from an XML-RPC function call parameter *----------------------------------------------------------------------------*/ Ptr::Ref XmlRpcTools :: extractFromTime( @@ -320,7 +353,7 @@ XmlRpcTools :: extractFromTime( /*------------------------------------------------------------------------------ - * Extract the to time from an XML-RPC function call parameter + * Extract the 'to' time from an XML-RPC function call parameter *----------------------------------------------------------------------------*/ Ptr::Ref XmlRpcTools :: extractToTime( @@ -339,6 +372,46 @@ XmlRpcTools :: extractToTime( } +/*------------------------------------------------------------------------------ + * Extract the 'start' time from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractStartTime( + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(startTimeName) + || xmlRpcValue[startTimeName].getType() + != XmlRpc::XmlRpcValue::TypeDateTime) { + throw std::invalid_argument("missing or bad 'start' time in " + "parameter structure"); + } + + struct tm time = (struct tm) xmlRpcValue[startTimeName]; + return TimeConversion::tmToPtime(&time); +} + + +/*------------------------------------------------------------------------------ + * Extract the 'end' time from an XML-RPC function call parameter + *----------------------------------------------------------------------------*/ +Ptr::Ref +XmlRpcTools :: extractEndTime( + XmlRpc::XmlRpcValue & xmlRpcValue) + throw (std::invalid_argument) +{ + if (!xmlRpcValue.hasMember(endTimeName) + || xmlRpcValue[endTimeName].getType() + != XmlRpc::XmlRpcValue::TypeDateTime) { + throw std::invalid_argument("missing or bad 'end' time in " + "parameter structure"); + } + + struct tm time = (struct tm) xmlRpcValue[endTimeName]; + return TimeConversion::tmToPtime(&time); +} + + /*------------------------------------------------------------------------------ * Convert a boost::posix_time::ptime to an XmlRpcValue *----------------------------------------------------------------------------*/ @@ -376,15 +449,15 @@ XmlRpcTools :: scheduleEntriesToXmlRpcValue( while (it != scheduleEntries->end()) { Ptr::Ref entry = *it; XmlRpc::XmlRpcValue returnStruct; - returnStruct["id"] = std::string(*entry->getId()); - returnStruct["playlistId"] = std::string(*entry->getPlaylistId()); + returnStruct[idName] = std::string(*entry->getId()); + returnStruct[playlistIdName] = std::string(*entry->getPlaylistId()); XmlRpc::XmlRpcValue time; ptimeToXmlRpcValue(entry->getStartTime(), time); - returnStruct["start"] = time; + returnStruct[startTimeName] = time; ptimeToXmlRpcValue(entry->getEndTime(), time); - returnStruct["end"] = time; + returnStruct[endTimeName] = time; returnValue[arraySize++] = returnStruct; ++it; @@ -392,6 +465,39 @@ XmlRpcTools :: scheduleEntriesToXmlRpcValue( } +/*------------------------------------------------------------------------------ + * Convert an XML-RPC value, holding an array of schedule entries + * to a vector holding the same ScheduleEntry object. + *----------------------------------------------------------------------------*/ +Ptr::Ref> >::Ref +XmlRpcTools :: extractScheduleEntries( + XmlRpc::XmlRpcValue & xmlRpcValue) + throw () +{ + Ptr::Ref> >::Ref entries; + entries.reset(new std::vector::Ref>()); + + int nEntries = xmlRpcValue.size(); + for (int i = 0; i < nEntries; ++i) { + XmlRpc::XmlRpcValue & entryValue = xmlRpcValue[i]; + + Ptr::Ref entryId = extractId(entryValue); + Ptr::Ref playlistId = extractPlaylistId(entryValue); + Ptr::Ref start = extractStartTime(entryValue); + Ptr::Ref end = extractEndTime(entryValue); + + Ptr::Ref entry(new ScheduleEntry(entryId, + playlistId, + start, + end)); + + entries->push_back(entry); + } + + return entries; +} + + /*------------------------------------------------------------------------------ * Extract the playtime from an XML-RPC function call parameter *----------------------------------------------------------------------------*/ @@ -463,6 +569,77 @@ XmlRpcTools :: scheduleEntryIdToXmlRpcValue( } +/*------------------------------------------------------------------------------ + * Add a session ID to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: sessionIdToXmlRpcValue( + Ptr::Ref sessionId, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + returnValue[sessionIdName] = sessionId->getId(); +} + + +/*------------------------------------------------------------------------------ + * Add a playlist ID to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: playlistIdToXmlRpcValue( + Ptr::Ref playlistId, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + returnValue[playlistIdName] = std::string(*playlistId); +} + + +/*------------------------------------------------------------------------------ + * Add a playtime value to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: playtimeToXmlRpcValue( + Ptr::Ref playtime, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + XmlRpc::XmlRpcValue timestamp; + ptimeToXmlRpcValue(playtime, timestamp); + returnValue[playtimeName] = timestamp; +} + + +/*------------------------------------------------------------------------------ + * Add a 'from' time value to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: fromTimeToXmlRpcValue( + Ptr::Ref from, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + XmlRpc::XmlRpcValue timestamp; + ptimeToXmlRpcValue(from, timestamp); + returnValue[fromTimeName] = timestamp; +} + + +/*------------------------------------------------------------------------------ + * Add a 'to' time value to an XmlRpcValue + *----------------------------------------------------------------------------*/ +void +XmlRpcTools :: toTimeToXmlRpcValue( + Ptr::Ref to, + XmlRpc::XmlRpcValue & returnValue) + throw () +{ + XmlRpc::XmlRpcValue timestamp; + ptimeToXmlRpcValue(to, timestamp); + returnValue[toTimeName] = timestamp; +} + + /*------------------------------------------------------------------------------ * Convert a PlayLogEntry to an XmlRpcValue *----------------------------------------------------------------------------*/