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