Modified ILibrary interface to also accept an IMessageQueue reference. The

implementation will dispatch results on this queue if it's non-null.
This commit is contained in:
casey langen 2017-01-07 22:53:06 -08:00
parent c9619c88d6
commit 442697e9ce
3 changed files with 53 additions and 3 deletions

View File

@ -39,6 +39,7 @@
#include <core/library/IIndexer.h>
#include <core/library/IQuery.h>
#include <core/runtime/IMessageQueue.h>
namespace musik { namespace core {
@ -56,6 +57,7 @@ namespace musik { namespace core {
virtual IIndexer *Indexer() = 0;
virtual int Id() = 0;
virtual const std::string& Name() = 0;
virtual void SetMessageQueue(musik::core::runtime::IMessageQueue& queue) = 0;
};
typedef std::shared_ptr<ILibrary> LibraryPtr;

View File

@ -40,14 +40,33 @@
#include <core/support/Common.h>
#include <core/support/Preferences.h>
#include <core/library/Indexer.h>
#include <core/runtime/Message.h>
#include <core/debug.h>
static const std::string TAG = "LocalLibrary";
using namespace musik::core;
using namespace musik::core::library;
using namespace musik::core::runtime;
#define VERBOSE_LOGGING 0
#define MESSAGE_QUERY_COMPLETED 5000
class QueryCompletedMessage : public Message {
public:
QueryCompletedMessage(IMessageTarget* target, IQueryPtr query)
: Message(target, MESSAGE_QUERY_COMPLETED, 0, 0) {
this->query = query;
}
virtual ~QueryCompletedMessage() {
}
IQueryPtr GetQuery() { return this->query; }
private:
IQueryPtr query;
};
LibraryPtr LocalLibrary::Create(std::string name, int id) {
LibraryPtr lib(new LocalLibrary(name, id));
@ -57,7 +76,8 @@ LibraryPtr LocalLibrary::Create(std::string name, int id) {
LocalLibrary::LocalLibrary(std::string name,int id)
: name(name)
, id(id)
, exit(false) {
, exit(false)
, messageQueue(nullptr) {
this->identifier = boost::lexical_cast<std::string>(id);
auto prefs = Preferences::ForComponent("library");
@ -165,7 +185,14 @@ void LocalLibrary::RunQuery(IQueryPtr query, bool notify) {
query->Run(this->db);
if (notify) {
this->QueryCompleted(query);
if (this->messageQueue) {
this->messageQueue->Post(
std::shared_ptr<QueryCompletedMessage>(
new QueryCompletedMessage(this, query)));
}
else {
this->QueryCompleted(query);
}
}
if (VERBOSE_LOGGING) {
@ -197,6 +224,16 @@ void LocalLibrary::ThreadProc() {
}
}
void LocalLibrary::SetMessageQueue(musik::core::runtime::IMessageQueue& queue) {
this->messageQueue = &queue;
}
void LocalLibrary::ProcessMessage(musik::core::runtime::IMessage &message) {
if (message.Type() == MESSAGE_QUERY_COMPLETED) {
this->QueryCompleted(static_cast<QueryCompletedMessage*>(&message)->GetQuery());
}
}
musik::core::IIndexer* LocalLibrary::Indexer() {
return this->indexer;
}

View File

@ -55,7 +55,11 @@
namespace musik { namespace core { namespace library {
class LocalLibrary : public ILibrary, boost::noncopyable {
class LocalLibrary :
public ILibrary,
public musik::core::runtime::IMessageTarget,
boost::noncopyable
{
protected:
LocalLibrary(std::string name, int id);
@ -64,10 +68,15 @@ namespace musik { namespace core { namespace library {
virtual ~LocalLibrary();
/* ILibrary */
virtual int Enqueue(IQueryPtr query, unsigned int options = 0);
virtual musik::core::IIndexer *Indexer();
virtual int Id();
virtual const std::string& Name();
virtual void SetMessageQueue(musik::core::runtime::IMessageQueue& queue);
/* IMessageTarget */
virtual void ProcessMessage(musik::core::runtime::IMessage &message);
std::string GetLibraryDirectory();
std::string GetDatabaseFilename();
@ -90,6 +99,8 @@ namespace musik { namespace core { namespace library {
QueryList queryQueue;
musik::core::runtime::IMessageQueue* messageQueue;
std::string identifier;
int id;
std::string name;