tahoma2d/toonz/sources/toonzfarm/tfarmclient/application.cpp

152 lines
3.7 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "application.h"
#include "tfarmcontroller.h"
#include "ttcpip.h"
#include "tfilepath.h"
#include "tsystem.h"
#include "tthread.h"
#include "tenv.h"
#include "tconvert.h"
#include "tfilepath_io.h"
#include <vector>
#include <set>
#include <assert.h>
#include <fstream>
using namespace std;
//===================================================================
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() {
2016-03-19 06:57:51 +13:00
#ifdef WIN32
2016-06-15 18:43:10 +12:00
TFilePath groot(TSystem::getSystemValue(
TFilePath("SOFTWARE\\Digital Video\\ToonzFarm\\1.0\\GLOBALROOT")));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return groot;
2016-03-19 06:57:51 +13:00
#else
2016-06-15 18:43:10 +12:00
TFilePath name = "TFARMGLOBALROOT";
char *s = getenv(name.getFullPath().c_str());
TFilePath rootDir = string(s ? s : "");
return rootDir;
2016-03-19 06:57:51 +13:00
#endif
}
};
2016-06-15 18:43:10 +12:00
class Application::Imp {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
Imp() : m_farmController(0) {}
~Imp() {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void loadControllerData();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TFarmController *m_farmController;
TFilePath m_currentFolder;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
ControllerData m_controllerData;
2016-03-19 06:57:51 +13:00
};
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void Application::Imp::loadControllerData() {
TFilePath groot = getGlobalRoot();
TFilePath fp = groot + "config" + "controller.txt";
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
::loadControllerData(fp, m_controllerData);
2016-03-19 06:57:51 +13:00
}
//===================================================================
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
//===================================================================
Application *theApp;
bool programEnded = false;
2016-06-15 18:43:10 +12:00
class Cleanup {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
~Cleanup() {
delete theApp;
theApp = 0;
programEnded = true;
}
2016-03-19 06:57:51 +13:00
} cleanup;
//===================================================================
2016-06-15 18:43:10 +12:00
} // end of anonymous namespace
2016-03-19 06:57:51 +13:00
//===================================================================
2016-06-15 18:43:10 +12:00
Application::Application() : m_imp(new Imp) {}
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
Application::~Application() { delete m_imp; }
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
Application *Application::instance() {
assert(!programEnded);
if (!theApp) {
static TThread::Mutex AppMutex;
TThread::ScopedLock sl(AppMutex);
if (programEnded) return 0;
if (!theApp) theApp = new Application;
}
return theApp;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TFarmController *Application::getController() {
return m_imp->m_farmController;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool Application::testControllerConnection() const {
TTcpIpClient client;
int sock;
int ret = client.connect(m_imp->m_controllerData.m_hostName,
m_imp->m_controllerData.m_ipAddress,
m_imp->m_controllerData.m_port, sock);
if (ret == OK) {
client.disconnect(sock);
return true;
}
return false;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void Application::getControllerData(string &hostName, string &ipAddr,
int &port) const {
hostName = m_imp->m_controllerData.m_hostName;
ipAddr = m_imp->m_controllerData.m_ipAddress;
port = m_imp->m_controllerData.m_port;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void Application::init() {
m_imp->loadControllerData();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TFarmControllerFactory factory;
int ret = factory.create(m_imp->m_controllerData, &m_imp->m_farmController);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void Application::setCurrentFolder(const TFilePath &fp) {
m_imp->m_currentFolder = fp;
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TFilePath Application::getCurrentFolder() { return m_imp->m_currentFolder; }