Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tfarmcontroller.h"
Toshihiro Shimizu 890ddd
#include "tfarmexecutor.h"
Toshihiro Shimizu 890ddd
#include "tfarmserver.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "service.h"
Toshihiro Shimizu 890ddd
#include "tcli.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tthreadmessage.h"
Toshihiro Shimizu 890ddd
#include "tthread.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "tlog.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qobject></qobject>
Toshihiro Shimizu 890ddd
#include <qcoreapplication></qcoreapplication>
Toshihiro Shimizu 890ddd
#include <qeventloop></qeventloop>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tthread.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <string></string>
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifndef _WIN32
Toshihiro Shimizu 890ddd
#include <sys param.h=""></sys>
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#include <sys timeb.h=""></sys>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int inline STRICMP(const QString &a, const QString &b)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return a.compare(b, Qt::CaseSensitive);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
int inline STRICMP(const char *a, const char *b)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString str(a);
Toshihiro Shimizu 890ddd
	return str.compare(QString(b), Qt::CaseSensitive);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#define STRICMP stricmp
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define STRICMP strcasecmp
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifndef _WIN32
Toshihiro Shimizu 890ddd
#define NO_ERROR 0
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath getGlobalRoot()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath rootDir;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	TFilePath name(L"SOFTWARE\\OpenToonz\\OpenToonz\\1.0\\FARMROOT");
Toshihiro Shimizu 890ddd
	rootDir = TFilePath(TSystem::getSystemValue(name).toStdString());
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Leggo la globalRoot da File txt
Toshihiro Shimizu 890ddd
	Tifstream is(TFilePath("./OpenToonz_1.0.app/Contents/Resources/configfarmroot.txt"));
Toshihiro Shimizu 890ddd
	if (is) {
Toshihiro Shimizu 890ddd
		char line[1024];
Toshihiro Shimizu 890ddd
		is.getline(line, 80);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		char *s = line;
Toshihiro Shimizu 890ddd
		while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\"')
Toshihiro Shimizu 890ddd
			s++;
Toshihiro Shimizu 890ddd
		if (*s != '\0') {
Toshihiro Shimizu 890ddd
			char *t = s;
Toshihiro Shimizu 890ddd
			while (*t)
Toshihiro Shimizu 890ddd
				t++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			string pathName(s, t - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			rootDir = TFilePath(pathName);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	return rootDir;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath getLocalRoot()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath lroot;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	TFilePath name(L"SOFTWARE\\OpenToonz\\OpenToonz\\1.0\\TOONZROOT");
Toshihiro Shimizu 890ddd
	lroot = TFilePath(TSystem::getSystemValue(name).toStdString()) + TFilePath("toonzfarm");
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	// Leggo la localRoot da File txt
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Tifstream is(TFilePath("./OpenToonz_1.0.app/Contents/Resources/configfarmroot.txt"));
Toshihiro Shimizu 890ddd
	if (is) {
Toshihiro Shimizu 890ddd
		char line[1024];
Toshihiro Shimizu 890ddd
		is.getline(line, 80);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		char *s = line;
Toshihiro Shimizu 890ddd
		while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\"')
Toshihiro Shimizu 890ddd
			s++;
Toshihiro Shimizu 890ddd
		if (*s != '\0') {
Toshihiro Shimizu 890ddd
			char *t = s;
Toshihiro Shimizu 890ddd
			while (*t)
Toshihiro Shimizu 890ddd
				t++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			string pathName(s, t - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			lroot = TFilePath(pathName);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//TFilePath name = TFilePath("TFARMLOCALROOT");
Toshihiro Shimizu 890ddd
//char *s = getenv(toString(name.getWideString()).c_str());
Toshihiro Shimizu 890ddd
//lroot = TFilePath(s?s:"");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	return lroot;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool myDoesExists(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool exists = false;
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	TFileStatus fs(fp);
Toshihiro Shimizu 890ddd
	exists = fs.doesExist();
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	int acc = access(toString(fp.getWideString()).c_str(), 00); // 00 == solo esistenza
Toshihiro Shimizu 890ddd
	exists = acc != -1;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	return exists;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool dirExists(const TFilePath &dirFp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool exists = false;
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	TFileStatus fs(dirFp);
Toshihiro Shimizu 890ddd
	exists = fs.isDirectory();
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	int acc = access(toString(dirFp.getWideString()).c_str(), 00); // 00 == solo esistenza
Toshihiro Shimizu 890ddd
	exists = acc != -1;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	return exists;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool loadControllerData(QString &hostName, QString &addr, int &port)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath groot = getGlobalRoot();
Toshihiro Shimizu 890ddd
	TFilePath fp = groot + "config" + "controller.txt";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ControllerData controllerData;
Toshihiro Shimizu 890ddd
	::loadControllerData(fp, controllerData);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	hostName = controllerData.m_hostName;
Toshihiro Shimizu 890ddd
	addr = controllerData.m_ipAddress;
Toshihiro Shimizu 890ddd
	port = controllerData.m_port;
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool isAScript(TFarmTask *task)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return false; //todo per gli script
Toshihiro Shimizu 890ddd
				  /*
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
   return task->m_cmdline.contains(".bat");
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
  return (task->m_cmdline.contains(".csh")|| 
Toshihiro Shimizu 890ddd
          task->m_cmdline.contains(".sh")|| 
Toshihiro Shimizu 890ddd
          task->m_cmdline.contains(".tcsh"))
Toshihiro Shimizu 890ddd
#endif   
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // anonymous namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class CtrlFarmTask : public TFarmTask
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	CtrlFarmTask() : m_toBeDeleted(false), m_failureCount(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	CtrlFarmTask(
Toshihiro Shimizu 890ddd
		const QString &id, const QString &name, const QString &cmdline,
Toshihiro Shimizu 890ddd
		const QString &user, const QString &host, int stepCount, int priority)
Toshihiro Shimizu 890ddd
		: TFarmTask(id, name, cmdline, user, host, stepCount, priority), m_toBeDeleted(false), m_failureCount(0)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_id = id;
Toshihiro Shimizu 890ddd
		m_status = Waiting;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	CtrlFarmTask(const CtrlFarmTask &rhs)
Toshihiro Shimizu 890ddd
		: TFarmTask(rhs)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_serverId = rhs.m_serverId;
Toshihiro Shimizu 890ddd
		m_subTasks = rhs.m_subTasks;
Toshihiro Shimizu 890ddd
		m_toBeDeleted = rhs.m_toBeDeleted;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// TPersist implementation
Toshihiro Shimizu 890ddd
	void loadData(TIStream &is);
Toshihiro Shimizu 890ddd
	void saveData(TOStream &os);
Toshihiro Shimizu 890ddd
	const TPersistDeclaration *getDeclaration() const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString m_serverId;
Toshihiro Shimizu 890ddd
	vector<qstring> m_subTasks;</qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool m_toBeDeleted;
Toshihiro Shimizu 890ddd
	int m_failureCount;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	vector<qstring> m_failedOnServers;</qstring>
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TFarmTaskDeclaration : public TPersistDeclaration
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 3bfa54
	TFarmTaskDeclaration(const std::string &id)
Toshihiro Shimizu 890ddd
		: TPersistDeclaration(id) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPersist *create() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return new CtrlFarmTask;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
} Declaration("tfarmtask");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CtrlFarmTask::loadData(TIStream &is)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	is >> m_name;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString cmdline;
Toshihiro Shimizu 890ddd
	is >> cmdline;
Toshihiro Shimizu 890ddd
	parseCommandLine(cmdline);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is >> m_priority;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is >> m_user;
Toshihiro Shimizu 890ddd
	is >> m_hostName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is >> m_id;
Toshihiro Shimizu 890ddd
	is >> m_parentId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int status;
Toshihiro Shimizu 890ddd
	is >> status;
Toshihiro Shimizu 890ddd
	m_status = (TaskState)status;
Toshihiro Shimizu 890ddd
	is >> m_server;
Toshihiro Shimizu 890ddd
	QString dateStr;
Toshihiro Shimizu 890ddd
	is >> dateStr;
Toshihiro Shimizu 890ddd
	m_submissionDate = QDateTime::fromString(dateStr);
Toshihiro Shimizu 890ddd
	is >> dateStr;
Toshihiro Shimizu 890ddd
	m_startDate = QDateTime::fromString(dateStr);
Toshihiro Shimizu 890ddd
	is >> dateStr;
Toshihiro Shimizu 890ddd
	m_completionDate = QDateTime::fromString(dateStr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is >> m_successfullSteps;
Toshihiro Shimizu 890ddd
	is >> m_failedSteps;
Toshihiro Shimizu 890ddd
	is >> m_stepCount;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is >> m_serverId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string tagName;
Toshihiro Shimizu 890ddd
	while (is.openChild(tagName)) {
Toshihiro Shimizu 890ddd
		if (tagName == "platform") {
Toshihiro Shimizu 890ddd
			int plat;
Toshihiro Shimizu 890ddd
			is >> plat;
Toshihiro Shimizu 890ddd
			switch (plat) {
Toshihiro Shimizu 890ddd
			case NoPlatform:
Toshihiro Shimizu 890ddd
				m_platform = NoPlatform;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			case Windows:
Toshihiro Shimizu 890ddd
				m_platform = Windows;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			case Irix:
Toshihiro Shimizu 890ddd
				m_platform = Irix;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			case Linux:
Toshihiro Shimizu 890ddd
				m_platform = Linux;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else if (tagName == "dependencies") {
Toshihiro Shimizu 890ddd
			int depCount = 0;
Toshihiro Shimizu 890ddd
			is >> depCount;
Toshihiro Shimizu 890ddd
			if (depCount > 0)
Toshihiro Shimizu 890ddd
				m_dependencies = new Dependencies();
Toshihiro Shimizu 890ddd
			while (depCount > 0) {
Toshihiro Shimizu 890ddd
				TFarmTask::Id id;
Toshihiro Shimizu 890ddd
				is >> id;
Toshihiro Shimizu 890ddd
				m_dependencies->add(id);
Toshihiro Shimizu 890ddd
				depCount--;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		is.closeChild();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CtrlFarmTask::saveData(TOStream &os)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	os << m_name;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os << getCommandLine();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os << m_priority;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os << m_user;
Toshihiro Shimizu 890ddd
	os << m_hostName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os << m_id;
Toshihiro Shimizu 890ddd
	os << m_parentId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os << (int)m_status;
Toshihiro Shimizu 890ddd
	os << m_server;
Toshihiro Shimizu 890ddd
	os << m_submissionDate.toString();
Toshihiro Shimizu 890ddd
	os << m_startDate.toString();
Toshihiro Shimizu 890ddd
	os << m_completionDate.toString();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os << m_successfullSteps;
Toshihiro Shimizu 890ddd
	os << m_failedSteps;
Toshihiro Shimizu 890ddd
	os << m_stepCount;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os << m_serverId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os.openChild("platform");
Toshihiro Shimizu 890ddd
	os << m_platform;
Toshihiro Shimizu 890ddd
	os.closeChild();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os.openChild("dependencies");
Toshihiro Shimizu 890ddd
	if (m_dependencies) {
Toshihiro Shimizu 890ddd
		int depCount = m_dependencies->getTaskCount();
Toshihiro Shimizu 890ddd
		os << depCount;
Toshihiro Shimizu 890ddd
		int i = 0;
Toshihiro Shimizu 890ddd
		while (i < depCount) {
Toshihiro Shimizu 890ddd
			TFarmTask::Id id = m_dependencies->getTaskId(i++);
Toshihiro Shimizu 890ddd
			os << id;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		os << 0;
Toshihiro Shimizu 890ddd
	os.closeChild();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TPersistDeclaration *CtrlFarmTask::getDeclaration() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return &Declaration;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class FarmServerProxy
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	FarmServerProxy(const QString &hostName, const QString &addr, int port, int maxTaskCount = 1)
Toshihiro Shimizu 890ddd
		: m_hostName(hostName), m_addr(addr), m_port(port), m_offline(false), m_attached(false), m_maxTaskCount(maxTaskCount), m_platform(NoPlatform)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TFarmServerFactory serverFactory;
Toshihiro Shimizu 890ddd
		serverFactory.create(m_hostName, m_addr, m_port, &m_server);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~FarmServerProxy() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString getId() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return getIpAddress();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString getHostName() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return m_hostName;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString getIpAddress() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return m_addr;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int getPort() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return m_port;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const vector<qstring> &getTasks() const</qstring>
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return m_tasks;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int addTask(const CtrlFarmTask *task);
Toshihiro Shimizu 890ddd
	void terminateTask(const QString &taskId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// e' possibile rimuovere un task solo se non running
Toshihiro Shimizu 890ddd
	void removeTask(const QString &taskId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool testConnection(int timeout);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void queryHwInfo(TFarmServer::HwInfo &hwInfo)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_server->queryHwInfo(hwInfo);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void attachController(const QString &name, const QString &addr, int port)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_server->attachController(name, addr, port);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void detachController(const QString &name, const QString &addr, int port)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_server->detachController(name, addr, port);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString m_hostName;
Toshihiro Shimizu 890ddd
	QString m_addr;
Toshihiro Shimizu 890ddd
	int m_port;
Toshihiro Shimizu 890ddd
	bool m_offline;
Toshihiro Shimizu 890ddd
	bool m_attached;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int m_maxTaskCount;
Toshihiro Shimizu 890ddd
	TFarmPlatform m_platform;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// vettore dei taskId assegnato al server
Toshihiro Shimizu 890ddd
	vector<qstring> m_tasks;</qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFarmServer *m_server;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int FarmServerProxy::addTask(const CtrlFarmTask *task)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int rc = m_server->addTask(task->m_id, task->getCommandLine());
Toshihiro Shimizu 890ddd
	if (rc == 0)
Toshihiro Shimizu 890ddd
		m_tasks.push_back(task->m_id);
Toshihiro Shimizu 890ddd
	return rc;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServerProxy::terminateTask(const QString &taskId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_server->terminateTask(taskId);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServerProxy::removeTask(const QString &taskId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	vector<qstring>::iterator it = find(m_tasks.begin(), m_tasks.end(), taskId);</qstring>
Toshihiro Shimizu 890ddd
	if (it != m_tasks.end()) {
Toshihiro Shimizu 890ddd
		m_tasks.erase(it);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool doTestConnection(const QString &hostName, const QString &addr, int port)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TTcpIpClient client;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int sock;
Toshihiro Shimizu 890ddd
	int ret = client.connect(hostName, addr, port, sock);
Toshihiro Shimizu 890ddd
	if (ret == OK) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
		closesocket(sock);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		close(sock);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
class ConnectionTest : public TThread::Runnable
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ConnectionTest(const FarmServerProxy *server, HANDLE hEvent)
Toshihiro Shimizu 890ddd
		: m_server(server), m_hEvent(hEvent) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void run();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const FarmServerProxy *m_server;
Toshihiro Shimizu 890ddd
	HANDLE m_hEvent;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ConnectionTest::run()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool res = doTestConnection(
Toshihiro Shimizu 890ddd
		m_server->m_hostName, m_server->m_addr, m_server->m_port);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	SetEvent(m_hEvent);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool FarmServerProxy::testConnection(int timeout)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
Toshihiro Shimizu 890ddd
	if (!hEvent) {
Toshihiro Shimizu 890ddd
		// se fallisce la creazione dell'evento ci provo comunque
Toshihiro Shimizu 890ddd
		// senza timeout
Toshihiro Shimizu 890ddd
		return doTestConnection(m_hostName, m_addr, m_port);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TThread::Executor executor;
Toshihiro Shimizu 890ddd
	executor.addTask(new ConnectionTest(this, hEvent));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	DWORD rr = WaitForSingleObject(hEvent, timeout);
Toshihiro Shimizu 890ddd
	CloseHandle(hEvent);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (rr == WAIT_TIMEOUT)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	return doTestConnection(m_hostName, m_addr, m_port);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TTcpIpClient client;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int sock;
Toshihiro Shimizu 890ddd
	int ret = client.connect(m_hostName, m_addr, m_port, sock);
Toshihiro Shimizu 890ddd
	if (ret == OK) {
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
		closesocket(sock);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		close(sock);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TaskId
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int m_id;
Toshihiro Shimizu 890ddd
	int m_subId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TaskId(int id, int subId = -1) : m_id(id), m_subId(m_subId){};
Toshihiro Shimizu 890ddd
	TaskId(const QString &id)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		int pos = id.indexOf(".");
Toshihiro Shimizu 890ddd
		if (pos != -1) {
Toshihiro Shimizu 890ddd
			m_id = id.left(pos).toInt();
Toshihiro Shimizu 890ddd
			m_subId = id.mid(pos + 1, id.length() - pos).toInt();
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			m_id = id.toInt();
Toshihiro Shimizu 890ddd
			m_subId = -1;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	inline bool operator==(const TaskId &f) const { return f.m_id == m_id && f.m_subId == m_subId; };
Toshihiro Shimizu 890ddd
	inline bool operator!=(const TaskId &f) const { return (m_id != f.m_id || m_subId != f.m_subId); };
Toshihiro Shimizu 890ddd
	inline bool operator<(const TaskId &f) const { return (m_id < f.m_id || (m_id == f.m_id && m_subId < f.m_subId)); };
Toshihiro Shimizu 890ddd
	inline bool operator>(const TaskId &f) const { return f < *this; }
Toshihiro Shimizu 890ddd
	inline bool operator>=(const TaskId &f) const { return !operator<(f); }
Toshihiro Shimizu 890ddd
	inline bool operator<=(const TaskId &f) const { return !operator>(f); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TaskId &operator=(const TaskId &f)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_id = f.m_id;
Toshihiro Shimizu 890ddd
		m_subId = f.m_subId;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//operator string() const;
Toshihiro Shimizu 890ddd
	QString toString() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		QString id(QString::number(m_id));
Toshihiro Shimizu 890ddd
		if (m_subId >= 0)
Toshihiro Shimizu 890ddd
			id += "." + ::QString::number(m_subId);
Toshihiro Shimizu 890ddd
		return id;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class FarmController : public TFarmExecutor, public TFarmController
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	FarmController(const QString &hostName, const QString &addr, int port, TUserLog *log);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void loadServersData(const TFilePath &globalRoot);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// TFarmExecutor interface implementation
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString execute(const vector<qstring> &argv);</qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// TFarmController interface methods implementation
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString addTask(
Toshihiro Shimizu 890ddd
		const QString &name, const QString &cmdline,
Toshihiro Shimizu 890ddd
		const QString &user, const QString &host,
Toshihiro Shimizu 890ddd
		bool suspended,
Toshihiro Shimizu 890ddd
		int priority,
Toshihiro Shimizu 890ddd
		TFarmPlatform platform);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString addTask(const TFarmTask &task, bool suspended);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void removeTask(const QString &id);
Toshihiro Shimizu 890ddd
	void suspendTask(const QString &id);
Toshihiro Shimizu 890ddd
	void activateTask(const QString &id);
Toshihiro Shimizu 890ddd
	void restartTask(const QString &id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void getTasks(vector<qstring> &tasks);</qstring>
Toshihiro Shimizu 890ddd
	void getTasks(const QString &parentId, vector<qstring> &tasks);</qstring>
Toshihiro Shimizu 890ddd
	void getTasks(const QString &parentId, vector<taskshortinfo> &tasks);</taskshortinfo>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void queryTaskInfo(const QString &id, TFarmTask &task);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void queryTaskShortInfo(
Toshihiro Shimizu 890ddd
		const QString &id,
Toshihiro Shimizu 890ddd
		QString &parentId,
Toshihiro Shimizu 890ddd
		QString &name,
Toshihiro Shimizu 890ddd
		TaskState &status);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// used (by a server) to notify a server start
Toshihiro Shimizu 890ddd
	void attachServer(const QString &name, const QString &addr, int port);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// used (by a server) to notify a server stop
Toshihiro Shimizu 890ddd
	void detachServer(const QString &name, const QString &addr, int port);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// used (by a server) to notify a task submission error
Toshihiro Shimizu 890ddd
	void taskSubmissionError(const QString &taskId, int errCode);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// used by a server to notify a task progress
Toshihiro Shimizu 890ddd
	void taskProgress(
Toshihiro Shimizu 890ddd
		const QString &taskId,
Toshihiro Shimizu 890ddd
		int step,
Toshihiro Shimizu 890ddd
		int stepCount,
Toshihiro Shimizu 890ddd
		int frameNumber,
Toshihiro Shimizu 890ddd
		FrameState state);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// used (by a server) to notify a task completion
Toshihiro Shimizu 890ddd
	void taskCompleted(const QString &taskId, int exitCode);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// fills the servers vector with the names of the servers
Toshihiro Shimizu 890ddd
	void getServers(vector<serveridentity> &servers);</serveridentity>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// returns the state of the server whose id has been specified
Toshihiro Shimizu 890ddd
	ServerState queryServerState2(const QString &id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// fills info with the infoes about the server whose id is specified
Toshihiro Shimizu 890ddd
	void queryServerInfo(const QString &id, ServerInfo &info);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// activates the server whose id has been specified
Toshihiro Shimizu 890ddd
	void activateServer(const QString &id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// deactivates the server whose id has been specified
Toshihiro Shimizu 890ddd
	// once deactivated, a server is not available for task rendering
Toshihiro Shimizu 890ddd
	void deactivateServer(const QString &id, bool completeRunningTasks);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// FarmController specific methods
Toshihiro Shimizu 890ddd
	CtrlFarmTask *doAddTask(
Toshihiro Shimizu 890ddd
		const QString &id, const QString &parentId,
Toshihiro Shimizu 890ddd
		const QString &name, const QString &cmdline,
Toshihiro Shimizu 890ddd
		const QString &user, const QString &host,
Toshihiro Shimizu 890ddd
		bool suspended, int stepCount, int priority,
Toshihiro Shimizu 890ddd
		TFarmPlatform platform);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void startTask(CtrlFarmTask *task, FarmServerProxy *server);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	CtrlFarmTask *getTaskToStart(FarmServerProxy *server = 0);
Toshihiro Shimizu 890ddd
	CtrlFarmTask *getNextTaskToStart(CtrlFarmTask *task, FarmServerProxy *server);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// looks for a ready server to which to assign the task
Toshihiro Shimizu 890ddd
	// returns true iff the task has been started
Toshihiro Shimizu 890ddd
	bool tryToStartTask(CtrlFarmTask *task);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ServerState getServerState(FarmServerProxy *server, QString &taskId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void initServer(FarmServerProxy *server);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void load(const TFilePath &fp);
Toshihiro Shimizu 890ddd
	void save(const TFilePath &fp) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void doRestartTask(const QString &id, bool fromClient, FarmServerProxy *server);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void activateReadyServers();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// controller name, address and port
Toshihiro Shimizu 890ddd
	QString m_hostName;
Toshihiro Shimizu 890ddd
	QString m_addr;
Toshihiro Shimizu 890ddd
	int m_port;
Toshihiro Shimizu 890ddd
	TUserLog *m_userLog;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask=""> m_tasks;</taskid,>
Toshihiro Shimizu 890ddd
	map<qstring, *="" farmserverproxy=""> m_servers;</qstring,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TThread::Mutex m_mutex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	static int NextTaskId;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int FarmController::NextTaskId = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FarmController::FarmController(const QString &hostName, const QString &addr, int port, TUserLog *log)
Toshihiro Shimizu 890ddd
	: TFarmExecutor(port), m_hostName(hostName), m_addr(addr), m_port(port), m_userLog(log)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath rootDir = getGlobalRoot();
Toshihiro Shimizu 890ddd
	TFilePath lastUsedIdFilePath = rootDir + "config" + "id.txt";
Toshihiro Shimizu 890ddd
	Tifstream is(lastUsedIdFilePath);
Toshihiro Shimizu 890ddd
	if (is.good())
Toshihiro Shimizu 890ddd
		is >> NextTaskId;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::loadServersData(const TFilePath &globalRoot)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp = globalRoot + "config" + "servers.txt";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Tifstream is(fp);
Toshihiro Shimizu 890ddd
	while (!is.eof()) {
Toshihiro Shimizu 890ddd
		char line[80];
Toshihiro Shimizu 890ddd
		is.getline(line, 80);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (line[0] != '#' && QString(line) != "") {
kogaki 5bbc3c
			istrstream iss(line);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			char hostName[512];
Toshihiro Shimizu 890ddd
			char ipAddr[80];
Toshihiro Shimizu 890ddd
			int port;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			iss >> hostName >> ipAddr >> port;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FarmServerProxy *server = new FarmServerProxy(hostName, ipAddr, port);
Toshihiro Shimizu 890ddd
			m_servers.insert(make_pair(QString(ipAddr), server));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (server->testConnection(500)) {
Toshihiro Shimizu 890ddd
				initServer(server);
Toshihiro Shimizu 890ddd
				try {
Toshihiro Shimizu 890ddd
					server->attachController(m_hostName, m_addr, m_port);
Toshihiro Shimizu 890ddd
				} catch (TException &) {
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline QString toString(const TFarmTask &task, int ver)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString ss = task.m_name + ",";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ss += task.getCommandLine() + ",";
Toshihiro Shimizu 890ddd
	ss += QString::number((int)(task.m_priority)) + ",";
Toshihiro Shimizu 890ddd
	ss += task.m_user + ",";
Toshihiro Shimizu 890ddd
	ss += task.m_hostName + ",";
Toshihiro Shimizu 890ddd
	ss += task.m_id + ",";
Toshihiro Shimizu 890ddd
	ss += task.m_parentId + ",";
Toshihiro Shimizu 890ddd
	ss += QString::number((int)(task.m_status)) + ",";
Toshihiro Shimizu 890ddd
	ss += task.m_server + ",";
Toshihiro Shimizu 890ddd
	ss += task.m_submissionDate.toString() + ",";
Toshihiro Shimizu 890ddd
	ss += task.m_startDate.toString() + ",";
Toshihiro Shimizu 890ddd
	ss += task.m_completionDate.toString() + ",";
Toshihiro Shimizu 890ddd
	ss += QString::number(task.m_successfullSteps) + ",";
Toshihiro Shimizu 890ddd
	ss += QString::number(task.m_failedSteps) + ",";
Toshihiro Shimizu 890ddd
	ss += QString::number(task.m_stepCount);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (ver == 2) {
Toshihiro Shimizu 890ddd
		ss += ",";
Toshihiro Shimizu 890ddd
		ss += QString::number(task.m_platform) + ",";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int depCount = 0;
Toshihiro Shimizu 890ddd
		if (task.m_dependencies)
Toshihiro Shimizu 890ddd
			depCount = task.m_dependencies->getTaskCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ss += QString::number(depCount);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int i = 0; i < depCount; ++i) {
Toshihiro Shimizu 890ddd
			TFarmTask::Id id = task.m_dependencies->getTaskId(i);
Toshihiro Shimizu 890ddd
			ss += "," + id;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ss += '\0';
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return ss;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline QString toString(const ServerInfo &info)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString ss = info.m_name + ",";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ss += info.m_ipAddress + ",";
Toshihiro Shimizu 890ddd
	ss += info.m_portNumber + ",";
Toshihiro Shimizu 890ddd
	ss += QString::number((int)(info.m_state)) + ",";
Toshihiro Shimizu 890ddd
	ss += info.m_platform + ",";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ss += QString::number((int)info.m_cpuCount) + ",";
Toshihiro Shimizu 890ddd
	ss += QString::number((int)info.m_totPhysMem) + ",";
Toshihiro Shimizu 890ddd
	ss += QString::number((int)info.m_totVirtMem) + ",";
Toshihiro Shimizu 890ddd
	ss += QString::number((int)info.m_availPhysMem) + ",";
Toshihiro Shimizu 890ddd
	ss += QString::number((int)info.m_availVirtMem) + ",";
Toshihiro Shimizu 890ddd
	ss += info.m_currentTaskId + '\0';
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return ss;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // anonymous namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString FarmController::execute(const vector<qstring> &argv)</qstring>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (argv.size() > 0) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef TRACE
Toshihiro Shimizu 890ddd
		for (int i = 0; i < argv.size(); i++) {
Toshihiro Shimizu 890ddd
			m_userLog->info(argv[i]);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		m_userLog->info('\n');
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (argv[0] == "addTask@string@string" && argv.size() > 5) {
Toshihiro Shimizu 890ddd
			// ordine degli argomenti:
Toshihiro Shimizu 890ddd
			// name, cmdline, user, host, suspended, priority, platform
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			bool suspended;
Toshihiro Shimizu 890ddd
			fromStr((int &)suspended, argv[5]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			int priority;
Toshihiro Shimizu 890ddd
			fromStr(priority, argv[6]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TFarmPlatform platform;
Toshihiro Shimizu 890ddd
			fromStr((int &)platform, argv[7]);
Toshihiro Shimizu 890ddd
			return addTask(argv[1], argv[2], argv[3], argv[4], suspended, priority, platform);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "addTask@TFarmTask" && argv.size() > 5) {
Toshihiro Shimizu 890ddd
			// ordine degli argomenti:
Toshihiro Shimizu 890ddd
			// name, cmdline, user, host, suspended, priority,
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			bool suspended;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			fromStr((int &)suspended, argv[5]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			int priority;
Toshihiro Shimizu 890ddd
			fromStr(priority, argv[6]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TFarmTaskGroup task("", argv[1], argv[2], argv[3], argv[4], priority, 50);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			for (int i = 7; i < (int)argv.size(); i += 5) {
Toshihiro Shimizu 890ddd
				// ordine degli argomenti:
Toshihiro Shimizu 890ddd
				// name, cmdline, user, host, priority,
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				int subTaskPriority;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				fromStr(subTaskPriority, argv[i + 4]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				TFarmTask *subTask = new TFarmTask("",
Toshihiro Shimizu 890ddd
												   argv[i], argv[i + 1], argv[i + 2], argv[i + 3], subTaskPriority, 50);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				task.addTask(subTask);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return addTask(task, suspended);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "addTask@TFarmTask_2" && argv.size() > 7) {
Toshihiro Shimizu 890ddd
			// ordine degli argomenti:
Toshihiro Shimizu 890ddd
			// name, cmdline, user, host, suspended, stepCount, priority, platform
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			int suspended;
Toshihiro Shimizu 890ddd
			fromStr(suspended, argv[5]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			int stepCount;
Toshihiro Shimizu 890ddd
			fromStr(stepCount, argv[6]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			int priority;
Toshihiro Shimizu 890ddd
			fromStr(priority, argv[7]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TFarmPlatform platform;
Toshihiro Shimizu 890ddd
			fromStr((int &)platform, argv[8]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TFarmTaskGroup task("", argv[1], argv[2], argv[3], argv[4], stepCount, priority);
Toshihiro Shimizu 890ddd
			task.m_platform = platform;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			int depCount;
Toshihiro Shimizu 890ddd
			fromStr(depCount, argv[9]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			int i = 0;
Toshihiro Shimizu 890ddd
			for (; i < depCount; ++i) {
Toshihiro Shimizu 890ddd
				QString depTaskId = argv[10 + i];
Toshihiro Shimizu 890ddd
				task.m_dependencies->add(depTaskId);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			for (i = 10 + depCount; i < (int)argv.size(); i += 6) {
Toshihiro Shimizu 890ddd
				// ordine degli argomenti:
Toshihiro Shimizu 890ddd
				// name, cmdline, user, host, stepCount, priority,
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				int subTaskStepCount;
Toshihiro Shimizu 890ddd
				fromStr(subTaskStepCount, argv[i + 4]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				int subTaskPriority;
Toshihiro Shimizu 890ddd
				fromStr(subTaskPriority, argv[i + 5]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				TFarmTask *subTask = new TFarmTask(
Toshihiro Shimizu 890ddd
					"", argv[i], argv[i + 1], argv[i + 2], argv[i + 3], subTaskStepCount, subTaskPriority);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				subTask->m_dependencies = new TFarmTask::Dependencies(*task.m_dependencies);
Toshihiro Shimizu 890ddd
				subTask->m_platform = platform;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				task.addTask(subTask);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return addTask(task, suspended != 0);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "removeTask" && argv.size() > 0) {
Toshihiro Shimizu 890ddd
			removeTask(argv[1]);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "suspendTask" && argv.size() > 0) {
Toshihiro Shimizu 890ddd
			suspendTask(argv[1]);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "activateTask" && argv.size() > 0) {
Toshihiro Shimizu 890ddd
			activateTask(argv[1]);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "restartTask" && argv.size() > 0) {
Toshihiro Shimizu 890ddd
			restartTask(argv[1]);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "getTasks@vector") {
Toshihiro Shimizu 890ddd
			vector<qstring> tasks;</qstring>
Toshihiro Shimizu 890ddd
			getTasks(tasks);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			QString reply;
Toshihiro Shimizu 890ddd
			std::vector<qstring>::iterator it = tasks.begin();</qstring>
Toshihiro Shimizu 890ddd
			for (; it != tasks.end(); ++it) {
Toshihiro Shimizu 890ddd
				reply += *it;
Toshihiro Shimizu 890ddd
				reply += ",";
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return reply;
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "getTasks@string@vector") {
Toshihiro Shimizu 890ddd
			QString parentId;
Toshihiro Shimizu 890ddd
			if (argv.size() > 1)
Toshihiro Shimizu 890ddd
				parentId = argv[1];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			vector<qstring> tasks;</qstring>
Toshihiro Shimizu 890ddd
			getTasks(parentId, tasks);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			QString reply;
Toshihiro Shimizu 890ddd
			std::vector<qstring>::iterator it = tasks.begin();</qstring>
Toshihiro Shimizu 890ddd
			for (; it != tasks.end(); ++it) {
Toshihiro Shimizu 890ddd
				reply += *it;
Toshihiro Shimizu 890ddd
				reply += ",";
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (reply.length() > 0)
Toshihiro Shimizu 890ddd
				reply = reply.left(reply.length() - 1);
Toshihiro Shimizu 890ddd
			return reply;
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "getTasks@string@vector$TaskShortInfo" && argv.size() > 0) {
Toshihiro Shimizu 890ddd
			vector<taskshortinfo> tasks;</taskshortinfo>
Toshihiro Shimizu 890ddd
			getTasks(argv[1], tasks);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			QString reply;
Toshihiro Shimizu 890ddd
			std::vector<taskshortinfo>::iterator it = tasks.begin();</taskshortinfo>
Toshihiro Shimizu 890ddd
			for (; it != tasks.end(); ++it) {
Toshihiro Shimizu 890ddd
				reply += (*it).m_id;
Toshihiro Shimizu 890ddd
				reply += ",";
Toshihiro Shimizu 890ddd
				reply += (*it).m_name;
Toshihiro Shimizu 890ddd
				reply += ",";
Toshihiro Shimizu 890ddd
				reply += QString::number((*it).m_status);
Toshihiro Shimizu 890ddd
				reply += ",";
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			if (reply.length() > 0)
Toshihiro Shimizu 890ddd
				reply = reply.left(reply.size() - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return reply;
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "queryTaskInfo" && argv.size() > 1) {
Toshihiro Shimizu 890ddd
			TFarmTask task;
Toshihiro Shimizu 890ddd
			queryTaskInfo(argv[1], task);
Toshihiro Shimizu 890ddd
			return toString(task, 1);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "queryTaskInfo_2" && argv.size() > 1) {
Toshihiro Shimizu 890ddd
			TFarmTask task;
Toshihiro Shimizu 890ddd
			queryTaskInfo(argv[1], task);
Toshihiro Shimizu 890ddd
			return toString(task, 2);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "queryTaskShortInfo" && argv.size() > 1) {
Toshihiro Shimizu 890ddd
			QString parentId, name;
Toshihiro Shimizu 890ddd
			TaskState status;
Toshihiro Shimizu 890ddd
			queryTaskShortInfo(argv[1], parentId, name, status);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			QString reply;
Toshihiro Shimizu 890ddd
			reply += parentId;
Toshihiro Shimizu 890ddd
			reply += ",";
Toshihiro Shimizu 890ddd
			reply += name;
Toshihiro Shimizu 890ddd
			reply += ",";
Toshihiro Shimizu 890ddd
			reply += QString::number((int)(status));
Toshihiro Shimizu 890ddd
			return reply;
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "taskSubmissionError" && argv.size() > 2) {
Toshihiro Shimizu 890ddd
			QString taskId = argv[1];
Toshihiro Shimizu 890ddd
			int errCode;
Toshihiro Shimizu 890ddd
			fromStr(errCode, argv[2]);
Toshihiro Shimizu 890ddd
			taskSubmissionError(taskId, errCode);
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "taskProgress" && argv.size() > 5) {
Toshihiro Shimizu 890ddd
			int step, stepCount, frameNumber;
Toshihiro Shimizu 890ddd
			step = argv[2].toInt();
Toshihiro Shimizu 890ddd
			stepCount = argv[3].toInt();
Toshihiro Shimizu 890ddd
			frameNumber = argv[4].toInt();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			FrameState state;
Toshihiro Shimizu 890ddd
			state = (FrameState)argv[5].toInt();
Toshihiro Shimizu 890ddd
			taskProgress(argv[1], step, stepCount, frameNumber, state);
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "taskCompleted" && argv.size() > 2) {
Toshihiro Shimizu 890ddd
			QString taskId = argv[1];
Toshihiro Shimizu 890ddd
			int exitCode;
Toshihiro Shimizu 890ddd
			exitCode = argv[2].toInt();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			taskCompleted(taskId, exitCode);
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "getServers") {
Toshihiro Shimizu 890ddd
			vector<serveridentity> servers;</serveridentity>
Toshihiro Shimizu 890ddd
			getServers(servers);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			QString reply;
Toshihiro Shimizu 890ddd
			std::vector<serveridentity>::iterator it = servers.begin();</serveridentity>
Toshihiro Shimizu 890ddd
			for (; it != servers.end(); ++it) {
Toshihiro Shimizu 890ddd
				reply += (*it).m_id;
Toshihiro Shimizu 890ddd
				reply += ",";
Toshihiro Shimizu 890ddd
				reply += (*it).m_name;
Toshihiro Shimizu 890ddd
				reply += ",";
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (reply.length() > 0)
Toshihiro Shimizu 890ddd
				reply = reply.left(reply.size() - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return reply;
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "queryServerState2" && argv.size() > 0) {
Toshihiro Shimizu 890ddd
			ServerState state = queryServerState2(argv[1]);
Toshihiro Shimizu 890ddd
			return QString::number(state);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "queryServerInfo" && argv.size() > 0) {
Toshihiro Shimizu 890ddd
			ServerInfo info;
Toshihiro Shimizu 890ddd
			queryServerInfo(argv[1], info);
Toshihiro Shimizu 890ddd
			return toString(info);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "activateServer" && argv.size() > 0) {
Toshihiro Shimizu 890ddd
			activateServer(argv[1]);
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "deactivateServer" && argv.size() > 1) {
Toshihiro Shimizu 890ddd
			int completeRunningTask = true;
Toshihiro Shimizu 890ddd
			fromStr(completeRunningTask, argv[2]);
Toshihiro Shimizu 890ddd
			deactivateServer(argv[1], !!completeRunningTask);
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "attachServer" && argv.size() > 3) {
Toshihiro Shimizu 890ddd
			int port;
Toshihiro Shimizu 890ddd
			fromStr(port, argv[3]);
Toshihiro Shimizu 890ddd
			attachServer(argv[1], argv[2], port);
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "detachServer" && argv.size() > 3) {
Toshihiro Shimizu 890ddd
			int port;
Toshihiro Shimizu 890ddd
			fromStr(port, argv[3]);
Toshihiro Shimizu 890ddd
			detachServer(argv[1], argv[2], port);
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#ifdef TRACE
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		m_userLog->info("empty command\n");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	return "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
CtrlFarmTask *FarmController::doAddTask(
Toshihiro Shimizu 890ddd
	const QString &id, const QString &parentId,
Toshihiro Shimizu 890ddd
	const QString &name, const QString &cmdline,
Toshihiro Shimizu 890ddd
	const QString &user, const QString &host,
Toshihiro Shimizu 890ddd
	bool suspended, int stepCount,
Toshihiro Shimizu 890ddd
	int priority, TFarmPlatform platform)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	CtrlFarmTask *task = new CtrlFarmTask(id, name, cmdline, user, host, stepCount, priority);
Toshihiro Shimizu 890ddd
	task->m_submissionDate = QDateTime::currentDateTime();
Toshihiro Shimizu 890ddd
	task->m_parentId = parentId;
Toshihiro Shimizu 890ddd
	task->m_platform = platform;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_tasks.insert(std::make_pair(TaskId(id), task));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_userLog->info("Task " + task->m_id + " received at " + task->m_submissionDate.toString() + "\n");
Toshihiro Shimizu 890ddd
	m_userLog->info("\"" + task->getCommandLine() + "\"\n");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (suspended)
Toshihiro Shimizu 890ddd
		task->m_status = Suspended;
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  else
Toshihiro Shimizu 890ddd
    tryToStartTask(task);
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
	return task;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::startTask(CtrlFarmTask *task, FarmServerProxy *server)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	CtrlFarmTask *taskToBeSubmittedParent = 0;
Toshihiro Shimizu 890ddd
	CtrlFarmTask *taskToBeSubmitted = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (task->m_subTasks.empty()) {
Toshihiro Shimizu 890ddd
		taskToBeSubmitted = task;
Toshihiro Shimizu 890ddd
		if (task->m_parentId != "") {
Toshihiro Shimizu 890ddd
			map<taskid, *="" ctrlfarmtask="">::iterator itTaskParent =</taskid,>
Toshihiro Shimizu 890ddd
				m_tasks.find(TaskId(task->m_parentId));
Toshihiro Shimizu 890ddd
			if (itTaskParent != m_tasks.end()) {
Toshihiro Shimizu 890ddd
				taskToBeSubmittedParent = itTaskParent->second;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		taskToBeSubmittedParent = task;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// cerca il primo subtask WAITING
Toshihiro Shimizu 890ddd
		std::vector<qstring>::iterator itSubTaskId = task->m_subTasks.begin();</qstring>
Toshihiro Shimizu 890ddd
		for (; itSubTaskId != task->m_subTasks.end(); ++itSubTaskId) {
Toshihiro Shimizu 890ddd
			QString subTaskId = *itSubTaskId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			map<taskid, *="" ctrlfarmtask="">::iterator itSubTask =</taskid,>
Toshihiro Shimizu 890ddd
				m_tasks.find(TaskId(subTaskId));
Toshihiro Shimizu 890ddd
			if (itSubTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
				CtrlFarmTask *subTask = itSubTask->second;
Toshihiro Shimizu 890ddd
				if (subTask->m_status == Waiting) {
Toshihiro Shimizu 890ddd
					taskToBeSubmitted = subTask;
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int rc = 0;
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		server->addTask(taskToBeSubmitted);
Toshihiro Shimizu 890ddd
	} catch (TException &e) {
Toshihiro Shimizu 890ddd
		throw e;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (rc == 0) {
Toshihiro Shimizu 890ddd
		QDateTime startDate = QDateTime::currentDateTime();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (taskToBeSubmittedParent && taskToBeSubmittedParent->m_status != Running) {
Toshihiro Shimizu 890ddd
			taskToBeSubmittedParent->m_status = Running;
Toshihiro Shimizu 890ddd
			taskToBeSubmittedParent->m_startDate = startDate;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		taskToBeSubmitted->m_status = Running;
Toshihiro Shimizu 890ddd
		taskToBeSubmitted->m_startDate = startDate;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		taskToBeSubmitted->m_serverId = server->getId();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		QString msg = "Task " + taskToBeSubmitted->m_id + " assigned to ";
Toshihiro Shimizu 890ddd
		msg += server->getHostName();
Toshihiro Shimizu 890ddd
		msg += "\n\n";
Toshihiro Shimizu 890ddd
		m_userLog->info(msg);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
CtrlFarmTask *FarmController::getTaskToStart(FarmServerProxy *server)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int maxPriority = 0;
Toshihiro Shimizu 890ddd
	CtrlFarmTask *candidate = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator itTask = m_tasks.begin();</taskid,>
Toshihiro Shimizu 890ddd
	for (; itTask != m_tasks.end(); ++itTask) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = itTask->second;
Toshihiro Shimizu 890ddd
		if ((!server || (task->m_platform == NoPlatform || task->m_platform == server->m_platform)) &&
Toshihiro Shimizu 890ddd
			((task->m_status == Waiting && task->m_priority > maxPriority) ||
Toshihiro Shimizu 890ddd
			 (task->m_status == Aborted && task->m_failureCount < 3) && task->m_parentId != "")) {
Toshihiro Shimizu 890ddd
			bool dependenciesCompleted = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (task->m_dependencies) {
Toshihiro Shimizu 890ddd
				int count = task->m_dependencies->getTaskCount();
Toshihiro Shimizu 890ddd
				for (int i = 0; i < count; ++i) {
Toshihiro Shimizu 890ddd
					TFarmTask::Id id = task->m_dependencies->getTaskId(i);
Toshihiro Shimizu 890ddd
					map<taskid, *="" ctrlfarmtask="">::iterator itDepTask = m_tasks.find(TaskId(id));</taskid,>
Toshihiro Shimizu 890ddd
					if (itDepTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
						CtrlFarmTask *depTask = itDepTask->second;
Toshihiro Shimizu 890ddd
						if (depTask->m_status != Completed) {
Toshihiro Shimizu 890ddd
							dependenciesCompleted = false;
Toshihiro Shimizu 890ddd
							break;
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (dependenciesCompleted) {
Toshihiro Shimizu 890ddd
				maxPriority = task->m_priority;
Toshihiro Shimizu 890ddd
				candidate = task;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return candidate;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// determina il prossimo task da avviare tra quelli Waiting, escludendo except
Toshihiro Shimizu 890ddd
// dalla ricerca
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
CtrlFarmTask *FarmController::getNextTaskToStart(CtrlFarmTask *except, FarmServerProxy *server)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int maxPriority = 0;
Toshihiro Shimizu 890ddd
	CtrlFarmTask *candidate = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator itTask = m_tasks.begin();</taskid,>
Toshihiro Shimizu 890ddd
	for (; itTask != m_tasks.end(); ++itTask) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = itTask->second;
Toshihiro Shimizu 890ddd
		if (except == task)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		if ((task->m_platform == NoPlatform || task->m_platform == server->m_platform) &&
Toshihiro Shimizu 890ddd
			task->m_status == Waiting && task->m_priority > maxPriority) {
Toshihiro Shimizu 890ddd
			bool dependenciesCompleted = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (task->m_dependencies) {
Toshihiro Shimizu 890ddd
				int count = task->m_dependencies->getTaskCount();
Toshihiro Shimizu 890ddd
				for (int i = 0; i < count; ++i) {
Toshihiro Shimizu 890ddd
					TFarmTask::Id id = task->m_dependencies->getTaskId(i);
Toshihiro Shimizu 890ddd
					map<taskid, *="" ctrlfarmtask="">::iterator itDepTask = m_tasks.find(TaskId(id));</taskid,>
Toshihiro Shimizu 890ddd
					if (itDepTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
						CtrlFarmTask *depTask = itDepTask->second;
Toshihiro Shimizu 890ddd
						if (depTask->m_status != Completed) {
Toshihiro Shimizu 890ddd
							dependenciesCompleted = false;
Toshihiro Shimizu 890ddd
							break;
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (dependenciesCompleted) {
Toshihiro Shimizu 890ddd
				maxPriority = task->m_priority;
Toshihiro Shimizu 890ddd
				candidate = task;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return candidate;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool FarmController::tryToStartTask(CtrlFarmTask *task)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool dependenciesCompleted = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (task->m_dependencies) {
Toshihiro Shimizu 890ddd
		int count = task->m_dependencies->getTaskCount();
Toshihiro Shimizu 890ddd
		for (int i = 0; i < count; ++i) {
Toshihiro Shimizu 890ddd
			TFarmTask::Id id = task->m_dependencies->getTaskId(i);
Toshihiro Shimizu 890ddd
			map<taskid, *="" ctrlfarmtask="">::iterator itDepTask = m_tasks.find(TaskId(id));</taskid,>
Toshihiro Shimizu 890ddd
			if (itDepTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
				CtrlFarmTask *depTask = itDepTask->second;
Toshihiro Shimizu 890ddd
				if (depTask->m_status != Completed) {
Toshihiro Shimizu 890ddd
					dependenciesCompleted = false;
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!dependenciesCompleted)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (task->m_subTasks.empty()) {
Toshihiro Shimizu 890ddd
		vector<farmserverproxy *=""> m_partiallyBusyServers;</farmserverproxy>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		map<qstring, *="" farmserverproxy="">::iterator it = m_servers.begin();</qstring,>
Toshihiro Shimizu 890ddd
		for (; it != m_servers.end(); ++it) {
Toshihiro Shimizu 890ddd
			FarmServerProxy *server = it->second;
Toshihiro Shimizu 890ddd
			if (server->m_attached && !server->m_offline &&
Toshihiro Shimizu 890ddd
				(int)server->getTasks().size() < server->m_maxTaskCount) {
Toshihiro Shimizu 890ddd
				if (!(task->m_platform == NoPlatform || task->m_platform == server->m_platform))
Toshihiro Shimizu 890ddd
					continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				vector<qstring>::iterator its =</qstring>
Toshihiro Shimizu 890ddd
					find(task->m_failedOnServers.begin(), task->m_failedOnServers.end(), server->getId());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (its != task->m_failedOnServers.end())
Toshihiro Shimizu 890ddd
					continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (server->testConnection(500)) {
Toshihiro Shimizu 890ddd
					if (server->getTasks().size() == 0) {
Toshihiro Shimizu 890ddd
						try {
Toshihiro Shimizu 890ddd
							startTask(task, server);
Toshihiro Shimizu 890ddd
						} catch (TException & /*e*/) {
Toshihiro Shimizu 890ddd
							continue;
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						return true;
Toshihiro Shimizu 890ddd
					} else
Toshihiro Shimizu 890ddd
						m_partiallyBusyServers.push_back(server);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		vector<farmserverproxy *="">::iterator it2 = m_partiallyBusyServers.begin();</farmserverproxy>
Toshihiro Shimizu 890ddd
		for (; it2 != m_partiallyBusyServers.end(); ++it2) {
Toshihiro Shimizu 890ddd
			FarmServerProxy *server = *it2;
Toshihiro Shimizu 890ddd
			if (server->testConnection(500)) {
Toshihiro Shimizu 890ddd
				try {
Toshihiro Shimizu 890ddd
					startTask(task, server);
Toshihiro Shimizu 890ddd
				} catch (TException & /*e*/) {
Toshihiro Shimizu 890ddd
					continue;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		// un task composto e' considerato started sse e' started almeno uno
Toshihiro Shimizu 890ddd
		// dei task che lo compongono
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bool started = false;
Toshihiro Shimizu 890ddd
		vector<qstring>::iterator itSubTaskId = task->m_subTasks.begin();</qstring>
Toshihiro Shimizu 890ddd
		for (; itSubTaskId != task->m_subTasks.end(); ++itSubTaskId) {
Toshihiro Shimizu 890ddd
			map<taskid, *="" ctrlfarmtask="">::iterator itSubTask = m_tasks.find(TaskId(*itSubTaskId));</taskid,>
Toshihiro Shimizu 890ddd
			if (itSubTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
				CtrlFarmTask *subTask = itSubTask->second;
Toshihiro Shimizu 890ddd
				if (tryToStartTask(subTask))
Toshihiro Shimizu 890ddd
					started = true;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return started;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ServerState FarmController::getServerState(FarmServerProxy *server, QString &taskId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ServerState state;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool connected = server->testConnection(500);
Toshihiro Shimizu 890ddd
	if (!connected) {
Toshihiro Shimizu 890ddd
		taskId = "";
Toshihiro Shimizu 890ddd
		state = Down;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (server->m_offline)
Toshihiro Shimizu 890ddd
			return Offline;
Toshihiro Shimizu 890ddd
		else if (server->getTasks().size() > 0) {
Toshihiro Shimizu 890ddd
			taskId = server->getTasks()[0];
Toshihiro Shimizu 890ddd
			state = Busy;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			taskId = "";
Toshihiro Shimizu 890ddd
			state = Ready;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return state;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class ServerInitializer : public TThread::Runnable
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ServerInitializer(FarmServerProxy *server) : m_server(server) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void run()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TFarmServer::HwInfo hwInfo;
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			m_server->queryHwInfo(hwInfo);
Toshihiro Shimizu 890ddd
		} catch (TException & /*e*/) {
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_server->m_attached = true;
Toshihiro Shimizu 890ddd
		//m_server->m_maxTaskCount = hwInfo.m_cpuCount;
Toshihiro Shimizu 890ddd
		m_server->m_maxTaskCount = 1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	FarmServerProxy *m_server;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::initServer(FarmServerProxy *server)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFarmServer::HwInfo hwInfo;
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		server->queryHwInfo(hwInfo);
Toshihiro Shimizu 890ddd
	} catch (TException & /*e*/) {
Toshihiro Shimizu 890ddd
		TThread::Executor exec;
Toshihiro Shimizu 890ddd
		exec.addTask(new ServerInitializer(server));
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	server->m_attached = true;
Toshihiro Shimizu 890ddd
	//server->m_maxTaskCount = hwInfo.m_cpuCount;
Toshihiro Shimizu 890ddd
	server->m_maxTaskCount = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	server->m_platform = hwInfo.m_type;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString FarmController::addTask(
Toshihiro Shimizu 890ddd
	const QString &name, const QString &cmdline,
Toshihiro Shimizu 890ddd
	const QString &user, const QString &host,
Toshihiro Shimizu 890ddd
	bool suspended, int priority, TFarmPlatform platform)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString parentId = "";
Toshihiro Shimizu 890ddd
	CtrlFarmTask *task = doAddTask(
Toshihiro Shimizu 890ddd
		QString::number(NextTaskId++), parentId,
Toshihiro Shimizu 890ddd
		name, cmdline, user, host, suspended,
Toshihiro Shimizu 890ddd
		1, priority, platform);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return task->m_id;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TaskStarter : public TThread::Runnable
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TaskStarter(
Toshihiro Shimizu 890ddd
		FarmController *controller,
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task,
Toshihiro Shimizu 890ddd
		FarmServerProxy *server = 0)
Toshihiro Shimizu 890ddd
		: m_controller(controller), m_task(task), m_server(server) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void run();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	FarmController *m_controller;
Toshihiro Shimizu 890ddd
	CtrlFarmTask *m_task;
Toshihiro Shimizu 890ddd
	FarmServerProxy *m_server;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TaskStarter::run()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_task->m_status != Suspended) {
Toshihiro Shimizu 890ddd
		if (m_server)
Toshihiro Shimizu 890ddd
			m_controller->startTask(m_task, m_server);
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			m_controller->tryToStartTask(m_task);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString FarmController::addTask(const TFarmTask &task, bool suspended)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString id = QString::number(NextTaskId++);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	CtrlFarmTask *myTask = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int count = task.getTaskCount();
Toshihiro Shimizu 890ddd
	if (count == 1) {
Toshihiro Shimizu 890ddd
		QString parentId = "";
Toshihiro Shimizu 890ddd
		myTask = doAddTask(
Toshihiro Shimizu 890ddd
			id, parentId,
Toshihiro Shimizu 890ddd
			task.m_name, task.getCommandLine(),
Toshihiro Shimizu 890ddd
			task.m_user, task.m_hostName,
Toshihiro Shimizu 890ddd
			suspended, task.m_stepCount,
Toshihiro Shimizu 890ddd
			task.m_priority, task.m_platform);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		myTask = new CtrlFarmTask(
Toshihiro Shimizu 890ddd
			id, task.m_name, task.getCommandLine(),
Toshihiro Shimizu 890ddd
			task.m_user, task.m_hostName, task.m_stepCount, task.m_priority);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		myTask->m_submissionDate = QDateTime::currentDateTime();
Toshihiro Shimizu 890ddd
		myTask->m_parentId = "";
Toshihiro Shimizu 890ddd
		myTask->m_dependencies = new TFarmTask::Dependencies(*task.m_dependencies);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (suspended)
Toshihiro Shimizu 890ddd
			myTask->m_status = Suspended;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_tasks.insert(std::make_pair(TaskId(id), myTask));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int i = 0; i < count; ++i) {
Toshihiro Shimizu 890ddd
			QString subTaskId = id + "." + QString::number(i);
Toshihiro Shimizu 890ddd
			TFarmTask &tt = const_cast<tfarmtask &="">(task);</tfarmtask>
Toshihiro Shimizu 890ddd
			TFarmTask *subtask = tt.getTask(i);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			CtrlFarmTask *mySubTask = doAddTask(
Toshihiro Shimizu 890ddd
				subTaskId, myTask->m_id,
Toshihiro Shimizu 890ddd
				subtask->m_name, subtask->getCommandLine(),
Toshihiro Shimizu 890ddd
				subtask->m_user, subtask->m_hostName,
Toshihiro Shimizu 890ddd
				suspended, subtask->m_stepCount,
Toshihiro Shimizu 890ddd
				subtask->m_priority, task.m_platform);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			mySubTask->m_dependencies = new TFarmTask::Dependencies(*task.m_dependencies);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			myTask->m_subTasks.push_back(subTaskId);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TThread::Executor executor;
Toshihiro Shimizu 890ddd
	executor.addTask(new TaskStarter(this, myTask));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return id;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::removeTask(const QString &id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator it = m_tasks.find(TaskId(id));</taskid,>
Toshihiro Shimizu 890ddd
	if (it != m_tasks.end()) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = it->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bool aSubtaskIsRunning = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		vector<qstring>::iterator it2 = task->m_subTasks.begin();</qstring>
Toshihiro Shimizu 890ddd
		for (; it2 != task->m_subTasks.end();) {
Toshihiro Shimizu 890ddd
			QString subTaskId = *it2;
Toshihiro Shimizu 890ddd
			map<taskid, *="" ctrlfarmtask="">::iterator it3 = m_tasks.find(TaskId(subTaskId));</taskid,>
Toshihiro Shimizu 890ddd
			if (it3 != m_tasks.end()) {
Toshihiro Shimizu 890ddd
				CtrlFarmTask *subTask = it3->second;
Toshihiro Shimizu 890ddd
				if (subTask->m_status != Running) {
Toshihiro Shimizu 890ddd
					it2 = task->m_subTasks.erase(it2);
Toshihiro Shimizu 890ddd
					m_tasks.erase(it3);
Toshihiro Shimizu 890ddd
					delete it3->second;
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					it2 = task->m_subTasks.erase(it2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					map<qstring, *="" farmserverproxy="">::iterator itServer =</qstring,>
Toshihiro Shimizu 890ddd
						m_servers.find(subTask->m_serverId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					if (itServer != m_servers.end()) {
Toshihiro Shimizu 890ddd
						FarmServerProxy *server = itServer->second;
Toshihiro Shimizu 890ddd
						if (server) {
Toshihiro Shimizu 890ddd
							vector<qstring>::const_iterator it3 = find(</qstring>
Toshihiro Shimizu 890ddd
								server->getTasks().begin(), server->getTasks().end(), subTask->m_id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
							if (it3 != server->getTasks().end()) {
Toshihiro Shimizu 890ddd
								aSubtaskIsRunning = true;
Toshihiro Shimizu 890ddd
								server->terminateTask(subTask->m_id);
Toshihiro Shimizu 890ddd
							}
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					subTask->m_toBeDeleted = true;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				++it2;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (task->m_status != Running || !aSubtaskIsRunning) {
Toshihiro Shimizu 890ddd
			m_tasks.erase(it);
Toshihiro Shimizu 890ddd
			delete it->second;
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			task->m_toBeDeleted = true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::suspendTask(const QString &id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator it = m_tasks.find(TaskId(id));</taskid,>
Toshihiro Shimizu 890ddd
	if (it != m_tasks.end()) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = it->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		vector<qstring>::iterator it2 = task->m_subTasks.begin();</qstring>
Toshihiro Shimizu 890ddd
		for (; it2 != task->m_subTasks.end(); ++it2) {
Toshihiro Shimizu 890ddd
			QString subTaskId = *it2;
Toshihiro Shimizu 890ddd
			map<taskid, *="" ctrlfarmtask="">::iterator it3 = m_tasks.find(TaskId(subTaskId));</taskid,>
Toshihiro Shimizu 890ddd
			if (it3 != m_tasks.end()) {
Toshihiro Shimizu 890ddd
				CtrlFarmTask *subTask = it3->second;
Toshihiro Shimizu 890ddd
				if (subTask->m_status == Running) {
Toshihiro Shimizu 890ddd
					map<qstring, *="" farmserverproxy="">::iterator itServer =</qstring,>
Toshihiro Shimizu 890ddd
						m_servers.find(subTask->m_serverId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					if (itServer != m_servers.end()) {
Toshihiro Shimizu 890ddd
						FarmServerProxy *server = itServer->second;
Toshihiro Shimizu 890ddd
						if (server) {
Toshihiro Shimizu 890ddd
							vector<qstring>::const_iterator it3 = find(</qstring>
Toshihiro Shimizu 890ddd
								server->getTasks().begin(), server->getTasks().end(), subTask->m_id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
							if (it3 != server->getTasks().end())
Toshihiro Shimizu 890ddd
								server->terminateTask(subTask->m_id);
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				subTask->m_status = Suspended;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		task->m_status = Suspended;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::activateTask(const QString &id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::restartTask(const QString &id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// la scelta del server e' lasciata al controller
Toshihiro Shimizu 890ddd
	FarmServerProxy *server = 0;
Toshihiro Shimizu 890ddd
	doRestartTask(id, true, server);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::doRestartTask(const QString &id, bool fromClient, FarmServerProxy *server)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator it = m_tasks.find(TaskId(id));</taskid,>
Toshihiro Shimizu 890ddd
	if (it != m_tasks.end()) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = it->second;
Toshihiro Shimizu 890ddd
		if (task->m_status != Running) {
Toshihiro Shimizu 890ddd
			if (fromClient) {
Toshihiro Shimizu 890ddd
				task->m_failedOnServers.clear();
Toshihiro Shimizu 890ddd
				task->m_status = Waiting;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			task->m_completionDate = QDateTime();
Toshihiro Shimizu 890ddd
			task->m_server = "";
Toshihiro Shimizu 890ddd
			task->m_serverId = "";
Toshihiro Shimizu 890ddd
			task->m_failedSteps = task->m_successfullSteps = 0;
Toshihiro Shimizu 890ddd
			task->m_failureCount = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (!task->m_subTasks.empty()) {
Toshihiro Shimizu 890ddd
				vector<qstring>::iterator itSubTaskId = task->m_subTasks.begin();</qstring>
Toshihiro Shimizu 890ddd
				for (; itSubTaskId != task->m_subTasks.end(); ++itSubTaskId) {
Toshihiro Shimizu 890ddd
					map<taskid, *="" ctrlfarmtask="">::iterator itSubTask =</taskid,>
Toshihiro Shimizu 890ddd
						m_tasks.find(TaskId(*itSubTaskId));
Toshihiro Shimizu 890ddd
					if (itSubTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
						CtrlFarmTask *subtask = itSubTask->second;
Toshihiro Shimizu 890ddd
						if (fromClient) {
Toshihiro Shimizu 890ddd
							subtask->m_failedOnServers.clear();
Toshihiro Shimizu 890ddd
							subtask->m_status = Waiting;
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						subtask->m_completionDate = QDateTime();
Toshihiro Shimizu 890ddd
						subtask->m_server = "";
Toshihiro Shimizu 890ddd
						subtask->m_serverId = "";
Toshihiro Shimizu 890ddd
						subtask->m_failedSteps = subtask->m_successfullSteps = 0;
Toshihiro Shimizu 890ddd
						subtask->m_failureCount = 0;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TThread::Executor executor;
Toshihiro Shimizu 890ddd
			executor.addTask(new TaskStarter(this, task, server));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::getTasks(vector<qstring> &tasks)</qstring>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator it = m_tasks.begin();</taskid,>
Toshihiro Shimizu 890ddd
	for (; it != m_tasks.end(); ++it) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = it->second;
Toshihiro Shimizu 890ddd
		tasks.push_back(task->m_id);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::getTasks(const QString &parentId, vector<qstring> &tasks)</qstring>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator it = m_tasks.begin();</taskid,>
Toshihiro Shimizu 890ddd
	for (; it != m_tasks.end(); ++it) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = it->second;
Toshihiro Shimizu 890ddd
		if (task->m_parentId == parentId)
Toshihiro Shimizu 890ddd
			tasks.push_back(task->m_id);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::getTasks(const QString &parentId, vector<taskshortinfo> &tasks)</taskshortinfo>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	tasks.clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator it = m_tasks.begin();</taskid,>
Toshihiro Shimizu 890ddd
	for (; it != m_tasks.end(); ++it) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = it->second;
Toshihiro Shimizu 890ddd
		if (task->m_parentId == parentId)
Toshihiro Shimizu 890ddd
			tasks.push_back(TaskShortInfo(task->m_id, task->m_name, task->m_status));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  map<taskid, ctrlfarmtask*="">::iterator it = m_tasks.find(parentId);</taskid,>
Toshihiro Shimizu 890ddd
  if (it != m_tasks.end())
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    CtrlFarmTask *task = it->second;
Shinya Kitaoka 3bfa54
    vector<std::string>::iterator itSubTakId = task->m_subTasks.begin();</std::string>
Toshihiro Shimizu 890ddd
    for ( ; itSubTakId != task->m_subTasks.end(); ++itSubTakId)
Toshihiro Shimizu 890ddd
    {
Shinya Kitaoka 3bfa54
      map<std::string, ctrlfarmtask*="">::iterator itSubTask = m_tasks.find(*itSubTakId);</std::string,>
Toshihiro Shimizu 890ddd
      if (itSubTask != m_tasks.end())
Toshihiro Shimizu 890ddd
      {
Toshihiro Shimizu 890ddd
        CtrlFarmTask *subTask = itSubTask->second;
Toshihiro Shimizu 890ddd
        tasks.push_back(TaskShortInfo(*itSubTakId, subTask->m_name, subTask->m_status));
Toshihiro Shimizu 890ddd
      }
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::queryTaskInfo(const QString &id, TFarmTask &task)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator it = m_tasks.find(TaskId(id));</taskid,>
Toshihiro Shimizu 890ddd
	if (it != m_tasks.end()) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *tt = it->second;
Toshihiro Shimizu 890ddd
		task = *tt;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		map<qstring, *="" farmserverproxy="">::iterator it2 = m_servers.find(it->second->m_serverId);</qstring,>
Toshihiro Shimizu 890ddd
		if (it2 != m_servers.end()) {
Toshihiro Shimizu 890ddd
			FarmServerProxy *server = it2->second;
Toshihiro Shimizu 890ddd
			task.m_server = server->getHostName();
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			task.m_server = "";
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		task.m_status = TaskUnknown;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::queryTaskShortInfo(
Toshihiro Shimizu 890ddd
	const QString &id, QString &parentId,
Toshihiro Shimizu 890ddd
	QString &name, TaskState &status)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator it = m_tasks.find(TaskId(id));</taskid,>
Toshihiro Shimizu 890ddd
	if (it != m_tasks.end()) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = it->second;
Toshihiro Shimizu 890ddd
		parentId = task->m_parentId;
Toshihiro Shimizu 890ddd
		name = task->m_name;
Toshihiro Shimizu 890ddd
		status = task->m_status;
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		status = TaskUnknown;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::attachServer(const QString &name, const QString &addr, int port)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FarmServerProxy *server = 0;
Toshihiro Shimizu 890ddd
	map<qstring, *="" farmserverproxy="">::iterator it = m_servers.begin();</qstring,>
Toshihiro Shimizu 890ddd
	for (; it != m_servers.end(); ++it) {
Toshihiro Shimizu 890ddd
		FarmServerProxy *s = it->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (STRICMP(s->getHostName(), name) == 0 ||
Toshihiro Shimizu 890ddd
			STRICMP(s->getIpAddress(), addr) == 0) {
Toshihiro Shimizu 890ddd
			server = s;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!server) {
Toshihiro Shimizu 890ddd
		server = new FarmServerProxy(name, addr, port);
Toshihiro Shimizu 890ddd
		m_servers.insert(make_pair(QString(addr), server));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	initServer(server);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::detachServer(const QString &name, const QString &addr, int port)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<qstring, *="" farmserverproxy="">::iterator it = m_servers.begin();</qstring,>
Toshihiro Shimizu 890ddd
	for (; it != m_servers.end(); ++it) {
Toshihiro Shimizu 890ddd
		FarmServerProxy *s = it->second;
Toshihiro Shimizu 890ddd
		if (STRICMP(s->getHostName(), name) == 0 ||
Toshihiro Shimizu 890ddd
			STRICMP(s->getIpAddress(), addr) == 0) {
Toshihiro Shimizu 890ddd
			s->m_attached = false;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::taskSubmissionError(const QString &taskId, int errCode)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FarmServerProxy *server = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator itTask = m_tasks.find(TaskId(taskId));</taskid,>
Toshihiro Shimizu 890ddd
	if (itTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = itTask->second;
Toshihiro Shimizu 890ddd
		task->m_status = Aborted;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		task->m_completionDate = QDateTime::currentDateTime();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (task->m_toBeDeleted)
Toshihiro Shimizu 890ddd
			m_tasks.erase(itTask);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		CtrlFarmTask *parentTask = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (task->m_parentId != "") {
Toshihiro Shimizu 890ddd
			map<taskid, *="" ctrlfarmtask="">::iterator itParent = m_tasks.find(TaskId(task->m_parentId));</taskid,>
Toshihiro Shimizu 890ddd
			if (itParent != m_tasks.end()) {
Toshihiro Shimizu 890ddd
				parentTask = itParent->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				TaskState parentTaskState = Aborted;
Toshihiro Shimizu 890ddd
				std::vector<qstring>::iterator itSubTaskId = parentTask->m_subTasks.begin();</qstring>
Toshihiro Shimizu 890ddd
				for (; itSubTaskId != parentTask->m_subTasks.end(); ++itSubTaskId) {
Toshihiro Shimizu 890ddd
					QString subTaskId = *itSubTaskId;
Toshihiro Shimizu 890ddd
					map<taskid, *="" ctrlfarmtask="">::iterator itSubTask = m_tasks.find(TaskId(subTaskId));</taskid,>
Toshihiro Shimizu 890ddd
					if (itSubTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
						CtrlFarmTask *subTask = itSubTask->second;
Toshihiro Shimizu 890ddd
						if (subTask->m_status == Running || subTask->m_status == Waiting) {
Toshihiro Shimizu 890ddd
							parentTaskState = Running;
Toshihiro Shimizu 890ddd
							break;
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				parentTask->m_status = parentTaskState;
Toshihiro Shimizu 890ddd
				if (parentTask->m_status == Aborted || parentTask->m_status == Aborted) {
Toshihiro Shimizu 890ddd
					parentTask->m_completionDate = task->m_completionDate;
Toshihiro Shimizu 890ddd
					if (parentTask->m_toBeDeleted)
Toshihiro Shimizu 890ddd
						m_tasks.erase(itParent);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		map<qstring, *="" farmserverproxy="">::iterator itServer = m_servers.find(task->m_serverId);</qstring,>
Toshihiro Shimizu 890ddd
		if (itServer != m_servers.end())
Toshihiro Shimizu 890ddd
			server = itServer->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (server) {
Toshihiro Shimizu 890ddd
			/*
Toshihiro Shimizu 890ddd
      string msg = "Task " + taskId + " completed on ";
Toshihiro Shimizu 890ddd
      msg += server->getHostName().c_str();
Toshihiro Shimizu 890ddd
      msg += "\n\n";
Toshihiro Shimizu 890ddd
      m_userLog->info(msg);
Toshihiro Shimizu 890ddd
      */
Toshihiro Shimizu 890ddd
			server->removeTask(taskId);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*
Toshihiro Shimizu 890ddd
    if (parentTask && parentTask->m_status == Completed)
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
      m_userLog->info("Task " + parentTask->m_id + " completed\n\n");
Toshihiro Shimizu 890ddd
    }
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
		if (task->m_toBeDeleted)
Toshihiro Shimizu 890ddd
			delete task;
Toshihiro Shimizu 890ddd
		if (parentTask && parentTask->m_toBeDeleted)
Toshihiro Shimizu 890ddd
			delete parentTask;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (server && !server->m_offline) {
Toshihiro Shimizu 890ddd
		// cerca un task da sottomettere al server
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		itTask = m_tasks.begin();
Toshihiro Shimizu 890ddd
		for (; itTask != m_tasks.end(); ++itTask) {
Toshihiro Shimizu 890ddd
			CtrlFarmTask *task = itTask->second;
Toshihiro Shimizu 890ddd
			if (task->m_status == Waiting) {
Toshihiro Shimizu 890ddd
				try {
Toshihiro Shimizu 890ddd
					startTask(task, server);
Toshihiro Shimizu 890ddd
				} catch (TException & /*e*/) {
Toshihiro Shimizu 890ddd
					continue;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::taskProgress(
Toshihiro Shimizu 890ddd
	const QString &taskId,
Toshihiro Shimizu 890ddd
	int step,
Toshihiro Shimizu 890ddd
	int stepCount,
Toshihiro Shimizu 890ddd
	int frameNumber,
Toshihiro Shimizu 890ddd
	FrameState state)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator itTask = m_tasks.find(TaskId(taskId));</taskid,>
Toshihiro Shimizu 890ddd
	if (itTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = itTask->second;
Toshihiro Shimizu 890ddd
		if (state == FrameDone)
Toshihiro Shimizu 890ddd
			++task->m_successfullSteps;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			++task->m_failedSteps;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (task->m_parentId != "") {
Toshihiro Shimizu 890ddd
			map<taskid, *="" ctrlfarmtask="">::iterator itParentTask = m_tasks.find(TaskId(task->m_parentId));</taskid,>
Toshihiro Shimizu 890ddd
			CtrlFarmTask *parentTask = itParentTask->second;
Toshihiro Shimizu 890ddd
			if (state == FrameDone)
Toshihiro Shimizu 890ddd
				++parentTask->m_successfullSteps;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				++parentTask->m_failedSteps;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::taskCompleted(const QString &taskId, int exitCode)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef TRACE
Toshihiro Shimizu 890ddd
	m_userLog->info("completed chiamata\n\n");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	FarmServerProxy *server = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::iterator itTask = m_tasks.find(TaskId(taskId));</taskid,>
Toshihiro Shimizu 890ddd
	if (itTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = itTask->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (task->getCommandLine().contains("runcasm")) {
Toshihiro Shimizu 890ddd
			if (task->m_failedSteps == 0 && task->m_successfullSteps > 0)
Toshihiro Shimizu 890ddd
				task->m_status = Completed;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				task->m_status = Aborted;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			switch (exitCode) {
Toshihiro Shimizu 890ddd
			case 0:
Toshihiro Shimizu 890ddd
				task->m_status = Completed;
Toshihiro Shimizu 890ddd
				if (isAScript(task))
Toshihiro Shimizu 890ddd
					task->m_successfullSteps = task->m_stepCount;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			case RENDER_LICENSE_NOT_FOUND:
Toshihiro Shimizu 890ddd
				task->m_status = Waiting;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			default:
Toshihiro Shimizu 890ddd
				if (task->m_status != Suspended)
Toshihiro Shimizu 890ddd
					task->m_status = Aborted;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		task->m_completionDate = QDateTime::currentDateTime();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (task->m_status == Aborted) {
Toshihiro Shimizu 890ddd
			task->m_failedOnServers.push_back(task->m_serverId);
Toshihiro Shimizu 890ddd
			++task->m_failureCount;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (task->m_toBeDeleted)
Toshihiro Shimizu 890ddd
			m_tasks.erase(itTask);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		CtrlFarmTask *parentTask = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (task->m_parentId != "") {
Toshihiro Shimizu 890ddd
			map<taskid, *="" ctrlfarmtask="">::iterator itParent = m_tasks.find(TaskId(task->m_parentId));</taskid,>
Toshihiro Shimizu 890ddd
			if (itParent != m_tasks.end()) {
Toshihiro Shimizu 890ddd
				parentTask = itParent->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				TaskState parentTaskState = Completed;
Toshihiro Shimizu 890ddd
				bool aSubTaskFailed = false;
Toshihiro Shimizu 890ddd
				bool noSubtaskRunning = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (parentTask->m_status != Suspended && parentTask->m_status != Aborted) {
Toshihiro Shimizu 890ddd
					std::vector<qstring>::iterator itSubTaskId = parentTask->m_subTasks.begin();</qstring>
Toshihiro Shimizu 890ddd
					for (; itSubTaskId != parentTask->m_subTasks.end(); ++itSubTaskId) {
Toshihiro Shimizu 890ddd
						QString subTaskId = *itSubTaskId;
Toshihiro Shimizu 890ddd
						map<taskid, *="" ctrlfarmtask="">::iterator itSubTask = m_tasks.find(TaskId(subTaskId));</taskid,>
Toshihiro Shimizu 890ddd
						if (itSubTask != m_tasks.end()) {
Toshihiro Shimizu 890ddd
							CtrlFarmTask *subTask = itSubTask->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
							if (subTask->m_status == Running || subTask->m_status == Waiting) {
Toshihiro Shimizu 890ddd
								parentTaskState = Running;
Toshihiro Shimizu 890ddd
								noSubtaskRunning = false;
Toshihiro Shimizu 890ddd
								break;
Toshihiro Shimizu 890ddd
							} else if (subTask->m_status == Aborted)
Toshihiro Shimizu 890ddd
								aSubTaskFailed = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
							/*
Toshihiro Shimizu 890ddd
              if (subTask->m_status == Running || subTask->m_status == Waiting)
Toshihiro Shimizu 890ddd
                parentTaskState = Running;
Toshihiro Shimizu 890ddd
              else
Toshihiro Shimizu 890ddd
              if (subTask->m_status == Aborted)
Toshihiro Shimizu 890ddd
                aSubTaskFailed = true;
Toshihiro Shimizu 890ddd
              */
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					aSubTaskFailed = true;
Toshihiro Shimizu 890ddd
				; // si arriva se e solo se il task padre e' stato terminato
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (aSubTaskFailed && noSubtaskRunning)
Toshihiro Shimizu 890ddd
					parentTask->m_status = Aborted;
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					parentTask->m_status = parentTaskState;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (parentTask->m_status == Completed || parentTask->m_status == Aborted) {
Toshihiro Shimizu 890ddd
					parentTask->m_completionDate = task->m_completionDate;
Toshihiro Shimizu 890ddd
					if (parentTask->m_toBeDeleted)
Toshihiro Shimizu 890ddd
						m_tasks.erase(itParent);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		map<qstring, *="" farmserverproxy="">::iterator itServer = m_servers.find(task->m_serverId);</qstring,>
Toshihiro Shimizu 890ddd
		if (itServer != m_servers.end())
Toshihiro Shimizu 890ddd
			server = itServer->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (server) {
Toshihiro Shimizu 890ddd
			if (task->m_status == Completed) {
Toshihiro Shimizu 890ddd
				QString msg = "Task " + taskId + " completed on ";
Toshihiro Shimizu 890ddd
				msg += server->getHostName();
Toshihiro Shimizu 890ddd
				msg += "\n\n";
Toshihiro Shimizu 890ddd
				m_userLog->info(msg);
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				QString msg = "Task " + taskId + " failed on ";
Toshihiro Shimizu 890ddd
				msg += server->getHostName();
Toshihiro Shimizu 890ddd
				msg += " with exit code " + QString::number(exitCode);
Toshihiro Shimizu 890ddd
				msg += "\n\n";
Toshihiro Shimizu 890ddd
				m_userLog->info(msg);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			server->removeTask(taskId);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bool allComplete = false;
Toshihiro Shimizu 890ddd
		if (parentTask && parentTask->m_status == Completed) {
Toshihiro Shimizu 890ddd
			m_userLog->info("Task " + parentTask->m_id + " completed\n\n");
Toshihiro Shimizu 890ddd
			allComplete = true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (task->m_toBeDeleted)
Toshihiro Shimizu 890ddd
			delete task;
Toshihiro Shimizu 890ddd
		if (parentTask && parentTask->m_toBeDeleted)
Toshihiro Shimizu 890ddd
			delete parentTask;
Toshihiro Shimizu 890ddd
		//*
Toshihiro Shimizu 890ddd
		if (allComplete) {
Toshihiro Shimizu 890ddd
			activateReadyServers();
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		//*/
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (server && !server->m_offline && exitCode != RENDER_LICENSE_NOT_FOUND) {
Toshihiro Shimizu 890ddd
		// cerca un task da sottomettere al server
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = getTaskToStart(server);
Toshihiro Shimizu 890ddd
		if (task) {
Toshihiro Shimizu 890ddd
			try {
Toshihiro Shimizu 890ddd
				if (task->m_status == Aborted) {
Toshihiro Shimizu 890ddd
					vector<qstring>::iterator it = find(</qstring>
Toshihiro Shimizu 890ddd
						task->m_failedOnServers.begin(),
Toshihiro Shimizu 890ddd
						task->m_failedOnServers.end(),
Toshihiro Shimizu 890ddd
						server->getId());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					if (it == task->m_failedOnServers.end())
Toshihiro Shimizu 890ddd
						doRestartTask(task->m_id, false, server);
Toshihiro Shimizu 890ddd
					else {
Toshihiro Shimizu 890ddd
						doRestartTask(task->m_id, false, 0);
Toshihiro Shimizu 890ddd
						CtrlFarmTask *nextTask = getNextTaskToStart(task, server);
Toshihiro Shimizu 890ddd
						if (nextTask)
Toshihiro Shimizu 890ddd
							startTask(nextTask, server);
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				} else
Toshihiro Shimizu 890ddd
					startTask(task, server);
Toshihiro Shimizu 890ddd
			} catch (TException & /*e*/) {
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::getServers(vector<serveridentity> &servers)</serveridentity>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<qstring, *="" farmserverproxy="">::iterator it = m_servers.begin();</qstring,>
Toshihiro Shimizu 890ddd
	for (; it != m_servers.end(); ++it) {
Toshihiro Shimizu 890ddd
		FarmServerProxy *server = it->second;
Toshihiro Shimizu 890ddd
		servers.push_back(ServerIdentity(server->m_addr, server->m_hostName));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ServerState FarmController::queryServerState2(const QString &id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ServerState state = ServerUnknown;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map<qstring, *="" farmserverproxy="">::iterator it = m_servers.find(id);</qstring,>
Toshihiro Shimizu 890ddd
	if (it != m_servers.end()) {
Toshihiro Shimizu 890ddd
		FarmServerProxy *server = it->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		QString taskId;
Toshihiro Shimizu 890ddd
		state = getServerState(server, taskId);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return state;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::queryServerInfo(const QString &id, ServerInfo &info)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<qstring, *="" farmserverproxy="">::iterator it = m_servers.find(id);</qstring,>
Toshihiro Shimizu 890ddd
	if (it != m_servers.end()) {
Toshihiro Shimizu 890ddd
		FarmServerProxy *server = it->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		info.m_name = server->getHostName();
Toshihiro Shimizu 890ddd
		info.m_ipAddress = server->getIpAddress();
Toshihiro Shimizu 890ddd
		info.m_portNumber = QString::number(server->getPort());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		info.m_state = getServerState(server, info.m_currentTaskId);
Toshihiro Shimizu 890ddd
		if (info.m_state != Down && info.m_state != ServerUnknown) {
Toshihiro Shimizu 890ddd
			TFarmServer::HwInfo hwInfo;
Toshihiro Shimizu 890ddd
			server->queryHwInfo(hwInfo);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			info.m_cpuCount = hwInfo.m_cpuCount;
Toshihiro Shimizu 890ddd
			info.m_totPhysMem = hwInfo.m_totPhysMem;
Toshihiro Shimizu 890ddd
			info.m_totVirtMem = hwInfo.m_totVirtMem;
Toshihiro Shimizu 890ddd
			info.m_availPhysMem = hwInfo.m_availPhysMem;
Toshihiro Shimizu 890ddd
			info.m_availVirtMem = hwInfo.m_availVirtMem;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::activateServer(const QString &id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<qstring, *="" farmserverproxy="">::iterator it = m_servers.find(id);</qstring,>
Toshihiro Shimizu 890ddd
	if (it != m_servers.end()) {
Toshihiro Shimizu 890ddd
		FarmServerProxy *server = it->second;
Toshihiro Shimizu 890ddd
		server->m_offline = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int i = 0; i < server->m_maxTaskCount; ++i) {
Toshihiro Shimizu 890ddd
			// cerca un task da sottomettere al server
Toshihiro Shimizu 890ddd
			CtrlFarmTask *task = getTaskToStart(server);
Toshihiro Shimizu 890ddd
			if (task) {
Toshihiro Shimizu 890ddd
				try {
Toshihiro Shimizu 890ddd
					if (task->m_status == Aborted) {
Toshihiro Shimizu 890ddd
						vector<qstring>::iterator it = find(</qstring>
Toshihiro Shimizu 890ddd
							task->m_failedOnServers.begin(),
Toshihiro Shimizu 890ddd
							task->m_failedOnServers.end(),
Toshihiro Shimizu 890ddd
							server->getId());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						if (it == task->m_failedOnServers.end())
Toshihiro Shimizu 890ddd
							doRestartTask(task->m_id, false, server);
Toshihiro Shimizu 890ddd
						else {
Toshihiro Shimizu 890ddd
							doRestartTask(task->m_id, false, 0);
Toshihiro Shimizu 890ddd
							CtrlFarmTask *nextTask = getNextTaskToStart(task, server);
Toshihiro Shimizu 890ddd
							if (nextTask)
Toshihiro Shimizu 890ddd
								startTask(nextTask, server);
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
					} else
Toshihiro Shimizu 890ddd
						startTask(task, server);
Toshihiro Shimizu 890ddd
				} catch (TException & /*e*/) {
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::deactivateServer(const QString &id, bool completeRunningTasks)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	map<qstring, *="" farmserverproxy="">::iterator it = m_servers.find(id);</qstring,>
Toshihiro Shimizu 890ddd
	if (it != m_servers.end()) {
Toshihiro Shimizu 890ddd
		FarmServerProxy *server = it->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		QString taskId;
Toshihiro Shimizu 890ddd
		ServerState state = getServerState(server, taskId);
Toshihiro Shimizu 890ddd
		if (state == Busy) {
Toshihiro Shimizu 890ddd
			const vector<qstring> &tasks = server->getTasks();</qstring>
Toshihiro Shimizu 890ddd
			vector<qstring>::const_iterator it = tasks.begin();</qstring>
Toshihiro Shimizu 890ddd
			for (; it != tasks.end(); ++it) {
Toshihiro Shimizu 890ddd
				server->terminateTask(*it);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			server->m_offline = true;
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			server->m_offline = true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::load(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TIStream is(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_tasks.clear();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string tagName;
Toshihiro Shimizu 890ddd
	is.openChild(tagName);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (tagName == "tfarmdata") {
Toshihiro Shimizu 890ddd
		is.openChild(tagName);
Toshihiro Shimizu 890ddd
		if (tagName == "tfarmtasks") {
Toshihiro Shimizu 890ddd
			while (!is.eos()) {
Toshihiro Shimizu 890ddd
				TPersist *p;
Toshihiro Shimizu 890ddd
				is >> p;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				CtrlFarmTask *task = dynamic_cast<ctrlfarmtask *="">(p);</ctrlfarmtask>
Toshihiro Shimizu 890ddd
				if (task)
Toshihiro Shimizu 890ddd
					m_tasks.insert(make_pair(TaskId(task->m_id), task));
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			is.closeChild();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	is.closeChild();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::const_iterator it = m_tasks.begin();</taskid,>
Toshihiro Shimizu 890ddd
	for (; it != m_tasks.end(); ++it) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = it->second;
Toshihiro Shimizu 890ddd
		if (task->m_parentId != "") {
Toshihiro Shimizu 890ddd
			map<taskid, *="" ctrlfarmtask="">::const_iterator it2 =</taskid,>
Toshihiro Shimizu 890ddd
				m_tasks.find(TaskId(task->m_parentId));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (it2 != m_tasks.end()) {
Toshihiro Shimizu 890ddd
				CtrlFarmTask *parent = it2->second;
Toshihiro Shimizu 890ddd
				parent->m_subTasks.push_back(task->m_id);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::save(const TFilePath &fp) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TOStream os(fp);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	map<std::string, string=""> attributes;</std::string,>
Toshihiro Shimizu 890ddd
	attributes.insert(make_pair("ver", "1.0"));
Toshihiro Shimizu 890ddd
	os.openChild("tfarmdata", attributes);
Toshihiro Shimizu 890ddd
	os.openChild("tfarmtasks");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map<taskid, *="" ctrlfarmtask="">::const_iterator it = m_tasks.begin();</taskid,>
Toshihiro Shimizu 890ddd
	for (; it != m_tasks.end(); ++it) {
Toshihiro Shimizu 890ddd
		CtrlFarmTask *task = it->second;
Toshihiro Shimizu 890ddd
		os << task;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os.closeChild();
Toshihiro Shimizu 890ddd
	os.closeChild();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmController::activateReadyServers()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	map<qstring, *="" farmserverproxy="">::iterator it = m_servers.begin();</qstring,>
Toshihiro Shimizu 890ddd
	for (; it != m_servers.end(); ++it) {
Toshihiro Shimizu 890ddd
		FarmServerProxy *server = it->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ServerState state = queryServerState2(server->getId());
Toshihiro Shimizu 890ddd
		int tasksCount = server->m_tasks.size();
Toshihiro Shimizu 890ddd
		if (state == Ready || state == Busy && tasksCount < server->m_maxTaskCount) {
Toshihiro Shimizu 890ddd
			for (int i = 0; i < (server->m_maxTaskCount - tasksCount); ++i) {
Toshihiro Shimizu 890ddd
				// cerca un task da sottomettere al server
Toshihiro Shimizu 890ddd
				CtrlFarmTask *task = getTaskToStart(server);
Toshihiro Shimizu 890ddd
				if (task) {
Toshihiro Shimizu 890ddd
					try {
Toshihiro Shimizu 890ddd
						if (task->m_status == Aborted) {
Toshihiro Shimizu 890ddd
							vector<qstring>::iterator it = find(</qstring>
Toshihiro Shimizu 890ddd
								task->m_failedOnServers.begin(),
Toshihiro Shimizu 890ddd
								task->m_failedOnServers.end(),
Toshihiro Shimizu 890ddd
								server->getId());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
							if (it == task->m_failedOnServers.end())
Toshihiro Shimizu 890ddd
								doRestartTask(task->m_id, false, server);
Toshihiro Shimizu 890ddd
							else {
Toshihiro Shimizu 890ddd
								doRestartTask(task->m_id, false, 0);
Toshihiro Shimizu 890ddd
								CtrlFarmTask *nextTask = getNextTaskToStart(task, server);
Toshihiro Shimizu 890ddd
								if (nextTask)
Toshihiro Shimizu 890ddd
									startTask(nextTask, server);
Toshihiro Shimizu 890ddd
							}
Toshihiro Shimizu 890ddd
						} else
Toshihiro Shimizu 890ddd
							startTask(task, server);
Toshihiro Shimizu 890ddd
					} catch (TException & /*e*/) {
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class ControllerService : public TService
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	ControllerService()
Toshihiro Shimizu 890ddd
		: TService("ToonzFarmController", "ToonzFarm Controller"), m_controller(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~ControllerService()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		delete m_controller;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void onStart(int argc, char *argv[]);
Toshihiro Shimizu 890ddd
	void onStop();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	static TFilePath getTasksDataFile()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TFilePath fp = getGlobalRoot() + "data" + "tasks.txt";
Toshihiro Shimizu 890ddd
		return fp;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	FarmController *m_controller;
Toshihiro Shimizu 890ddd
	TUserLog *m_userLog;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ControllerService::onStart(int argc, char *argv[])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// Initialize thread components
Toshihiro Shimizu 890ddd
	TThread::init();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
  DebugBreak();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
	if (isRunningAsConsoleApp()) {
Toshihiro Shimizu 890ddd
		// i messaggi verranno ridiretti sullo standard output
Toshihiro Shimizu 890ddd
		m_userLog = new TUserLog();
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		TFilePath lRootDir = getLocalRoot();
Toshihiro Shimizu 890ddd
		bool lRootDirExists = dirExists(lRootDir);
Toshihiro Shimizu 890ddd
		if (!lRootDirExists) {
Toshihiro Shimizu 890ddd
			QString errMsg("Unable to start the Controller");
Toshihiro Shimizu 890ddd
			errMsg += "\n";
Toshihiro Shimizu 890ddd
			errMsg += "The directory specified as Local Root does not exist";
Toshihiro Shimizu 890ddd
			errMsg += "\n";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			addToMessageLog(errMsg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			// exit the program
Toshihiro Shimizu 890ddd
			setStatus(TService::Stopped, NO_ERROR, 0);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePath logFilePath = lRootDir + "controller.log";
Toshihiro Shimizu 890ddd
		m_userLog = new TUserLog(logFilePath);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_userLog->info("ToonzFarm Controller 1.0\n\n");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath globalRoot = getGlobalRoot();
Toshihiro Shimizu 890ddd
	if (globalRoot.isEmpty()) {
kusano 898953
		QString errMsg("Unable to get FARMROOT environment variable\n");
Toshihiro Shimizu 890ddd
		addToMessageLog(errMsg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// exit the program
Toshihiro Shimizu 890ddd
		setStatus(TService::Stopped, NO_ERROR, 0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool globalRootExists = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFileStatus fs(globalRoot);
Toshihiro Shimizu 890ddd
	if (!fs.isDirectory())
Toshihiro Shimizu 890ddd
		globalRootExists = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!globalRootExists) {
Toshihiro Shimizu 890ddd
		QString errMsg("The directory specified as TFARMGLOBALROOT does not exist\n");
Toshihiro Shimizu 890ddd
		addToMessageLog(errMsg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// exit the program
Toshihiro Shimizu 890ddd
		setStatus(TService::Stopped, NO_ERROR, 0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int port = 8000;
Toshihiro Shimizu 890ddd
	QString hostName, addr;
Toshihiro Shimizu 890ddd
	bool ret = loadControllerData(hostName, addr, port);
Toshihiro Shimizu 890ddd
	if (!ret) {
Toshihiro Shimizu 890ddd
		QString msg("Unable to get the port number of ");
Toshihiro Shimizu 890ddd
		msg += TSystem::getHostName();
Toshihiro Shimizu 890ddd
		msg += " from the Controller config file";
Toshihiro Shimizu 890ddd
		msg += "\n";
Toshihiro Shimizu 890ddd
		msg += "Using the default port number ";
Toshihiro Shimizu 890ddd
		msg += QString::number(port);
Toshihiro Shimizu 890ddd
		msg += "\n";
Toshihiro Shimizu 890ddd
		m_userLog->info(msg);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		std::ofstream os("/tmp/.tfarmcontroller.dat");
Toshihiro Shimizu 890ddd
		os << port;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_controller = new FarmController(hostName, addr, port, m_userLog);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// configurazione e inizializzazione dei server lato client
Toshihiro Shimizu 890ddd
	// (il controller e' un client dei ToonzFarm server)
Toshihiro Shimizu 890ddd
	m_controller->loadServersData(globalRoot);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath fp = getTasksDataFile();
Toshihiro Shimizu 890ddd
	if (myDoesExists(fp))
Toshihiro Shimizu 890ddd
		m_controller->load(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// si avvia uno dei task caricati da disco
Toshihiro Shimizu 890ddd
	CtrlFarmTask *task = m_controller->getTaskToStart();
Toshihiro Shimizu 890ddd
	if (task) {
Toshihiro Shimizu 890ddd
		TThread::Executor executor;
Toshihiro Shimizu 890ddd
		executor.addTask(new TaskStarter(m_controller, task));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString msg("Starting Controller on port ");
Toshihiro Shimizu 890ddd
	msg += QString::number(m_controller->m_port);
Toshihiro Shimizu 890ddd
	msg += "\n\n";
Toshihiro Shimizu 890ddd
	m_userLog->info(msg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//std::cout << msg;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QEventLoop eventLoop;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Connect the server's listening finished signal to main loop quit.
Toshihiro Shimizu 890ddd
	QObject::connect(m_controller, SIGNAL(finished()), &eventLoop, SLOT(quit()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Start the TcpIp server's listening thread
Toshihiro Shimizu 890ddd
	m_controller->start();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Enter main event loop
Toshihiro Shimizu 890ddd
	eventLoop.exec();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//----------------------Farm controller loops here------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	msg = "Controller exited with exit code ";
Toshihiro Shimizu 890ddd
	msg += QString::number(m_controller->getExitCode());
Toshihiro Shimizu 890ddd
	msg += "\n";
Toshihiro Shimizu 890ddd
	m_userLog->info(msg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//std::cout << msg;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		remove("/tmp/.tfarmcontroller.dat");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ControllerService::onStop()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//TFilePath fp = getTasksDataFile();
Toshihiro Shimizu 890ddd
	//m_controller->save(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath rootDir = getGlobalRoot();
Toshihiro Shimizu 890ddd
	TFilePath lastUsedIdFilePath = rootDir + "config" + "id.txt";
Toshihiro Shimizu 890ddd
	Tofstream os(lastUsedIdFilePath);
Toshihiro Shimizu 890ddd
	if (os.good())
Toshihiro Shimizu 890ddd
		os << FarmController::NextTaskId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TTcpIpClient client;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int socketId;
Toshihiro Shimizu 890ddd
	int ret = client.connect(TSystem::getHostName(), "", m_controller->getPort(), socketId);
Toshihiro Shimizu 890ddd
	if (ret == OK) {
Toshihiro Shimizu 890ddd
		client.send(socketId, "shutdown");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int main(int argc, char **argv)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QCoreApplication a(argc, argv);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// LO METTO A TRUE COSI' VEDO QUELLO CHE SCRIVE !!
Toshihiro Shimizu 890ddd
	// RIMETTERLO A FALSE ALTRIMENTI NON PARTE COME SERVIZIO SU WIN
Toshihiro Shimizu 890ddd
	bool console = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (argc > 1) {
Toshihiro Shimizu 890ddd
		string serviceName("ToonzFarmController"); //Must be the same of the installer's
Toshihiro Shimizu 890ddd
		string serviceDisplayName = serviceName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TCli::SimpleQualifier consoleQualifier("-console", "Run as console app");
Toshihiro Shimizu 890ddd
		TCli::StringQualifier installQualifier("-install name", "Install service as 'name'");
Toshihiro Shimizu 890ddd
		TCli::SimpleQualifier removeQualifier("-remove", "Remove service");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TCli::Usage usage(argv[0]);
Toshihiro Shimizu 890ddd
		usage.add(consoleQualifier + installQualifier + removeQualifier);
Toshihiro Shimizu 890ddd
		if (!usage.parse(argc, argv))
Toshihiro Shimizu 890ddd
			exit(1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
		if (installQualifier.isSelected()) {
Toshihiro Shimizu 890ddd
			char szPath[512];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (installQualifier.getValue() != "")
Toshihiro Shimizu 890ddd
				serviceDisplayName = installQualifier.getValue();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (GetModuleFileName(NULL, szPath, 512) == 0) {
Toshihiro Shimizu 890ddd
				std::cout << "Unable to install";
Toshihiro Shimizu 890ddd
				std::cout << serviceName << " - ";
Toshihiro Shimizu 890ddd
				std::cout << getLastErrorText().c_str() << std::endl
Toshihiro Shimizu 890ddd
						  << std::endl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				return 0;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			TService::install(
Toshihiro Shimizu 890ddd
				serviceName,
Toshihiro Shimizu 890ddd
				serviceDisplayName,
Toshihiro Shimizu 890ddd
				TFilePath(szPath));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (removeQualifier.isSelected()) {
Toshihiro Shimizu 890ddd
			TService::remove(serviceName);
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (consoleQualifier.isSelected())
Toshihiro Shimizu 890ddd
			console = true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TSystem::hasMainLoop(false);
Toshihiro Shimizu 890ddd
	TService::instance()->run(argc, argv, console);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ControllerService Service;