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