2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#include "tfarmserver.h"
|
|
|
|
#include "tfarmexecutor.h"
|
|
|
|
#include "tfarmcontroller.h"
|
|
|
|
#include "tthreadmessage.h"
|
|
|
|
#include "tthread.h"
|
|
|
|
#include "tsystem.h"
|
|
|
|
#include "tsmartpointer.h"
|
|
|
|
#include "service.h"
|
|
|
|
#include "tlog.h"
|
|
|
|
#include "tfilepath_io.h"
|
|
|
|
#include "tcli.h"
|
2017-08-03 17:29:04 +12:00
|
|
|
#include "tversion.h"
|
|
|
|
using namespace TVER;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2016-06-14 00:37:09 +12:00
|
|
|
#include <sstream>
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QEventLoop>
|
|
|
|
|
|
|
|
#include "tthread.h"
|
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
#include <iostream>
|
|
|
|
#else
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2021-07-08 13:39:28 +12:00
|
|
|
//#define REDIRECT_OUTPUT
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
#define QUOTE_STR "\""
|
|
|
|
#define CASMPMETER "casmpmeter.exe"
|
|
|
|
#else
|
|
|
|
#define QUOTE_STR "'"
|
|
|
|
#define CASMPMETER "casmpmeter"
|
|
|
|
#endif
|
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifndef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
#define NO_ERROR 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// forward declaration
|
|
|
|
class FarmServer;
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath getGlobalRoot() {
|
2017-08-03 17:29:04 +12:00
|
|
|
TVER::ToonzVersion tver;
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath rootDir;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2017-08-03 17:29:04 +12:00
|
|
|
std::string regpath = "SOFTWARE\\" + tver.getAppName() + "\\" +
|
|
|
|
tver.getAppName() + "\\" + tver.getAppVersionString() +
|
|
|
|
"\\FARMROOT";
|
|
|
|
TFilePath name(regpath);
|
2016-06-15 18:43:10 +12:00
|
|
|
rootDir = TFilePath(TSystem::getSystemValue(name).toStdString());
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2017-08-03 17:29:04 +12:00
|
|
|
// Leggo la localRoot da File txt
|
|
|
|
#ifdef MACOSX
|
|
|
|
// If MACOSX, change to MACOSX path
|
2020-05-23 16:20:51 +12:00
|
|
|
std::string unixpath =
|
|
|
|
"./" + tver.getAppName() + ".app/Contents/Resources/configfarmroot.txt";
|
2017-08-03 17:29:04 +12:00
|
|
|
#else
|
|
|
|
// set path to something suitable for most linux (Unix?) systems
|
2020-05-23 16:20:51 +12:00
|
|
|
std::string unixpath = "/etc/" + tver.getAppName() + "/tahoma.conf";
|
2017-08-03 17:29:04 +12:00
|
|
|
#endif
|
|
|
|
TFilePath name(unixpath);
|
|
|
|
Tifstream is(name);
|
2016-06-15 18:43:10 +12:00
|
|
|
if (is) {
|
|
|
|
char line[1024];
|
|
|
|
is.getline(line, 80);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
char *s = line;
|
|
|
|
while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\"') s++;
|
|
|
|
if (*s != '\0') {
|
|
|
|
char *t = s;
|
|
|
|
while (*t) t++;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string pathName(s, t - 1);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
rootDir = TFilePath(pathName);
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
return rootDir;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath getLocalRoot() {
|
2017-08-03 17:29:04 +12:00
|
|
|
TVER::ToonzVersion tver;
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath lroot;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2017-08-03 17:29:04 +12:00
|
|
|
QString regpath = QString::fromStdString(
|
|
|
|
"SOFTWARE\\" + tver.getAppName() + "\\" + tver.getAppName() + "\\" +
|
|
|
|
tver.getAppVersionString() + "\\FARMROOT");
|
|
|
|
TFilePath name(regpath);
|
2016-06-15 18:43:10 +12:00
|
|
|
lroot = TFilePath(TSystem::getSystemValue(name).toStdString()) +
|
|
|
|
TFilePath("toonzfarm");
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2017-08-03 17:29:04 +12:00
|
|
|
#ifdef MACOSX
|
|
|
|
// If MACOSX, change to MACOSX path
|
2020-05-23 16:20:51 +12:00
|
|
|
std::string unixpath =
|
|
|
|
"./" + tver.getAppName() + ".app/Contents/Resources/configfarmroot.txt";
|
2017-08-03 17:29:04 +12:00
|
|
|
#else
|
|
|
|
// set path to something suitable for most linux (Unix?) systems
|
2020-05-23 02:17:41 +12:00
|
|
|
#ifdef FREEBSD
|
|
|
|
std::string unixpath = "/usr/local/etc/" + tver.getAppName() + "/tahoma.conf";
|
|
|
|
#else
|
2020-05-23 16:20:51 +12:00
|
|
|
std::string unixpath = "/etc/" + tver.getAppName() + "/tahoma.conf";
|
2020-05-23 02:17:41 +12:00
|
|
|
#endif
|
2017-08-03 17:29:04 +12:00
|
|
|
#endif
|
|
|
|
TFilePath name(unixpath);
|
|
|
|
Tifstream is(name);
|
2016-06-15 18:43:10 +12:00
|
|
|
if (is) {
|
|
|
|
char line[1024];
|
|
|
|
is.getline(line, 80);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
char *s = line;
|
|
|
|
while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\"') s++;
|
|
|
|
if (*s != '\0') {
|
|
|
|
char *t = s;
|
|
|
|
while (*t) t++;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string pathName(s, t - 1);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
lroot = TFilePath(pathName);
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
return lroot;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath getBinRoot() {
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
return TSystem::getBinDir();
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
return getLocalRoot() + "bin";
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool dirExists(const TFilePath &dirFp) {
|
|
|
|
bool exists = false;
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
TFileStatus fs(dirFp);
|
|
|
|
exists = fs.isDirectory();
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
int acc = access(::to_string(dirFp).c_str(), 00); // 00 == solo esistenza
|
|
|
|
exists = acc != -1;
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
return exists;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool myDoesExists(const TFilePath &fp) {
|
|
|
|
bool exists = false;
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
TFileStatus fs(fp);
|
|
|
|
exists = fs.doesExist();
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
int acc = access(::to_string(fp).c_str(), 00); // 00 == solo esistenza
|
|
|
|
exists = acc != -1;
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
return exists;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
inline bool isBlank(char c) { return c == ' ' || c == '\t' || c == '\n'; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // anonymous namespace
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class FarmServerService final : public TService {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
FarmServerService(std::ostream &os)
|
|
|
|
: TService("ToonzFarm Server", "ToonzFarm Server")
|
|
|
|
, m_os(os)
|
|
|
|
, m_userLog(0) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
~FarmServerService() { delete m_userLog; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
void onStart(int argc, char *argv[]) override;
|
|
|
|
void onStop() override;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2020-11-13 10:40:58 +13:00
|
|
|
void loadControllerData(QString &hostName, std::string &ipAddr, int &port);
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
void loadDiskMountingPoints(const TFilePath &fp);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void mountDisks();
|
|
|
|
void unmountDisks();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::map<std::string, std::string> m_disks;
|
2020-11-13 10:40:58 +13:00
|
|
|
std::vector<std::string> m_disksMounted;
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int m_port;
|
|
|
|
QString m_addr;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
FarmServer *m_farmServer;
|
|
|
|
std::ostream &m_os;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TUserLog *m_userLog;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
FarmServerService service(std::cout);
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class FarmControllerProxy final : public TSmartObject {
|
2016-06-15 18:43:10 +12:00
|
|
|
TFarmController *m_controller;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
FarmControllerProxy(TFarmController *controller) : m_controller(controller) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
~FarmControllerProxy() { delete m_controller; }
|
|
|
|
TFarmController *getController() const { return m_controller; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
// not implemented
|
|
|
|
FarmControllerProxy(const FarmControllerProxy &);
|
|
|
|
FarmControllerProxy &operator=(const FarmControllerProxy &);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
// non posso usare lo smartpointer di tnzcore perche' non linka su .NET
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class FarmControllerProxyP {
|
|
|
|
FarmControllerProxy *m_proxy;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
FarmControllerProxyP() : m_proxy(0) {}
|
|
|
|
~FarmControllerProxyP() {
|
|
|
|
if (m_proxy) m_proxy->release();
|
|
|
|
}
|
|
|
|
FarmControllerProxyP(const FarmControllerProxyP &src) : m_proxy(src.m_proxy) {
|
|
|
|
if (m_proxy) m_proxy->addRef();
|
|
|
|
}
|
|
|
|
FarmControllerProxyP &operator=(const FarmControllerProxyP &src) {
|
|
|
|
FarmControllerProxyP tmp(*this);
|
2018-08-30 20:18:43 +12:00
|
|
|
std::swap(tmp.m_proxy, m_proxy);
|
2016-06-15 18:43:10 +12:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FarmControllerProxyP &operator=(TFarmController *controller) {
|
|
|
|
if (m_proxy && m_proxy->getController() == controller) return *this;
|
|
|
|
if (m_proxy) m_proxy->release();
|
|
|
|
m_proxy = new FarmControllerProxy(controller);
|
|
|
|
m_proxy->addRef();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TFarmController *operator->() { return getPointer(); }
|
|
|
|
TFarmController *getPointer() const {
|
|
|
|
return m_proxy ? m_proxy->getController() : 0;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class FarmServer final : public TFarmExecutor, public TFarmServer {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
FarmServer(int port, TUserLog *log);
|
|
|
|
~FarmServer();
|
|
|
|
|
|
|
|
void setController(const ControllerData &data) {
|
|
|
|
m_controllerData = data;
|
|
|
|
TFarmController *controller = 0;
|
|
|
|
TFarmControllerFactory factory;
|
|
|
|
factory.create(data, &controller);
|
|
|
|
m_controller = controller;
|
|
|
|
}
|
|
|
|
TFarmController *getController() const { return m_controller.getPointer(); }
|
2020-11-13 10:40:58 +13:00
|
|
|
void setAppPaths(const std::vector<TFilePath> &);
|
2016-06-15 18:43:10 +12:00
|
|
|
|
2020-11-13 10:40:58 +13:00
|
|
|
QString execute(const std::vector<QString> &argv) override;
|
2016-06-15 18:43:10 +12:00
|
|
|
|
|
|
|
// TFarmServer overrides
|
2016-06-19 20:06:29 +12:00
|
|
|
int addTask(const QString &taskid, const QString &cmdline) override;
|
|
|
|
int terminateTask(const QString &taskid) override;
|
2020-11-13 10:40:58 +13:00
|
|
|
int getTasks(std::vector<QString> &tasks) override;
|
2016-06-15 18:43:10 +12:00
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
void queryHwInfo(HwInfo &hwInfo) override;
|
2016-06-15 18:43:10 +12:00
|
|
|
|
|
|
|
void attachController(const ControllerData &data);
|
2016-06-20 14:23:05 +12:00
|
|
|
void attachController(const QString &name, const QString &addr,
|
|
|
|
int port) override {
|
2016-06-15 18:43:10 +12:00
|
|
|
attachController(ControllerData(name, addr, port));
|
|
|
|
}
|
|
|
|
|
|
|
|
void detachController(const ControllerData &data);
|
2016-06-20 14:23:05 +12:00
|
|
|
void detachController(const QString &name, const QString &addr,
|
|
|
|
int port) override {
|
2016-06-15 18:43:10 +12:00
|
|
|
detachController(ControllerData(name, addr, port));
|
|
|
|
}
|
|
|
|
|
|
|
|
// class specific methods
|
|
|
|
void removeTask(const QString &id);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::Executor *m_executor;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
ControllerData m_controllerData;
|
|
|
|
FarmControllerProxyP m_controller;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TThread::Mutex m_mux;
|
2020-11-13 10:40:58 +13:00
|
|
|
std::vector<QString> m_tasks;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TUserLog *m_userLog;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
// vector<TFilePath> m_appPaths;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
// not implemented
|
|
|
|
FarmServer(const FarmServer &);
|
|
|
|
FarmServer &operator=(const FarmServer &);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//===================================================================
|
|
|
|
//===================================================================
|
|
|
|
//
|
|
|
|
// class Task
|
|
|
|
//
|
|
|
|
//===================================================================
|
|
|
|
//===================================================================
|
|
|
|
|
2016-06-29 18:17:12 +12:00
|
|
|
class Task final : public TThread::Runnable {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
Task(const QString &id, const QString &cmdline, TUserLog *log,
|
|
|
|
FarmServer *server, const FarmControllerProxyP &controller)
|
|
|
|
: m_id(id)
|
|
|
|
, m_cmdline(cmdline)
|
|
|
|
, m_log(log)
|
|
|
|
, m_server(server)
|
|
|
|
, m_controller(controller) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-19 20:06:29 +12:00
|
|
|
void run() override;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
QString m_id;
|
|
|
|
QString m_cmdline;
|
|
|
|
TUserLog *m_log;
|
|
|
|
FarmServer *m_server;
|
|
|
|
FarmControllerProxyP m_controller;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
// not implemented
|
|
|
|
Task(const Task &);
|
|
|
|
Task &operator=(const Task &);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
2019-06-30 14:59:14 +12:00
|
|
|
static QString getExeName(bool isComposer) {
|
2018-05-22 21:00:46 +12:00
|
|
|
QString name = isComposer ? "tcomposer" : "tcleanup";
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
return name + ".exe ";
|
2019-06-30 14:59:14 +12:00
|
|
|
#elif defined(MACOSX)
|
2018-05-22 21:00:46 +12:00
|
|
|
TVER::ToonzVersion tver;
|
2019-12-16 13:33:27 +13:00
|
|
|
return "\"./" + QString::fromStdString(tver.getAppName()) +
|
2018-05-22 21:00:46 +12:00
|
|
|
".app/Contents/MacOS/" + name + "\" ";
|
|
|
|
#else
|
|
|
|
return name;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void Task::run() {
|
2018-05-22 21:00:46 +12:00
|
|
|
QString cmdline;
|
|
|
|
|
|
|
|
// ===========
|
|
|
|
// remap commandLine to local executable
|
|
|
|
|
|
|
|
QStringList l = m_cmdline.split(" ");
|
|
|
|
QString appName = l.at(0);
|
|
|
|
// m_log->info(appName);
|
|
|
|
if (appName.contains("tcomposer") || appName.contains("tcleanup")) {
|
|
|
|
bool m_isComposerTask = appName.contains("tcomposer");
|
|
|
|
// m_log->info(QString::number(m_isComposerTask));
|
|
|
|
appName = getExeName(m_isComposerTask);
|
|
|
|
// m_log->info(appName);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
cmdline = appName;
|
|
|
|
// m_log->info(cmdline);
|
|
|
|
for (i = 1; i < l.size(); i++) {
|
|
|
|
cmdline += " ";
|
|
|
|
cmdline += l.at(i);
|
|
|
|
// m_log->info(cmdline);
|
|
|
|
}
|
|
|
|
// m_log->info("remap commandLine to local executable");
|
|
|
|
// m_log->info(appName);
|
|
|
|
} else {
|
|
|
|
cmdline = m_cmdline;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ===========
|
2016-06-15 18:43:10 +12:00
|
|
|
QString logMsg("Starting task at ");
|
|
|
|
logMsg += QDateTime::currentDateTime().toString();
|
|
|
|
logMsg += "\n";
|
2018-05-22 21:00:46 +12:00
|
|
|
logMsg += "\"" + cmdline + "\"";
|
2016-06-15 18:43:10 +12:00
|
|
|
logMsg += "\n\n";
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
m_log->info(logMsg);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2020-05-23 16:20:51 +12:00
|
|
|
// ===========
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
if (m_cmdline.contains("runcasm")) service.mountDisks();
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (m_cmdline.contains(".bat"))
|
2018-05-22 21:00:46 +12:00
|
|
|
cmdline = "cmd /C " + cmdline;
|
2016-06-15 18:43:10 +12:00
|
|
|
else
|
2018-05-22 21:00:46 +12:00
|
|
|
cmdline = cmdline;
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifdef LEVO
|
2016-06-15 18:43:10 +12:00
|
|
|
else {
|
|
|
|
// metto da parte il primo token della command line che e' il nome
|
|
|
|
// dell'eseguibile
|
|
|
|
// Attenzione: case sensitive!
|
|
|
|
QStringList l = m_cmdline.split(" ");
|
|
|
|
// assert(!"CONTROLLARE QUI");
|
|
|
|
QString appName = l.at(1);
|
|
|
|
int i;
|
|
|
|
for (i = 2; i < l.size(); i++) cmdline += l.at(i);
|
|
|
|
|
|
|
|
// cerco se il nome dell'applicazione e' tra quelle del file di
|
|
|
|
// configurazione
|
2020-11-13 10:40:58 +13:00
|
|
|
for (auto const &appPath : m_server->m_appPaths) {
|
2016-06-15 18:43:10 +12:00
|
|
|
if (appPath.getName() == appName.toStdString()) {
|
|
|
|
exename = QString::fromStdWString(appPath.getWideString());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // LEVO
|
|
|
|
|
|
|
|
// cout << exename << endl;
|
|
|
|
// cout << cmdline << endl;
|
|
|
|
|
|
|
|
QProcess process;
|
|
|
|
|
|
|
|
process.start(cmdline);
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
|
|
|
|
int exitCode = process.exitCode();
|
|
|
|
int errorCode = process.error();
|
|
|
|
bool ret = (errorCode != QProcess::UnknownError) || exitCode;
|
|
|
|
|
|
|
|
// int ret=QProcess::execute(/*"C:\\depot\\vincenzo\\toonz\\main\\x86_debug\\"
|
|
|
|
// +*/cmdline);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
QString logMsg("Task aborted ");
|
|
|
|
logMsg += "\n\n";
|
|
|
|
m_log->warning(logMsg);
|
|
|
|
m_controller->taskSubmissionError(m_id, exitCode);
|
|
|
|
} else {
|
|
|
|
logMsg = "Task completed at ";
|
|
|
|
logMsg += QDateTime::currentDateTime().toString();
|
|
|
|
logMsg += "\n\n";
|
|
|
|
|
|
|
|
m_log->info(logMsg);
|
|
|
|
m_controller->taskCompleted(m_id, exitCode);
|
|
|
|
|
|
|
|
// CloseHandle(hJob);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_server->removeTask(m_id);
|
|
|
|
|
|
|
|
//************* COMMENTATO A CAUSA DI UN PROBLEMA SU XP
|
|
|
|
// ora i dischi vengono montati al primo task di tipo "runcasm"
|
|
|
|
// e smontati allo stop del servizio
|
|
|
|
// service.unmountDisks();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
FarmServer::FarmServer(int port, TUserLog *log)
|
2016-06-15 18:43:10 +12:00
|
|
|
: TFarmExecutor(port), m_controller(), m_userLog(log) {
|
|
|
|
TFarmServer::HwInfo hwInfo;
|
|
|
|
queryHwInfo(hwInfo);
|
|
|
|
m_executor = new TThread::Executor;
|
|
|
|
m_executor->setMaxActiveTasks(1);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
FarmServer::~FarmServer() { delete m_executor; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2020-11-13 10:40:58 +13:00
|
|
|
QString FarmServer::execute(const std::vector<QString> &argv) {
|
2016-06-15 18:43:10 +12:00
|
|
|
if (argv.size() > 0) {
|
|
|
|
if (argv[0] == "addTask" && argv.size() == 3) {
|
|
|
|
// assert(!"Da fare");
|
|
|
|
int ret = addTask(argv[1], argv[2]);
|
|
|
|
return QString::number(ret);
|
|
|
|
} else if (argv[0] == "terminateTask" && argv.size() > 1) {
|
|
|
|
int ret = terminateTask(argv[1]);
|
|
|
|
return QString::number(ret);
|
|
|
|
} else if (argv[0] == "getTasks") {
|
2020-11-13 10:40:58 +13:00
|
|
|
std::vector<QString> tasks;
|
2016-06-15 18:43:10 +12:00
|
|
|
int ret = getTasks(tasks);
|
|
|
|
|
|
|
|
QString reply(QString::number(ret));
|
|
|
|
reply += ",";
|
|
|
|
|
2020-11-13 10:40:58 +13:00
|
|
|
for (auto const &e : tasks) {
|
|
|
|
reply += e;
|
2016-06-15 18:43:10 +12:00
|
|
|
reply += ",";
|
|
|
|
}
|
|
|
|
|
2020-04-12 17:19:20 +12:00
|
|
|
if (!reply.isEmpty()) reply = reply.left(reply.size() - 1);
|
2016-06-15 18:43:10 +12:00
|
|
|
|
|
|
|
return reply;
|
|
|
|
} else if (argv[0] == "queryHwInfo") {
|
|
|
|
TFarmServer::HwInfo hwInfo;
|
|
|
|
queryHwInfo(hwInfo);
|
|
|
|
|
|
|
|
QString ret;
|
|
|
|
ret += QString::number((unsigned long)hwInfo.m_cpuCount);
|
|
|
|
ret += ",";
|
|
|
|
ret += QString::number((unsigned long)(hwInfo.m_totPhysMem / 1024));
|
|
|
|
ret += ",";
|
|
|
|
ret += QString::number((unsigned long)(hwInfo.m_availPhysMem / 1024));
|
|
|
|
ret += ",";
|
|
|
|
ret += QString::number((unsigned long)(hwInfo.m_totVirtMem / 1024));
|
|
|
|
ret += ",";
|
|
|
|
ret += QString::number((unsigned long)(hwInfo.m_availVirtMem / 1024));
|
|
|
|
ret += ",";
|
|
|
|
ret += QString::number(hwInfo.m_type);
|
|
|
|
return ret;
|
|
|
|
} else if (argv[0] == "attachController" && argv.size() > 3) {
|
|
|
|
int port;
|
|
|
|
fromStr(port, argv[3]);
|
|
|
|
attachController(ControllerData(argv[1], argv[2], port));
|
|
|
|
return "";
|
|
|
|
} else if (argv[0] == "detachController" && argv.size() > 3) {
|
|
|
|
int port;
|
|
|
|
fromStr(port, argv[3]);
|
|
|
|
detachController(ControllerData(argv[1], argv[2], port));
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString::number(-1);
|
|
|
|
;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int FarmServer::addTask(const QString &id, const QString &cmdline) {
|
|
|
|
// std::cout << "Server: addTask" << id << cmdline << std::endl;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QString lcmdline = cmdline;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (lcmdline.contains("runcasm")) {
|
|
|
|
// il task e' runcasm
|
|
|
|
TFilePath rootDir = getGlobalRoot();
|
|
|
|
TFilePath logfilePath =
|
|
|
|
(rootDir + "logs" + id.toStdString()).withType(".log");
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
lcmdline +=
|
|
|
|
" -logfile " + QString::fromStdWString(logfilePath.getWideString());
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath casmpmeterFp = getBinRoot() + CASMPMETER;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
lcmdline += " -ac " + QString::fromStdWString(casmpmeterFp.getWideString());
|
|
|
|
lcmdline += " -ac_args " + QString(QUOTE_STR);
|
|
|
|
lcmdline += "$count $total $frame $filename " + id + QUOTE_STR;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (lcmdline.contains("zrender")) lcmdline += " -taskid " + id;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (lcmdline.contains("tcomposer")) {
|
|
|
|
lcmdline += " -farm " + QString::number(m_controllerData.m_port) + "@" +
|
|
|
|
m_controllerData.m_hostName;
|
|
|
|
lcmdline += " -id " + id;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
m_executor->addTask(new Task(id, lcmdline, m_userLog, this, m_controller));
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QMutexLocker sl(&m_mux);
|
|
|
|
m_tasks.push_back(id);
|
|
|
|
return 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int FarmServer::terminateTask(const QString &taskid) {
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
HANDLE hJob = OpenJobObject(MAXIMUM_ALLOWED, // access right
|
|
|
|
TRUE, // inheritance state
|
2016-03-19 06:57:51 +13:00
|
|
|
#if QT_VERSION >= 0x050500
|
2016-06-15 18:43:10 +12:00
|
|
|
taskid.toUtf8()); // job name
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
taskid.toAscii()); // job name
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (hJob != NULL) {
|
|
|
|
BOOL res = TerminateJobObject(hJob, // handle to job
|
|
|
|
2); // exit code
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
return 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2020-11-13 10:40:58 +13:00
|
|
|
int FarmServer::getTasks(std::vector<QString> &tasks) {
|
2016-06-15 18:43:10 +12:00
|
|
|
QMutexLocker sl(&m_mux);
|
|
|
|
tasks = m_tasks;
|
|
|
|
return m_tasks.size();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void FarmServer::queryHwInfo(HwInfo &hwInfo) {
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
MEMORYSTATUS buff;
|
|
|
|
GlobalMemoryStatus(&buff);
|
|
|
|
|
|
|
|
hwInfo.m_totPhysMem = buff.dwTotalPhys;
|
|
|
|
hwInfo.m_availPhysMem = buff.dwAvailPhys;
|
|
|
|
hwInfo.m_totVirtMem = buff.dwTotalVirtual;
|
|
|
|
hwInfo.m_availVirtMem = buff.dwAvailVirtual;
|
|
|
|
hwInfo.m_cpuCount = TSystem::getProcessorCount();
|
|
|
|
hwInfo.m_type = Windows;
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
// We can just retrieve the overall physical memory - the rest is defaulted to
|
|
|
|
// 500 MB
|
2020-05-29 10:17:17 +12:00
|
|
|
hwInfo.m_totPhysMem = TSystem::getMemorySize(true);
|
|
|
|
hwInfo.m_availPhysMem = TSystem::getFreeMemorySize(true);
|
2016-06-15 18:43:10 +12:00
|
|
|
hwInfo.m_totVirtMem = 500000000;
|
|
|
|
hwInfo.m_availVirtMem = 500000000;
|
2020-05-29 10:17:17 +12:00
|
|
|
hwInfo.m_cpuCount = TSystem::getProcessorCount();
|
|
|
|
#ifdef __sgi
|
|
|
|
hwInfo.m_type = Irix;
|
|
|
|
#else
|
2020-11-13 10:40:58 +13:00
|
|
|
hwInfo.m_type = Linux;
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void FarmServer::attachController(const ControllerData &data) {
|
|
|
|
setController(data);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void FarmServer::detachController(const ControllerData &data) {
|
|
|
|
if (m_controllerData == data) {
|
|
|
|
// delete m_controller;
|
|
|
|
m_controller = 0;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void FarmServer::removeTask(const QString &id) {
|
|
|
|
QMutexLocker sl(&m_mux);
|
2020-11-13 10:40:58 +13:00
|
|
|
std::vector<QString>::iterator it = find(m_tasks.begin(), m_tasks.end(), id);
|
2016-06-15 18:43:10 +12:00
|
|
|
if (it != m_tasks.end()) m_tasks.erase(it);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::string getLine(std::istream &is) {
|
|
|
|
std::string out;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
while (!is.eof()) {
|
|
|
|
is.get(c);
|
2020-04-12 17:19:20 +12:00
|
|
|
if (c != '\r') {
|
|
|
|
if (c != '\n') {
|
|
|
|
if (!is.fail()) {
|
2016-06-15 18:43:10 +12:00
|
|
|
out.append(1, c);
|
2020-04-12 17:19:20 +12:00
|
|
|
} else {
|
2016-06-15 18:43:10 +12:00
|
|
|
break;
|
2020-05-23 16:20:51 +12:00
|
|
|
}
|
2020-04-12 17:19:20 +12:00
|
|
|
} else {
|
2016-06-15 18:43:10 +12:00
|
|
|
break;
|
2020-04-12 17:19:20 +12:00
|
|
|
}
|
|
|
|
}
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
|
|
|
return out;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // anonymous namespace
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int inline STRICMP(const QString &a, const QString &b) {
|
|
|
|
return a.compare(b, Qt::CaseSensitive);
|
|
|
|
}
|
|
|
|
int inline STRICMP(const char *a, const char *b) {
|
|
|
|
QString str(a);
|
|
|
|
return str.compare(QString(b), Qt::CaseSensitive);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-07-14 18:51:27 +12:00
|
|
|
static bool loadServerData(const QString &hostname, QString &addr, int &port) {
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath rootDir = getGlobalRoot();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath fp = rootDir + "config" + "servers.txt";
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifndef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
int acc = access(::to_string(fp).c_str(), 00); // 00 == solo esistenza
|
|
|
|
bool fileExists = acc != -1;
|
|
|
|
if (!fileExists) return false;
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Tifstream is(fp);
|
|
|
|
if (!is.good()) return false;
|
|
|
|
while (!is.eof()) {
|
|
|
|
std::string line = getLine(is);
|
|
|
|
std::istringstream iss(line);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string name;
|
|
|
|
std::string ipAddress;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
iss >> name >> ipAddress >> port;
|
|
|
|
if (name[0] == '#') continue;
|
2016-03-19 06:57:51 +13:00
|
|
|
#if QT_VERSION >= 0x050500
|
2016-06-15 18:43:10 +12:00
|
|
|
if (STRICMP(hostname.toUtf8(), name.c_str()) == 0)
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
if (STRICMP(hostname.toAscii(), name.c_str()) == 0)
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
{
|
|
|
|
addr = QString(ipAddress.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void FarmServerService::onStart(int argc, char *argv[]) {
|
|
|
|
// Initialize thread components
|
|
|
|
TThread::init();
|
2017-08-03 17:29:04 +12:00
|
|
|
TVER::ToonzVersion tver;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
// DebugBreak();
|
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath lRootDir = getLocalRoot();
|
|
|
|
TFileStatus fs(lRootDir);
|
|
|
|
bool lRootDirExists = fs.isDirectory();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (!lRootDirExists) {
|
|
|
|
std::string errMsg("Unable to start the Server");
|
|
|
|
errMsg += "\n";
|
2017-08-03 17:29:04 +12:00
|
|
|
errMsg += "The directory " + ::to_string(lRootDir) +
|
|
|
|
" specified as Local Root does not exist";
|
2016-06-15 18:43:10 +12:00
|
|
|
errMsg += "\n";
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
addToMessageLog(errMsg);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
// DEBUG MAC SERVIZIO (DA TOGLIERE)
|
|
|
|
#ifdef MACOSX
|
2016-06-15 18:43:10 +12:00
|
|
|
system("echo 'local root does not exist' >> err.log");
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// exit the program
|
|
|
|
setStatus(TService::Stopped, NO_ERROR, 0);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath gRootDir = getGlobalRoot();
|
|
|
|
if (::to_string(gRootDir) == "") {
|
2017-08-03 17:29:04 +12:00
|
|
|
std::string errMsg("Unable to get TFARMGLOBALROOT environment variable (" +
|
|
|
|
::to_string(gRootDir) + ")");
|
2016-06-15 18:43:10 +12:00
|
|
|
addToMessageLog(errMsg);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
// DEBUG MAC SERVIZIO (DA TOGLIERE)
|
|
|
|
#ifdef MACOSX
|
2016-06-15 18:43:10 +12:00
|
|
|
system("echo 'Unable to set the global root' >> err.log");
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// exit the program
|
|
|
|
setStatus(TService::Stopped, NO_ERROR, 0);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool gRootDirExists = dirExists(gRootDir);
|
|
|
|
;
|
|
|
|
if (!gRootDirExists) {
|
|
|
|
std::string errMsg("Unable to start the Server");
|
|
|
|
errMsg += "\n";
|
|
|
|
errMsg += "The directory " + ::to_string(gRootDir) +
|
|
|
|
" specified as Global Root does not exist";
|
|
|
|
;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
addToMessageLog(errMsg);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
// DEBUG MAC SERVIZIO (DA TOGLIERE)
|
|
|
|
#ifdef MACOSX
|
2016-06-15 18:43:10 +12:00
|
|
|
system("echo 'Global root does not exist' >> err.log");
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// exit the program
|
|
|
|
setStatus(TService::Stopped, NO_ERROR, 0);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// legge dal file di configurazione le informazioni sul controller
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath fp = gRootDir + "config" + "controller.txt";
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
ControllerData controllerData;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
try {
|
|
|
|
::loadControllerData(fp, controllerData);
|
|
|
|
} catch (TException &e) {
|
|
|
|
std::string errMsg("Unable to start the Server");
|
|
|
|
errMsg += "\n";
|
|
|
|
errMsg += ::to_string(e.getMessage());
|
|
|
|
addToMessageLog(errMsg);
|
|
|
|
setStatus(TService::Stopped, NO_ERROR, 0); // exit the program
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (isRunningAsConsoleApp()) {
|
|
|
|
// i messaggi verranno ridiretti sullo standard output
|
|
|
|
m_userLog = new TUserLog();
|
|
|
|
} else {
|
|
|
|
TFilePath logFilePath = lRootDir + "server.log";
|
|
|
|
m_userLog = new TUserLog(logFilePath);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2019-12-24 21:58:05 +13:00
|
|
|
std::string appverinfo = tver.getAppVersionInfo("Farm Server") + "\n\n";
|
2017-08-03 17:29:04 +12:00
|
|
|
m_userLog->info(appverinfo.c_str());
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// legge dal file di configurazione dei server il numero di porta da
|
|
|
|
// utilizzare
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool ret = loadServerData(TSystem::getHostName(), m_addr, m_port);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (!ret) {
|
|
|
|
QString msg("Unable to get the port number of ");
|
|
|
|
msg += TSystem::getHostName();
|
|
|
|
msg += " from the servers config file";
|
|
|
|
msg += "\n";
|
2017-08-03 17:29:04 +12:00
|
|
|
msg += "Using the default port number ";
|
|
|
|
msg += QString::number(m_port);
|
2016-06-15 18:43:10 +12:00
|
|
|
msg += "\n";
|
|
|
|
msg += "\n";
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
m_userLog->info(msg);
|
|
|
|
m_port = 8002;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#ifdef __sgi
|
2016-06-15 18:43:10 +12:00
|
|
|
{
|
|
|
|
std::ofstream os("/tmp/.tfarmserverd.dat");
|
|
|
|
os << m_port;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
m_farmServer = new FarmServer(m_port, m_userLog);
|
|
|
|
m_farmServer->setController(controllerData);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
try {
|
|
|
|
m_farmServer->getController()->attachServer(TSystem::getHostName(), m_addr,
|
|
|
|
m_port);
|
|
|
|
} catch (TException const &) {
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath diskMountingsFilePath = lRootDir + "config" + "diskmap.cfg";
|
|
|
|
if (myDoesExists(diskMountingsFilePath)) {
|
|
|
|
loadDiskMountingPoints(diskMountingsFilePath);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// i dischi vengono montati al primo task di tipo "runcasm"
|
|
|
|
// e smontati allo stop del servizio
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// mountDisks();
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Carica da un file di configurazione i path dei programmi da lanciare.
|
|
|
|
// Per tutti i programmi il cui path non e' contenuto nel file di
|
|
|
|
// configurazione
|
|
|
|
// si assume che il path del folder del programma sia specificato
|
|
|
|
// nella variabile di sistema PATH
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QEventLoop eventLoop;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Connect the server's listening finished signal to main loop quit.
|
|
|
|
QObject::connect(m_farmServer, SIGNAL(finished()), &eventLoop, SLOT(quit()));
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Run the TcpIp server's listening state
|
|
|
|
m_farmServer->start();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Main loop starts here
|
|
|
|
eventLoop.exec();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//----------------------Farm server loops here------------------------
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int rc = m_farmServer->getExitCode();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#ifdef __sgi
|
2016-06-15 18:43:10 +12:00
|
|
|
remove("/tmp/.tfarmserver.dat");
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (rc != 0) {
|
|
|
|
std::string msg("An error occurred starting the ToonzFarm Server");
|
|
|
|
msg += "\n";
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL, rc,
|
|
|
|
0, // Default language
|
|
|
|
(LPTSTR)&lpMsgBuf, 0, NULL);
|
|
|
|
|
|
|
|
msg += std::string((char *)lpMsgBuf);
|
|
|
|
|
|
|
|
// Free the buffer.
|
|
|
|
LocalFree(lpMsgBuf);
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
addToMessageLog(msg);
|
|
|
|
m_userLog->error(QString::fromStdString(msg));
|
|
|
|
setStatus(TService::Stopped, NO_ERROR, 0);
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string msg("Exiting with code ");
|
|
|
|
msg += std::to_string(ret);
|
|
|
|
msg += "\n";
|
|
|
|
m_userLog->info(QString::fromStdString(msg));
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void FarmServerService::onStop() {
|
|
|
|
try {
|
|
|
|
m_farmServer->getController()->detachServer(TSystem::getHostName(), m_addr,
|
|
|
|
m_port);
|
|
|
|
} catch (TException & /*e*/) {
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2020-05-23 16:20:51 +12:00
|
|
|
// i dischi vengono montati al primo task di tipo "runcasm"
|
|
|
|
// e smontati allo stop del servizio
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
unmountDisks();
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TTcpIpClient client;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int socketId;
|
|
|
|
int ret = client.connect(TSystem::getHostName(), "", m_farmServer->getPort(),
|
|
|
|
socketId);
|
|
|
|
if (ret == OK) {
|
|
|
|
client.send(socketId, "shutdown");
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void FarmServerService::loadDiskMountingPoints(const TFilePath &fp) {
|
|
|
|
Tifstream is(fp);
|
|
|
|
if (!is) throw std::string("File " + ::to_string(fp) + " not found");
|
|
|
|
char buffer[1024];
|
|
|
|
while (is.getline(buffer, sizeof(buffer))) {
|
|
|
|
char *s = buffer;
|
|
|
|
while (isBlank(*s)) s++;
|
|
|
|
if (*s == '\0' || *s == '#' || *s == '!') continue;
|
|
|
|
if (*s == '=') continue; // errore: from vuoto
|
|
|
|
char *t = s;
|
|
|
|
while (*t && *t != '=') t++;
|
|
|
|
if (*t != '=') continue; // errore: manca '='
|
|
|
|
char *q = t;
|
|
|
|
while (q > s && isBlank(*(q - 1))) q--;
|
|
|
|
if (q == s)
|
|
|
|
continue; // non dovrebbe succedere mai: prima di '=' tutti blanks
|
|
|
|
std::string from(s, q - s);
|
|
|
|
s = t + 1;
|
|
|
|
while (isBlank(*s)) s++;
|
|
|
|
if (*s == '\0') continue; // errore: dst vuoto
|
|
|
|
t = s;
|
|
|
|
while (*t) t++;
|
|
|
|
while (t > s && isBlank(*(t - 1))) t--;
|
|
|
|
if (t == s) continue; // non dovrebbe succedere mai: dst vuoto
|
2020-11-13 10:40:58 +13:00
|
|
|
std::string dst(s, t - s);
|
2016-06-15 18:43:10 +12:00
|
|
|
m_disks[from] = dst;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void FarmServerService::mountDisks() {
|
|
|
|
std::map<std::string, std::string>::iterator it = m_disks.begin();
|
|
|
|
for (; it != m_disks.end(); ++it) {
|
|
|
|
std::string drive = it->first;
|
|
|
|
std::string remoteName = it->second;
|
|
|
|
|
|
|
|
NETRESOURCE NetResource;
|
|
|
|
NetResource.dwType = RESOURCETYPE_DISK;
|
|
|
|
NetResource.lpLocalName = (LPSTR)drive.c_str(); // "O:";
|
|
|
|
NetResource.lpRemoteName =
|
|
|
|
(LPSTR)remoteName.c_str(); // "\\\\vega\\PERSONALI";
|
|
|
|
NetResource.lpProvider = NULL;
|
|
|
|
|
|
|
|
DWORD res =
|
|
|
|
WNetAddConnection2(&NetResource, // connection details
|
|
|
|
0, // password
|
2016-03-19 06:57:51 +13:00
|
|
|
#if QT_VERSION >= 0x050500
|
2016-06-15 18:43:10 +12:00
|
|
|
TSystem::getUserName().toUtf8(), // user name
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
TSystem::getUserName().toAscii(), // user name
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
0); // connection options
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (res == NO_ERROR) m_disksMounted.push_back(drive);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (res != NO_ERROR && res != ERROR_ALREADY_ASSIGNED) {
|
|
|
|
// ERROR_BAD_NET_NAME
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
DWORD dwLastError;
|
|
|
|
char errorBuf[1024];
|
|
|
|
char nameBuf[1024];
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
DWORD rett =
|
|
|
|
WNetGetLastError(&dwLastError, // error code
|
|
|
|
errorBuf, // error description buffer
|
|
|
|
sizeof(errorBuf), // size of description buffer
|
|
|
|
nameBuf, // buffer for provider name
|
|
|
|
sizeof(nameBuf)); // size of provider name buffer
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string errorMessage("Unable to map ");
|
|
|
|
errorMessage += NetResource.lpRemoteName;
|
|
|
|
errorMessage += " to logic volume ";
|
|
|
|
errorMessage += NetResource.lpLocalName;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
addToMessageLog(errorMessage);
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void FarmServerService::unmountDisks() {
|
2020-11-13 10:40:58 +13:00
|
|
|
for (auto const &drive : m_disksMounted) {
|
2016-06-15 18:43:10 +12:00
|
|
|
DWORD res =
|
|
|
|
WNetCancelConnection2(drive.c_str(), // resource name
|
|
|
|
CONNECT_UPDATE_PROFILE, // connection type
|
|
|
|
TRUE); // unconditional disconnect option
|
|
|
|
|
|
|
|
if (res != NO_ERROR && res != ERROR_NOT_CONNECTED) {
|
|
|
|
std::string errorMessage("Unable to unmap ");
|
|
|
|
errorMessage += drive.c_str();
|
|
|
|
addToMessageLog(errorMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_disksMounted.clear();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
//==============================================================================
|
|
|
|
//==============================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
QCoreApplication a(argc, argv);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool console = false;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (argc > 1) {
|
|
|
|
std::string serviceName(
|
|
|
|
"ToonzFarmServer"); // Must be the same of the installer's
|
|
|
|
std::string serviceDisplayName = serviceName;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TCli::SimpleQualifier consoleQualifier("-console", "Run as console app");
|
|
|
|
TCli::StringQualifier installQualifier("-install name",
|
|
|
|
"Install service as 'name'");
|
|
|
|
TCli::SimpleQualifier removeQualifier("-remove", "Remove service");
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TCli::Usage usage(argv[0]);
|
|
|
|
usage.add(consoleQualifier + installQualifier + removeQualifier);
|
|
|
|
if (!usage.parse(argc, argv)) exit(1);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
if (installQualifier.isSelected()) {
|
|
|
|
char szPath[512];
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (installQualifier.getValue() != "")
|
|
|
|
serviceDisplayName = installQualifier.getValue();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (GetModuleFileName(NULL, szPath, 512) == 0) {
|
|
|
|
std::cout << "Unable to install";
|
|
|
|
std::cout << serviceName << " - ";
|
|
|
|
std::cout << getLastErrorText().c_str() << std::endl << std::endl;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TService::install(serviceName, serviceDisplayName, TFilePath(szPath));
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (removeQualifier.isSelected()) {
|
|
|
|
TService::remove(serviceName);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (consoleQualifier.isSelected()) console = true;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TSystem::hasMainLoop(false);
|
|
|
|
TService::instance()->run(argc, argv, console);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|