Put MessageQueue behind an interface so implementation can vary on

different platforms.
This commit is contained in:
casey langen 2017-01-06 15:39:17 -08:00
parent 3ba692d20a
commit 45bac55dc3
12 changed files with 103 additions and 21 deletions

View File

@ -149,6 +149,7 @@
<ClInclude Include="config.h" />
<ClInclude Include="plugin\PluginFactory.h" />
<ClInclude Include="runtime\IMessage.h" />
<ClInclude Include="runtime\IMessageQueue.h" />
<ClInclude Include="runtime\IMessageTarget.h" />
<ClInclude Include="runtime\Message.h" />
<ClInclude Include="runtime\MessageQueue.h" />

View File

@ -318,5 +318,8 @@
<ClInclude Include="audio\Crossfader.h">
<Filter>src\audio</Filter>
</ClInclude>
<ClInclude Include="runtime\IMessageQueue.h">
<Filter>src\runtime</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -0,0 +1,55 @@
//////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2007-2016 musikcube team
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the author nor the names of other contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once
#include "IMessage.h"
#include "IMessageTarget.h"
namespace musik {
namespace core {
namespace runtime {
class IMessageQueue {
public:
virtual ~IMessageQueue() { }
virtual void Post(IMessagePtr message, int64 delayMs = 0) = 0;
virtual int Remove(IMessageTarget *target, int type = -1) = 0;
virtual bool Contains(IMessageTarget *target, int type = -1) = 0;
virtual void Debounce(IMessagePtr message, int64 delayMs = 0) = 0;
virtual void WaitAndDispatch() = 0;
virtual void Dispatch() = 0;
};
}
}
}

View File

@ -67,6 +67,10 @@ void MessageQueue::WaitAndDispatch() {
this->Dispatch();
}
MessageQueue::~MessageQueue() {
}
void MessageQueue::Dispatch() {
milliseconds now = duration_cast<milliseconds>(
system_clock::now().time_since_epoch());

View File

@ -34,8 +34,7 @@
#pragma once
#include "IMessage.h"
#include "IMessageTarget.h"
#include "IMessageQueue.h"
#include <list>
#include <mutex>
@ -44,17 +43,17 @@
#include <atomic>
namespace musik { namespace core { namespace runtime {
class MessageQueue {
class MessageQueue : public IMessageQueue {
public:
MessageQueue();
virtual ~MessageQueue();
void Post(IMessagePtr message, int64 delayMs = 0);
int Remove(IMessageTarget *target, int type = -1);
bool Contains(IMessageTarget *target, int type = -1);
void Debounce(IMessagePtr message, int64 delayMs = 0);
void WaitAndDispatch();
void Dispatch();
virtual void Post(IMessagePtr message, int64 delayMs = 0);
virtual int Remove(IMessageTarget *target, int type = -1);
virtual bool Contains(IMessageTarget *target, int type = -1);
virtual void Debounce(IMessagePtr message, int64 delayMs = 0);
virtual void WaitAndDispatch();
virtual void Dispatch();
private:
struct EnqueuedMessage {

View File

@ -110,7 +110,7 @@ int main(int argc, char* argv[])
LibraryPtr library = LibraryFactory::Libraries().at(0);
MasterTransport transport;
PlaybackService playback(library, transport);
PlaybackService playback(Window::MessageQueue(), library, transport);
GlobalHotkeys globalHotkeys(playback, library);

View File

@ -88,11 +88,11 @@ class StreamMessage : public Message {
};
#define POST(instance, type, user1, user2) \
cursespp::Window::MessageQueue().Post( \
this->messageQueue.Post( \
musik::core::runtime::Message::Create(instance, type, user1, user2));
#define POST_STREAM_MESSAGE(instance, eventType, uri) \
cursespp::Window::MessageQueue().Post( \
this->messageQueue.Post( \
musik::core::runtime::IMessagePtr(new StreamMessage(instance, eventType, uri)));
static inline void loadPreferences(
@ -117,12 +117,16 @@ static inline void savePreferences(
prefs->SetInt(keys::RepeatMode, playback.GetRepeatMode());
}
PlaybackService::PlaybackService(LibraryPtr library, ITransport& transport)
PlaybackService::PlaybackService(
IMessageQueue& messageQueue,
LibraryPtr library,
ITransport& transport)
: library(library)
, transport(transport)
, playlist(library)
, unshuffled(library)
, repeatMode(RepeatNone)
, messageQueue(messageQueue)
, prefs(Preferences::ForComponent(components::Playback)) {
transport.StreamEvent.connect(this, &PlaybackService::OnStreamEvent);
transport.PlaybackEvent.connect(this, &PlaybackService::OnPlaybackEvent);

View File

@ -44,7 +44,7 @@
#include <core/library/ILibrary.h>
#include <core/audio/ITransport.h>
#include <core/support/Preferences.h>
#include <core/runtime/IMessageTarget.h>
#include <core/runtime/IMessageQueue.h>
#include <mutex>
@ -61,6 +61,7 @@ namespace musik {
sigslot::signal1<bool> Shuffled;
PlaybackService(
musik::core::runtime::IMessageQueue& messageQueue,
musik::core::LibraryPtr library,
musik::core::audio::ITransport& transport);
@ -118,6 +119,8 @@ namespace musik {
musik::core::audio::ITransport& transport;
size_t index, nextIndex;
musik::core::sdk::RepeatMode repeatMode;
musik::core::runtime::IMessageQueue& messageQueue;
};
}
}

View File

@ -40,6 +40,7 @@
#include "Screen.h"
#include <core/runtime/Message.h>
#include <core/runtime/MessageQueue.h>
using namespace cursespp;
using namespace musik::core::runtime;
@ -95,7 +96,7 @@ void Window::Unfreeze() {
}
}
MessageQueue& Window::MessageQueue() {
IMessageQueue& Window::MessageQueue() {
return messageQueue;
}

View File

@ -37,7 +37,7 @@
#include "curses_config.h"
#include "IWindow.h"
#include <core/runtime/MessageQueue.h>
#include <core/runtime/IMessageQueue.h>
#ifdef WIN32
#define IDLE_TIMEOUT_MS 0
@ -105,7 +105,7 @@ namespace cursespp {
static void Freeze();
static void Unfreeze();
static musik::core::runtime::MessageQueue& MessageQueue();
static musik::core::runtime::IMessageQueue& MessageQueue();
protected:
IWindow* GetParent() const;

View File

@ -76,7 +76,8 @@
<IgnoreSpecificDefaultLibraries>%(IgnoreSpecificDefaultLibraries)</IgnoreSpecificDefaultLibraries>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
<ImportLibrary>../../bin/$(Configuration)/mC2.lib</ImportLibrary>
<ImportLibrary>
</ImportLibrary>
<TargetMachine>MachineX86</TargetMachine>
<AdditionalDependencies>pdcursesd.lib;pdh.lib;psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ImageHasSafeExceptionHandlers>false</ImageHasSafeExceptionHandlers>

View File

@ -72,6 +72,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)\bin\$(Configuration)\</OutDir>
<IntDir>.\obj\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
@ -79,6 +80,8 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)\bin\$(Configuration)\</OutDir>
<IntDir>.\obj\$(Configuration)\</IntDir>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
@ -99,7 +102,7 @@
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>../../../boost_1_60_0/lib32-msvc-14.0;$(SolutionDir)bin\$(Configuration)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>3rdparty.lib;core.lib;comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -136,7 +139,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>../../../boost_1_60_0/lib32-msvc-14.0;$(SolutionDir)bin\$(Configuration)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>3rdparty.lib;core.lib;comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>comctl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -246,6 +249,14 @@
<None Include="win32cpp\win32cpp.sln" />
<None Include="win32cpp\win32cpp.vcproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\3rdparty\3rdparty.vcxproj">
<Project>{b2165720-b4b2-4f4b-8888-8c390c3cb4db}</Project>
</ProjectReference>
<ProjectReference Include="..\core\core.vcxproj">
<Project>{b2165720-b4b2-4f4b-9634-8c390c3cb4db}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>