Commit 83f3c95c authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

parser: Split out services and worker code

parent ac63e93d
......@@ -30,7 +30,7 @@ namespace medialibrary
class AlbumTrack;
class MetadataParser : public ParserService
class MetadataParser : public IParserService
{
public:
MetadataParser();
......
......@@ -27,7 +27,7 @@
#include <vlcpp/vlc.hpp>
#include <mutex>
#include "parser/ParserService.h"
#include "parser/IParserService.h"
#include "parser/Parser.h"
#include "AlbumTrack.h"
......@@ -36,7 +36,7 @@ namespace medialibrary
class IParserCb;
class VLCMetadataService : public ParserService
class VLCMetadataService : public IParserService
{
public:
explicit VLCMetadataService();
......
......@@ -53,8 +53,9 @@ Parser::~Parser()
void Parser::addService( ServicePtr service )
{
service->initialize2( m_ml, this );
m_services.push_back( std::move( service ) );
auto worker = std::make_unique<ParserService>();
worker->initialize( m_ml, this, std::move( service ) );
m_services.push_back( std::move( worker ) );
}
void Parser::parse( std::shared_ptr<parser::Task> task )
......
......@@ -31,6 +31,7 @@
namespace medialibrary
{
class IParserService;
class ParserService;
// Use an interface to expose only the "done" method
......@@ -45,7 +46,7 @@ public:
class Parser : IParserCb
{
public:
using ServicePtr = std::unique_ptr<ParserService>;
using ServicePtr = std::unique_ptr<IParserService>;
Parser( MediaLibrary* ml );
virtual ~Parser();
......@@ -72,7 +73,7 @@ private:
virtual void onIdleChanged( bool idle ) override;
private:
typedef std::vector<ServicePtr> ServiceList;
typedef std::vector<std::unique_ptr<ParserService>> ServiceList;
private:
ServiceList m_services;
......
......@@ -43,7 +43,7 @@ void ParserService::start()
{
// Ensure we don't start multiple times.
assert( m_threads.size() == 0 );
for ( auto i = 0u; i < nbThreads(); ++i )
for ( auto i = 0u; i < m_service->nbThreads(); ++i )
m_threads.emplace_back( &ParserService::mainloop, this );
}
......@@ -98,11 +98,12 @@ void ParserService::parse( std::shared_ptr<parser::Task> t )
}
}
void ParserService::initialize2( MediaLibrary* ml, IParserCb* parserCb )
void ParserService::initialize( MediaLibrary* ml, IParserCb* parserCb, std::unique_ptr<IParserService> service )
{
m_service = std::move( service );
m_parserCb = parserCb;
// Run the service specific initializer
initialize( ml );
m_service->initialize( ml );
}
bool ParserService::isIdle() const
......@@ -119,12 +120,12 @@ void ParserService::flush()
});
while ( m_tasks.empty() == false )
m_tasks.pop();
onFlushing();
m_service->onFlushing();
}
void ParserService::restart()
{
onRestarted();
m_service->onRestarted();
}
void ParserService::mainloop()
......@@ -132,7 +133,7 @@ void ParserService::mainloop()
// It would be unsafe to call name() at the end of this function, since
// we might stop the thread during ParserService destruction. This implies
// that the underlying service has been deleted already.
std::string serviceName = name();
std::string serviceName = m_service->name();
LOG_INFO("Entering ParserService [", serviceName, "] thread");
setIdle( false );
......@@ -161,7 +162,7 @@ void ParserService::mainloop()
task = std::move( m_tasks.front() );
m_tasks.pop();
}
if ( isCompleted( *task ) == true )
if ( m_service->isCompleted( *task ) == true )
{
LOG_INFO( "Skipping completed task [", serviceName, "] on ", task->mrl );
m_parserCb->done( std::move( task ), parser::Task::Status::Success );
......@@ -178,7 +179,7 @@ void ParserService::mainloop()
else
{
task->startParserStep();
status = run( *task );
status = m_service->run( *task );
auto duration = std::chrono::steady_clock::now() - chrono;
LOG_INFO( "Done executing ", serviceName, " task on ", task->mrl, " in ",
std::chrono::duration_cast<std::chrono::milliseconds>( duration ).count(), "ms" );
......
......@@ -40,11 +40,10 @@ class IParserCb;
class ModificationNotifier;
class MediaLibrary;
class ParserService : public IParserService
class ParserService
{
public:
ParserService();
virtual ~ParserService() = default;
void pause();
void resume();
......@@ -61,21 +60,20 @@ public:
///
void stop();
void parse( std::shared_ptr<parser::Task> t );
// temporary name to avoid shadowing IParserService::initialize
void initialize2( MediaLibrary* ml, IParserCb* parserCb );
void initialize( MediaLibrary* ml, IParserCb* parserCb, std::unique_ptr<IParserService> service );
bool isIdle() const;
///
/// \brief flush flush every currently scheduled tasks
///
/// The service needs to be previously paused or unstarted
///
virtual void flush();
void flush();
///
/// \brief restart Prepare the parser services for a restart.
/// This assumes a flush was triggered before
///
virtual void restart();
void restart();
private:
// Thread(s) entry point
......@@ -84,6 +82,7 @@ private:
void setIdle( bool isIdle );
private:
std::unique_ptr<IParserService> m_service;
IParserCb* m_parserCb;
bool m_stopParser;
bool m_paused;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment