#ifndef NLP_SERVICE_SERVICEMANAGER_H
#define NLP_SERVICE_SERVICEMANAGER_H

#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/threadpool.hpp>	// this is NOT officiall boost lib! Check http://threadpool.sourceforge.net/ for details

#include "logger.h"
#include "service.h"
#include "taskmanager.h"

namespace nlp_service {

/* Class responsible for running the whole machinery. It initializes process pool
 * and delegates new task (obtained via TaskManager) to the processes in the pool.
 */
class ServiceManager {
public:
	ServiceManager(ServicePtr service, Service::service_config config,
					ServiceLoggerPtr logger, bool run_as_daemon = false);
	~ServiceManager();

	/* Main function. Runs the service (either in background or foreground
	 * depending on run_as_daemon argument provided to the constructor) and
	 * the get_task->delegate->finish_task loop.
	 */
	void run();
private:
	void run_loop();
	void process(TaskManager::task_description task);

	void init_threadpool();
	void detach();

	Service::service_config config;
	ServiceLoggerPtr logger;
	TaskManagerPtr task_manager;
	ServicePtr service;
	boost::shared_ptr<boost::threadpool::pool> thread_pool_ptr;
	bool run_as_daemon;
};

}

#endif // NLP_SERVICE_SERVICEMANAGER_H
