Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "serverstatuspage.h"
Toshihiro Shimizu 890ddd
#include "tfarmcontroller.h"
Toshihiro Shimizu 890ddd
#include "application.h"
Toshihiro Shimizu 890ddd
#include "textlist.h"
Toshihiro Shimizu 890ddd
#include "tw/mainshell.h"
Toshihiro Shimizu 890ddd
#include "tw/textfield.h"
Toshihiro Shimizu 890ddd
#include "tw/event.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tw/message.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
#include <set></set>
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class ServerList : public TTextList {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ServerList(TWidget *parent);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void rightButtonDown(const TMouseEvent &e);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void onDeactivate();
Shinya Kitaoka 120a6e
  void onActivate();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPopupMenuItem *m_activationItem;
Shinya Kitaoka 120a6e
  TPopupMenu *m_popupMenu;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ServerList::ServerList(TWidget *parent) : TTextList(parent, "servers") {
Shinya Kitaoka 120a6e
  m_popupMenu = new TPopupMenu(this);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPopupMenuItem *item = new TPopupMenuItem(m_popupMenu, "deactivate");
Shinya Kitaoka 120a6e
  TGuiCommand("deactivate")
Shinya Kitaoka 120a6e
      .setAction(new TCommandAction<serverlist>(this, onDeactivate));</serverlist>
Shinya Kitaoka 120a6e
  TGuiCommand("deactivate").add(item);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  item = new TPopupMenuItem(m_popupMenu, "activate");
Shinya Kitaoka 120a6e
  TGuiCommand("activate")
Shinya Kitaoka 120a6e
      .setAction(new TCommandAction<serverlist>(this, onActivate));</serverlist>
Shinya Kitaoka 120a6e
  TGuiCommand("activate").add(item);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ServerList::rightButtonDown(const TMouseEvent &e) {
Shinya Kitaoka 120a6e
  leftButtonDown(e);
Shinya Kitaoka 120a6e
  TTextListItem *item = getSelectedItem(0);
Shinya Kitaoka 120a6e
  if (item) {
Shinya Kitaoka 120a6e
    TFarmController *controller = Application::instance()->getController();
Shinya Kitaoka 120a6e
    ServerState state;
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      state = controller->queryServerState2(item->getId());
Shinya Kitaoka 120a6e
    } catch (TException &e) {
Shinya Kitaoka 120a6e
      TMessage::error(toString(e.getMessage()));
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (state != Offline) {
Shinya Kitaoka 120a6e
      TGuiCommand("activate").disable();
Shinya Kitaoka 120a6e
      TGuiCommand("deactivate").enable();
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      TGuiCommand("activate").enable();
Shinya Kitaoka 120a6e
      TGuiCommand("deactivate").disable();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TPoint pos =
Shinya Kitaoka 120a6e
        getAbsolutePosition() + TPoint(e.m_pos.x, getSize().ly - e.m_pos.y);
Shinya Kitaoka 120a6e
    m_popupMenu->popup(pos);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ServerList::onDeactivate() {
Shinya Kitaoka 120a6e
  TTextListItem *item = getSelectedItem(0);
Shinya Kitaoka 120a6e
  if (item) {
Shinya Kitaoka 120a6e
    TFarmController *controller = Application::instance()->getController();
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      controller->deactivateServer(item->getId());
Shinya Kitaoka 120a6e
    } catch (TException &e) {
Shinya Kitaoka 120a6e
      TMessage::error(toString(e.getMessage()));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ServerList::onActivate() {
Shinya Kitaoka 120a6e
  TTextListItem *item = getSelectedItem(0);
Shinya Kitaoka 120a6e
  if (item) {
Shinya Kitaoka 120a6e
    TFarmController *controller = Application::instance()->getController();
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      controller->activateServer(item->getId());
Shinya Kitaoka 120a6e
    } catch (TException &e) {
Shinya Kitaoka 120a6e
      TMessage::error(toString(e.getMessage()));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class ServerStatusPage::Data {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Data(ServerStatusPage *parent) {
Shinya Kitaoka 120a6e
    m_serverList = new ServerList(parent);
Shinya Kitaoka 120a6e
    m_serverList->setSelAction(
Shinya Kitaoka 120a6e
        new TTextListAction<data>(this, &Data::onServerSelection));</data>
Shinya Kitaoka 120a6e
    m_name       = new TTextField(parent, "name");
Shinya Kitaoka 120a6e
    m_ipAddress  = new TTextField(parent, "ipAddress");
Shinya Kitaoka 120a6e
    m_portNumber = new TTextField(parent, "portNumber");
Shinya Kitaoka 120a6e
    m_tasks      = new TTextField(parent, "tasks");
Shinya Kitaoka 120a6e
    m_state      = new TTextField(parent, "state");
Shinya Kitaoka 120a6e
    m_cpuCount   = new TTextField(parent, "cpuCount");
Shinya Kitaoka 120a6e
    m_totPhysMem = new TTextField(parent, "totalPhysMemory");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_nameLbl       = new TLabel(parent, "name");
Shinya Kitaoka 120a6e
    m_ipAddressLbl  = new TLabel(parent, "ipAddress");
Shinya Kitaoka 120a6e
    m_portNumberLbl = new TLabel(parent, "portNumber");
Shinya Kitaoka 120a6e
    m_tasksLbl      = new TLabel(parent, "tasks");
Shinya Kitaoka 120a6e
    m_stateLbl      = new TLabel(parent, "state");
Shinya Kitaoka 120a6e
    m_cpuCountLbl   = new TLabel(parent, "cpuCount");
Shinya Kitaoka 120a6e
    m_totPhysMemLbl = new TLabel(parent, "totalPhysMemory");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ~Data() {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void onServerSelection(int index) {
Shinya Kitaoka 120a6e
    TTextListItem *item = m_serverList->getSelectedItem(0);
Shinya Kitaoka 120a6e
    if (item) {
Shinya Kitaoka 120a6e
      showServerInfo(item->getId());
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void showServerInfo(const string &id) {
Shinya Kitaoka 120a6e
    TFarmController *controller = Application::instance()->getController();
Shinya Kitaoka 120a6e
    ServerInfo info;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      controller->queryServerInfo(id, info);
Shinya Kitaoka 120a6e
    } catch (TException &e) {
Shinya Kitaoka 120a6e
      TMessage::error(toString(e.getMessage()));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    switch (info.m_state) {
Shinya Kitaoka 120a6e
    case Ready:
Shinya Kitaoka 120a6e
      m_state->setText("Ready");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case Busy:
Shinya Kitaoka 120a6e
      m_state->setText("Busy");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case NotResponding:
Shinya Kitaoka 120a6e
      m_state->setText("Not Responding");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case Down:
Shinya Kitaoka 120a6e
      m_state->setText("Down");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case Offline:
Shinya Kitaoka 120a6e
      m_state->setText("Offline");
Shinya Kitaoka 120a6e
      break;
Shinya Kitaoka 120a6e
    case ServerUnknown:
Shinya Kitaoka 120a6e
      m_state->setText("");
Shinya Kitaoka 120a6e
      m_name->setText("");
Shinya Kitaoka 120a6e
      m_ipAddress->setText("");
Shinya Kitaoka 120a6e
      m_portNumber->setText("");
Shinya Kitaoka 120a6e
      m_tasks->setText("");
Shinya Kitaoka 120a6e
      m_cpuCount->setText("");
Shinya Kitaoka 120a6e
      m_totPhysMem->setText("");
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_name->setText(info.m_name);
Shinya Kitaoka 120a6e
    m_ipAddress->setText(info.m_ipAddress);
Shinya Kitaoka 120a6e
    m_portNumber->setText(info.m_portNumber);
Shinya Kitaoka 120a6e
    if (info.m_currentTaskId == "")
Shinya Kitaoka 120a6e
      m_tasks->setText("");
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      TFarmTask task;
Shinya Kitaoka 120a6e
      try {
Shinya Kitaoka 120a6e
        controller->queryTaskInfo(info.m_currentTaskId, task);
Shinya Kitaoka 120a6e
        m_tasks->setText("<" + task.m_id + "> " + task.m_name);
Shinya Kitaoka 120a6e
      } catch (TException &e) {
Shinya Kitaoka 120a6e
        m_tasks->setText("");
Shinya Kitaoka 120a6e
        TMessage::error(toString(e.getMessage()));
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (info.m_state != Down) {
Shinya Kitaoka 120a6e
      m_cpuCount->setText(toString(info.m_cpuCount));
Shinya Kitaoka 120a6e
      m_totPhysMem->setText(toString((long)info.m_totPhysMem));
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      m_cpuCount->setText("");
Shinya Kitaoka 120a6e
      m_totPhysMem->setText("");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void addServer(const ServerIdentity &sid) {
Shinya Kitaoka 120a6e
    m_serverList->addItem(new TTextListItem(sid.m_id, sid.m_name));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void configureNotify(const TDimension &size) {
Shinya Kitaoka 120a6e
    const int dx       = 10;
Shinya Kitaoka 120a6e
    const int dy       = 5;
Shinya Kitaoka 120a6e
    const int h        = 20;
Shinya Kitaoka 120a6e
    const int lw       = 120;
Shinya Kitaoka 120a6e
    const int leftSize = tmin(250, size.lx / 3);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    const int w = size.lx - leftSize - lw - dx * 3;
Shinya Kitaoka 120a6e
    int left    = leftSize + dx;
Shinya Kitaoka 120a6e
    int y0      = size.ly - 30;
Shinya Kitaoka 120a6e
    int x0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // prima la parte a sx
Shinya Kitaoka 120a6e
    m_serverList->setGeometry(0, 0, leftSize /*100*/, size.ly - 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // ora la parte a dx
Shinya Kitaoka 120a6e
    x0 = left;
Shinya Kitaoka 120a6e
    m_nameLbl->setGeometry(x0, y0, x0 + lw, y0 + h);
Shinya Kitaoka 120a6e
    x0 += lw + dx;
Shinya Kitaoka 120a6e
    m_name->setGeometry(x0, y0, x0 + w, y0 + h);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    x0 = left;
Shinya Kitaoka 120a6e
    y0 -= h + dy;
Shinya Kitaoka 120a6e
    m_ipAddressLbl->setGeometry(x0, y0, x0 + lw, y0 + h);
Shinya Kitaoka 120a6e
    x0 += lw + dx;
Shinya Kitaoka 120a6e
    m_ipAddress->setGeometry(x0, y0, x0 + w, y0 + h);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    x0 = left;
Shinya Kitaoka 120a6e
    y0 -= h + dy;
Shinya Kitaoka 120a6e
    m_portNumberLbl->setGeometry(x0, y0, x0 + lw, y0 + h);
Shinya Kitaoka 120a6e
    x0 += lw + dx;
Shinya Kitaoka 120a6e
    m_portNumber->setGeometry(x0, y0, x0 + w, y0 + h);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    x0 = left;
Shinya Kitaoka 120a6e
    y0 -= h + dy;
Shinya Kitaoka 120a6e
    m_tasksLbl->setGeometry(x0, y0, x0 + lw, y0 + h);
Shinya Kitaoka 120a6e
    x0 += lw + dx;
Shinya Kitaoka 120a6e
    m_tasks->setGeometry(x0, y0, x0 + w, y0 + h);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    x0 = left;
Shinya Kitaoka 120a6e
    y0 -= h + dy;
Shinya Kitaoka 120a6e
    m_stateLbl->setGeometry(x0, y0, x0 + lw, y0 + h);
Shinya Kitaoka 120a6e
    x0 += lw + dx;
Shinya Kitaoka 120a6e
    m_state->setGeometry(x0, y0, x0 + w, y0 + h);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    x0 = left;
Shinya Kitaoka 120a6e
    y0 -= h + dy;
Shinya Kitaoka 120a6e
    m_cpuCountLbl->setGeometry(x0, y0, x0 + lw, y0 + h);
Shinya Kitaoka 120a6e
    x0 += lw + dx;
Shinya Kitaoka 120a6e
    m_cpuCount->setGeometry(x0, y0, x0 + w, y0 + h);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    x0 = left;
Shinya Kitaoka 120a6e
    y0 -= h + dy;
Shinya Kitaoka 120a6e
    m_totPhysMemLbl->setGeometry(x0, y0, x0 + lw, y0 + h);
Shinya Kitaoka 120a6e
    x0 += lw + dx;
Shinya Kitaoka 120a6e
    m_totPhysMem->setGeometry(x0, y0, x0 + w, y0 + h);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TTextList *m_serverList;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TTextField *m_name, *m_ipAddress, *m_portNumber, *m_tasks, *m_state,
Shinya Kitaoka 120a6e
      *m_cpuCount, *m_totPhysMem;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TLabel *m_nameLbl, *m_ipAddressLbl, *m_portNumberLbl, *m_tasksLbl,
Shinya Kitaoka 120a6e
      *m_stateLbl, *m_cpuCountLbl, *m_totPhysMemLbl;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ServerStatusPage::onActivate() {
Shinya Kitaoka 120a6e
  TFarmController *controller = Application::instance()->getController();
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    vector<serveridentity> servers;</serveridentity>
Shinya Kitaoka 120a6e
    controller->getServers(servers);
Shinya Kitaoka 120a6e
    vector<serveridentity>::iterator it = servers.begin();</serveridentity>
Shinya Kitaoka 120a6e
    for (; it != servers.end(); ++it) m_data->addServer(*it);
Shinya Kitaoka 120a6e
  } catch (TException &e) {
Shinya Kitaoka 120a6e
    TMessage::error(toString(e.getMessage()));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ServerStatusPage::onDeactivate() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ServerStatusPage::ServerStatusPage(TWidget *parent)
Shinya Kitaoka 120a6e
    : TabPage(parent, "Servers") {
Shinya Kitaoka 120a6e
  m_data = new Data(this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ServerStatusPage::~ServerStatusPage() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ServerStatusPage::configureNotify(const TDimension &size) {
Shinya Kitaoka 120a6e
  m_data->configureNotify(size);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ServerStatusPage::update() {
Shinya Kitaoka 120a6e
  TFarmController *controller = Application::instance()->getController();
Shinya Kitaoka 120a6e
  vector<serveridentity> servers;</serveridentity>
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    controller->getServers(servers);
Shinya Kitaoka 120a6e
  } catch (TException &e) {
Shinya Kitaoka 120a6e
    TMessage::error(toString(e.getMessage()));
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  vector<serveridentity>::iterator it = servers.begin();</serveridentity>
Shinya Kitaoka 120a6e
  vector<serveridentity> newServers;</serveridentity>
Shinya Kitaoka 120a6e
  std::set<int> oldServers;</int>
Shinya Kitaoka 120a6e
  TTextList *sl = m_data->m_serverList;
Shinya Kitaoka 120a6e
  for (; it != servers.end(); ++it) {
Shinya Kitaoka 120a6e
    int index;
Shinya Kitaoka 120a6e
    if ((index = sl->itemToIndex(it->m_id)) == -1)
Shinya Kitaoka 120a6e
      newServers.push_back(*it);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      oldServers.insert(index);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  int i = 0, count = sl->getItemCount();
Shinya Kitaoka 120a6e
  for (; i < count; ++i)
Shinya Kitaoka 120a6e
    if (oldServers.find(i) == oldServers.end()) {
Shinya Kitaoka 120a6e
      string itemId = sl->getItem(i)->getId();
Shinya Kitaoka 120a6e
      /*
Shinya Kitaoka 120a6e
if(sl->isSelected(itemId))
Shinya Kitaoka 120a6e
sl->select((i+1)%sl->getItemCount(), true);
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
      sl->removeItem(itemId);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  it = newServers.begin();
Shinya Kitaoka 120a6e
  for (; it != newServers.end(); it++)
Shinya Kitaoka 120a6e
    sl->addItem(new TTextListItem(it->m_id, it->m_name));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TTextListItem *item = m_data->m_serverList->getSelectedItem(0);
Shinya Kitaoka 120a6e
  if (item) m_data->showServerInfo(item->getId());
Shinya Kitaoka 120a6e
  invalidate();
Toshihiro Shimizu 890ddd
}