Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tfarmserver.h"
Toshihiro Shimizu 890ddd
#include "tfarmexecutor.h"
Toshihiro Shimizu 890ddd
#include "tfarmcontroller.h"
Toshihiro Shimizu 890ddd
#include "tthreadmessage.h"
Toshihiro Shimizu 890ddd
#include "tthread.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tsmartpointer.h"
Toshihiro Shimizu 890ddd
#include "service.h"
Toshihiro Shimizu 890ddd
#include "tlog.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "tcli.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <string></string>
Toshihiro Shimizu 890ddd
#include <map></map>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qstring></qstring>
Toshihiro Shimizu 890ddd
#include <qprocess></qprocess>
Toshihiro Shimizu 890ddd
#include <qcoreapplication></qcoreapplication>
Toshihiro Shimizu 890ddd
#include <qeventloop></qeventloop>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tthread.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include <iostream></iostream>
Toshihiro Shimizu 890ddd
#else
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
//#define REDIRECT_OUPUT
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#define QUOTE_STR "\""
Toshihiro Shimizu 890ddd
#define CASMPMETER "casmpmeter.exe"
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define QUOTE_STR "'"
Toshihiro Shimizu 890ddd
#define CASMPMETER "casmpmeter"
Toshihiro Shimizu 890ddd
#endif
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
#ifdef MACOSX
Toshihiro Shimizu 890ddd
#include <sys sysctl.h=""> //To retrieve MAC HW infos</sys>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Campbell Barton 1db1c0
#ifdef LINUX
Campbell Barton b69688
#include <sys sysctl.h=""></sys>
Campbell Barton 1db1c0
#endif
Campbell Barton 1db1c0
Toshihiro Shimizu 890ddd
// forward declaration
Toshihiro Shimizu 890ddd
class FarmServer;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
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
	// 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
Shinya Kitaoka 3bfa54
			std::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("SOFTWARE\\OpenToonz\\OpenToonz\\1.0\\TOONZROOT");
Toshihiro Shimizu 890ddd
	lroot = TFilePath(TSystem::getSystemValue(name).toStdString()) + TFilePath("toonzfarm");
Toshihiro Shimizu 890ddd
#else
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
Shinya Kitaoka 3bfa54
			std::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
#endif
Toshihiro Shimizu 890ddd
	return lroot;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
TFilePath getAppsCfgFilePath()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  TFilePath appsRoot = getLocalRoot();
Toshihiro Shimizu 890ddd
  return appsRoot + "config" + "apppath.cfg";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath getBinRoot()
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	return TSystem::getBinDir();
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	return getLocalRoot() + "bin";
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
string myGetHostName()
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
  return TSystem::getHostName();
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
  char hostName[MAXHOSTNAMELEN];
Toshihiro Shimizu 890ddd
  gethostname((char*)&hostName, MAXHOSTNAMELEN);
Toshihiro Shimizu 890ddd
  return hostName;
Toshihiro Shimizu 890ddd
#endif
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 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
inline bool isBlank(char c)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return c == ' ' || c == '\t' || c == '\n';
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // anonymous namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class FarmServerService : public TService
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	FarmServerService(std::ostream &os)
Toshihiro Shimizu 890ddd
		: TService("ToonzFarm Server", "ToonzFarm Server"), m_os(os), m_userLog(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~FarmServerService()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		delete m_userLog;
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
	void loadControllerData(QString &hostName, string &ipAddr, int &port);
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	void loadDiskMountingPoints(const TFilePath &fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void mountDisks();
Toshihiro Shimizu 890ddd
	void unmountDisks();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::map<std::string, std::string=""> m_disks;</std::string,>
Shinya Kitaoka 3bfa54
	vector<std::string> m_disksMounted;</std::string>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int m_port;
Toshihiro Shimizu 890ddd
	QString m_addr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	FarmServer *m_farmServer;
Toshihiro Shimizu 890ddd
	std::ostream &m_os;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TUserLog *m_userLog;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FarmServerService service(std::cout);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class FarmControllerProxy : public TSmartObject
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFarmController *m_controller;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	FarmControllerProxy(TFarmController *controller)
Toshihiro Shimizu 890ddd
		: m_controller(controller) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~FarmControllerProxy()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		delete m_controller;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TFarmController *getController() const { return m_controller; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// not implemented
Toshihiro Shimizu 890ddd
	FarmControllerProxy(const FarmControllerProxy &);
Toshihiro Shimizu 890ddd
	FarmControllerProxy &operator=(const FarmControllerProxy &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// non posso usare lo smartpointer di tnzcore perche' non linka su .NET
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class FarmControllerProxyP
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FarmControllerProxy *m_proxy;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	FarmControllerProxyP() : m_proxy(0) {}
Toshihiro Shimizu 890ddd
	~FarmControllerProxyP()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (m_proxy)
Toshihiro Shimizu 890ddd
			m_proxy->release();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	FarmControllerProxyP(const FarmControllerProxyP &src) : m_proxy(src.m_proxy)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (m_proxy)
Toshihiro Shimizu 890ddd
			m_proxy->addRef();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	FarmControllerProxyP &operator=(const FarmControllerProxyP &src)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		FarmControllerProxyP tmp(*this);
Toshihiro Shimizu 890ddd
		tswap(tmp.m_proxy, m_proxy);
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	FarmControllerProxyP &operator=(TFarmController *controller)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (m_proxy && m_proxy->getController() == controller)
Toshihiro Shimizu 890ddd
			return *this;
Toshihiro Shimizu 890ddd
		if (m_proxy)
Toshihiro Shimizu 890ddd
			m_proxy->release();
Toshihiro Shimizu 890ddd
		m_proxy = new FarmControllerProxy(controller);
Toshihiro Shimizu 890ddd
		m_proxy->addRef();
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFarmController *operator->()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return getPointer();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TFarmController *getPointer() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return m_proxy ? m_proxy->getController() : 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class FarmServer : public TFarmExecutor, public TFarmServer
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	FarmServer(int port, TUserLog *log);
Toshihiro Shimizu 890ddd
	~FarmServer();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setController(const ControllerData &data)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_controllerData = data;
Toshihiro Shimizu 890ddd
		TFarmController *controller = 0;
Toshihiro Shimizu 890ddd
		TFarmControllerFactory factory;
Toshihiro Shimizu 890ddd
		factory.create(data, &controller);
Toshihiro Shimizu 890ddd
		m_controller = controller;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TFarmController *getController() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return m_controller.getPointer();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void setAppPaths(const vector<tfilepath> &);</tfilepath>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString execute(const vector<qstring> &argv);</qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// TFarmServer overrides
Toshihiro Shimizu 890ddd
	int addTask(const QString &taskid, const QString &cmdline);
Toshihiro Shimizu 890ddd
	int terminateTask(const QString &taskid);
Toshihiro Shimizu 890ddd
	int getTasks(vector<qstring> &tasks);</qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void queryHwInfo(HwInfo &hwInfo);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void attachController(const ControllerData &data);
Toshihiro Shimizu 890ddd
	void attachController(const QString &name, const QString &addr, int port)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		attachController(ControllerData(name, addr, port));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void detachController(const ControllerData &data);
Toshihiro Shimizu 890ddd
	void detachController(const QString &name, const QString &addr, int port)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		detachController(ControllerData(name, addr, port));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// class specific methods
Toshihiro Shimizu 890ddd
	void removeTask(const QString &id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	TThread::Executor *m_executor;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ControllerData m_controllerData;
Toshihiro Shimizu 890ddd
	FarmControllerProxyP m_controller;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TThread::Mutex m_mux;
Toshihiro Shimizu 890ddd
	vector<qstring> m_tasks;</qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TUserLog *m_userLog;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	//vector<tfilepath> m_appPaths;</tfilepath>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// not implemented
Toshihiro Shimizu 890ddd
	FarmServer(const FarmServer &);
Toshihiro Shimizu 890ddd
	FarmServer &operator=(const FarmServer &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// class Task
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class Task : public TThread::Runnable
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Task(const QString &id,
Toshihiro Shimizu 890ddd
		 const QString &cmdline,
Toshihiro Shimizu 890ddd
		 TUserLog *log,
Toshihiro Shimizu 890ddd
		 FarmServer *server,
Toshihiro Shimizu 890ddd
		 const FarmControllerProxyP &controller)
Toshihiro Shimizu 890ddd
		: m_id(id), m_cmdline(cmdline), m_log(log), m_server(server), m_controller(controller) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void run();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	QString m_id;
Toshihiro Shimizu 890ddd
	QString m_cmdline;
Toshihiro Shimizu 890ddd
	TUserLog *m_log;
Toshihiro Shimizu 890ddd
	FarmServer *m_server;
Toshihiro Shimizu 890ddd
	FarmControllerProxyP m_controller;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	// not implemented
Toshihiro Shimizu 890ddd
	Task(const Task &);
Toshihiro Shimizu 890ddd
	Task &operator=(const Task &);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Task::run()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString logMsg("Starting task at ");
Toshihiro Shimizu 890ddd
	logMsg += QDateTime::currentDateTime().toString();
Toshihiro Shimizu 890ddd
	logMsg += "\n";
Toshihiro Shimizu 890ddd
	logMsg += "\"" + m_cmdline + "\"";
Toshihiro Shimizu 890ddd
	logMsg += "\n\n";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_log->info(logMsg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// ===========
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	if (m_cmdline.contains("runcasm"))
Toshihiro Shimizu 890ddd
		service.mountDisks();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString cmdline;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_cmdline.contains(".bat"))
Toshihiro Shimizu 890ddd
		cmdline = "cmd /C " + m_cmdline;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		cmdline = m_cmdline;
Toshihiro Shimizu 890ddd
#ifdef LEVO
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		// metto da parte il primo token della command line che e' il nome
Toshihiro Shimizu 890ddd
		// dell'eseguibile
Toshihiro Shimizu 890ddd
		// Attenzione: case sensitive!
Toshihiro Shimizu 890ddd
		QStringList l = m_cmdline.split(" ");
Toshihiro Shimizu 890ddd
		//assert(!"CONTROLLARE QUI");
Toshihiro Shimizu 890ddd
		QString appName = l.at(1);
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
		for (i = 2; i < l.size(); i++)
Toshihiro Shimizu 890ddd
			cmdline += l.at(i);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// cerco se il nome dell'applicazione e' tra quelle del file di configurazione
Toshihiro Shimizu 890ddd
		bool foundApp = false;
Toshihiro Shimizu 890ddd
		vector<tfilepath>::iterator it = m_server->m_appPaths.begin();</tfilepath>
Toshihiro Shimizu 890ddd
		for (; it != m_server->m_appPaths.end(); ++it) {
Toshihiro Shimizu 890ddd
			TFilePath appPath = *it;
Toshihiro Shimizu 890ddd
			if (appPath.getName() == appName.toStdString()) {
Toshihiro Shimizu 890ddd
				exename = QString::fromStdWString(appPath.getWideString());
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif //LEVO
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//cout << exename << endl;
Toshihiro Shimizu 890ddd
	//cout << cmdline << endl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QProcess process;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	process.start(cmdline);
Toshihiro Shimizu 890ddd
	process.waitForFinished(-1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int exitCode = process.exitCode();
Toshihiro Shimizu 890ddd
	int errorCode = process.error();
Toshihiro Shimizu 890ddd
	bool ret = (errorCode != QProcess::UnknownError) || exitCode;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//int ret=QProcess::execute(/*"C:\\depot\\vincenzo\\toonz\\main\\x86_debug\\" +*/cmdline);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (ret != 0) {
Toshihiro Shimizu 890ddd
		QString logMsg("Task aborted ");
Toshihiro Shimizu 890ddd
		logMsg += "\n\n";
Toshihiro Shimizu 890ddd
		m_log->warning(logMsg);
Toshihiro Shimizu 890ddd
		m_controller->taskSubmissionError(m_id, exitCode);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		logMsg = "Task completed at ";
Toshihiro Shimizu 890ddd
		logMsg += QDateTime::currentDateTime().toString();
Toshihiro Shimizu 890ddd
		logMsg += "\n\n";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_log->info(logMsg);
Toshihiro Shimizu 890ddd
		m_controller->taskCompleted(m_id, exitCode);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//CloseHandle(hJob);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_server->removeTask(m_id);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//************* COMMENTATO A CAUSA DI UN PROBLEMA SU XP
Toshihiro Shimizu 890ddd
	// ora i dischi vengono montati al primo task di tipo "runcasm"
Toshihiro Shimizu 890ddd
	// e smontati allo stop del servizio
Toshihiro Shimizu 890ddd
	//service.unmountDisks();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FarmServer::FarmServer(int port, TUserLog *log)
Toshihiro Shimizu 890ddd
	: TFarmExecutor(port), m_controller(), m_userLog(log)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFarmServer::HwInfo hwInfo;
Toshihiro Shimizu 890ddd
	queryHwInfo(hwInfo);
Toshihiro Shimizu 890ddd
	m_executor = new TThread::Executor;
Toshihiro Shimizu 890ddd
	m_executor->setMaxActiveTasks(1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FarmServer::~FarmServer()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	delete m_executor;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
inline std::string toString(unsigned long value)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::ostrstream ss;
Toshihiro Shimizu 890ddd
	ss << value << '\0';
Shinya Kitaoka 3bfa54
	std::string s = ss.str();
kogaki 5bbc3c
	ss.freeze(false);
Toshihiro Shimizu 890ddd
	return s;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void FarmServer::setAppPaths(const vector<tfilepath> &appPaths)</tfilepath>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  m_appPaths = appPaths;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
QString FarmServer::execute(const vector<qstring> &argv)</qstring>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
  std::cout << endl << "executing " << argv[0].c_str() << endl << endl;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (argv.size() > 0) {
Toshihiro Shimizu 890ddd
		/*
Toshihiro Shimizu 890ddd
#ifdef _DEBUG
Toshihiro Shimizu 890ddd
    for (int i=0; i
Toshihiro Shimizu 890ddd
      std::cout << argv[i] << " ";
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
		if (argv[0] == "addTask" && argv.size() == 3) {
Toshihiro Shimizu 890ddd
			//assert(!"Da fare");
Toshihiro Shimizu 890ddd
			int ret = addTask(argv[1], argv[2]);
Toshihiro Shimizu 890ddd
			return QString::number(ret);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "terminateTask" && argv.size() > 1) {
Toshihiro Shimizu 890ddd
			int ret = terminateTask(argv[1]);
Toshihiro Shimizu 890ddd
			return QString::number(ret);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "getTasks") {
Toshihiro Shimizu 890ddd
			vector<qstring> tasks;</qstring>
Toshihiro Shimizu 890ddd
			int ret = getTasks(tasks);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			QString reply(QString::number(ret));
Toshihiro Shimizu 890ddd
			reply += ",";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			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.isEmpty())
Toshihiro Shimizu 890ddd
				reply.left(reply.size() - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return reply;
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "queryHwInfo") {
Toshihiro Shimizu 890ddd
			TFarmServer::HwInfo hwInfo;
Toshihiro Shimizu 890ddd
			queryHwInfo(hwInfo);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			QString ret;
Toshihiro Shimizu 890ddd
			ret += QString::number((unsigned long)hwInfo.m_cpuCount);
Toshihiro Shimizu 890ddd
			ret += ",";
Toshihiro Shimizu 890ddd
			ret += QString::number((unsigned long)(hwInfo.m_totPhysMem / 1024));
Toshihiro Shimizu 890ddd
			ret += ",";
Toshihiro Shimizu 890ddd
			ret += QString::number((unsigned long)(hwInfo.m_availPhysMem / 1024));
Toshihiro Shimizu 890ddd
			ret += ",";
Toshihiro Shimizu 890ddd
			ret += QString::number((unsigned long)(hwInfo.m_totVirtMem / 1024));
Toshihiro Shimizu 890ddd
			ret += ",";
Toshihiro Shimizu 890ddd
			ret += QString::number((unsigned long)(hwInfo.m_availVirtMem / 1024));
Toshihiro Shimizu 890ddd
			ret += ",";
Toshihiro Shimizu 890ddd
			ret += QString::number(hwInfo.m_type);
Toshihiro Shimizu 890ddd
			return ret;
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "attachController" && argv.size() > 3) {
Toshihiro Shimizu 890ddd
			int port;
Toshihiro Shimizu 890ddd
			fromStr(port, argv[3]);
Toshihiro Shimizu 890ddd
			attachController(ControllerData(argv[1], argv[2], port));
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "detachController" && argv.size() > 3) {
Toshihiro Shimizu 890ddd
			int port;
Toshihiro Shimizu 890ddd
			fromStr(port, argv[3]);
Toshihiro Shimizu 890ddd
			detachController(ControllerData(argv[1], argv[2], port));
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return QString::number(-1);
Toshihiro Shimizu 890ddd
	;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int FarmServer::addTask(const QString &id, const QString &cmdline)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//std::cout << "Server: addTask" << id << cmdline << std::endl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString lcmdline = cmdline;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (lcmdline.contains("runcasm")) {
Toshihiro Shimizu 890ddd
		// il task e' runcasm
Toshihiro Shimizu 890ddd
		TFilePath rootDir = getGlobalRoot();
Toshihiro Shimizu 890ddd
		TFilePath logfilePath = (rootDir + "logs" + id.toStdString()).withType(".log");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		lcmdline += " -logfile " + QString::fromStdWString(logfilePath.getWideString());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TFilePath casmpmeterFp = getBinRoot() + CASMPMETER;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		lcmdline += " -ac " + QString::fromStdWString(casmpmeterFp.getWideString());
Toshihiro Shimizu 890ddd
		lcmdline += " -ac_args " + QString(QUOTE_STR);
Toshihiro Shimizu 890ddd
		lcmdline += "$count $total $frame $filename " + id + QUOTE_STR;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (lcmdline.contains("zrender"))
Toshihiro Shimizu 890ddd
		lcmdline += " -taskid " + id;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (lcmdline.contains("tcomposer")) {
Toshihiro Shimizu 890ddd
		lcmdline += " -farm " + QString::number(m_controllerData.m_port) + "@" + m_controllerData.m_hostName;
Toshihiro Shimizu 890ddd
		lcmdline += " -id " + id;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_executor->addTask(new Task(id, lcmdline, m_userLog, this, m_controller));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mux);
Toshihiro Shimizu 890ddd
	m_tasks.push_back(id);
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int FarmServer::terminateTask(const QString &taskid)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	HANDLE hJob = OpenJobObject(
Toshihiro Shimizu 890ddd
		MAXIMUM_ALLOWED, // access right
Toshihiro Shimizu 890ddd
		TRUE,			 // inheritance state
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050500
Toshihiro Shimizu 890ddd
		taskid.toUtf8()); // job name
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		taskid.toAscii()); // job name
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (hJob != NULL) {
Toshihiro Shimizu 890ddd
		BOOL res = TerminateJobObject(
Toshihiro Shimizu 890ddd
			hJob, // handle to job
Toshihiro Shimizu 890ddd
			2);   // exit code
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int FarmServer::getTasks(vector<qstring> &tasks)</qstring>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mux);
Toshihiro Shimizu 890ddd
	tasks = m_tasks;
Toshihiro Shimizu 890ddd
	return m_tasks.size();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServer::queryHwInfo(HwInfo &hwInfo)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	MEMORYSTATUS buff;
Toshihiro Shimizu 890ddd
	GlobalMemoryStatus(&buff);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	hwInfo.m_totPhysMem = buff.dwTotalPhys;
Toshihiro Shimizu 890ddd
	hwInfo.m_availPhysMem = buff.dwAvailPhys;
Toshihiro Shimizu 890ddd
	hwInfo.m_totVirtMem = buff.dwTotalVirtual;
Toshihiro Shimizu 890ddd
	hwInfo.m_availVirtMem = buff.dwAvailVirtual;
Toshihiro Shimizu 890ddd
	hwInfo.m_cpuCount = TSystem::getProcessorCount();
Toshihiro Shimizu 890ddd
	hwInfo.m_type = Windows;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
	hwInfo.m_cpuCount = sysconf(_SC_NPROC_CONF);
Toshihiro Shimizu 890ddd
	hwInfo.m_type = Irix;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
Campbell Barton 1db1c0
#ifdef MACOSX
Campbell Barton 1db1c0
	int mib[2];
Campbell Barton b69688
	TINT64 physMemSize;
Toshihiro Shimizu 890ddd
	size_t len;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	mib[0] = CTL_HW;
Toshihiro Shimizu 890ddd
	mib[1] = HW_MEMSIZE;
Toshihiro Shimizu 890ddd
	len = sizeof(physMemSize);
Toshihiro Shimizu 890ddd
	sysctl(mib, 2, &physMemSize, &len, NULL, 0);
Campbell Barton b69688
#endif
Toshihiro Shimizu 890ddd
Campbell Barton b69688
#ifdef LINUX
Campbell Barton b69688
	TINT64 physMemSize = (TINT64)sysconf(_SC_PHYS_PAGES) * (TINT64)sysconf(_SC_PAGE_SIZE);
Campbell Barton 1db1c0
#endif
Campbell Barton 1db1c0
Toshihiro Shimizu 890ddd
	hwInfo.m_cpuCount = TSystem::getProcessorCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//We can just retrieve the overall physical memory - the rest is defaulted to 500 MB
Toshihiro Shimizu 890ddd
	hwInfo.m_totPhysMem = physMemSize;
Toshihiro Shimizu 890ddd
	hwInfo.m_availPhysMem = 500000000;
Toshihiro Shimizu 890ddd
	hwInfo.m_totVirtMem = 500000000;
Toshihiro Shimizu 890ddd
	hwInfo.m_availVirtMem = 500000000;
Toshihiro Shimizu 890ddd
	hwInfo.m_type = Linux;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServer::attachController(const ControllerData &data)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setController(data);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServer::detachController(const ControllerData &data)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_controllerData == data) {
Toshihiro Shimizu 890ddd
		//delete m_controller;
Toshihiro Shimizu 890ddd
		m_controller = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServer::removeTask(const QString &id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mux);
Toshihiro Shimizu 890ddd
	vector<qstring>::iterator it = find(m_tasks.begin(), m_tasks.end(), id);</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
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
std::string getLine(std::istream &is)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::string out;
Toshihiro Shimizu 890ddd
	char c;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (!is.eof()) {
Toshihiro Shimizu 890ddd
		is.get(c);
Toshihiro Shimizu 890ddd
		if (c != '\r')
Toshihiro Shimizu 890ddd
			if (c != '\n')
Toshihiro Shimizu 890ddd
				if (!is.fail())
Toshihiro Shimizu 890ddd
					out.append(1, c);
Toshihiro Shimizu 890ddd
				else
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return out;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // anonymous namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int inline STRICMP(const QString &a, const QString &b) { return a.compare(b, Qt::CaseSensitive); }
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
Toshihiro Shimizu 890ddd
bool loadServerData(const QString &hostname, QString &addr, int &port)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath rootDir = getGlobalRoot();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath fp = rootDir + "config" + "servers.txt";
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifndef _WIN32
Toshihiro Shimizu 890ddd
	int acc = access(toString(fp.getWideString()).c_str(), 00); // 00 == solo esistenza
Toshihiro Shimizu 890ddd
	bool fileExists = acc != -1;
Toshihiro Shimizu 890ddd
	if (!fileExists)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Tifstream is(fp);
Toshihiro Shimizu 890ddd
	if (!is.good())
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	while (!is.eof()) {
Toshihiro Shimizu 890ddd
		/*
Toshihiro Shimizu 890ddd
    char line[256];
Toshihiro Shimizu 890ddd
    is.getline(line, 256);
Toshihiro Shimizu 890ddd
    */
Shinya Kitaoka 3bfa54
		std::string line = getLine(is);
Shinya Kitaoka 3bfa54
		std::istrstream iss(line.c_str());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		char name[80];
Toshihiro Shimizu 890ddd
		char ipAddress[80];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		iss >> name >> ipAddress >> port;
Toshihiro Shimizu 890ddd
		if (name[0] == '#')
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050500
Toshihiro Shimizu 890ddd
		if (STRICMP(hostname.toUtf8(), name) == 0)
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		if (STRICMP(hostname.toAscii(), name) == 0)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			addr = QString(ipAddress);
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServerService::onStart(int argc, char *argv[])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// Initialize thread components
Toshihiro Shimizu 890ddd
	TThread::init();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
//  DebugBreak();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath lRootDir = getLocalRoot();
Toshihiro Shimizu 890ddd
	TFileStatus fs(lRootDir);
Toshihiro Shimizu 890ddd
	bool lRootDirExists = fs.isDirectory();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!lRootDirExists) {
Shinya Kitaoka 3bfa54
		std::string errMsg("Unable to start the Server");
Toshihiro Shimizu 890ddd
		errMsg += "\n";
kusano 898953
		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
// DEBUG MAC SERVIZIO (DA TOGLIERE)
Toshihiro Shimizu 890ddd
#ifdef MACOSX
Toshihiro Shimizu 890ddd
		system("echo 'local root does not exist' >> err.log");
Toshihiro Shimizu 890ddd
#endif
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 gRootDir = getGlobalRoot();
Toshihiro Shimizu 890ddd
	if (toString(gRootDir.getWideString()) == "") {
Shinya Kitaoka 3bfa54
		std::string errMsg("Unable to get TFARMGLOBALROOT environment variable");
Toshihiro Shimizu 890ddd
		addToMessageLog(errMsg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// DEBUG MAC SERVIZIO (DA TOGLIERE)
Toshihiro Shimizu 890ddd
#ifdef MACOSX
Toshihiro Shimizu 890ddd
		system("echo 'Unable to set the global root' >> err.log");
Toshihiro Shimizu 890ddd
#endif
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 gRootDirExists = dirExists(gRootDir);
Toshihiro Shimizu 890ddd
	;
Toshihiro Shimizu 890ddd
	if (!gRootDirExists) {
Shinya Kitaoka 3bfa54
		std::string errMsg("Unable to start the Server");
Toshihiro Shimizu 890ddd
		errMsg += "\n";
Toshihiro Shimizu 890ddd
		errMsg += "The directory " + toString(gRootDir.getWideString()) + " specified as Global Root does not exist";
Toshihiro Shimizu 890ddd
		;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		addToMessageLog(errMsg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// DEBUG MAC SERVIZIO (DA TOGLIERE)
Toshihiro Shimizu 890ddd
#ifdef MACOSX
Toshihiro Shimizu 890ddd
		system("echo 'Global root does not exist' >> err.log");
Toshihiro Shimizu 890ddd
#endif
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
	// legge dal file di configurazione le informazioni sul controller
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath fp = gRootDir + "config" + "controller.txt";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ControllerData controllerData;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		::loadControllerData(fp, controllerData);
Toshihiro Shimizu 890ddd
	} catch (TException &e) {
Shinya Kitaoka 3bfa54
		std::string errMsg("Unable to start the Server");
Toshihiro Shimizu 890ddd
		errMsg += "\n";
Toshihiro Shimizu 890ddd
		errMsg += toString(e.getMessage());
Toshihiro Shimizu 890ddd
		addToMessageLog(errMsg);
Toshihiro Shimizu 890ddd
		setStatus(TService::Stopped, NO_ERROR, 0); // exit the program
Toshihiro Shimizu 890ddd
	}
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 logFilePath = lRootDir + "server.log";
Toshihiro Shimizu 890ddd
		m_userLog = new TUserLog(logFilePath);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_userLog->info("ToonzFarm Server 1.0");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// legge dal file di configurazione dei server il numero di porta da utilizzare
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool ret = loadServerData(TSystem::getHostName(), m_addr, m_port);
Toshihiro Shimizu 890ddd
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 servers config file";
Toshihiro Shimizu 890ddd
		msg += "\n";
Toshihiro Shimizu 890ddd
		msg += "Using the default port number 8002";
Toshihiro Shimizu 890ddd
		msg += "\n";
Toshihiro Shimizu 890ddd
		msg += "\n";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_userLog->info(msg);
Toshihiro Shimizu 890ddd
		m_port = 8002;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		std::ofstream os("/tmp/.tfarmserverd.dat");
Toshihiro Shimizu 890ddd
		os << m_port;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_farmServer = new FarmServer(m_port, m_userLog);
Toshihiro Shimizu 890ddd
	m_farmServer->setController(controllerData);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		m_farmServer->getController()->attachServer(TSystem::getHostName(), m_addr, m_port);
Toshihiro Shimizu 890ddd
	} catch (TException & /*e*/) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	TFilePath diskMountingsFilePath = lRootDir + "config" + "diskmap.cfg";
Toshihiro Shimizu 890ddd
	if (myDoesExists(diskMountingsFilePath)) {
Toshihiro Shimizu 890ddd
		loadDiskMountingPoints(diskMountingsFilePath);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// i dischi vengono montati al primo task di tipo "runcasm"
Toshihiro Shimizu 890ddd
		// e smontati allo stop del servizio
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//mountDisks();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// Carica da un file di configurazione i path dei programmi da lanciare.
Toshihiro Shimizu 890ddd
	// Per tutti i programmi il cui path non e' contenuto nel file di configurazione
Toshihiro Shimizu 890ddd
	// si assume che il path del folder del programma sia specificato
Toshihiro Shimizu 890ddd
	// nella variabile di sistema PATH
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  vector<tfilepath> appPaths;</tfilepath>
Toshihiro Shimizu 890ddd
  TFilePath appsCfgFile = getAppsCfgFilePath();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  Tifstream isAppCfgFile(appsCfgFile);
Toshihiro Shimizu 890ddd
  if (!isAppCfgFile.good())
Toshihiro Shimizu 890ddd
    std::cout << "Error: " << appsCfgFile << endl;
Toshihiro Shimizu 890ddd
  while (!isAppCfgFile.eof())
Toshihiro Shimizu 890ddd
  {
Shinya Kitaoka 3bfa54
    std::string line = getLine(isAppCfgFile);
kogaki 5bbc3c
    istrstream iss(line.c_str());
Toshihiro Shimizu 890ddd
    TFilePath appPath = TFilePath(line);
Toshihiro Shimizu 890ddd
    appPaths.push_back(appPath);
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  m_farmServer->setAppPaths(appPaths);
Toshihiro Shimizu 890ddd
  */
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_farmServer, SIGNAL(finished()), &eventLoop, SLOT(quit()));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Run the TcpIp server's listening state
Toshihiro Shimizu 890ddd
	m_farmServer->start();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Main loop starts here
Toshihiro Shimizu 890ddd
	eventLoop.exec();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//----------------------Farm server loops here------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int rc = m_farmServer->getExitCode();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __sgi
Toshihiro Shimizu 890ddd
	remove("/tmp/.tfarmserver.dat");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (rc != 0) {
Shinya Kitaoka 3bfa54
		std::string msg("An error occurred starting the ToonzFarm Server");
Toshihiro Shimizu 890ddd
		msg += "\n";
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
		LPVOID lpMsgBuf;
Toshihiro Shimizu 890ddd
		FormatMessage(
Toshihiro Shimizu 890ddd
			FORMAT_MESSAGE_ALLOCATE_BUFFER |
Toshihiro Shimizu 890ddd
				FORMAT_MESSAGE_FROM_SYSTEM |
Toshihiro Shimizu 890ddd
				FORMAT_MESSAGE_IGNORE_INSERTS,
Toshihiro Shimizu 890ddd
			NULL,
Toshihiro Shimizu 890ddd
			rc,
Toshihiro Shimizu 890ddd
			0, // Default language
Toshihiro Shimizu 890ddd
			(LPTSTR)&lpMsgBuf,
Toshihiro Shimizu 890ddd
			0,
Toshihiro Shimizu 890ddd
			NULL);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
		msg += std::string((char *)lpMsgBuf);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Free the buffer.
Toshihiro Shimizu 890ddd
		LocalFree(lpMsgBuf);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		addToMessageLog(msg);
Toshihiro Shimizu 890ddd
		m_userLog->error(QString::fromStdString(msg));
Toshihiro Shimizu 890ddd
		setStatus(TService::Stopped, NO_ERROR, 0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::string msg("Exiting with code ");
Toshihiro Shimizu 890ddd
	msg += toString(ret);
Toshihiro Shimizu 890ddd
	msg += "\n";
Toshihiro Shimizu 890ddd
	m_userLog->info(QString::fromStdString(msg));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServerService::onStop()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		m_farmServer->getController()->detachServer(TSystem::getHostName(), m_addr, m_port);
Toshihiro Shimizu 890ddd
	} catch (TException & /*e*/) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// i dischi vengono montati al primo task di tipo "runcasm"
Toshihiro Shimizu 890ddd
// e smontati allo stop del servizio
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	unmountDisks();
Toshihiro Shimizu 890ddd
#endif
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_farmServer->getPort(), socketId);
Toshihiro Shimizu 890ddd
	if (ret == OK) {
Toshihiro Shimizu 890ddd
		client.send(socketId, "shutdown");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServerService::loadDiskMountingPoints(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Tifstream is(fp);
Toshihiro Shimizu 890ddd
	if (!is)
Shinya Kitaoka 3bfa54
		throw std::string("File " + toString(fp.getWideString()) + " not found");
Toshihiro Shimizu 890ddd
	char buffer[1024];
Toshihiro Shimizu 890ddd
	while (is.getline(buffer, sizeof(buffer))) {
Toshihiro Shimizu 890ddd
		char *s = buffer;
Toshihiro Shimizu 890ddd
		while (isBlank(*s))
Toshihiro Shimizu 890ddd
			s++;
Toshihiro Shimizu 890ddd
		if (*s == '\0' || *s == '#' || *s == '!')
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		if (*s == '=')
Toshihiro Shimizu 890ddd
			continue; // errore: from vuoto
Toshihiro Shimizu 890ddd
		char *t = s;
Toshihiro Shimizu 890ddd
		while (*t && *t != '=')
Toshihiro Shimizu 890ddd
			t++;
Toshihiro Shimizu 890ddd
		if (*t != '=')
Toshihiro Shimizu 890ddd
			continue; // errore: manca '='
Toshihiro Shimizu 890ddd
		char *q = t;
Toshihiro Shimizu 890ddd
		while (q > s && isBlank(*(q - 1)))
Toshihiro Shimizu 890ddd
			q--;
Toshihiro Shimizu 890ddd
		if (q == s)
Toshihiro Shimizu 890ddd
			continue; // non dovrebbe succedere mai: prima di '=' tutti blanks
Shinya Kitaoka 3bfa54
		std::string from(s, q - s);
Toshihiro Shimizu 890ddd
		s = t + 1;
Toshihiro Shimizu 890ddd
		while (isBlank(*s))
Toshihiro Shimizu 890ddd
			s++;
Toshihiro Shimizu 890ddd
		if (*s == '\0')
Toshihiro Shimizu 890ddd
			continue; // errore: dst vuoto
Toshihiro Shimizu 890ddd
		t = s;
Toshihiro Shimizu 890ddd
		while (*t)
Toshihiro Shimizu 890ddd
			t++;
Toshihiro Shimizu 890ddd
		while (t > s && isBlank(*(t - 1)))
Toshihiro Shimizu 890ddd
			t--;
Toshihiro Shimizu 890ddd
		if (t == s)
Toshihiro Shimizu 890ddd
			continue; // non dovrebbe succedere mai: dst vuoto
Toshihiro Shimizu 890ddd
		string dst(s, t - s);
Toshihiro Shimizu 890ddd
		m_disks[from] = dst;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServerService::mountDisks()
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::map<std::string, std::string="">::iterator it = m_disks.begin();</std::string,>
Toshihiro Shimizu 890ddd
	for (; it != m_disks.end(); ++it) {
Shinya Kitaoka 3bfa54
		std::string drive = it->first;
Shinya Kitaoka 3bfa54
		std::string remoteName = it->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		NETRESOURCE NetResource;
Toshihiro Shimizu 890ddd
		NetResource.dwType = RESOURCETYPE_DISK;
Toshihiro Shimizu 890ddd
		NetResource.lpLocalName = (LPSTR)drive.c_str();		  // "O:";
Toshihiro Shimizu 890ddd
		NetResource.lpRemoteName = (LPSTR)remoteName.c_str(); // "\\\\vega\\PERSONALI";
Toshihiro Shimizu 890ddd
		NetResource.lpProvider = NULL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		DWORD res = WNetAddConnection2(
Toshihiro Shimizu 890ddd
			&NetResource, // connection details
Toshihiro Shimizu 890ddd
			0,			  // password
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050500
Toshihiro Shimizu 890ddd
			TSystem::getUserName().toUtf8(), // user name
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
			TSystem::getUserName().toAscii(), // user name
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
			0); // connection options
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (res == NO_ERROR)
Toshihiro Shimizu 890ddd
			m_disksMounted.push_back(drive);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (res != NO_ERROR && res != ERROR_ALREADY_ASSIGNED) {
Toshihiro Shimizu 890ddd
			// ERROR_BAD_NET_NAME
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			DWORD dwLastError;
Toshihiro Shimizu 890ddd
			char errorBuf[1024];
Toshihiro Shimizu 890ddd
			char nameBuf[1024];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			DWORD rett = WNetGetLastError(
Toshihiro Shimizu 890ddd
				&dwLastError,	 // error code
Toshihiro Shimizu 890ddd
				errorBuf,		  // error description buffer
Toshihiro Shimizu 890ddd
				sizeof(errorBuf), // size of description buffer
Toshihiro Shimizu 890ddd
				nameBuf,		  // buffer for provider name
Toshihiro Shimizu 890ddd
				sizeof(nameBuf)); // size of provider name buffer
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
			std::string errorMessage("Unable to map ");
Toshihiro Shimizu 890ddd
			errorMessage += NetResource.lpRemoteName;
Toshihiro Shimizu 890ddd
			errorMessage += " to logic volume ";
Toshihiro Shimizu 890ddd
			errorMessage += NetResource.lpLocalName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			addToMessageLog(errorMessage);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FarmServerService::unmountDisks()
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	vector<std::string>::iterator it = m_disksMounted.begin();</std::string>
Toshihiro Shimizu 890ddd
	for (; it != m_disksMounted.end(); ++it) {
Shinya Kitaoka 3bfa54
		std::string drive = *it;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		DWORD res = WNetCancelConnection2(
Toshihiro Shimizu 890ddd
			drive.c_str(),			// resource name
Toshihiro Shimizu 890ddd
			CONNECT_UPDATE_PROFILE, // connection type
Toshihiro Shimizu 890ddd
			TRUE);					// unconditional disconnect option
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (res != NO_ERROR && res != ERROR_NOT_CONNECTED) {
Shinya Kitaoka 3bfa54
			std::string errorMessage("Unable to unmap ");
Toshihiro Shimizu 890ddd
			errorMessage += drive.c_str();
Toshihiro Shimizu 890ddd
			addToMessageLog(errorMessage);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_disksMounted.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif
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
	bool console = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (argc > 1) {
Shinya Kitaoka 3bfa54
		std::string serviceName("ToonzFarmServer"); //Must be the same of the installer's
Shinya Kitaoka 3bfa54
		std::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
}