#include "OwnCommandProcessorDefs.h"
#include <ICommandProcessor.h>
#include <IDiagRuntimeSystem.h>
#include <ISerializable.h>
#include <mutex>
#include <queue>
#include <Command.h>
class OwnCommandProcessor : public OpenTestSystem::Otx::DiagManager::Common::ICommandProcessor
{
private:
static std::mutex _mutex;
static std::queue <OpenTestSystem::Otx::DiagManager::Common::Command> _cmdQueue;
static bool _isExecuting;
std::size_t GetCommandQueueSize();
public:
OwnCommandProcessor();
virtual ~OwnCommandProcessor();
void SetExceptionHandler(std::function<void(const OpenTestSystem::Otx::DiagManager::Common::Exception&)> handler) override;
};
This class is used for communication between layers.
Definition: Command.h:24
Interface which a DiagRuntimeSystem must be implemented
Definition: IDiagRuntimeSystem.h:21
This class is used for communication between layers.
Definition: Result.h:26
#include "OwnCommandProcessor.h"
#include <ComChannel.h>
bool OwnCommandProcessor::_isExecuting = false;
std::queue <OpenTestSystem::Otx::DiagManager::Common::Command> OwnCommandProcessor::_cmdQueue;
std::mutex OwnCommandProcessor::_mutex;
OwnCommandProcessor::OwnCommandProcessor()
: OwnCommandProcessor(nullptr)
{
}
: _diagRuntimeSystem(drs)
{
}
OwnCommandProcessor::~OwnCommandProcessor()
{
delete _diagRuntimeSystem;
}
Result * OwnCommandProcessor::Execute(const Command & command)
{
if (_diagRuntimeSystem != nullptr)
return _diagRuntimeSystem->Execute(command);
return nullptr;
}
Result * OwnCommandProcessor::ExecuteAsync(const Command & command)
{
AddCommandToQueue(command);
if (_isExecuting == false)
{
do
{
_isExecuting = true;
if (_diagRuntimeSystem != nullptr)
{
Command cmd = PopCommandFromQueue();
_diagRuntimeSystem->ExecuteAsync(cmd);
}
}
while (GetCommandQueueSize() != 0);
_isExecuting = false;
}
return nullptr;
}
void OwnCommandProcessor::SetAsyncResultHandler(std::function<void(const Result&)> handler)
{
if (_diagRuntimeSystem != nullptr)
_diagRuntimeSystem->SetAsyncResultHandler(handler);
}
void OwnCommandProcessor::SetExceptionHandler(std::function<void(const Exception&)> handler)
{
if (_diagRuntimeSystem != nullptr)
_diagRuntimeSystem->SetExceptionHandler(handler);
}
void OwnCommandProcessor::AddCommandToQueue(const Command & command)
{
std::unique_lock<std::mutex> lock(_mutex);
_cmdQueue.push(command);
}
Command OwnCommandProcessor::PopCommandFromQueue()
{
std::unique_lock<std::mutex> lock(_mutex);
Command cmd = _cmdQueue.front();
_cmdQueue.pop();
return cmd;
}
std::size_t OwnCommandProcessor::GetCommandQueueSize()
{
std::unique_lock<std::mutex> lock(_mutex);
return _cmdQueue.size();
}
Namespace containing all common methods for the DiagManager