/***************************************************************************
                Running
                -----------------------------------------
	begin      : 2011/11/01
	copyright  : (C) 2011 SolutionBox Inc.
	author     : Service 1 Team
	email      : svc1@solbox.com
	version    : 3.0.1

    CopyRight(C) 2011 SolutionBox Inc. All Rights reserved.
    Redistribution and use in source and binary forms, with or with out
    modification, are not permitted in outside of SolutionBox Inc.
***************************************************************************/
#ifndef __RUNNING_H__
#define __RUNNING_H__

#include <sys/time.h>
#include <errno.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <list>
#include <algorithm>

using namespace std;

class CScheduler;
class CRunning;

class CRunlist
{
public:
	enum RUNLIST_FIND
	{
		START_TIME	=	1,
		EDN_TIME	=	2
	};

public:
	static CRunlist* getInstance();
	static void release();
	static void init();

	bool	addlist(CRunning *run);
	void	remove(CRunning *run);

private:
	CRunlist();
	~CRunlist();

	time_t	findnewtime(int findtype, time_t find);
	bool	findnewtime(time_t s, time_t e, time_t &ns, time_t &ne);

private:
	static CRunlist * m_inst;

	list<CRunning*> m_list;

	pthread_mutex_t		m_findmutex;
};

class CRunning
{
public:
	CRunning(CScheduler* scheduler, int task, time_t r);
	~CRunning();

	bool	run(time_t s, time_t e);

	time_t	includestart(time_t s);
	time_t	includeend(time_t e);

	void	setstart(time_t t)	{ m_starttime = t; }
	void	setend(time_t t)	{ m_endtime = t; }

	time_t	getstart()	{ return m_starttime; }
	time_t	getend()	{ return m_endtime; }

private:
	static void* runfn( void* pdata );

private:
	time_t	m_starttime;
	time_t	m_endtime;
	time_t	m_runed;

	int		m_taskindex;

	CScheduler* m_scheduler;
};

#endif // __RUNNING_H__
