WorkingDirManager.h 3.09 KB
#ifndef WORKINGDIRMANAGER_H
#define WORKINGDIRMANAGER_H

#include <string>

#include "WorkingDirListLoader.h"
#include "../Application/Application.h"

#define WORKINGDIR_LIST_FILE "list.xml"

//max try to find a unique id for a new working dir
#define WORKINGDIR_MAX_TRY 10

//name of the testdir
#define WORKINGDIR_TEST_ID "testdir"

//name of the lokc file
#define WORKINGDIR_LOCK_FILE "lock"

//time to wait between two test to know if a working dir is locked (in seconds)
#define WORKINGDIR_LOCK_SLEEPTIME 1

//timeout (in seconds) during what a process can wait to lock a dir
#define WORKINGDIR_LOCK_WAITTIMEOUT 600

using namespace TREPS::Application;

namespace TREPS
{
	namespace WorkingDirManager
	{
		class WorkingDirManagerClass
		{
			public:
				WorkingDirManagerClass(void);
				
				~WorkingDirManagerClass(void);

				//create a new workind dir - add it in the list file
				string createNewDir(bool useTestDir);

				//get the path to a working dir from operation id
				string getWorkingDirFromId(const char *id);

				//remove a working dir from operation id
				bool removeWorkingDirFromId(const char *id);

				//get the full path of the source file from operation id
				string getSourcePathFromId(const char *id);

				//get the full path to the result file from operation id
				string getResultPathFromId(const char *id);

				//get the full path to the export file from operation id
				string getExportedPathFromId(const char *id);

				//get the full path to the transformation request file from operation id
				string getTransformationRequestFromId(const char *id);

				//get the full path to the lock file from operation id
				//if operation is null, get the full path tot the lock of the general working dir
				string getLockFilePath(const char *id = NULL);

				//set a working dir active for log
				//lockFileName become the new log file in the workind dir
				void setActiveForLog(const char *id, const char *logFileName);

				//lock a working dir
				//if id is null, lock the general working dir
				bool lockDir(const char *id = NULL);

				//unlock a working dir
				//if id is null, unlock the general working dir
				void unlockDir(const char *id = NULL);

				//get the current status from operation id
				string getStatusFromId(const char *id);

				//set the current status from operation id
				bool setStatusFromId(const char *id, const char *requestId, const char *status);

				//reset the current status from operation id
				void resetStatusFromId(const char *id);

			private:
				ApplicationClass *app;
				
				string mainDir;

				//loader for the operations list file
				WorkingDirListLoaderClass *listLoader;

				//get tje full path of the operations list file
				string getListFilePath(void);

				//clean old working dir
				bool cleanOldWorkingDirs(void);

				//manager initialization
				//if clean == true, clean old working dir
				bool init(bool clean = false);

				//get the PID that currently block a dircetory
				int getCrtLockedPID(const char *lockPath);

				//try to modify a lock file for this process
				bool tryToModifyLockFile(const char *lockPath, bool unlock);
		};
	}
}

#endif