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
Author : $Author: maroy $
Version : $Revision: 1.9 $
Author : $Author: fgerlits $
Version : $Revision: 1.10 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClient.cxx,v $
------------------------------------------------------------------------------*/
@ -33,6 +33,12 @@
#include "configure.h"
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#else
#error "Need unistd.h"
#endif
#include <boost/date_time/posix_time/posix_time.hpp>
#include "TestStorageClient.h"
@ -55,7 +61,7 @@ const std::string TestStorageClient::configElementNameStr = "testStorage";
/*------------------------------------------------------------------------------
* 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.
@ -192,7 +198,7 @@ TestStorageClient :: getPlaylist(Ptr<const UniqueId>::Ref id) const
/*------------------------------------------------------------------------------
* Acquire resources for a playlist.
*----------------------------------------------------------------------------*/
Ptr<std::string>::Ref
Ptr<Playlist>::Ref
TestStorageClient :: acquirePlaylist(Ptr<const UniqueId>::Ref id) const
throw (std::logic_error)
{
@ -202,8 +208,11 @@ TestStorageClient :: acquirePlaylist(Ptr<const UniqueId>::Ref id) const
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
smilDocument(new xmlpp::Document(xmlVersion));
xmlpp::Element * smilRootNode
@ -218,25 +227,39 @@ TestStorageClient :: acquirePlaylist(Ptr<const UniqueId>::Ref id) const
xmlpp::Element * smilSeqNode
= 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
= smilSeqNode->add_child(smilAudioClipNodeName);
smilAudioClipNode->set_attribute(
smilAudioClipUriAttrName,
* acquireAudioClip(it->second->getAudioClip()->getId()));
++it; // may throw exception
*(audioClip->getUri()) );
++it;
}
std::stringstream fileNameBuffer;
fileNameBuffer << localTempStoragePath << "tempPlaylist"
<< id->getId()
<< ".smil";
Ptr<std::string>::Ref fileName(new std::string(fileNameBuffer.str()));
std::string smilFileName = "file://";
smilFileName += tempnam(0, "smil");
smilDocument->write_to_file(smilFileName, "UTF-8");
smilDocument->write_to_file(*fileName, "UTF-8");
return fileName;
Ptr<std::string>::Ref playlistUri(new std::string(smilFileName));
newPlaylist->setUri(playlistUri);
return newPlaylist;
}
@ -244,38 +267,43 @@ TestStorageClient :: acquirePlaylist(Ptr<const UniqueId>::Ref id) const
* Release a playlist.
*----------------------------------------------------------------------------*/
void
TestStorageClient :: releasePlaylist(Ptr<const UniqueId>::Ref id) const
TestStorageClient :: releasePlaylist(Ptr<const Playlist>::Ref playlist) const
throw (std::logic_error)
{
PlaylistMap::const_iterator playlistMapIt = playlistMap.find(id->getId());
if (playlistMapIt == playlistMap.end()) {
throw std::invalid_argument("no such playlist");
if (! playlist->getUri()) {
throw std::logic_error("playlist URI not found");
}
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();
while (it != playlist->end()) {
try {
releaseAudioClip(it->second->getAudioClip()->getId());
releaseAudioClip(it->second->getAudioClip());
}
catch (std::invalid_argument &e) {
success = false;
++badAudioClips;
}
++it;
}
std::stringstream fileNameBuffer;
fileNameBuffer << localTempStoragePath << "tempPlaylist"
<< id->getId()
<< ".smil";
std::remove(fileNameBuffer.str().c_str());
if (!success) {
throw std::logic_error("could not release some audio clips"
" in playlist");
if (badAudioClips) {
std::stringstream eMsg;
eMsg << "could not release " << badAudioClips
<< " audio clips in playlist";
throw std::logic_error(eMsg.str());
}
}
@ -368,7 +396,7 @@ TestStorageClient :: getAudioClip(Ptr<const UniqueId>::Ref id) const
/*------------------------------------------------------------------------------
* Acquire resources for an audio clip.
*----------------------------------------------------------------------------*/
Ptr<std::string>::Ref
Ptr<AudioClip>::Ref
TestStorageClient :: acquireAudioClip(Ptr<const UniqueId>::Ref id) const
throw (std::logic_error)
{
@ -378,12 +406,36 @@ TestStorageClient :: acquireAudioClip(Ptr<const UniqueId>::Ref id) const
throw std::invalid_argument("no such audio clip");
}
std::stringstream fileNameBuffer;
fileNameBuffer << localTempStoragePath << "test"
<< id->getId()
<< ".mp3";
Ptr<std::string>::Ref fileName(new std::string(fileNameBuffer.str()));
return fileName;
Ptr<AudioClip>::Ref storedAudioClip = it->second;
if (! storedAudioClip->getUri()) {
throw std::logic_error("audio clip URI not found");
}
// 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.
*----------------------------------------------------------------------------*/
void
TestStorageClient :: releaseAudioClip(Ptr<const UniqueId>::Ref id) const
TestStorageClient :: releaseAudioClip(Ptr<const AudioClip>::Ref audioClip) const
throw (std::logic_error)
{
AudioClipMap::const_iterator it = audioClipMap.find(id->getId());
if (it == audioClipMap.end()) {
throw std::invalid_argument("no such audio clip");
if (*(audioClip->getUri()) == "") {
throw std::logic_error("audio clip URI not found");
}
}

View File

@ -22,7 +22,7 @@
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 $
------------------------------------------------------------------------------*/
@ -67,7 +67,7 @@ using namespace LiveSupport::Core;
* A dummy storage client, only used for test purposes.
*
* @author $Author: fgerlits $
* @version $Revision: 1.10 $
* @version $Revision: 1.11 $
*/
class TestStorageClient :
virtual public Configurable,
@ -168,27 +168,30 @@ class TestStorageClient :
/**
* 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.
* @return a path (in the local storage) to the Playlist SMIL
* temp file.
* @return a new Playlist instance containing a uri field which
* points to an executable (playable) SMIL representation of
* the playlist (in the local storage).
* @exception std::invalid_argument if no playlist with the specified
* specified id exists.
*/
virtual Ptr<std::string>::Ref
virtual Ptr<Playlist>::Ref
acquirePlaylist(Ptr<const UniqueId>::Ref id) const
throw (std::logic_error);
/**
* Release the resources (audio clips, other playlists) used
* in a playlist.
* At this point, this does not do anything.
*
* @param id the id of the playlist to release.
* @exception std::invalid_argument if no playlist with the specified
* specified id exists.
* @param playlist the playlist to release.
* @exception std::logic_error if the playlist has no uri field,
* or the file does not exist, etc.
*/
virtual void
releasePlaylist(Ptr<const UniqueId>::Ref id) const
releasePlaylist(Ptr<const Playlist>::Ref playlist) const
throw (std::logic_error);
/**
@ -244,25 +247,28 @@ class TestStorageClient :
/**
* 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.
* @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
* specified id exists.
*/
virtual Ptr<std::string>::Ref
virtual Ptr<AudioClip>::Ref
acquireAudioClip(Ptr<const UniqueId>::Ref id) const
throw (std::logic_error);
/**
* Release the lock on an audio clip with the specified id.
* At this point, this does not do anything.
* Release the resource (sound file) used by an audio clip.
*
* @param id the id of the audio clip to release.
* @exception std::invalid_argument if no audio clip with the
* specified id exists.
* @exception std::logic_error if the audio clip has no uri field,
* or the file does not exist, etc.
*/
virtual void
releaseAudioClip(Ptr<const UniqueId>::Ref id) const
releaseAudioClip(Ptr<const AudioClip>::Ref audioClip) const
throw (std::logic_error);
/**

View File

@ -22,7 +22,7 @@
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 $
------------------------------------------------------------------------------*/
@ -215,20 +215,23 @@ TestStorageClientTest :: acquireAudioClipTest(void)
{
Ptr<const UniqueId>::Ref id2(new UniqueId(10002));
Ptr<const UniqueId>::Ref id77(new UniqueId(10077));
Ptr<std::string>::Ref audioClipPath;
Ptr<AudioClip>::Ref audioClip;
try {
audioClipPath = tsc->acquireAudioClip(id2);
audioClip = tsc->acquireAudioClip(id2);
}
catch (std::logic_error &e) {
std::string eMsg = "could not acquire audio clip:\n";
eMsg += e.what();
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 {
tsc->releaseAudioClip(id2);
tsc->releaseAudioClip(audioClip);
}
catch (std::logic_error &e) {
std::string eMsg = "could not release audio clip:\n";
@ -237,11 +240,11 @@ TestStorageClientTest :: acquireAudioClipTest(void)
}
try {
audioClipPath = tsc->acquireAudioClip(id77);
audioClip = tsc->acquireAudioClip(id77);
CPPUNIT_FAIL("allowed to acquire non-existent audio clip");
}
catch (std::logic_error &e) {
}
}
}
@ -254,29 +257,31 @@ TestStorageClientTest :: acquirePlaylistTest(void)
{
Ptr<UniqueId>::Ref id1(new UniqueId(1));
Ptr<UniqueId>::Ref id77(new UniqueId(77));
Ptr<std::string>::Ref playlistPath;
Ptr<Playlist>::Ref playlist;
try {
playlistPath = tsc->acquirePlaylist(id1);
playlist = tsc->acquirePlaylist(id1);
}
catch (std::logic_error &e) {
std::string eMsg = "could not acquire playlist:\n";
eMsg += e.what();
CPPUNIT_FAIL(eMsg);
}
CPPUNIT_ASSERT(*playlistPath == "var/tempPlaylist1.smil");
CPPUNIT_ASSERT(playlist->getUri());
CPPUNIT_ASSERT(playlist->getUri()->substr(0,7) == "file://");
try {
std::FILE * f = fopen(playlistPath->c_str(), "r");
std::FILE * f = fopen(playlist->getUri()->substr(7).c_str(), "r");
CPPUNIT_ASSERT(f);
std::fclose(f);
}
catch (std::exception &e) {
CPPUNIT_FAIL("temp file not created correctly");
}
string savedTempFilePath = playlist->getUri()->substr(7);
try {
tsc->releasePlaylist(id1);
tsc->releasePlaylist(playlist);
}
catch (std::logic_error &e) {
std::string eMsg = "could not release playlist:\n";
@ -284,17 +289,17 @@ TestStorageClientTest :: acquirePlaylistTest(void)
CPPUNIT_FAIL(eMsg);
}
try {
std::FILE * f = fopen(playlistPath->c_str(), "r");
std::FILE * f = fopen(savedTempFilePath.c_str(), "r");
CPPUNIT_ASSERT(!f);
}
catch (std::exception &e) {
CPPUNIT_FAIL("temp file not created correctly");
CPPUNIT_FAIL("temp file not destroyed correctly");
}
try {
playlistPath = tsc->acquirePlaylist(id77);
playlist = tsc->acquirePlaylist(id77);
CPPUNIT_FAIL("allowed to acquire non-existent playlist");
}
catch (std::logic_error &e) {
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.