part 2 of the new acquire/release methods

This commit is contained in:
fgerlits 2004-11-09 16:54:03 +00:00
parent 0e0d7335a1
commit 830325ca92
6 changed files with 144 additions and 83 deletions

View File

@ -21,8 +21,8 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $ Author : $Author: fgerlits $
Version : $Revision: 1.9 $ Version : $Revision: 1.10 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClient.cxx,v $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClient.cxx,v $
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -33,6 +33,12 @@
#include "configure.h" #include "configure.h"
#endif #endif
#if HAVE_UNISTD_H
#include <unistd.h>
#else
#error "Need unistd.h"
#endif
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include "TestStorageClient.h" #include "TestStorageClient.h"
@ -55,7 +61,7 @@ const std::string TestStorageClient::configElementNameStr = "testStorage";
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* The path to the local temp storage * The path to the local temp storage
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
const std::string TestStorageClient::localTempStoragePath = "tmp/"; const std::string TestStorageClient::localTempStoragePath = "var/";
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* The XML version used to create the SMIL file. * The XML version used to create the SMIL file.
@ -192,7 +198,7 @@ TestStorageClient :: getPlaylist(Ptr<const UniqueId>::Ref id) const
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* Acquire resources for a playlist. * Acquire resources for a playlist.
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
Ptr<std::string>::Ref Ptr<Playlist>::Ref
TestStorageClient :: acquirePlaylist(Ptr<const UniqueId>::Ref id) const TestStorageClient :: acquirePlaylist(Ptr<const UniqueId>::Ref id) const
throw (std::logic_error) throw (std::logic_error)
{ {
@ -202,8 +208,11 @@ TestStorageClient :: acquirePlaylist(Ptr<const UniqueId>::Ref id) const
throw std::invalid_argument("no such playlist"); throw std::invalid_argument("no such playlist");
} }
Ptr<Playlist>::Ref playlist = playlistMapIt->second; Ptr<Playlist>::Ref oldPlaylist = playlistMapIt->second;
Ptr<time_duration>::Ref playlength(new time_duration(
*(oldPlaylist->getPlaylength()) ));
Ptr<Playlist>::Ref newPlaylist(new Playlist(UniqueId::generateId(),
playlength));
Ptr<xmlpp::Document>::Ref Ptr<xmlpp::Document>::Ref
smilDocument(new xmlpp::Document(xmlVersion)); smilDocument(new xmlpp::Document(xmlVersion));
xmlpp::Element * smilRootNode xmlpp::Element * smilRootNode
@ -218,25 +227,39 @@ TestStorageClient :: acquirePlaylist(Ptr<const UniqueId>::Ref id) const
xmlpp::Element * smilSeqNode xmlpp::Element * smilSeqNode
= smilBodyNode->add_child(smilSeqNodeName); = smilBodyNode->add_child(smilSeqNodeName);
Playlist::const_iterator it = playlist->begin(); Playlist::const_iterator it = oldPlaylist->begin();
while (it != oldPlaylist->end()) {
Ptr<AudioClip>::Ref audioClip = acquireAudioClip( it->second
->getAudioClip()
->getId() );
Ptr<time_duration>::Ref relativeOffset(new time_duration(
*(it->second->getRelativeOffset()) ));
Ptr<const FadeInfo>::Ref oldFadeInfo = it->second->getFadeInfo();
Ptr<FadeInfo>::Ref newFadeInfo;
if (oldFadeInfo) { // careful: fadeInfo may be 0
newFadeInfo.reset(new FadeInfo(*oldFadeInfo));
}
newPlaylist->addAudioClip(audioClip,
relativeOffset,
newFadeInfo);
while (it != playlist->end()) {
xmlpp::Element * smilAudioClipNode xmlpp::Element * smilAudioClipNode
= smilSeqNode->add_child(smilAudioClipNodeName); = smilSeqNode->add_child(smilAudioClipNodeName);
smilAudioClipNode->set_attribute( smilAudioClipNode->set_attribute(
smilAudioClipUriAttrName, smilAudioClipUriAttrName,
* acquireAudioClip(it->second->getAudioClip()->getId())); *(audioClip->getUri()) );
++it; // may throw exception ++it;
} }
std::stringstream fileNameBuffer; std::string smilFileName = "file://";
fileNameBuffer << localTempStoragePath << "tempPlaylist" smilFileName += tempnam(0, "smil");
<< id->getId() smilDocument->write_to_file(smilFileName, "UTF-8");
<< ".smil";
Ptr<std::string>::Ref fileName(new std::string(fileNameBuffer.str()));
smilDocument->write_to_file(*fileName, "UTF-8"); Ptr<std::string>::Ref playlistUri(new std::string(smilFileName));
return fileName; newPlaylist->setUri(playlistUri);
return newPlaylist;
} }
@ -244,38 +267,43 @@ TestStorageClient :: acquirePlaylist(Ptr<const UniqueId>::Ref id) const
* Release a playlist. * Release a playlist.
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
void void
TestStorageClient :: releasePlaylist(Ptr<const UniqueId>::Ref id) const TestStorageClient :: releasePlaylist(Ptr<const Playlist>::Ref playlist) const
throw (std::logic_error) throw (std::logic_error)
{ {
PlaylistMap::const_iterator playlistMapIt = playlistMap.find(id->getId()); if (! playlist->getUri()) {
throw std::logic_error("playlist URI not found");
if (playlistMapIt == playlistMap.end()) {
throw std::invalid_argument("no such playlist");
} }
Ptr<Playlist>::Ref playlist = playlistMapIt->second; try {
std::FILE * f = fopen(playlist->getUri()->substr(7).c_str(), "r");
if (!f) {
throw std::logic_error("playlist temp file not found");
}
std::fclose(f);
}
catch (std::exception &e) {
throw std::logic_error("playlist temp file I/O error");
}
std::remove(playlist->getUri()->substr(7).c_str());
bool success = true; int badAudioClips = 0;
Playlist::const_iterator it = playlist->begin(); Playlist::const_iterator it = playlist->begin();
while (it != playlist->end()) { while (it != playlist->end()) {
try { try {
releaseAudioClip(it->second->getAudioClip()->getId()); releaseAudioClip(it->second->getAudioClip());
} }
catch (std::invalid_argument &e) { catch (std::invalid_argument &e) {
success = false; ++badAudioClips;
} }
++it; ++it;
} }
std::stringstream fileNameBuffer; if (badAudioClips) {
fileNameBuffer << localTempStoragePath << "tempPlaylist" std::stringstream eMsg;
<< id->getId() eMsg << "could not release " << badAudioClips
<< ".smil"; << " audio clips in playlist";
std::remove(fileNameBuffer.str().c_str()); throw std::logic_error(eMsg.str());
if (!success) {
throw std::logic_error("could not release some audio clips"
" in playlist");
} }
} }
@ -368,7 +396,7 @@ TestStorageClient :: getAudioClip(Ptr<const UniqueId>::Ref id) const
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* Acquire resources for an audio clip. * Acquire resources for an audio clip.
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
Ptr<std::string>::Ref Ptr<AudioClip>::Ref
TestStorageClient :: acquireAudioClip(Ptr<const UniqueId>::Ref id) const TestStorageClient :: acquireAudioClip(Ptr<const UniqueId>::Ref id) const
throw (std::logic_error) throw (std::logic_error)
{ {
@ -378,12 +406,36 @@ TestStorageClient :: acquireAudioClip(Ptr<const UniqueId>::Ref id) const
throw std::invalid_argument("no such audio clip"); throw std::invalid_argument("no such audio clip");
} }
std::stringstream fileNameBuffer; Ptr<AudioClip>::Ref storedAudioClip = it->second;
fileNameBuffer << localTempStoragePath << "test"
<< id->getId() if (! storedAudioClip->getUri()) {
<< ".mp3"; throw std::logic_error("audio clip URI not found");
Ptr<std::string>::Ref fileName(new std::string(fileNameBuffer.str())); }
return fileName; // cut the "file:" off
std::string audioClipFileName = storedAudioClip->getUri()->substr(5);
try {
std::FILE * f = fopen(audioClipFileName.c_str(), "r");
if (f) {
std::fclose(f);
}
else {
throw std::logic_error("could not read audio clip");
}
}
catch (std::exception &e) {
throw std::logic_error("could not read audio clip");
}
Ptr<AudioClip>::Ref audioClip(new AudioClip(*storedAudioClip));
Ptr<std::string>::Ref audioClipUri(new std::string("file://"));
*audioClipUri += get_current_dir_name(); // doesn't work if current
*audioClipUri += "/"; // dir = /, but OK for now
*audioClipUri += audioClipFileName;
audioClip->setUri(audioClipUri);
return audioClip;
} }
@ -391,13 +443,11 @@ TestStorageClient :: acquireAudioClip(Ptr<const UniqueId>::Ref id) const
* Release an audio clip. * Release an audio clip.
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
void void
TestStorageClient :: releaseAudioClip(Ptr<const UniqueId>::Ref id) const TestStorageClient :: releaseAudioClip(Ptr<const AudioClip>::Ref audioClip) const
throw (std::logic_error) throw (std::logic_error)
{ {
AudioClipMap::const_iterator it = audioClipMap.find(id->getId()); if (*(audioClip->getUri()) == "") {
throw std::logic_error("audio clip URI not found");
if (it == audioClipMap.end()) {
throw std::invalid_argument("no such audio clip");
} }
} }

View File

@ -22,7 +22,7 @@
Author : $Author: fgerlits $ Author : $Author: fgerlits $
Version : $Revision: 1.10 $ Version : $Revision: 1.11 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClient.h,v $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClient.h,v $
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -67,7 +67,7 @@ using namespace LiveSupport::Core;
* A dummy storage client, only used for test purposes. * A dummy storage client, only used for test purposes.
* *
* @author $Author: fgerlits $ * @author $Author: fgerlits $
* @version $Revision: 1.10 $ * @version $Revision: 1.11 $
*/ */
class TestStorageClient : class TestStorageClient :
virtual public Configurable, virtual public Configurable,
@ -168,27 +168,30 @@ class TestStorageClient :
/** /**
* Acquire the resources for the playlist. * Acquire the resources for the playlist.
* *
* Produces absolute paths prefixed by "file://"; e.g.,
* "file:///some/dir/test1.mp3" and "file:///tmp/tempfileXXXX.smil".
*
* @param id the id of the playlist to acquire. * @param id the id of the playlist to acquire.
* @return a path (in the local storage) to the Playlist SMIL * @return a new Playlist instance containing a uri field which
* temp file. * points to an executable (playable) SMIL representation of
* the playlist (in the local storage).
* @exception std::invalid_argument if no playlist with the specified * @exception std::invalid_argument if no playlist with the specified
* specified id exists. * specified id exists.
*/ */
virtual Ptr<std::string>::Ref virtual Ptr<Playlist>::Ref
acquirePlaylist(Ptr<const UniqueId>::Ref id) const acquirePlaylist(Ptr<const UniqueId>::Ref id) const
throw (std::logic_error); throw (std::logic_error);
/** /**
* Release the resources (audio clips, other playlists) used * Release the resources (audio clips, other playlists) used
* in a playlist. * in a playlist.
* At this point, this does not do anything.
* *
* @param id the id of the playlist to release. * @param playlist the playlist to release.
* @exception std::invalid_argument if no playlist with the specified * @exception std::logic_error if the playlist has no uri field,
* specified id exists. * or the file does not exist, etc.
*/ */
virtual void virtual void
releasePlaylist(Ptr<const UniqueId>::Ref id) const releasePlaylist(Ptr<const Playlist>::Ref playlist) const
throw (std::logic_error); throw (std::logic_error);
/** /**
@ -244,25 +247,28 @@ class TestStorageClient :
/** /**
* Acquire the resources for the audio clip with the specified id. * Acquire the resources for the audio clip with the specified id.
* *
* Assumes URIs in the config file are relative paths prefixed by
* "file:"; e.g., "file:var/test1.mp3".
*
* @param id the id of the audio clip to acquire. * @param id the id of the audio clip to acquire.
* @return a URI to the audio clip. * @return a new AudioClip instance, containing a uri field which
* points to (a way of getting) the sound file.
* @exception std::invalid_argument if no audio clip with the * @exception std::invalid_argument if no audio clip with the
* specified id exists. * specified id exists.
*/ */
virtual Ptr<std::string>::Ref virtual Ptr<AudioClip>::Ref
acquireAudioClip(Ptr<const UniqueId>::Ref id) const acquireAudioClip(Ptr<const UniqueId>::Ref id) const
throw (std::logic_error); throw (std::logic_error);
/** /**
* Release the lock on an audio clip with the specified id. * Release the resource (sound file) used by an audio clip.
* At this point, this does not do anything.
* *
* @param id the id of the audio clip to release. * @param id the id of the audio clip to release.
* @exception std::invalid_argument if no audio clip with the * @exception std::logic_error if the audio clip has no uri field,
* specified id exists. * or the file does not exist, etc.
*/ */
virtual void virtual void
releaseAudioClip(Ptr<const UniqueId>::Ref id) const releaseAudioClip(Ptr<const AudioClip>::Ref audioClip) const
throw (std::logic_error); throw (std::logic_error);
/** /**

View File

@ -22,7 +22,7 @@
Author : $Author: fgerlits $ Author : $Author: fgerlits $
Version : $Revision: 1.9 $ Version : $Revision: 1.10 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClientTest.cxx,v $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClientTest.cxx,v $
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -215,20 +215,23 @@ TestStorageClientTest :: acquireAudioClipTest(void)
{ {
Ptr<const UniqueId>::Ref id2(new UniqueId(10002)); Ptr<const UniqueId>::Ref id2(new UniqueId(10002));
Ptr<const UniqueId>::Ref id77(new UniqueId(10077)); Ptr<const UniqueId>::Ref id77(new UniqueId(10077));
Ptr<std::string>::Ref audioClipPath; Ptr<AudioClip>::Ref audioClip;
try { try {
audioClipPath = tsc->acquireAudioClip(id2); audioClip = tsc->acquireAudioClip(id2);
} }
catch (std::logic_error &e) { catch (std::logic_error &e) {
std::string eMsg = "could not acquire audio clip:\n"; std::string eMsg = "could not acquire audio clip:\n";
eMsg += e.what(); eMsg += e.what();
CPPUNIT_FAIL(eMsg); CPPUNIT_FAIL(eMsg);
} }
CPPUNIT_ASSERT(*audioClipPath == "var/test10002.mp3"); string audioClipUri("file://");
audioClipUri += get_current_dir_name();
audioClipUri += "/var/test2.mp3";
CPPUNIT_ASSERT(*(audioClip->getUri()) == audioClipUri);
try { try {
tsc->releaseAudioClip(id2); tsc->releaseAudioClip(audioClip);
} }
catch (std::logic_error &e) { catch (std::logic_error &e) {
std::string eMsg = "could not release audio clip:\n"; std::string eMsg = "could not release audio clip:\n";
@ -237,11 +240,11 @@ TestStorageClientTest :: acquireAudioClipTest(void)
} }
try { try {
audioClipPath = tsc->acquireAudioClip(id77); audioClip = tsc->acquireAudioClip(id77);
CPPUNIT_FAIL("allowed to acquire non-existent audio clip"); CPPUNIT_FAIL("allowed to acquire non-existent audio clip");
} }
catch (std::logic_error &e) { catch (std::logic_error &e) {
} }
} }
@ -254,29 +257,31 @@ TestStorageClientTest :: acquirePlaylistTest(void)
{ {
Ptr<UniqueId>::Ref id1(new UniqueId(1)); Ptr<UniqueId>::Ref id1(new UniqueId(1));
Ptr<UniqueId>::Ref id77(new UniqueId(77)); Ptr<UniqueId>::Ref id77(new UniqueId(77));
Ptr<std::string>::Ref playlistPath; Ptr<Playlist>::Ref playlist;
try { try {
playlistPath = tsc->acquirePlaylist(id1); playlist = tsc->acquirePlaylist(id1);
} }
catch (std::logic_error &e) { catch (std::logic_error &e) {
std::string eMsg = "could not acquire playlist:\n"; std::string eMsg = "could not acquire playlist:\n";
eMsg += e.what(); eMsg += e.what();
CPPUNIT_FAIL(eMsg); CPPUNIT_FAIL(eMsg);
} }
CPPUNIT_ASSERT(*playlistPath == "var/tempPlaylist1.smil"); CPPUNIT_ASSERT(playlist->getUri());
CPPUNIT_ASSERT(playlist->getUri()->substr(0,7) == "file://");
try { try {
std::FILE * f = fopen(playlistPath->c_str(), "r"); std::FILE * f = fopen(playlist->getUri()->substr(7).c_str(), "r");
CPPUNIT_ASSERT(f); CPPUNIT_ASSERT(f);
std::fclose(f); std::fclose(f);
} }
catch (std::exception &e) { catch (std::exception &e) {
CPPUNIT_FAIL("temp file not created correctly"); CPPUNIT_FAIL("temp file not created correctly");
} }
string savedTempFilePath = playlist->getUri()->substr(7);
try { try {
tsc->releasePlaylist(id1); tsc->releasePlaylist(playlist);
} }
catch (std::logic_error &e) { catch (std::logic_error &e) {
std::string eMsg = "could not release playlist:\n"; std::string eMsg = "could not release playlist:\n";
@ -284,17 +289,17 @@ TestStorageClientTest :: acquirePlaylistTest(void)
CPPUNIT_FAIL(eMsg); CPPUNIT_FAIL(eMsg);
} }
try { try {
std::FILE * f = fopen(playlistPath->c_str(), "r"); std::FILE * f = fopen(savedTempFilePath.c_str(), "r");
CPPUNIT_ASSERT(!f); CPPUNIT_ASSERT(!f);
} }
catch (std::exception &e) { catch (std::exception &e) {
CPPUNIT_FAIL("temp file not created correctly"); CPPUNIT_FAIL("temp file not destroyed correctly");
} }
try { try {
playlistPath = tsc->acquirePlaylist(id77); playlist = tsc->acquirePlaylist(id77);
CPPUNIT_FAIL("allowed to acquire non-existent playlist"); CPPUNIT_FAIL("allowed to acquire non-existent playlist");
} }
catch (std::logic_error &e) { catch (std::logic_error &e) {
} }
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.